diff --git a/avrrfm.c b/avrrfm.c index 3fb3b7a..c8437e1 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -36,7 +36,7 @@ #include "unifont.h" #define TRANSMIT_FAST 1 // 4 ~ 32 seconds -#define TRANSMIT_SLOW 9 // 38 ~ 5 minutes +#define TRANSMIT_SLOW 9 // 38 ~ 5 minutes #define MAX_TIMEOUTS 9 // slow down tx attempts after so many timeouts #define LABEL_OFFSET 10 @@ -63,6 +63,9 @@ static y_t yo = 0; static width_t width = 0; +/* Averaged output power requested from transmitter */ +static int8_t power = DBM_MAX; + /** * Wakes up the controller and increments the watchdog bark counter. */ @@ -203,11 +206,12 @@ int16_t tempx10 = convertTSens(temp->raw); div_t tdiv = div(tempx10, 10); - char buf[42]; - // display some info (receiver RSSI + CRC, transmitter output power) - snprintf(buf, sizeof (buf), "RSSI: %4d dBm, CRC: %d, PA: %+3d dBm", - -_rssi, crc, temp->power); + char paf[5]; + char buf[42]; + snprintf(paf, sizeof (paf), "%+3d", temp->power); + snprintf(buf, sizeof (buf), "RSSI: %4d dBm, CRC: %d, PA: %s dBm", + -_rssi, crc, crc ? paf : "---"); const __flash Font *unifont = &unifontFont; writeString(0, 0, unifont, buf, BLACK, WHITE); @@ -267,10 +271,10 @@ uint8_t response[1]; int8_t len = rfmReceivePayload(response, sizeof (response), true); if (len > 0) { - // request more output power starting from -95 dBm - // TODO needs some hysteresis/something more elaborate + // set more output power starting from -95 dBm int8_t rssi = divRoundNearest(response[0], 2); - rfmSetOutputPower(rssi - 97); + power = divRoundNearest(power + rssi - 97, 2); + rfmSetOutputPower(min(max(power, DBM_MIN), DBM_MAX)); return false; } diff --git a/librfm.a b/librfm.a index 15715ed..c0c50de 100644 --- a/librfm.a +++ b/librfm.a Binary files differ diff --git a/librfm.h b/librfm.h index f8d598f..f921684 100644 --- a/librfm.h +++ b/librfm.h @@ -69,9 +69,11 @@ #define MODE_TX 0x0c #define MODE_RX 0x10 -#define PA_OFF 18 +#define DBM_MIN -2 +#define DBM_MAX 13 #define PA_MIN 16 #define PA_MAX 31 +#define PA_OFF 18 #define MESSAGE_SIZE 63 #define F_STEP 6103515625ULL diff --git a/usart.c b/usart.c index 93cb8d9..6c6571a 100644 --- a/usart.c +++ b/usart.c @@ -33,7 +33,7 @@ UCSR0B = (1 << TXEN0) | (1 << RXEN0); UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); - + // enable USART RX complete interrupt 0 UCSR0B |= (1 << RXCIE0); } @@ -60,8 +60,8 @@ } } -void printUint(uint8_t data) { - char buf[6]; +void printInt(int8_t data) { + char buf[7]; snprintf(buf, sizeof (buf), "%d\r\n", data); printString(buf); } diff --git a/usart.h b/usart.h index d533251..1ae4cd3 100644 --- a/usart.h +++ b/usart.h @@ -46,9 +46,9 @@ void printString(const char *data); /** - * Prints the given unsigned integer including CR + LF via USART. + * Prints the given integer including CR + LF via USART. */ -void printUint(uint8_t data); +void printInt(int8_t data); /** * Prints the given unsigned integer in hex notation including CR + LF