Simple transmitter application for SX127x radio.
Dependencies: MbedJSONValue SX127x sx12xx_hal
Diff: main.cpp
- Revision:
- 4:b2fc780be0b3
- Parent:
- 3:5e25ec621190
- Child:
- 5:a8b4e0857e8b
--- a/main.cpp Fri Sep 06 19:55:25 2019 +0000 +++ b/main.cpp Fri Sep 06 22:59:36 2019 +0000 @@ -27,25 +27,41 @@ /******************** Setup radio transmitter ****************************/ +// Configure the User button as an interrupt +InterruptIn button(USER_BUTTON, PullUp); + +// Initialize global variables +uint8_t seq = 0; // Set initial transmit sequence to 0 volatile bool txDone; CircularBuffer<uint8_t, BUF_SIZE> msg; +MbedJSONValue message; +Timer t; +// Transmit Done Callback Handler void txDoneCB() { txDone = true; + printf("Tx Done.\r\n"); } +// Receive Done Callback Handler void rxDoneCB(uint8_t size, float rssi, float snr) { + printf("Rx Done.\r\n"); } - // Define radio events for transmitter +// Transmit Timeout Callback Handler +void txTimeoutCB() +{ + printf("Tx Timeout.\r\n"); +} +// Define radio events for transmitter const RadioEvents_t rev = { /* Dio0_top_half */ NULL, /* TxDone_topHalf */ NULL, /* TxDone_botHalf */ txDoneCB, - /* TxTimeout */ NULL, + /* TxTimeout */ txTimeoutCB, /* RxDone */ rxDoneCB, /* RxTimeout */ NULL, /* RxError */ NULL, @@ -53,6 +69,9 @@ /* CadDone */ NULL }; +/** +* Function for printing the contents of the SX127x transmit buffer +*/ void print_radio_tx_buffer(uint8_t len) { printf("Tx Buffer = "); @@ -63,6 +82,9 @@ printf("\r\n\n"); } +/** +* Function for adding bytes to the circular buffer for the message. +*/ void msg_append_bytes(uint8_t data[], uint8_t len) { for(int i=0; i < len; i++) @@ -79,11 +101,20 @@ } } +/** +* Function for adding a string to the circular buffer for the message +*/ void msg_append_string(string str, uint8_t len) { msg_append_bytes(reinterpret_cast<uint8_t*>(&str[0]), len); } +/** +* Function for sending the message. +* +* @note This function copies the contents of the circular buffer to the transmit +* buffer. +*/ void msg_send() { uint8_t len = 0; @@ -98,62 +129,92 @@ print_radio_tx_buffer(len); txDone = false; Radio::Send(len, 0, 0, 0); // begin transmission of payload +} - while (!txDone) - { - Radio::service(); - } - - printf("Tx Done.\r\n"); +/** +* Interrupt handler for the User button. +*/ +void button_send() +{ + // Create an array to hold byte data + uint8_t data[] = {seq}; + + // Append the byte array to the message + msg_append_bytes(data, sizeof(data)/sizeof(data[0])); + + // Append a new value to the message + data[0] = seq+1; + msg_append_bytes(data, sizeof(data)/sizeof(data[0])); + + // Append a string to the message + string myString = "Hello World!"; + msg_append_string(myString, myString.length()); + msg_send(); + + seq+=2; // change payload (increment sequence number) } +/** +* Interrupt Handler for the User button. +*/ +void button_send_json() +{ + string s; + seq++; + + //fill the object + message["btn_count"] = seq; + message["btn_timer"] = t.read(); + message["my_str"] = "Hello World!"; + message["my_boolean"] = false; + + //serialize it into a JSON string + s = message.serialize(); + printf("json: %s\r\n", s.c_str()); + + memcpy(Radio::radio.tx_buf, s.c_str(), s.length()); + txDone = false; + Radio::Send(s.length(), 0, 0, 0); // begin transmission of payload + t.reset(); +} + +/** +* Function for initializing the SX127x radio. +*/ void radio_init() { // Start radio transmitter after POR or reset - Radio::Init(&rev); //Set radio properties for transmitter - Radio::Standby(); Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); Radio::SetChannel(CF_HZ); // Set transmitter output power - Radio::set_tx_dbm(TX_DBM); // Setup transmit packet payload -> preambleLen, fixLen, crcOn, invIQ - Radio::LoRaPacketConfig(8, false, true, false); } int main() { printf("\r\nreset-tx \n"); +// button.fall(&button_send); + button.fall(&button_send_json); radio_init(); - uint8_t seq = 0; // Set initial transmit sequence to 0 + // Start a timer to track time between button presses + t.start(); - for (;;) { - // Create an array to hold byte data - uint8_t data[] = {seq}; - - // Append the byte array to the message - msg_append_bytes(data, sizeof(data)/sizeof(data[0])); - - // Append a new value to the message - data[0] = seq+1; - msg_append_bytes(data, sizeof(data)/sizeof(data[0])); - - // Append a string to the message - string myString = "Hello World!"; - msg_append_string(myString, myString.length()); - msg_send(); - - // Transmit payload every 500mS - wait(0.5); // throttle sending rate - seq+=2; // change payload (increment sequence number) + for (;;) + { + // Transmit message on button press, service radio until tx complete. + while (!txDone) + { + Radio::service(); } + } }