-
Notifications
You must be signed in to change notification settings - Fork 0
/
TschengginsLaempli.ino
230 lines (209 loc) · 8.15 KB
/
TschengginsLaempli.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*!
\file
\brief flipflip's Tschenggins Lämpli: documentation (see \ref mainpage)
- Copyright (c) 2017-2020 Philippe Kehl (flipflip at oinkzwurgl dot org),
https://oinkzwurgl.org/projaeggd/tschenggins-laempli
*/
#define DO_TESTS 0
/* ********************************************************************************************** */
#include <Arduino.h>
#if defined(ESP8266)
# include <ESP8266WiFi.h>
#elif defined(ESP32)
# include <WiFi.h>
#else
# error Unsupported target!
#endif
#if DO_TESTS
# include <Ticker.h>
# include <SPI.h>
#endif
#include "src/stuff.h"
#include "src/config.h"
#include "src/debug.h"
#include "src/status.h"
#include "src/leds.h"
#include "src/cfg.h"
#include "src/wifi.h"
#include "src/backend.h"
#include "src/jenkins.h"
/* ********************************************************************************************** */
void setup()
{
// initialise stuff
debugInit();
statusInit();
cfgInit();
ledsInit();
jenkinsInit();
backendInit();
randomSeed(ESP.getCycleCount());
// say hello
NOTICE("--------------------------------------------------------------------------------------------");
NOTICE("Tschenggins Lämpli " CONFIG_SOFTWARE_VERSION
" (" CONFIG_VERSION_GIT_HASH ", " CONFIG_PLATFORM_NAME ", " CONFIG_VERSION_YYYYMMDD " " CONFIG_VERSION_HHMMSS ")");
NOTICE("Copyright (c) 2018-2020 Philippe Kehl & flipflip industries <flipflip at oinkzwurgl dot org>");
NOTICE("Parts copyright by others. See source code.");
NOTICE("https://oinkzwurgl.org/projaeggd/tschenggins-laempli");
NOTICE("--------------------------------------------------------------------------------------------");
#if defined(ESP8266)
DEBUG("ESP8266: chip=0x%08x, bootVer=%u, bootMode=%u, cpu=%uMHz",
ESP.getChipId(), ESP.getBootVersion(), ESP.getBootMode(), ESP.getCpuFreqMHz());
DEBUG("ESP8266: %s", ESP.getFullVersion().c_str());
DEBUG("ESP8266: flash: id=0x%08x, size=%u (%uKiB), real=%u (%uKiB), speed=%ukHz",
ESP.getFlashChipId(), ESP.getFlashChipSize(), ESP.getFlashChipSize() >> 10,
ESP.getFlashChipRealSize(), ESP.getFlashChipRealSize() >> 10,
ESP.getFlashChipSpeed() / 1000);
DEBUG("ESP8266: heap: free=%u, maxBlock=%u, frag=%u%%",
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(), ESP.getHeapFragmentation());
switch ((enum rst_reason)ESP.getResetInfoPtr()->reason)
{
case REASON_WDT_RST:
case REASON_EXCEPTION_RST:
case REASON_SOFT_WDT_RST:
ERROR("ESP8266: reset: %s (%s)", ESP.getResetReason().c_str(), ESP.getResetInfo().c_str());
break;
case REASON_DEFAULT_RST:
WARNING("ESP8266: reset: %s (%s)", ESP.getResetReason().c_str(), ESP.getResetInfo().c_str());
break;
default:
DEBUG("ESP8266: reset: %s (%s)", ESP.getResetReason().c_str(), ESP.getResetInfo().c_str());
break;
}
//ESP.resetFreeContStack(); uint32_t maxStack = ESP.getFreeContStack();
#elif defined(ESP32)
DEBUG("ESP32: rev=0x%02x, cpu=%uMHz", ESP.getChipRevision(), ESP.getCpuFreqMHz());
DEBUG("ESP32: %s", ESP.getSdkVersion());
DEBUG("ESP32: flash: size=%u (%uKiB)", ESP.getFlashChipSize(), ESP.getFlashChipSize() >> 10);
DEBUG("ESP32: heap: "/*size=%u, "*/"free=%u, maxBlock=%u, minFree=%u",
/*EPS.getHeapSize(), */ESP.getFreeHeap(), ESP.getMaxAllocHeap(), ESP.getMinFreeHeap());
#endif
DEBUG("GCC: " __VERSION__); // /*", FreeRTOS " tskKERNEL_VERSION_NUMBER */
//DEBUG("Arduino: sketch=%u (%uKiB), free=%u (%uKiB)",
// ESP.getSketchSize(), ESP.getSketchSize() >> 10,
// ESP.getFreeSketchSpace(), ESP.getFreeSketchSpace() >> 10);
// initialise more things
wifiInit();
NOTICE("Here we go...");
statusLed(STATUS_LED_OFFLINE);
statusNoise(STATUS_NOISE_FFI);
}
void loop()
{
#if DO_TESTS
NOTICE("\n\n\n\nDO_TESTS start\n");
# if 1
NOTICE("\n\n\n\nTEST config\n");
const char jsonConfigBad[] = "{\"driver\":\"WS2801\",\"model\":\"badword\",\"noise\":\"some\",\"order\":\"RGB\"}";
cfgApply(jsonConfigBad);
const char jsonConfigGood[] = "{\"driver\":\"WS2801\",\"model\":\"standard\",\"noise\":\"most\",\"order\":\"RGB\",\"bright\":\"medium\"}";
cfgApply(jsonConfigGood);
delay(1500);
# endif
# if 1
NOTICE("\n\n\n\nTEST leds\n");
for (uint8_t o = 0; o < 4; o++)
{
for (uint8_t i = 0; i < CONFIG_NUM_CH; i++)
{
switch (rand() % 4)
{
case 0: {
LEDS_PARAM_t ledState1 = LEDS_MAKE_PARAM((o * 30) + (i * 45), 255, 255, STILL, 0);
ledsSetState(i, &ledState1);
break; }
case 1: {
LEDS_PARAM_t ledState2 = LEDS_MAKE_PARAM((o * 30) + (i * 45), 255, 255, PULSE, 0);
ledsSetState(i, &ledState2);
break; }
case 2: {
LEDS_PARAM_t ledState3 = LEDS_MAKE_PARAM((o * 30) + (i * 45), 255, 255, FLICKER, 0);
ledsSetState(i, &ledState3);
break; }
case 3: {
LEDS_PARAM_t ledState4 = LEDS_MAKE_PARAM((o * 30) + (i * 45), 255, 255, BLINK, LEDS_FPS / 3);
ledsSetState(i, &ledState4);
break; }
}
}
delay(3500);
}
for (uint8_t i = 0; i < CONFIG_NUM_CH; i++)
{
LEDS_PARAM_t ledState = LEDS_MAKE_PARAM(0, 0, 0, STILL, 0);
ledsSetState(i, &ledState);
}
# endif
# if 1
NOTICE("\n\n\n\nTEST status noise\n");
statusNoise(STATUS_NOISE_ABORT); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_FAIL); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_ONLINE); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_OTHER); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_TICK); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_ERROR); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_BOMB); while (statusTonePlaying()) { delay(100); }; delay(1000);
statusNoise(STATUS_NOISE_FFI); while (statusTonePlaying()) { delay(100); }; delay(1000);
# endif
# if 1
NOTICE("\n\n\n\nTEST melody\n");
statusMelody(PSTR("random")); while (statusTonePlaying()) {delay(100); }; delay(1000);
# endif
# if 1
NOTICE("\n\n\n\nTEST status led\n");
statusLed(STATUS_LED_HEARTBEAT); delay(5000);
statusLed(STATUS_LED_OFFLINE); delay(5000);
statusLed(STATUS_LED_FAIL); delay(5000);
statusLed(STATUS_LED_UPDATE); delay(5000);
statusLed(STATUS_LED_NONE); delay(5000);
# endif
DEBUG("\n\n\n\nDO_TESTS done\n");
delay(10000);
#endif // DO_TESTS
#if !DO_TESTS
// wait for wifi connection
PRINT("main: waiting for wifi connection");
while (!wifiWaitForConnect())
{
delay(100);
}
// connect to backend
PRINT("main: attempting connection to backend");
const bool res = wifiConnectBackend();
statusLed(STATUS_LED_FAIL);
// planned reconnect
if (res)
{
delay(1000);
return;
}
// connection failed, determine how long we wait to attempt a reconnect
static uint32_t lastFail;
const uint32_t now = millis();
int waitTime = (now - lastFail) > (1000 * CONFIG_STABLE_CONN_THRS) ?
CONFIG_RECONNECT_DELAY : CONFIG_RECONNECT_DELAY_SLOW;
lastFail = now;
PRINT("main: failure... waiting %us", waitTime);
// wait until timeout and wifi ready
while (waitTime > 0)
{
while (!wifiWaitForConnect())
{
delay(100);
}
// announce countdown
if ( (waitTime < 10) || ((waitTime % 10) == 0) )
{
DEBUG("main: wait... %d", waitTime);
}
if (waitTime <= 3)
{
statusNoise(STATUS_NOISE_TICK);
while (statusTonePlaying()) { delay(1); } // or the delay(1000) below won't work (on ESP8266) FIXME: wtf?!
}
waitTime--;
delay(1000);
}
// and keep going...
#endif // !DO_TESTS
}