diff --git a/avrrfm.c b/avrrfm.c index ad8216f..7fc454f 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -33,13 +33,10 @@ #include "dejavu.h" #include "unifont.h" -#define MEASURE_INTS 4 // about 32 seconds - #define LABEL_OFFSET 10 - -#define BLACK 0x0000 -#define RED 0xf800 -#define WHITE 0xffff +#define BLACK 0x0000 +#define RED 0xf800 +#define WHITE 0xffff #define NODE1 0x24 #define NODE2 0x42 @@ -48,9 +45,6 @@ #define RECEIVER 1 #endif -/* 1 int = 8 seconds */ -static volatile uint8_t wdints = 0; - /* Temp. label coordinates */ static x_t xl = 0; static y_t yl = 0; @@ -59,15 +53,14 @@ static width_t width = 0; /** - * Called when the watchdog barks. + * Called when the watchdog barks to wake up the transmitter. */ ISR(WDT_vect) { - wdints++; + barkRadio(); } /** - * Called about 30 times a second while the controller - * isn't in power down sleep mode. + * Called while the controller isn't in power down sleep mode. */ ISR(TIMER0_COMPA_vect) { timeRadio(); @@ -298,9 +291,7 @@ // _delay_ms(1000); if (!RECEIVER) { - if (wdints % MEASURE_INTS == 0) { - wdints = 0; - + if (wouldTransmit()) { enableSPI(); wakeTSens(); wakeRadio(); diff --git a/rfm69.c b/rfm69.c index cbf0757..26d594a 100644 --- a/rfm69.c +++ b/rfm69.c @@ -11,8 +11,11 @@ static volatile uint8_t irqFlags1 = 0; static volatile uint8_t irqFlags2 = 0; -static volatile bool timeoutEnabled = false; -static volatile uint8_t timeoutInts = 0; +static uint8_t watchdogInts = 0; +static uint8_t measureInts = TRANSMIT_FAST; +static uint8_t timeoutInts = 0; +static uint8_t timeoutCount = 0; +static bool timeoutEnabled = false; /** * Selects the radio to talk to via SPI. @@ -86,6 +89,10 @@ */ static void timeout(void) { irqFlags1 |= (1 << 2); + if (timeoutCount++ > MAX_TIMEOUTS) { + measureInts = TRANSMIT_SLOW; + timeoutCount = 0; + } } /** @@ -217,6 +224,20 @@ return regRead(PA_LEVEL); } +void barkRadio(void) { + watchdogInts++; +} + +bool wouldTransmit(void) { + if (watchdogInts % measureInts == 0) { + watchdogInts = 0; + + return true; + } + + return false; +} + void timeRadio(void) { if (timeoutEnabled && timeoutInts++ >= TIMEOUT_INTS) { timeoutEnable(false); @@ -285,10 +306,15 @@ bool timedout = irqFlags1 & (1 << 2); clearIrqFlags(); - if (ready) timeoutEnable(false); + if (ready) { + timeoutEnable(false); + timeoutCount = 0; + measureInts = TRANSMIT_FAST; + } setMode(MODE_STDBY); if (timedout) { + // full power as last resort regWrite(PA_LEVEL, 0x5f); return 0; diff --git a/rfm69.h b/rfm69.h index 113953e..d0c9c71 100644 --- a/rfm69.h +++ b/rfm69.h @@ -80,7 +80,10 @@ #define F_STEP 6103515625ULL #define CAST_ADDRESS 0x84 -#define TIMEOUT_INTS 3 // about 100 milliseconds +#define TRANSMIT_FAST 1 // 4 ~ 32 seconds +#define TRANSMIT_SLOW 9 // 38 ~ 5 minutes +#define TIMEOUT_INTS 3 // about 100 milliseconds +#define MAX_TIMEOUTS 9 // slow down tx attempts after so many timeouts /** * Initializes the radio module with the given carrier frequency in kilohertz @@ -103,6 +106,18 @@ uint8_t getOutputPower(void); /** + * Gives a watchdog "wakeup" pulse to the radio. + */ +void barkRadio(void); + +/** + * Returns true if the transmitter would actually transmit. + * + * @return transmit or not + */ +bool wouldTransmit(void); + +/** * Gives a timer pulse to the radio. */ void timeRadio(void);