-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
161 lines (135 loc) · 3.77 KB
/
main.cpp
File metadata and controls
161 lines (135 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Arduino.h"
#include "Pins.h"
#include "WebCfgServer.h"
#include <RTOS.h>
#include "PreferencesKeys.h"
#include "PresenceDetection.h"
#include "hardware/W5500EthServer.h"
#include "hardware/WifiEthServer.h"
#include "Gpio.h"
#include "Logger.h"
Network* network = nullptr;
Gpio* gpio = nullptr;
WebCfgServer* webCfgServer = nullptr;
BleScanner::Scanner* bleScanner = nullptr;
PresenceDetection* presenceDetection = nullptr;
Preferences* preferences = nullptr;
EthServer* ethServer = nullptr;
RTC_NOINIT_ATTR int restartReason;
RTC_NOINIT_ATTR uint64_t restartReasonValid;
void networkTask(void *pvParameters)
{
while(true)
{
int r = network->update();
switch(r)
{
case 0:
case 1:
network->update();
webCfgServer->update();
break;
// Neither Network Devicce or MQTT is connected
default:
network->update();
break;
}
delay(50);
}
}
void bleScannerTask(void *pvParameters)
{
while(true)
{
bleScanner->update();
delay(20);
}
}
void presenceDetectionTask(void *pvParameters)
{
while(true)
{
presenceDetection->update();
}
}
void checkMillisTask(void *pvParameters)
{
while(true)
{
delay(60000);
// millis() is about to overflow. Restart device to prevent problems with overflow
if(millis() > (2^32) - 5 * 60000)
{
Serial.println(F("millis() is about to overflow. Restarting device."));
vTaskDelay( 2000 / portTICK_PERIOD_MS);
ESP.restart();
}
}
}
void setupTasks()
{
// configMAX_PRIORITIES is 25
xTaskCreatePinnedToCore(networkTask, "ntw", 8192, NULL, 3, NULL, 1);
xTaskCreatePinnedToCore(bleScannerTask, "scan", 4096, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(presenceDetectionTask, "prdet", 1024, NULL, 5, NULL, 1);
xTaskCreatePinnedToCore(checkMillisTask, "mlchk", 768, NULL, 1, NULL, 1);
}
uint32_t getRandomId()
{
uint8_t rnd[4];
for(int i=0; i<4; i++)
{
rnd[i] = random(255);
}
uint32_t deviceId;
memcpy(&deviceId, &rnd, sizeof(deviceId));
return deviceId;
}
void initEthServer(const NetworkDeviceType device)
{
switch (device)
{
case NetworkDeviceType::W5500:
ethServer = new W5500EthServer(80);
break;
case NetworkDeviceType::WiFi:
ethServer = new WifiEthServer(80);
break;
default:
ethServer = new WifiEthServer(80);
break;
}
}
void setup()
{
pinMode(NETWORK_SELECT, INPUT_PULLUP);
Serial.begin(115200);
Log = &Serial;
preferences = new Preferences();
preferences->begin("blescanner", false);
// const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi;
const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500;
network = new Network(preferences);
network->initialize();
if(preferences->getBool(preference_gpio_enabled))
{
gpio = new Gpio(network);
}
uint32_t deviceId = preferences->getUInt(preference_deviceId);
if(deviceId == 0)
{
deviceId = getRandomId();
preferences->putUInt(preference_deviceId, deviceId);
}
initEthServer(networkDevice);
bleScanner = new BleScanner::Scanner();
bleScanner->initialize("blescanner");
bleScanner->setScanDuration(10);
webCfgServer = new WebCfgServer(network, ethServer, preferences, networkDevice == NetworkDeviceType::WiFi);
webCfgServer->initialize();
presenceDetection = new PresenceDetection(preferences, bleScanner, network);
presenceDetection->initialize();
setupTasks();
}
void loop()
{}