diff --git a/crossbow/config.h b/crossbow/config.h index ebd82da..9dc179b 100644 --- a/crossbow/config.h +++ b/crossbow/config.h @@ -15,8 +15,8 @@ * DEVICE_MODE_TX * DEVICE_MODE_RX */ -// #define DEVICE_MODE_TX -#define DEVICE_MODE_RX +#define DEVICE_MODE_TX +// #define DEVICE_MODE_RX #define FEATURE_TX_OLED // #define FORCE_TX_WITHOUT_INPUT diff --git a/crossbow/crossbow.ino b/crossbow/crossbow.ino index 3aa6fd9..e3c3138 100644 --- a/crossbow/crossbow.ino +++ b/crossbow/crossbow.ino @@ -87,8 +87,6 @@ QspConfiguration_t qsp = {}; RxDeviceState_t rxDeviceState = {}; TxDeviceState_t txDeviceState = {}; -uint8_t tmpBuffer[MAX_PACKET_SIZE]; - void onQspSuccess(QspConfiguration_t *qsp, TxDeviceState_t *txDeviceState, RxDeviceState_t *rxDeviceState, uint8_t receivedChannel) { //If recide received a valid frame, that means it can start to talk radioNode.lastReceivedChannel = receivedChannel; @@ -162,30 +160,7 @@ void setup(void) qsp.deviceState = DEVICE_STATE_OK; #endif - /* - * Setup hardware - */ - LoRa.setPins( - LORA_SS_PIN, - LORA_RST_PIN, - LORA_DI0_PIN - ); - - if (!LoRa.begin(radioNode.getFrequencyForChannel(radioNode.getChannel()))) { - while (true); - } - - //Configure LoRa module - LoRa.setSignalBandwidth(radioNode.loraBandwidth); - LoRa.setSpreadingFactor(radioNode.loraSpreadingFactor); - LoRa.setCodingRate4(radioNode.loraCodingRate); - LoRa.setTxPower(radioNode.loraTxPower); - LoRa.enableCrc(); - - //Setup ISR callback and start receiving - LoRa.onReceive(onReceive); - LoRa.receive(); - radioNode.deviceState = RADIO_STATE_RX; + radioNode.init(LORA_SS_PIN, LORA_RST_PIN, LORA_DI0_PIN, onReceive); #ifdef DEVICE_MODE_RX //initiallize default ppm values @@ -485,17 +460,7 @@ void loop(void) if (qsp.canTransmit && transmitPayload) { - uint8_t size; - LoRa.beginPacket(); - //Prepare packet - qspEncodeFrame(&qsp, tmpBuffer, &size, radioNode.getChannel()); - //Sent it to radio in one SPI transaction - LoRa.write(tmpBuffer, size); - LoRa.endPacketAsync(); - - //Set state to be able to detect the moment when TX is done - radioNode.deviceState = RADIO_STATE_TX; - + radioNode.handleTx(&qsp); transmitPayload = false; } diff --git a/crossbow/radio_node.cpp b/crossbow/radio_node.cpp index c44461e..39fa248 100644 --- a/crossbow/radio_node.cpp +++ b/crossbow/radio_node.cpp @@ -5,6 +5,33 @@ RadioNode::RadioNode(void) { } +void RadioNode::init(uint8_t ss, uint8_t rst, uint8_t di0, void(*callback)(int)) { + /* + * Setup hardware + */ + LoRa.setPins( + ss, + rst, + di0 + ); + + if (!LoRa.begin(getFrequencyForChannel(getChannel()))) { + while (true); + } + + //Configure LoRa module + LoRa.setSignalBandwidth(loraBandwidth); + LoRa.setSpreadingFactor(loraSpreadingFactor); + LoRa.setCodingRate4(loraCodingRate); + LoRa.setTxPower(loraTxPower); + LoRa.enableCrc(); + + //Setup ISR callback and start receiving + LoRa.onReceive(callback); + LoRa.receive(); + deviceState = RADIO_STATE_RX; +} + void RadioNode::readRssi(void) { rssi = 164 - constrain(LoRa.packetRssi() * -1, 0, 164); @@ -111,4 +138,19 @@ void RadioNode::handleTxDoneState(void) { deviceState = RADIO_STATE_RX; nextTxCheckMillis = currentMillis + 1; //We check of TX done every 1ms } +} + +void RadioNode::handleTx(QspConfiguration_t *qsp) { + uint8_t size; + uint8_t tmpBuffer[MAX_PACKET_SIZE]; + + LoRa.beginPacket(); + //Prepare packet + qspEncodeFrame(qsp, tmpBuffer, &size, getChannel()); + //Sent it to radio in one SPI transaction + LoRa.write(tmpBuffer, size); + LoRa.endPacketAsync(); + + //Set state to be able to detect the moment when TX is done + deviceState = RADIO_STATE_TX; } \ No newline at end of file diff --git a/crossbow/radio_node.h b/crossbow/radio_node.h index 80d9123..e88fcc1 100644 --- a/crossbow/radio_node.h +++ b/crossbow/radio_node.h @@ -18,6 +18,7 @@ class RadioNode { public: RadioNode(void); + void init(uint8_t ss, uint8_t rst, uint8_t di0, void(*callback)(int)); void readRssi(void); void readSnr(void); static uint32_t getFrequencyForChannel(uint8_t channel); @@ -33,6 +34,7 @@ class RadioNode { uint32_t getChannelEntryMillis(void); void handleChannelDwell(void); void handleTxDoneState(); + void handleTx(QspConfiguration_t *qsp); int8_t bytesToRead = -1; uint8_t deviceState = RADIO_STATE_RX; uint8_t rssi = 0;