diff --git a/avrrfm.c b/avrrfm.c index ae9c233..bd90c6b 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -66,6 +66,14 @@ } /** + * Called about 30 times a second while the controller + * isn't in power down sleep mode. + */ +ISR(TIMER0_COMPA_vect) { + timeRadio(); +} + +/** * Sets up the pins. */ static void initPins(void) { @@ -177,20 +185,6 @@ } /** - * Waits for a response from the receiver with timeout. - */ -static void waitResponse(void) { - timeoutEnable(true); - uint8_t response[1]; - int8_t len = receivePayload(response, sizeof (response)); - if (len > 0) { - // receiver RSSI - int8_t rssi = divRoundNearest(response[0], 2); - setOutputPower(rssi); - } -} - -/** * Converts the raw temperature to °C and lets it float around the display. * * @param rssi RSSI value @@ -241,6 +235,37 @@ return temp; } +/** + * Handles the payload received from the transmitter. + * + * @param flags + */ +static void handlePayload(PayloadFlags flags) { + uint8_t rssi = readRssi(); + Temperature temp = readTemp(); + + // communicate RSSI back to transmitter + uint8_t payload[] = {rssi}; + transmitPayload(payload, sizeof (payload), NODE2); + + displayTemp(rssi, flags.crc, &temp); + startReceive(); +} + +/** + * Waits for a response from the receiver with timeout. + */ +static void waitResponse(void) { + timeoutEnable(true); + uint8_t response[1]; + int8_t len = receivePayload(response, sizeof (response)); + if (len > 0) { + // receiver RSSI + int8_t rssi = divRoundNearest(response[0], 2); + setOutputPower(rssi); + } +} + int main(void) { initUSART(); initPins(); @@ -289,15 +314,7 @@ } else { PayloadFlags flags = payloadReady(); if (flags.ready) { - uint8_t rssi = readRssi(); - Temperature temp = readTemp(); - - // communicate RSSI back to transmitter - uint8_t payload[] = {rssi}; - transmitPayload(payload, sizeof (payload), NODE2); - - displayTemp(rssi, flags.crc, &temp); - startReceive(); + handlePayload(flags); } } diff --git a/rfm69.c b/rfm69.c index 4cb7552..2481426 100644 --- a/rfm69.c +++ b/rfm69.c @@ -80,18 +80,6 @@ // 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); @@ -212,6 +200,13 @@ return regRead(PA_LEVEL); } +void timeRadio(void) { + if (toena && toints++ >= TIMEOUT_INTS) { + timeoutEnable(false); + timeoutRadio(); + } +} + void timeoutEnable(bool enable) { toena = enable; if (!enable) toints = 0; diff --git a/rfm69.h b/rfm69.h index baab99a..c54f2ef 100644 --- a/rfm69.h +++ b/rfm69.h @@ -103,6 +103,11 @@ uint8_t getOutputPower(void); /** + * Gives a timer pulse to the radio. + */ +void timeRadio(void); + +/** * Enables or disables the radio timeout. * * @param enable