use common library

This commit is contained in:
Nicu Hodos 2017-01-29 20:29:43 +01:00
parent 2a70b3a12e
commit c951dffaec

View File

@ -1,23 +1,15 @@
#include <avr/sleep.h> #include <TinyPower.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <RCSwitch.h> #include <RCSwitch.h>
#include <SoftwareSerial_Tiny.h> #include <SoftwareSerial_Tiny.h>
#define DEBUG 0 #define DEBUG 0
#define RC_SWITCH 1 #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 // Pins
#define SWITCH 0 #define SWITCH 0
#if DEBUG #if DEBUG
#define RxD 0 #define RxD 3
#define TxD 4 #define TxD 4
SoftwareSerial AttinySerial(RxD,TxD); SoftwareSerial AttinySerial(RxD,TxD);
#endif #endif
@ -27,9 +19,6 @@ SoftwareSerial AttinySerial(RxD,TxD);
RCSwitch mySwitch = RCSwitch(); RCSwitch mySwitch = RCSwitch();
#endif #endif
void sleep();
void enableWdt();
void disableWdt();
void debug(const char* msg); void debug(const char* msg);
char* group = "11111"; char* group = "11111";
@ -48,13 +37,12 @@ void setup() {
mySwitch.setProtocol(1); mySwitch.setProtocol(1);
#endif #endif
set_sleep_mode(SLEEP_MODE_PWR_DOWN); TinyPower::setup();
enable_pin_interrupts();
} }
void loop() { void loop() {
debug("loop"); debug("loop");
sleep(); TinyPower::sleep();
} }
void sendCommand(boolean on) { void sendCommand(boolean on) {
@ -75,12 +63,12 @@ ISR(PCINT0_vect) {
byte state = digitalRead(SWITCH); byte state = digitalRead(SWITCH);
if (state == LOW) { if (state == LOW) {
debug("state is low"); debug("state is low");
enableWdt(); TinyPower::enableWdt(WDTO_1S);
} }
} }
ISR(WDT_vect) { ISR(WDT_vect) {
disableWdt(); TinyPower::disableWdt();
debug("wdt"); debug("wdt");
byte state = digitalRead(SWITCH); byte state = digitalRead(SWITCH);
if (state == LOW) { 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) { void debug(const char* msg) {
#if DEBUG #if DEBUG
AttinySerial.println(msg); AttinySerial.println(msg);