diff --git a/arduino/HalMultiSensor/HalMultiSensor.ino b/arduino/HalMultiSensor/HalMultiSensor.ino index f458ea74..c9a9b9b4 100755 --- a/arduino/HalMultiSensor/HalMultiSensor.ino +++ b/arduino/HalMultiSensor/HalMultiSensor.ino @@ -37,8 +37,8 @@ void timerInterruptFunc(); void setup() { timerMultiplierMAX = POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER; // Find a lowest common denominator - interrupt = new Interrupt(timerInterruptFunc); - interrupt->setupTimerInterrupt(60*1000); // one minute scheduled interrupt + Interrupt::setCallback(timerInterruptFunc); + Interrupt::setupTimerInterrupt(60*1000); // one minute scheduled interrupt // Setup Sensors and protocols #ifdef POWERCON_ENABLED @@ -64,15 +64,15 @@ void setup() } -void loop() {} - - void timerInterruptFunc() { ++timerMultiplier; if (timerMultiplier > timerMultiplierMAX) timerMultiplier = 1; +} +void loop() +{ // Send power consumption #ifdef POWERCON_ENABLED if(timerMultiplier == POWER_TIMER_MULTIPLIER) diff --git a/arduino/HalMultiSensor/Interrupt.cpp b/arduino/HalMultiSensor/Interrupt.cpp index 3000fdf5..0d17deee 100755 --- a/arduino/HalMultiSensor/Interrupt.cpp +++ b/arduino/HalMultiSensor/Interrupt.cpp @@ -1,24 +1,39 @@ #include "Interrupt.h" +#include #include #include +#include +void emptyFunc(){} +InterruptFunction Interrupt::callback = emptyFunc; +bool Interrupt::wakeUpNow = false; -void __Interrupt_pinInterrupt__() // the interrupt is handled here after wakeup +void __interruptHandler__() // 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. - // we don't really need to execute any special functions here, since we - // just want the thing to wake up + + noInterrupts(); // disable all interrupts + (Interrupt::getCallback()) (); + interrupts(); // enable all interrupts } -void Interrupt::setupPinInterrupt(int pin) +ISR(Timer1_COMPA_vect) // timer compare interrupt service routine { - + __interruptHandler__(); } -void Interrupt::setupTimerInterrupt(unsigned int milliseconds) +ISR(WDT_vect) { } + + +void Interrupt::wakeUp() +{ + wakeUpNow = true; +} + +void Interrupt::sleep() { /* * The 5 different modes are: @@ -33,9 +48,32 @@ void Interrupt::setupTimerInterrupt(unsigned int milliseconds) * sleep mode: SLEEP_MODE_PWR_DOWN */ set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here + wakeUpNow = false; - sleep_enable(); // enables the sleep bit in the mcucr register - // so sleep is possible. just a safety pin + sleep_enable(); // enables the sleep bit in the mcucr register + // so sleep is possible. just a safety pin + /* + power_adc_disable(); + power_spi_disable(); + power_timer0_disable(); + power_timer1_disable(); + power_timer2_disable(); + power_twi_disable(); + */ + while( ! Interrupt::wakeUpNow) + { + sleep_mode(); // here the device is actually put to sleep!! + // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP + } + sleep_disable(); // first thing after waking from sleep: + // disable sleep... + + power_all_enable(); // during normal running time. +} + +void Interrupt::setupPinInterrupt(int pin) +{ + noInterrupts(); // disable all interrupts /* Now it is time to enable an interrupt. * In the function call attachInterrupt(A, B, C) @@ -49,22 +87,37 @@ void Interrupt::setupTimerInterrupt(unsigned int milliseconds) * * In all but the IDLE sleep modes only LOW can be used. */ - attachInterrupt(0,__Interrupt_pinInterrupt__, LOW); - /* - power_adc_disable(); - power_spi_disable(); - power_timer0_disable(); - power_timer1_disable(); - power_timer2_disable(); - power_twi_disable(); - */ - sleep_mode(); // here the device is actually put to sleep!! - // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP + attachInterrupt(pin, __interruptHandler__, LOW); - sleep_disable(); // first thing after waking from sleep: - // disable sleep... - detachInterrupt(0); // disables interrupt 0 on pin 2 so the + //detachInterrupt(0); // disables interrupt 0 on pin 2 so the // wakeUpNow code will not be executed - // during normal running time. - power_all_enable(); + + interrupts(); // enable all interrupts } + +void Interrupt::setupTimerInterrupt(unsigned int milliseconds) +{ + noInterrupts(); // disable all interrupts + + // initialize Timer1 + TCCR1A = 0; + TCCR1B = 0; + + /* Clock Select Bit Description + * CS12 CS11 CS10 Description + * 0 0 0 No clock source (Stop timer) + * 0 0 1 clk/1 (No prescaling) + * 0 1 0 clk/8 + * 0 1 1 clk/64 + * 1 0 0 clk/256 + * 1 0 1 clk/1024 + * 1 1 0 External clock source on T1 pin. Clock on falling edge. + * 1 1 1 External clock source on T1 pin. Clock on rising edge. + */ + TCCR1B |= (1 << CS12); // 256 prescaler + TCNT1 = 34286; // preload timer 65536-16MHz/256/2Hz + TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt + + interrupts(); // enable all interrupts +} + diff --git a/arduino/HalMultiSensor/Interrupt.h b/arduino/HalMultiSensor/Interrupt.h index f4aa9e6a..58dc7a02 100755 --- a/arduino/HalMultiSensor/Interrupt.h +++ b/arduino/HalMultiSensor/Interrupt.h @@ -1,21 +1,28 @@ #ifndef INTERRUPT_H #define INTERRUPT_H -#include - typedef void (*InterruptFunction) (); class Interrupt { public: - Interrupt(InterruptFunction callback) : callback(callback) {}; + static void wakeUp(); + static void sleep(); + static void setupPinInterrupt(int pin); + static void setupTimerInterrupt(unsigned int milliseconds); + + static void setCallback(InterruptFunction callback){ Interrupt::callback = callback;} + static InterruptFunction getCallback(){ return Interrupt::callback;} - //static void sleep(int milliseconds); - void setupPinInterrupt(int pin); - void setupTimerInterrupt(unsigned int milliseconds); private: - InterruptFunction callback; + static InterruptFunction callback; + static bool wakeUpNow; + + Interrupt() {}; + Interrupt(Interrupt const&); + void operator=(Interrupt const&); }; + #endif // INTERRUPT_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/SensorPhotocell.cpp b/arduino/HalMultiSensor/SensorPhotocell.cpp index d535168e..46eafa9c 100755 --- a/arduino/HalMultiSensor/SensorPhotocell.cpp +++ b/arduino/HalMultiSensor/SensorPhotocell.cpp @@ -1,16 +1,10 @@ #include "SensorPhotocell.h" - - -void pinInterrupt() -{ - //++pulse; -} +#include void SensorPhotocell::setup() { - interrupt = new Interrupt(pinInterrupt); - interrupt->setupPinInterrupt(PC2); //PC3 + Interrupt::setupPinInterrupt(PC2); //PC3 } unsigned int SensorPhotocell::getConsumption()