From cfa740209f02186e9d8cfdf36a1e8310f72d0928 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Thu, 26 Sep 2024 16:37:58 -0700 Subject: [PATCH] Changed C radio source to CPP --- src/Radio/Radio.c | 16 ----------- src/Radio/Radio.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++ src/Radio/Radio.h | 18 +++++++++++- 3 files changed, 85 insertions(+), 17 deletions(-) delete mode 100644 src/Radio/Radio.c create mode 100644 src/Radio/Radio.cpp diff --git a/src/Radio/Radio.c b/src/Radio/Radio.c deleted file mode 100644 index a00700c..0000000 --- a/src/Radio/Radio.c +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Author: username - * Date: 2024 - * filename: Radio.c - * description: module_purpose - */ - -#include "Radio.h" - -// dumb test function -int add_two(int a) -{ - int b = a; - b += 2; - return b; -} diff --git a/src/Radio/Radio.cpp b/src/Radio/Radio.cpp new file mode 100644 index 0000000..d28f6d8 --- /dev/null +++ b/src/Radio/Radio.cpp @@ -0,0 +1,68 @@ +/* + * Author: username + * Date: 2024 + * filename: Radio.c + * description: module_purpose + */ + +#include "Radio.h" + +void Radio_setup(void) { + pinMode(RFM95_RST, OUTPUT); + digitalWrite(RFM95_RST, HIGH); + + while(!rf95.init()){} + Serial.println("radio: Initialized"); + + if(rf95.setFrequency(RF95_FREQ)) { + Serial.println("radio: Error could not set frequency"); + while(true){} //Loop forever + } + Serial.print("radio: Frequency = "); + Serial.println(RF95_FREQ); + + rf95.setTxPower(RF95_MIN_DB, false); +} + + +void Radio_reset(void) { + digitalWrite(RFM95_RST, LOW); + delay(10); + digitalWrite(RFM95_RST, HIGH); + delay(10); +} + + +int Radio_SendData() { + int8_t tx_power = RF95_MIN_DB; + uint8_t received_id = 0; + + //Set the radio output to lowest power. + rf95.setTxPower(tx_power, false); + + //Set the radio into TX mode. + rf95.setModeTx(); + + //Try transmitting data + rf95.send(node.id, 1); + rf95.waitPacketSent(); + + //Wait for ACK or Radio Msg. (Timeout Should be included). + //On timeout increase the transmit power and re-attempt. + if(!rf95.waitAvailbleTimeout(RF95_TIMEOUT)) { + if(rf95.recv(reciver_buffer, RF95_BUF_SZ)) { + + } + else { + //Failed to receive the message. + } + } + else { + //No reply of any kind. + } + + //Set the radio mode to idle. + rf95.setModeIdle(); +} + + diff --git a/src/Radio/Radio.h b/src/Radio/Radio.h index c373d97..316e2a0 100644 --- a/src/Radio/Radio.h +++ b/src/Radio/Radio.h @@ -9,6 +9,22 @@ #define RADIO -int add_two(int a); +/** + * @brief Preforms radio setup using defined values. + */ +void Radio_setup(void); + +/** + * @brief Resets the radio module. + */ +void Radio_reset(void); + + +/** + * @brief Sends data to any receiving radios. + */ +int Radio_SendData(); + + #endif //RADIO