scheduling

This functionality will drive the release of the LoRa buffers, but also enables the user to define their own function to be run at intervals. This will let the gateway send various stats like voltage or temp through the network.
scheduleFDRS(functionName, interval) initializes a timer to run "functionName()" every "interval" milliseconds. I'll add some documentation on how to actually inject a DataReading into the system later.
pull/136/head
Timm Bogner 1 year ago
parent 63619cfe4b
commit 9c27e11206

@ -23,7 +23,7 @@ bool is_ping = false;
#include "fdrs_gateway_filesystem.h"
#include "fdrs_gateway_mqtt.h"
#include "fdrs_gateway_serial.h"
#include "fdrs_gateway_scheduler.h"
#ifdef DEBUG_CONFIG
#include "fdrs_checkConfig.h"
#endif
@ -44,6 +44,7 @@ void beginFDRS()
DBG("Address:" + String(UNIT_MAC, HEX));
#ifdef USE_LORA
begin_lora();
scheduleFDRS(asyncReleaseLoRaFirst, FDRS_LORA_INTERVAL);
#endif
#ifdef USE_WIFI
begin_wifi();
@ -59,7 +60,6 @@ void beginFDRS()
begin_FS();
#endif
// DBG(sizeof(DataReading));
#ifdef USE_WIFI
client.publish(TOPIC_STATUS, "FDRS initialized");
#endif
@ -83,6 +83,7 @@ void handleCommands()
void loopFDRS()
{
handle_schedule();
handleCommands();
#if defined(USE_SD_LOG) || defined(USE_FS_LOG)
handleLogger();

@ -66,6 +66,13 @@
#define FDRS_LORA_SYNCWORD GLOBAL_LORA_SYNCWORD
#endif // LORA_SYNCWORD
// select LoRa Release Interval configuration
#if defined(LORA_INTERVAL)
#define FDRS_LORA_INTERVAL LORA_INTERVAL
#else
#define FDRS_LORA_INTERVAL GLOBAL_LORA_INTERVAL
#endif // LORA_INTERVAL
const uint8_t lora_size = 256 / sizeof(DataReading);
RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1);
@ -100,6 +107,7 @@ enum
uint8_t tx_buffer_position = 0;
uint32_t tx_start_time;
bool tx_time_set = false;
#endif // USE_LORA
@ -438,7 +446,7 @@ void broadcastLoRa()
{
LORABBuffer.buffer[LORABBuffer.len + i] = theData[i];
}
LORA2Buffer.len += ln;
LORABBuffer.len += ln;
#endif // USE_LORA
}
@ -474,82 +482,123 @@ void asyncReleaseLoRa(bool first_run)
if (first_run)
{
TxStatus = TxLoRa1;
tx_time_set = true;
tx_start_time = millis();
}
switch (TxStatus)
{
case TxLoRa1:
if (LORA1Buffer.len - tx_buffer_position > lora_size)
{
transmitLoRa(&LoRa1, &LORA1Buffer.buffer[tx_buffer_position], lora_size);
tx_buffer_position += lora_size;
}
else
if (LORA1Buffer.len == 0)
{
transmitLoRa(&LoRa1, &LORA1Buffer.buffer[tx_buffer_position], LORA1Buffer.len - tx_buffer_position);
tx_buffer_position = 0;
TxStatus = TxLoRa2;
}
break;
case TxLoRa2:
if (LORA2Buffer.len - tx_buffer_position > lora_size)
{
transmitLoRa(&LoRa2, &LORA2Buffer.buffer[tx_buffer_position], lora_size);
tx_buffer_position += lora_size;
goto TxL2;
}
else
{
transmitLoRa(&LoRa2, &LORA2Buffer.buffer[tx_buffer_position], LORA2Buffer.len - tx_buffer_position);
tx_buffer_position = 0;
TxStatus = TxLoRaB;
}
break;
case TxLoRaB:
if (LORABBuffer.len - tx_buffer_position > lora_size)
{
transmitLoRa(&loraBroadcast, &LORABBuffer.buffer[tx_buffer_position], lora_size);
tx_buffer_position += lora_size;
}
else
{
transmitLoRa(&loraBroadcast, &LORABBuffer.buffer[tx_buffer_position], LORABBuffer.len - tx_buffer_position);
DBG(millis() - tx_start_time);
tx_buffer_position = 0;
TxStatus = TxIdle;
if (LORA1Buffer.len - tx_buffer_position > lora_size)
{
transmitLoRa(&LoRa1, &LORA1Buffer.buffer[tx_buffer_position], lora_size);
tx_buffer_position += lora_size;
}
else
{
transmitLoRa(&LoRa1, &LORA1Buffer.buffer[tx_buffer_position], LORA1Buffer.len - tx_buffer_position);
tx_buffer_position = 0;
TxStatus = TxLoRa2;
}
break;
case TxLoRa2:
TxL2:
if (LORA2Buffer.len == 0)
{
TxStatus = TxLoRaB;
goto TxLB;
}
else
{
if (LORA2Buffer.len - tx_buffer_position > lora_size)
{
transmitLoRa(&LoRa2, &LORA2Buffer.buffer[tx_buffer_position], lora_size);
tx_buffer_position += lora_size;
}
else
{
transmitLoRa(&LoRa2, &LORA2Buffer.buffer[tx_buffer_position], LORA2Buffer.len - tx_buffer_position);
tx_buffer_position = 0;
TxStatus = TxLoRaB;
}
}
break;
case TxLoRaB:
TxLB:
// DBG(LORABBuffer.len);
if (LORABBuffer.len == 0)
{
TxStatus = TxIdle;
goto TxFin;
}
else
{
if (LORABBuffer.len - tx_buffer_position > lora_size)
{
transmitLoRa(&loraBroadcast, &LORABBuffer.buffer[tx_buffer_position], lora_size);
tx_buffer_position += lora_size;
}
else
{
transmitLoRa(&loraBroadcast, &LORABBuffer.buffer[tx_buffer_position], LORABBuffer.len - tx_buffer_position);
TxFin:
LORABBuffer.len = 0;
LORA1Buffer.len = 0;
LORA2Buffer.len = 0;
tx_time_set = false;
tx_buffer_position = 0;
TxStatus = TxIdle;
}
}
break;
}
break;
}
}
void asyncReleaseLoRaFirst()
{
asyncReleaseLoRa(true);
}
void handleLoRa()
{
if (operationDone) // the interrupt was triggered
void handleLoRa()
{
enableInterrupt = false;
operationDone = false;
if (transmitFlag) // the previous operation was transmission
if (operationDone) // the interrupt was triggered
{
if (TxStatus != TxIdle)
enableInterrupt = false;
operationDone = false;
if (transmitFlag) // the previous operation was transmission
{
asyncReleaseLoRa(false);
enableInterrupt = true;
if (TxStatus != TxIdle)
{
asyncReleaseLoRa(false);
enableInterrupt = true;
}
else
{
if (tx_time_set){
DBG("ToA: " + String(millis() - tx_start_time));
tx_time_set = false;
}
radio.startReceive(); // return to listen mode
enableInterrupt = true;
transmitFlag = false;
}
}
else
else // the previous operation was reception
{
radio.startReceive(); // return to listen mode
returnCRC = getLoRa();
if (!transmitFlag) // return to listen if no transmission was begun
{
radio.startReceive();
}
enableInterrupt = true;
transmitFlag = false;
}
}
else // the previous operation was reception
{
returnCRC = getLoRa();
if (!transmitFlag) // return to listen if no transmission was begun
{
radio.startReceive();
}
enableInterrupt = true;
}
}
}
#endif // USE_LORA

@ -1,3 +1,37 @@
void check_scheduler(){
}
typedef struct ScheduleItem
{
uint32_t interval;
uint32_t start;
void (*functionName)();
bool active;
} ScheduleItem;
ScheduleItem theSchedule[16];
bool scheduleFDRS(void (*added_func)(), uint32_t added_interval)
{
ScheduleItem arg = {.interval = added_interval, .start = millis(), .functionName = added_func, .active = true};
for (int i = 0; i < 16; i++)
{
if (!theSchedule[i].active)
{
theSchedule[i] = arg;
return true;
}
}
DBG("Schedule is full!");
return false;
}
void handle_schedule()
{
for (int i = 0; i < 16; i++)
{
if (theSchedule[i].active && (millis() - theSchedule[i].start > theSchedule[i].interval))
{
theSchedule[i].start = millis();
theSchedule[i].functionName();
}
}
}

@ -24,6 +24,7 @@
#define GLOBAL_LORA_BANDWIDTH 125.0 // LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
#define GLOBAL_LORA_CR 5 // LoRa link coding rate denominator. Allowed values range from 5 to 8.
#define GLOBAL_LORA_SYNCWORD 0x12 // LoRa sync word. Can be used to distinguish different networks. Note that 0x34 is reserved for LoRaWAN.
#define GLOBAL_LORA_INTERVAL 10000 // Interval between LoRa buffer releases. Must be longer than transmission time-on-air.
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
#define PEER_TIMEOUT 300000

Loading…
Cancel
Save