Skip to content

Commit 706f9d5

Browse files
authored
Merge pull request #7 from awonak/pin-change-interrupts
Add pin change interrupt handlers for A-RYTH-MATIK CLK & RST inputs.
2 parents f008a58 + 64d9fd2 commit 706f9d5

4 files changed

Lines changed: 60 additions & 10 deletions

File tree

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ using namespace arythmatik;
4545
// Declare A-RYTH-MATIK hardware variable.
4646
Arythmatik hw;
4747

48-
byte counter = 0;
48+
// Since counter is modified in the CLK/RST handler, it should be volatile.
49+
volatile int counter;
4950

5051
void setup() {
5152
// Inside the setup, set config values prior to calling hw.Init().
@@ -60,6 +61,10 @@ void setup() {
6061
hw.eb.setEncoderHandler(UpdateRotate);
6162
hw.eb.setClickHandler(UpdatePress);
6263

64+
// Attach CLK & RST pin change handlers.
65+
hw.AttachClockHandler(HandleClockPinChange);
66+
hw.AttachResetHandler(HandleResetPinChange);
67+
6368
// Initialize the A-RYTH-MATIK peripherials.
6469
hw.Init();
6570
}
@@ -68,11 +73,6 @@ void loop() {
6873
// Read cv inputs and process encoder state to determine state for this loop.
6974
hw.ProcessInputs();
7075

71-
// Advance the counter on CLK input
72-
if (hw.clk.State() == DigitalInput::STATE_RISING) {
73-
counter = ++counter % OUTPUT_COUNT;
74-
}
75-
7676
// Activate the current counter output.
7777
for (int i = 0; i < OUTPUT_COUNT; i++) {
7878
(i == counter)
@@ -87,6 +87,19 @@ void loop() {
8787
hw.display.display();
8888
}
8989

90+
void HandleClockPinChange() {
91+
// Advance the counter on CLK input
92+
if (hw.clk.Read() == HIGH) {
93+
counter = ++counter % OUTPUT_COUNT;
94+
}
95+
}
96+
97+
void HandleResetPinChange() {
98+
if (hw.rst.Read() == HIGH) {
99+
counter = 0;
100+
}
101+
}
102+
90103
void UpdateRotate(EncoderButton &eb) {
91104
// Read the configured encoder direction (according to hw.config.ReverseEncoder setting).
92105
Direction dir = hw.EncoderDirection();

analog_output.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AnalogOutput {
4646
/**
4747
* @brief Set the output pin to the given 8 bit value.
4848
*
49-
* @param val Arduino analog value between 0 and 255 (0..5v).
49+
* @param val Arduino analog value between 0 and 255 (0..10v).
5050
*/
5151
inline void Update(int val) {
5252
update((val <= MAX_OUTPUT) ? val : MAX_OUTPUT);
@@ -55,22 +55,22 @@ class AnalogOutput {
5555
/**
5656
* @brief Set the output pin to the given 10 bit value.
5757
*
58-
* @param val Arduino analog value between 0 and 1024 (0..5v).
58+
* @param val Arduino analog value between 0 and 1024 (0..10v).
5959
*/
6060
inline void Update10bit(int val) {
6161
val = val <= MAX_OUTPUT_10BIT ? val : MAX_OUTPUT_10BIT;
6262
val = map(val, 0, MAX_OUTPUT_10BIT, 0, MAX_OUTPUT);
6363
update(val);
6464
}
6565

66-
/// @brief Sets the cv output HIGH to about 5v.
66+
/// @brief Sets the cv output HIGH to about 10v.
6767
inline void High() { update(MAX_OUTPUT); }
6868

6969
/// @brief Sets the cv output LOW to 0v.
7070
inline void Low() { update(0); }
7171

7272
/**
73-
* @brief Return an integer value between 0 and 1023 (0..5v) representing the current value of the output.
73+
* @brief Return an integer value between 0 and 1023 (0..10v) representing the current value of the output.
7474
*
7575
* @return integer value of cv from 0 to 1023.
7676
*/

arythmatik.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
using namespace modulove;
1515
using namespace arythmatik;
1616

17+
static void (*ClockHandler)(void);
18+
static void (*ResetHandler)(void);
19+
void noop() {};
20+
21+
1722
void Arythmatik::Init() {
1823
InitInputs();
1924
InitOutputs();
@@ -45,6 +50,26 @@ void Arythmatik::InitInputs() {
4550
clk.Init(CLK_PIN);
4651
rst.Init(RST_PIN);
4752
}
53+
54+
// If the handler has not yet been set, assign a noop function.
55+
if (ClockHandler == NULL) ClockHandler = noop;
56+
if (ResetHandler == NULL) ResetHandler = noop;
57+
58+
// Pin Change Interrupt for CLK & RST.
59+
// Thanks to Sitka Instruments for the tip and docs from https://dronebotworkshop.com/interrupts/
60+
// Enable PCIE0 Bit0 = 1 (Port B)
61+
PCICR |= B00000001;
62+
// Enable PCINT5 & PCINT3 (Pin 13 & Pin 11)
63+
PCMSK0 |= B00101000;
64+
// ISR (PCINT0_vect) - ISR for Port B (D8 - D13)
65+
}
66+
67+
void Arythmatik::AttachClockHandler(void (*callback)(void)) {
68+
ClockHandler = callback;
69+
}
70+
71+
void Arythmatik::AttachResetHandler(void (*callback)(void)) {
72+
ResetHandler = callback;
4873
}
4974

5075
void Arythmatik::InitOutputs() {
@@ -82,3 +107,10 @@ void Arythmatik::ProcessInputs() {
82107
Direction Arythmatik::EncoderDirection() {
83108
return Rotate(eb.increment(), config.ReverseEncoder);
84109
}
110+
111+
// Define ISR
112+
// Pin Change Interrupt on Port B (D13 CLK & D11 RST).
113+
ISR(PCINT0_vect) {
114+
ClockHandler();
115+
ResetHandler();
116+
};

arythmatik.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Arythmatik {
3535
/// @brief Initializes the Arduino, and A-RYTH-MATIK hardware.
3636
void Init();
3737

38+
/// @brief Pin change handlers
39+
/// @param callback
40+
void AttachClockHandler(void (*callback)(void));
41+
void AttachResetHandler(void (*callback)(void));
42+
3843
/// @brief Read the state of the CLK and RST inputs.
3944
void ProcessInputs();
4045

0 commit comments

Comments
 (0)