Merge pull request #1269 from pkendall64/crsf-max-output
[ExpressLRS.git] / src / lib / DEVICE / device.cpp
blobe79c5797fb1fcbfac379eb93b80ef0057b2a23f0
1 #include "targets.h"
2 #include "common.h"
3 #include "logging.h"
4 #include "helpers.h"
5 #include "device.h"
7 ///////////////////////////////////////
8 // Even though we aren't using anything this keeps the PIO dependency analyzer happy!
10 #if defined(Regulatory_Domain_AU_915) || defined(Regulatory_Domain_EU_868) || defined(Regulatory_Domain_IN_866) || defined(Regulatory_Domain_FCC_915) || defined(Regulatory_Domain_AU_433) || defined(Regulatory_Domain_EU_433)
11 #include "SX127xDriver.h"
12 #endif
14 #if defined(Regulatory_Domain_ISM_2400)
15 #include "SX1280Driver.h"
16 #endif
18 ///////////////////////////////////////
20 static device_affinity_t *uiDevices;
21 static uint8_t deviceCount;
23 static bool eventFired[2] = {false, false};
24 static connectionState_e lastConnectionState[2] = {disconnected, disconnected};
26 static unsigned long deviceTimeout[16] = {0};
28 #if defined(PLATFORM_ESP32)
29 static TaskHandle_t xDeviceTask = NULL;
30 static SemaphoreHandle_t taskSemaphore;
31 static SemaphoreHandle_t completeSemaphore;
32 static void deviceTask(void *pvArgs);
33 #define CURRENT_CORE xPortGetCoreID()
34 #else
35 #define CURRENT_CORE -1
36 #endif
38 void devicesRegister(device_affinity_t *devices, uint8_t count)
40 uiDevices = devices;
41 deviceCount = count;
43 #if defined(PLATFORM_ESP32)
44 taskSemaphore = xSemaphoreCreateBinary();
45 completeSemaphore = xSemaphoreCreateBinary();
46 disableCore0WDT();
47 xTaskCreatePinnedToCore(deviceTask, "deviceTask", 3000, NULL, 0, &xDeviceTask, 0);
48 #endif
51 void devicesInit()
53 int32_t core = CURRENT_CORE;
55 for(size_t i=0 ; i<deviceCount ; i++) {
56 if (uiDevices[i].core == core || core == -1) {
57 if (uiDevices[i].device->initialize) {
58 (uiDevices[i].device->initialize)();
62 #if defined(PLATFORM_ESP32)
63 if (core == 1)
65 xSemaphoreGive(taskSemaphore);
66 xSemaphoreTake(completeSemaphore, portMAX_DELAY);
68 #endif
71 void devicesStart()
73 int32_t core = CURRENT_CORE;
74 unsigned long now = millis();
76 for(size_t i=0 ; i<deviceCount ; i++)
78 if (uiDevices[i].core == core || core == -1) {
79 deviceTimeout[i] = 0xFFFFFFFF;
80 if (uiDevices[i].device->start)
82 int delay = (uiDevices[i].device->start)();
83 deviceTimeout[i] = delay == DURATION_NEVER ? 0xFFFFFFFF : now + delay;
87 #if defined(PLATFORM_ESP32)
88 if (core == 1)
90 xSemaphoreGive(taskSemaphore);
91 xSemaphoreTake(completeSemaphore, portMAX_DELAY);
93 #endif
96 void devicesTriggerEvent()
98 eventFired[0] = true;
99 eventFired[1] = true;
102 void devicesUpdate(unsigned long now)
104 int32_t core = CURRENT_CORE;
106 bool handleEvents = eventFired[core==-1?0:core];
107 eventFired[core==-1?0:core] = false;
109 for(size_t i=0 ; i<deviceCount ; i++)
111 if (uiDevices[i].core == core || core == -1) {
112 if ((handleEvents || lastConnectionState[core==-1?0:core] != connectionState) && uiDevices[i].device->event)
114 int delay = (uiDevices[i].device->event)();
115 if (delay != DURATION_IGNORE)
117 deviceTimeout[i] = delay == DURATION_NEVER ? 0xFFFFFFFF : now + delay;
122 lastConnectionState[core==-1?0:core] = connectionState;
124 for(size_t i=0 ; i<deviceCount ; i++)
126 if (uiDevices[i].core == core || core == -1) {
127 if (uiDevices[i].device->timeout && now >= deviceTimeout[i])
129 int delay = (uiDevices[i].device->timeout)();
130 deviceTimeout[i] = delay == DURATION_NEVER ? 0xFFFFFFFF : now + delay;
136 #if defined(PLATFORM_ESP32)
137 static void deviceTask(void *pvArgs)
139 xSemaphoreTake(taskSemaphore, portMAX_DELAY);
140 devicesInit();
141 xSemaphoreGive(completeSemaphore);
142 xSemaphoreTake(taskSemaphore, portMAX_DELAY);
143 devicesStart();
144 xSemaphoreGive(completeSemaphore);
145 for (;;)
147 devicesUpdate(millis());
150 #endif