Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Zero and MKR1000 interrupts #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,26 @@ class Encoder
if (DIRECT_PIN_READ(encoder.pin2_register, encoder.pin2_bitmask)) s |= 2;
encoder.state = s;
#ifdef ENCODER_USE_INTERRUPTS
interrupts_in_use = attach_interrupt(pin1, &encoder);
interrupts_in_use += attach_interrupt(pin2, &encoder);
interrupts_initialized = false;
interrupt_pin1 = pin1;
interrupt_pin2 = pin2;
#endif
//update_finishup(); // to force linker to include the code (does not work)
}

void begin(void) {
#ifdef ENCODER_USE_INTERRUPTS
interrupts_in_use = attach_interrupt(interrupt_pin1, &encoder);
interrupts_in_use += attach_interrupt(interrupt_pin2, &encoder);
interrupts_initialized = true;
#endif
}

#ifdef ENCODER_USE_INTERRUPTS
inline int32_t read() {
if (!interrupts_initialized) {
begin();
}
if (interrupts_in_use < 2) {
noInterrupts();
update(&encoder);
Expand Down Expand Up @@ -130,6 +141,9 @@ class Encoder
Encoder_internal_state_t encoder;
#ifdef ENCODER_USE_INTERRUPTS
uint8_t interrupts_in_use;
uint8_t interrupts_initialized;
uint8_t interrupt_pin1;
uint8_t interrupt_pin2;
#endif
public:
static Encoder_internal_state_t * interruptArgs[ENCODER_ARGLIST_SIZE];
Expand Down
3 changes: 1 addition & 2 deletions utility/interrupt_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@
#define CORE_INT14_PIN 14
#define CORE_INT15_PIN 15

// Arduino Zero - TODO: interrupts do not seem to work
// please help, contribute a fix!
// Arduino Zero
#elif defined(__SAMD21G18A__)
#define CORE_NUM_INTERRUPT 20
#define CORE_INT0_PIN 0
Expand Down