External interrupt integrated into driver
This commit is contained in:
@@ -6,8 +6,7 @@
|
||||
#include "digital_out.h"
|
||||
|
||||
class RotaryEncoderDriver {
|
||||
int count{0};
|
||||
bool last_lo{false};
|
||||
volatile int count{0};
|
||||
|
||||
Digital_out led{5};
|
||||
|
||||
@@ -19,32 +18,32 @@ public:
|
||||
led.init();
|
||||
eA.init();
|
||||
eB.init();
|
||||
last_lo = eA.is_lo();
|
||||
|
||||
EICRA |= (1<<ISC01) | (1<<ISC00);
|
||||
EIMSK |= (1<<INT0);
|
||||
}
|
||||
|
||||
void update() {
|
||||
count += eB.is_hi() ? 1 : -1;
|
||||
led.set_hi();
|
||||
}
|
||||
|
||||
int position() {
|
||||
bool curr_hi = eA.is_hi();
|
||||
|
||||
if (last_lo && curr_hi) {
|
||||
count += eB.is_hi() ? 1 : -1;
|
||||
led.set_hi();
|
||||
} else {
|
||||
led.set_lo();
|
||||
}
|
||||
|
||||
last_lo = !curr_hi;
|
||||
led.set_lo();
|
||||
return count;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
RotaryEncoderDriver encoder{2,5};
|
||||
|
||||
int main() {
|
||||
uint8_t cycle{0};
|
||||
|
||||
RotaryEncoderDriver encoder;
|
||||
Serial.begin(9600);
|
||||
|
||||
int prev_pos = encoder.position();
|
||||
sei();
|
||||
while (true) {
|
||||
_delay_us(100);
|
||||
int curr_pos = encoder.position();
|
||||
@@ -63,3 +62,7 @@ int main() {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ISR(INT0_vect) {
|
||||
encoder.update();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user