From e3cae61ae859e1b6e5fda901269fd73f7770a261 Mon Sep 17 00:00:00 2001 From: Zou Hanya Date: Sat, 10 Sep 2016 22:58:15 +0900 Subject: [PATCH] Fix Zero and MKR1000 interrupts --- Encoder.h | 18 ++++++++++++++++-- utility/interrupt_pins.h | 3 +-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Encoder.h b/Encoder.h index 4289ff8..6ae6188 100644 --- a/Encoder.h +++ b/Encoder.h @@ -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); @@ -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]; diff --git a/utility/interrupt_pins.h b/utility/interrupt_pins.h index 322d32e..6adf751 100644 --- a/utility/interrupt_pins.h +++ b/utility/interrupt_pins.h @@ -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