diff --git a/README.md b/README.md index da5cecb..6555353 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ with an RSSI of about -90 dBm at about 2.2 km distance - with simple wire antennas. What would be the range with +20 dBm and decent antennas? -![IMG_20250306_180139c](https://github.com/user-attachments/assets/27c66e7a-ec16-4e98-9f94-7713fe54c7d0) +![IMG_20250315_175920c](https://github.com/user-attachments/assets/03405774-57c2-42f1-a39a-8fa4cc2801ac) ![FieldTest3](https://github.com/user-attachments/assets/f2289f8e-1f81-4b85-9146-07c2ce1bb563) diff --git a/avrrfm.c b/avrrfm.c index 3bb8a35..ae9c233 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -34,7 +34,6 @@ #include "unifont.h" #define MEASURE_INTS 4 // about 32 seconds -#define TIMEOUT_INTS 30 // about one second #define LABEL_OFFSET 10 @@ -52,9 +51,6 @@ /* 1 int = 8 seconds */ static volatile uint8_t wdints = 0; -static volatile bool toena = false; -static volatile uint8_t toints = 0; - /* Temp. label coordinates */ static x_t xl = 0; static y_t yl = 0; @@ -62,18 +58,13 @@ static y_t yo = 0; static width_t width = 0; +/** + * Called when the watchdog barks. + */ ISR(WDT_vect) { wdints++; } -ISR(TIMER0_COMPA_vect) { - if (toena && toints++ >= TIMEOUT_INTS) { - toints = 0; - toena = false; - timeoutRadio(); - } -} - /** * Sets up the pins. */ @@ -189,7 +180,7 @@ * Waits for a response from the receiver with timeout. */ static void waitResponse(void) { - toena = true; + timeoutEnable(true); uint8_t response[1]; int8_t len = receivePayload(response, sizeof (response)); if (len > 0) { @@ -301,8 +292,7 @@ uint8_t rssi = readRssi(); Temperature temp = readTemp(); - // TODO delay? - _delay_ms(10); + // communicate RSSI back to transmitter uint8_t payload[] = {rssi}; transmitPayload(payload, sizeof (payload), NODE2); diff --git a/rfm69.c b/rfm69.c index 90bbd36..4cb7552 100644 --- a/rfm69.c +++ b/rfm69.c @@ -11,6 +11,9 @@ static volatile uint8_t irqFlags1 = 0; static volatile uint8_t irqFlags2 = 0; +static volatile bool toena = false; +static volatile uint8_t toints = 0; + /** * Selects the radio to talk to via SPI. */ @@ -68,12 +71,27 @@ irqFlags2 = 0; } +/** + * Reads interrupt flags when a radio interrupt occurs. + */ ISR(INT0_vect) { irqFlags1 = regRead(IRQ_FLAGS1); irqFlags2 = regRead(IRQ_FLAGS2); // printString("irq\r\n"); } +/** + * Called about 30 times a second while the controller + * isn't in power down sleep mode. + */ +ISR(TIMER0_COMPA_vect) { + if (toena && toints++ >= TIMEOUT_INTS) { + toints = 0; + toena = false; + timeoutRadio(); + } +} + void initRadio(uint64_t freq, uint8_t node) { // wait a bit after power on _delay_ms(10); @@ -194,6 +212,11 @@ return regRead(PA_LEVEL); } +void timeoutEnable(bool enable) { + toena = enable; + if (!enable) toints = 0; +} + void timeoutRadio(void) { irqFlags1 |= (1 << 2); } @@ -254,8 +277,11 @@ // wait until "PayloadReady" or timeout do {} while (!(irqFlags2 & (1 << 2)) && !(irqFlags1 & (1 << 2))); + bool ready = irqFlags2 & (1 << 2); bool timeout = irqFlags1 & (1 << 2); + clearIrqFlags(); + if (ready) timeoutEnable(false); setMode(MODE_STDBY); if (timeout) { diff --git a/rfm69.h b/rfm69.h index b9530ef..baab99a 100644 --- a/rfm69.h +++ b/rfm69.h @@ -80,6 +80,8 @@ #define F_STEP 6103515625ULL #define CAST_ADDRESS 0x84 +#define TIMEOUT_INTS 3 // about 100 milliseconds + /** * Initializes the radio module with the given carrier frequency in kilohertz * and node address. @@ -101,6 +103,13 @@ uint8_t getOutputPower(void); /** + * Enables or disables the radio timeout. + * + * @param enable + */ +void timeoutEnable(bool enable); + +/** * Indicates a timeout. */ void timeoutRadio(void);