use common library
This commit is contained in:
parent
2a70b3a12e
commit
c951dffaec
@ -1,23 +1,15 @@
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h>
|
||||
#include <avr/wdt.h>
|
||||
|
||||
#include <TinyPower.h>
|
||||
#include <RCSwitch.h>
|
||||
#include <SoftwareSerial_Tiny.h>
|
||||
|
||||
#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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user