From c7080a641d81978bdf481ad407830f15cf1fa137 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Sun, 29 Jan 2017 20:29:43 +0100 Subject: [PATCH] use common library --- lamp_switch/lamp_switch.ino | 56 ++++--------------------------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/lamp_switch/lamp_switch.ino b/lamp_switch/lamp_switch.ino index 7093363..4012521 100644 --- a/lamp_switch/lamp_switch.ino +++ b/lamp_switch/lamp_switch.ino @@ -1,23 +1,15 @@ -#include -#include -#include - +#include #include #include #define DEBUG 0 #define RC_SWITCH 1 -// Utility macros -#define adc_disable() (ADCSRA &= ~_BV(ADEN)) // disable ADC (before power-off) -#define adc_enable() (ADCSRA |= _BV(ADEN)) // re-enable ADC -#define enable_pin_interrupts() (GIMSK |= _BV(PCIE)) // Enable Pin Change Interrupts - // Pins #define SWITCH 0 #if DEBUG -#define RxD 0 +#define RxD 3 #define TxD 4 SoftwareSerial AttinySerial(RxD,TxD); #endif @@ -27,9 +19,6 @@ SoftwareSerial AttinySerial(RxD,TxD); RCSwitch mySwitch = RCSwitch(); #endif -void sleep(); -void enableWdt(); -void disableWdt(); void debug(const char* msg); char* group = "11111"; @@ -48,13 +37,12 @@ void setup() { mySwitch.setProtocol(1); #endif - set_sleep_mode(SLEEP_MODE_PWR_DOWN); - enable_pin_interrupts(); + TinyPower::setup(); } void loop() { debug("loop"); - sleep(); + TinyPower::sleep(); } void sendCommand(boolean on) { @@ -75,12 +63,12 @@ ISR(PCINT0_vect) { byte state = digitalRead(SWITCH); if (state == LOW) { debug("state is low"); - enableWdt(); + TinyPower::enableWdt(WDTO_1S); } } ISR(WDT_vect) { - disableWdt(); + TinyPower::disableWdt(); debug("wdt"); byte state = digitalRead(SWITCH); if (state == LOW) { @@ -90,38 +78,6 @@ ISR(WDT_vect) { } } -void sleep() { - PCMSK |= _BV(PCINT0); // Use PB0 as interrupt pin - adc_disable(); - - sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) - sleep_bod_disable(); - sei(); // Enable interrupts - - sleep_cpu(); // sleep - - sleep_disable(); // Clear SE bit - cli(); // Disable interrupts - PCMSK &= ~_BV(PCINT0); // Turn off PB0 as interrupt pin - adc_enable(); - - sei(); // Enable interrupts -} - -//enable the wdt for 1 sec interrupt -inline void enableWdt() { - wdt_reset(); - MCUSR = 0x00; - WDTCR |= _BV(WDCE) | _BV(WDE); - WDTCR = _BV(WDIE) | _BV(WDP2) | _BV(WDP1); -} - -inline void disableWdt() { - MCUSR = 0x00; - WDTCR |= _BV(WDCE) | _BV(WDE); - WDTCR = 0x00; -} - void debug(const char* msg) { #if DEBUG AttinySerial.println(msg);