Skip to content

Commit

Permalink
testing rotary encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
rabidaudio committed Jan 1, 2023
1 parent 9e3ae04 commit 92ede29
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 109 deletions.
32 changes: 4 additions & 28 deletions clock/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
# Clock

## [Writeup and documentation](https://rabid.audio/projects/synth/clk/)

- LED 7-segment display
- Knob: continuous rotary selector
- rotary encoder
- Swing control
- Out
- subdivision out
- LED blink on clock
- Tap tempo
- CV in


States:
Main State:
- Display: current BPM (35-287)
- Dots: show beats
- knob: BPM
- press button B: tap tempo

swing control state (hold button A):
- Display: swing amount (0-100%)
- knob: swing amount

Subdivision state (hold button B):
- Display: number of subdivisions 1-16
- knob: controls subdivision amount

Reset (hold both A and B):
- Display: 000
- Freeze clock
- Restart subdivisions

Full Reset (hold both A and B for 2 seconds):
- Display: blink 000
- Restart subdivisions
- Restore default settings

https://en.wikipedia.org/wiki/Swing_(jazz_performance_style)
https://github.com/adafruit/TinyWireM/blob/master/USI_TWI_Master.h
2 changes: 1 addition & 1 deletion clock/clock/clock.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
update=Friday, September 23, 2022 at 10:51:29 PM
update=Saturday, December 03, 2022 at 06:14:21 PM
version=1
last_client=kicad
[general]
Expand Down
37 changes: 18 additions & 19 deletions clock/clock/clock.sch
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,6 @@ Wire Wire Line
1850 5500 1850 5350
Text Label 3250 3200 0 50 ~ 0
SCK
Text Label 2700 3750 0 50 ~ 0
B_KNOBL
Wire Wire Line
2700 3550 2450 3550
Text Label 3250 3000 0 50 ~ 0
Expand Down Expand Up @@ -759,10 +757,6 @@ Text Label 2700 3150 0 50 ~ 0
B_LEDR
Text Label 2700 4350 0 50 ~ 0
B_LED0
Text Label 2700 4450 0 50 ~ 0
B_LED1
Text Label 2700 4550 0 50 ~ 0
B_LED2
Text Label 2700 4650 0 50 ~ 0
B_LED3
Text Label 2700 4750 0 50 ~ 0
Expand Down Expand Up @@ -1181,8 +1175,6 @@ Wire Wire Line
7950 5950 7950 5850
Text Label 7250 5550 0 50 ~ 0
AGND
Text Notes 7900 5400 0 50 ~ 0
bound input CV to 0-5V
Text Label 5100 4200 2 50 ~ 0
KNOBL
Wire Wire Line
Expand Down Expand Up @@ -1218,8 +1210,8 @@ $Comp
L Device:R R10
U 1 1 63860657
P 5650 3950
F 0 "R10" H 5500 4000 50 0000 L CNN
F 1 "10K" V 5650 3900 50 0000 L CNN
F 0 "R10" H 5450 4000 50 0000 L CNN
F 1 "10K" H 5450 3900 50 0000 L CNN
F 2 "Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P2.54mm_Vertical" V 5580 3950 50 0001 C CNN
F 3 "~" H 5650 3950 50 0001 C CNN
1 5650 3950
Expand All @@ -1230,7 +1222,7 @@ L Device:R R9
U 1 1 6386138C
P 5400 4200
F 0 "R9" V 5500 4200 50 0000 C CNN
F 1 "10K" V 5400 4200 50 0000 C CNN
F 1 "10K" V 5600 4200 50 0000 C CNN
F 2 "Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P2.54mm_Vertical" V 5330 4200 50 0001 C CNN
F 3 "~" H 5400 4200 50 0001 C CNN
1 5400 4200
Expand Down Expand Up @@ -1275,8 +1267,8 @@ $Comp
L Device:R R11
U 1 1 638F4837
P 6450 3950
F 0 "R11" H 6300 4000 50 0000 L CNN
F 1 "10K" V 6450 3900 50 0000 L CNN
F 0 "R11" H 6250 4000 50 0000 L CNN
F 1 "10K" H 6250 3900 50 0000 L CNN
F 2 "Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P2.54mm_Vertical" V 6380 3950 50 0001 C CNN
F 3 "~" H 6450 3950 50 0001 C CNN
1 6450 3950
Expand All @@ -1287,7 +1279,7 @@ L Device:R R12
U 1 1 638F4841
P 6700 4200
F 0 "R12" V 6800 4200 50 0000 C CNN
F 1 "10K" V 6700 4200 50 0000 C CNN
F 1 "10K" V 6900 4200 50 0000 C CNN
F 2 "Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P2.54mm_Vertical" V 6630 4200 50 0001 C CNN
F 3 "~" H 6700 4200 50 0001 C CNN
1 6700 4200
Expand Down Expand Up @@ -1325,8 +1317,6 @@ Text Label 10500 6050 0 50 ~ 0
KNOBR
Text Label 9500 6050 2 50 ~ 0
B_KNOBR
Text Label 2700 5050 0 50 ~ 0
B_KNOBR
Wire Wire Line
3250 3100 2600 3100
Wire Wire Line
Expand All @@ -1341,8 +1331,6 @@ Wire Wire Line
Connection ~ 2600 3150
Wire Wire Line
2600 3150 2700 3150
Text Label 2700 3550 0 50 ~ 0
B_SW3
Text Label 5950 3500 1 50 ~ 0
AVCC
$Comp
Expand Down Expand Up @@ -1413,5 +1401,16 @@ Wire Wire Line
9600 5550 9500 5550
Wire Wire Line
9500 5650 9600 5650
Connection ~ 2650 -1300
Text Label 2700 3750 0 50 ~ 0
B_KNOBL
Text Label 2700 4550 0 50 ~ 0
B_LED2
Text Label 2700 4450 0 50 ~ 0
B_LED1
Text Label 2700 3550 0 50 ~ 0
B_SW3
Text Label 2700 5050 0 50 ~ 0
B_KNOBR
Text Notes 9550 3300 0 50 ~ 0
top-bottom board interface
$EndSCHEMATC
83 changes: 83 additions & 0 deletions clock/src/RotaryEncoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include <Arduino.h>

#define ROT_A bit(1)
#define ROT_B bit(2)

void enableInterrupts(pin)
{
*digitalPinToPCMSK(pin) |= bit(digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit(digitalPinToPCICRbit(pin)); // clear outstanding interrupt
PCICR |= bit(digitalPinToPCICRbit(pin)); // enable interrupt for group
}

// A class for reading incremental rotary encoder rotations
// and reporting changes using pin change interrupts.
// Assumes pins are active low.
class _RotaryEncoder
{
private:
uint8_t _aPin;
uint8_t _bPin;
bool _aState;
bool _aPrevState;
volatile int8_t _counter;

public:
void begin(aPin, bPin)
{
_aPin = aPin;
_bPin = bPin;
_aState = 0;
_aPrevState = 0;
_counter = 0;
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
// enable PC interrupts on each pin
noInterrupts();
enableInterrupts(aPin);
enableInterrupts(bPin);
tick();
interrupts();
}

void tick()
{
_aPrevState = _aState;
_aState = digitalRead(_aPin) == LOW;
if (_aState != _aPrevState) {
if (digitalRead(_bPin) == LOW) {
_counter++;
} else {
_counter--;
}
}
}

// Compare the current state to where it was last and return the number
// of turns that have happened since the last check.
int8_t readChanges()
{
int8_t v = _counter;
_counter = 0;
return v;
}
}

// Global instance to assign
_RotaryEncoder Knob;

// Tick when PC interrupt is triggered

ISR (PCINT0_vect) {
Knob.tick();
}

ISR (PCINT1_vect) {
Knob.tick();
}

ISR (PCINT2_vect) {
Knob.tick();
}
6 changes: 3 additions & 3 deletions clock/src/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ class Timer1
uint8_t _clockPin;
uint8_t _subdivisionPin;
uint8_t _subdivisions = DEFAULT_SUBDIVISIONS;
uint8_t _subdivIdx = 0;
volatile uint8_t _subdivIdx = 0;
int8_t _swing = 0;
uint16_t _oddTick;
uint16_t _evenTick;
bool _isEven = true;
bool _clockHigh = false;
bool _subdivHigh = false;
volatile bool _clockHigh = false;
volatile bool _subdivHigh = false;

void updateTimer()
{
Expand Down
Loading

0 comments on commit 92ede29

Please sign in to comment.