From 15555ea7ebe8eba43e5670efd3ba624d017168fc Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 26 May 2016 16:35:22 +0200 Subject: [PATCH] Arduino: separated interrupt callbacks Java: fixed TC --- arduino/HalMultiSensor/HalMultiSensor.ino | 2 +- arduino/HalMultiSensor/Interrupt.cpp | 41 ++++++++----------- arduino/HalMultiSensor/Interrupt.h | 17 ++++++-- arduino/HalMultiSensor/SensorPhotocell.cpp | 2 +- .../protocols/NexaSelfLearningTest.java | 2 +- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/arduino/HalMultiSensor/HalMultiSensor.ino b/arduino/HalMultiSensor/HalMultiSensor.ino index cec66b59..39955ece 100755 --- a/arduino/HalMultiSensor/HalMultiSensor.ino +++ b/arduino/HalMultiSensor/HalMultiSensor.ino @@ -61,7 +61,7 @@ void setup() #endif DEBUG("Setup INTERRUPT"); - Interrupt::setCallback(timerInterruptFunc); + Interrupt::setWatchDogCallback(timerInterruptFunc); Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt diff --git a/arduino/HalMultiSensor/Interrupt.cpp b/arduino/HalMultiSensor/Interrupt.cpp index 97dcf8f9..21f944c9 100755 --- a/arduino/HalMultiSensor/Interrupt.cpp +++ b/arduino/HalMultiSensor/Interrupt.cpp @@ -6,25 +6,15 @@ #include "HalInterfaces.h" void emptyFunc(){} -InterruptFunction Interrupt::callback = emptyFunc; bool Interrupt::wakeUpNow = false; +InterruptFunction Interrupt::pinCallback = emptyFunc; +InterruptFunction Interrupt::wdtCallback = emptyFunc; -void __interruptHandler__() // the interrupt is handled here after wakeup +void Interrupt::handlePinInterrupt() // the interrupt is handled here after wakeup { - // execute code here after wake-up before returning to the loop() function - // timers and code using timers (serial.print and more...) will not work here. - - noInterrupts(); // disable all interrupts - (Interrupt::getCallback()) (); - interrupts(); // enable all interrupts - Interrupt::wakeUp(); - -} - -void Interrupt::wakeUp() -{ - wakeUpNow = true; + (*Interrupt::pinCallback) (); + //Interrupt::wakeUp(); } void Interrupt::sleep() @@ -81,7 +71,7 @@ void Interrupt::setupPinInterrupt(int pin) * * In all but the IDLE sleep modes only LOW can be used. */ - attachInterrupt((pin == PIND2 ? 0 : 1), __interruptHandler__, LOW); + attachInterrupt((pin == PIND2 ? 0 : 1), Interrupt::handlePinInterrupt, LOW); //detachInterrupt(0); // disables interrupt 0 on pin 2 so the // wakeUpNow code will not be executed @@ -93,9 +83,9 @@ void Interrupt::setupPinInterrupt(int pin) // Watchdog timer unsigned int wdtTime; long wdtTimeLeft; -void __setWatchDogInterrupt(); -ISR(WDT_vect) + +void Interrupt::handleWatchDogInterrupt() { //DEBUG("WDT Interrupt"); wdt_disable(); @@ -103,19 +93,24 @@ ISR(WDT_vect) { DEBUG("WDT interrupt"); Interrupt::wakeUp(); - __interruptHandler__(); + (*Interrupt::wdtCallback) (); wdtTimeLeft = wdtTime; } - __setWatchDogInterrupt(); + setupWatchDogInterrupt(); +} + +ISR(WDT_vect) +{ + Interrupt::handleWatchDogInterrupt(); } void Interrupt::setupWatchDogInterrupt(unsigned int milliseconds) { wdtTimeLeft = wdtTime = milliseconds; - __setWatchDogInterrupt(); + setupWatchDogInterrupt(); } -void __setWatchDogInterrupt() +void Interrupt::setupWatchDogInterrupt() { noInterrupts(); @@ -194,7 +189,7 @@ void __setWatchDogInterrupt() ISR(Timer1_COMPA_vect) // timer compare interrupt service routine { //DEBUG("Timer1e Interrupt"); - __interruptHandler__(); + __pinInterruptHandler__(); } */ /* diff --git a/arduino/HalMultiSensor/Interrupt.h b/arduino/HalMultiSensor/Interrupt.h index fae51d92..34094ee5 100755 --- a/arduino/HalMultiSensor/Interrupt.h +++ b/arduino/HalMultiSensor/Interrupt.h @@ -7,22 +7,31 @@ typedef void (*InterruptFunction) (); class Interrupt { public: - static void wakeUp(); + static void wakeUp() { wakeUpNow = true; }; static void sleep(); static void setupPinInterrupt(int pin); static void setupWatchDogInterrupt(unsigned int milliseconds); //static void setupTimerInterrupt(unsigned int milliseconds); - static void setCallback(InterruptFunction callback){ Interrupt::callback = callback;} - static InterruptFunction getCallback(){ return Interrupt::callback;} + static void setPinCallback(InterruptFunction callback){ Interrupt::pinCallback = callback;} + static void setWatchDogCallback(InterruptFunction callback){ Interrupt::wdtCallback = callback;} + /* Should not be called externally, used as triggering functions */ + static void handlePinInterrupt(); + static void handleWatchDogInterrupt(); private: - static InterruptFunction callback; static bool wakeUpNow; + static InterruptFunction pinCallback; + static InterruptFunction wdtCallback; + + static void setupWatchDogInterrupt(); + + // Disable constructors and copy operators Interrupt() {}; Interrupt(Interrupt const&); void operator=(Interrupt const&); + }; diff --git a/arduino/HalMultiSensor/SensorPhotocell.cpp b/arduino/HalMultiSensor/SensorPhotocell.cpp index 518e4775..0e626fd9 100755 --- a/arduino/HalMultiSensor/SensorPhotocell.cpp +++ b/arduino/HalMultiSensor/SensorPhotocell.cpp @@ -9,7 +9,7 @@ void SensorPhotocell::interruptHandler() void SensorPhotocell::setup() { - Interrupt::setCallback(SensorPhotocell::interruptHandler); + Interrupt::setPinCallback(SensorPhotocell::interruptHandler); Interrupt::setupPinInterrupt(PC2); //PC3 } diff --git a/test/se/hal/plugin/tellstick/protocols/NexaSelfLearningTest.java b/test/se/hal/plugin/tellstick/protocols/NexaSelfLearningTest.java index 7e436d77..73beaa52 100755 --- a/test/se/hal/plugin/tellstick/protocols/NexaSelfLearningTest.java +++ b/test/se/hal/plugin/tellstick/protocols/NexaSelfLearningTest.java @@ -43,7 +43,7 @@ public class NexaSelfLearningTest { 127, 255, 24, 0, // timings 134, // length - 0xF9, // preamble + 0x09, // preamble 168, 168, 138, 168, 138, 138, 168, 168, 138, 138, 138, 168, 138, 168, 168, 168, 168, 168, 168, 138, 138, 168, 168, 138, 138, 168, 168, 138, 168, 168,