-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp32-scoring-device.ino
187 lines (174 loc) · 5.97 KB
/
esp32-scoring-device.ino
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//Copyright (c) Piet Wauters 2022 <[email protected]>
/*! \mainpage ESP32 Scoring Machine for fencing
*
* \section intro_sec Introduction
*
* For more information about this project pls. go to https://github.com/pietwauters/esp32-scoring-device/wiki
*
* \section main_structure_sec Main structure
* The esp32 has 2 cores. One core is used for the physical scoring device sensor.
* The other core is used for everything else: State machine, timers, network, Cyrano, Configuration Portal, ...
*
* \subsection The sensor
*
*
*/
//#include "LedMatrix.h"
#include "3WeaponSensor.h"
#include "WS2812BLedStrip.h"
#include "FencingStateMachine.h"
#include "FPA422Handler.h"
#include "RepeaterReceiver.h"
#include "RepeaterSender.h"
#include "UDPIOHandler.h"
#include "TimeScoreDisplay.h"
#include "CyranoHandler.h"
#include "driver/adc.h"
#include "esp_task_wdt.h"
#include "network.h"
WS2812B_LedStrip *MyLedStrip;
TimeScoreDisplay *MyTimeScoreDisplay;
MultiWeaponSensor *MySensor;
NetWork *MyNetWork;
FencingStateMachine *MyStatemachine;
FPA422Handler *MyFPA422Handler;
UDPIOHandler *MyUDPIOHandler;
CyranoHandler *MyCyranoHandler;
RepeaterReceiver *MyRepeaterReiver;
RepeaterSender *MyRepeaterSender;
bool bIsRepeater = false;
void setup() {
cout << "Time to get here: " << millis() << endl;
MyLedStrip = &WS2812B_LedStrip::getInstance();
MyLedStrip->begin();
MyTimeScoreDisplay = new TimeScoreDisplay();
MyLedStrip->ClearAll();
MySensor = &MultiWeaponSensor::getInstance();
MyLedStrip->ClearAll();
MyStatemachine= &FencingStateMachine::getInstance();
// put your setup code here, to run once:
Serial.begin(115200);
MyLedStrip->ClearAll();
MyTimeScoreDisplay->begin();
MyLedStrip->ClearAll();
MyTimeScoreDisplay->DisplayPisteId();
MyFPA422Handler = new FPA422Handler();
Preferences mypreferences;
mypreferences.begin("scoringdevice", RO_MODE);
bIsRepeater = mypreferences.getBool("RepeaterMode",false);
mypreferences.end();
MyNetWork = &NetWork::getInstance();
MyNetWork->begin();
MyNetWork->GlobalStartWiFi();
Serial.println("Wifi started");
Serial.println("by now the you should have seen all the lights one by one");
MyUDPIOHandler = &UDPIOHandler::getInstance();
MyUDPIOHandler->ConnectToAP();
MyUDPIOHandler->attach(*MyNetWork);
esp_task_wdt_init(20, false);
// In repeater mode don't start these 2 tasks
if(!bIsRepeater){
Serial.println("Bwahahaaha I am the master!");
MyCyranoHandler = &CyranoHandler::getInstance();
MyStatemachine->ResetAll();
MyFPA422Handler->StartWiFi();
MyStatemachine->attach(*MyFPA422Handler);
MyStatemachine->attach(*MyTimeScoreDisplay);
MyUDPIOHandler->attach(*MyStatemachine);
MyStatemachine->attach(*MyUDPIOHandler);
MyStatemachine->attach(*MyCyranoHandler);
MyUDPIOHandler->attach(*MyCyranoHandler);
MyCyranoHandler->attach(*MyStatemachine);
MyCyranoHandler->attach(*MyFPA422Handler);
MySensor->attach(*MyStatemachine);
MyStatemachine->RegisterMultiWeaponSensor(MySensor);
MyStatemachine->begin();
MySensor->begin();
MyCyranoHandler->Begin();
MyRepeaterSender = &RepeaterSender::getInstance();
MyRepeaterSender->begin();
MyStatemachine->attach(*MyRepeaterSender);
MyStatemachine->attach(*MyLedStrip);
MyStatemachine->SetMachineWeapon(MySensor->GetActualWeapon());
switch (MySensor->GetActualWeapon()) {
case FOIL:
MyStatemachine->StateChanged(EVENT_WEAPON | WEAPON_MASK_FOIL);
break;
case EPEE:
MyStatemachine->StateChanged(EVENT_WEAPON | WEAPON_MASK_EPEE);
break;
case SABRE:
MyStatemachine->StateChanged(EVENT_WEAPON | WEAPON_MASK_SABRE);
break;
}
}
else{
// When running in repeater mode
MyRepeaterReiver = &RepeaterReceiver::getInstance();
Serial.println("Ouch! I am a repeater!");
MyRepeaterReiver->begin();
MyRepeaterReiver->attach(*MyLedStrip);
MyRepeaterReiver->attach(*MyTimeScoreDisplay);
MyRepeaterReiver->StartWatchDog();
MyLedStrip->SetMirroring(MyRepeaterReiver->Mirror());
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(1); // without this it simply doesn't work
//vTaskDelay(1 / portTICK_PERIOD_MS);
if(!bIsRepeater){
MyFPA422Handler->WifiPeriodicalUpdate();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
MyTimeScoreDisplay->ProcessEvents();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
MyFPA422Handler->WifiPeriodicalUpdate();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
if(MyNetWork->IsExternalWifiAvailable())
{
MyCyranoHandler->PeriodicallyBroadcastStatus();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
MyCyranoHandler->CheckConnection();
MyFPA422Handler->WifiPeriodicalUpdate(); // Not really needed because already done above
}
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
if(MyStatemachine->IsConnectedToRemote())
{
MyTimeScoreDisplay->CycleScoreMatchAndTimeWhenNotFighting();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
MyFPA422Handler->WifiPeriodicalUpdate();
MyFPA422Handler->WifiPeriodicalUpdate();
}
MyStatemachine->PeriodicallyBroadcastFullState(MyRepeaterSender,FULL_STATUS_REPETITION_PERIOD);
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
MyRepeaterSender->RepeatLastMessage();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
//MyRepeaterSender->BroadcastHeartBeat();
if(MyStatemachine->GoToSleep())
{
prepareforDeepSleep();
}
}
else{ // when in repeater mode
MyTimeScoreDisplay->ProcessEvents();
MyTimeScoreDisplay->CycleScoreMatchAndTimeWhenNotFighting();
esp_task_wdt_reset();
vTaskDelay(1 / portTICK_PERIOD_MS);
if(MyRepeaterReiver->IsWatchDogTriggered())
{ // We lost connection with the master scoring device
// clear displays and start looking for MasterId
MyTimeScoreDisplay->DisplayPisteId();
MyLedStrip->ClearAll();
MyNetWork->FindAndSetMasterChannel(1,false);
}
}
esp_task_wdt_reset();
}