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"
14 #if defined(Regulatory_Domain_ISM_2400)
15 #include "SX1280Driver.h"
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()
35 #define CURRENT_CORE -1
38 void devicesRegister(device_affinity_t
*devices
, uint8_t count
)
43 #if defined(PLATFORM_ESP32)
44 taskSemaphore
= xSemaphoreCreateBinary();
45 completeSemaphore
= xSemaphoreCreateBinary();
47 xTaskCreatePinnedToCore(deviceTask
, "deviceTask", 3000, NULL
, 0, &xDeviceTask
, 0);
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)
65 xSemaphoreGive(taskSemaphore
);
66 xSemaphoreTake(completeSemaphore
, portMAX_DELAY
);
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)
90 xSemaphoreGive(taskSemaphore
);
91 xSemaphoreTake(completeSemaphore
, portMAX_DELAY
);
96 void devicesTriggerEvent()
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
);
141 xSemaphoreGive(completeSemaphore
);
142 xSemaphoreTake(taskSemaphore
, portMAX_DELAY
);
144 xSemaphoreGive(completeSemaphore
);
147 devicesUpdate(millis());