This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKniwwelino.h
382 lines (311 loc) · 11.1 KB
/
Kniwwelino.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/***************************************************
KniwwelinoLIB
Copyright (C) 2017 Luxembourg Institute of Science and Technology.
This program is free software: you can redistribute it and/or modify
it under the terms of the Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License.
Derived from Adafruit_LED_Backpack_Library library
Written by Limor Fried/Ladyada for Adafruit Industries.
****************************************************/
#ifndef Kniwwelino_h
#define Kniwwelino_h
#include "Arduino.h"
#include "KniwwelinoIcons.h"
#include "KniwwelinoTones.h"
#include <Ticker.h>
#include <Wire.h>
#include <EEPROM.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
// comment to disable debugging output via serial port.
#define DEBUG
#define LIB_VERSION "kniwwelinoLIB_1.2.1"
#define FW_VERSION "kniwwelino_121"
#define DEF_TYPE "Kniwwelino"
#define NAME_PREFIX "Kniwwelino"
#define TICK_FREQ 0.05
// definitions for the HT16K33 LED Matrix Driver
#define HT16K33_ADDRESS 0x70
#define HT16K33_BLINK_CMD 0x80
#define HT16K33_CMD_BRIGHTNESS 0xE0
#define HT16K33_DISP_REGISTER 0x00
#define HT16K33_KEYS_REGISTER 0x40
#define HT16K33_KEYINT_REGISTER 0x60
#define HT16K33_BLINK_DISPLAYON 0x01
#define MATRIX_STATIC 0
#define MATRIX_BLINK_2HZ 1
#define MATRIX_BLINK_1HZ 2
#define MATRIX_BLINK_HALFHZ 3
#define MATRIX_MIN_BRIGHTNESS 0
#define MATRIX_MAX_BRIGHTNESS 15
#define MATRIX_DEFAULT_BRIGHTNESS 10
#define MATRIX_SPEED 10
#define MATRIX_FOREVER -1
#define MATRIX_SCROLL_DIV 3
#define EEPROM_ADR_UPDATE 510
#define EEPROM_ADR_NUM 511
#define RGB_PIN 15
#define RGB_BRIGHTNESS 100
#define RGB_FOREVER -1
#define RGB_ON 10
#define RGB_BLINK 5
#define RGB_FLASH 1
#define RGB_OFF 0
#define RGB_UNUSED -1
#define RGB_SPARK 20
#define RGB_GLOW 21
#define PIN_ON 10
#define PIN_BLINK 5
#define PIN_FLASH 1
#define PIN_OFF 0
#define PIN_UNUSED -1
#define PIN_INPUT -2
#define RGB_COLOR_RED 0xFF0000
#define RGB_COLOR_GREEN 0x00FF00
#define RGB_COLOR_BLUE 0x0000FF
#define RGB_COLOR_ORANGE 0xC93B03
#define RGB_COLOR_CYAN 0x00FFFF
#define STATE_WIFI 0x000022
#define STATE_WIFIMGR 0x110022
#define STATE_ON 0x000100
#define STATE_OFF 0x000000
#define STATE_NONE 0x000000
#define STATE_SLEEPL 0x000100
#define STATE_SLEEPD 0x000000
#define STATE_ERR 0x220000
#define STATE_MQTT 0x010001
#define STATE_CONF 0x000101
#define STATE_UPDATE RGB_COLOR_ORANGE
#define FILE_WIFI "/wifi.conf"
#define FILE_FORCED_WIFI "/forcwifi.conf"
#define FILE_CONF "/conf.json"
#define DEF_UPDATESERVER "broker.kniwwelino.lu"
#define DEF_MQTTSERVER "broker.kniwwelino.lu"
#define DEF_MQTTPORT 1883
#define DEF_MQTTUSER "kniwwelino"
#define DEF_MQTTPW "esp8266"
#define DEF_MQTTPUBLICDELAY 300
#define DEF_MQTTBASETOPIC "kniwwelino/"
#define DEF_FWUPDATEURL "/updateFW"
#define DEF_CONFUPDATEURL "/updateConf"
#define MQTT_RGB "RGB"
#define MQTT_RGBCOLOR "RGB/COLOR"
#define MQTT_MATRIX "MATRIX"
#define MQTT_MATRIXICON "MATRIX/ICON"
#define MQTT_MATRIXTEXT "MATRIX/TEXT"
#define NTP_SERVER "lu.pool.ntp.org"
#define NTP_PORT 8888
#define NTP_TIMEZONE 1
#define NTP_PACKET_SIZE 48 // NTP time is in the first 48 bytes of message
static uint32_t _tick = 0;
static boolean mqttLogEnabled = false;
class KniwwelinoLib: public Adafruit_GFX {
public:
KniwwelinoLib();
void begin();
void begin(boolean enableWifi, boolean fast);
void begin(boolean enableWifi, boolean fast, boolean mqttLog);
void begin(const char nameStr[], boolean enableWifi, boolean fast, boolean mqttLog);
void setSilent();
//==== Kniwwelino functions===================================================
String getID();
String getName();
String getIP();
String getMAC();
void sleep(unsigned long millis);
void loop();
boolean isConnected();
void bgI2CStop();
void bgI2CStart();
//==== logging =============================================================
void log(const String s);
void logln(const String s);
void log(const char s[]);
void logln(const char s[]);
//==== IO Functions =========================================================
void PINsetEffect(uint8_t pin, int effect);
void PINclear(uint8_t pin);
void PINenableButton(uint8_t pin);
boolean PINbuttonClicked(uint8_t pin);
boolean PINbuttonDown(uint8_t pin);
//==== RGB LED functions ====================================================
void RGBsetColor(String color);
void RGBsetEffect(uint8_t effect, int count);
void RGBsetColorEffect(String color, uint8_t effect, int count);
void RGBsetColor(unsigned long color);
void RGBsetColorEffect(unsigned long color, uint8_t effect, int count);
void RGBsetColor(uint8_t red, uint8_t green, uint8_t blue);
void RGBsetColorEffect(uint8_t red, uint8_t green, uint8_t blue,
uint8_t effect, int count);
void RGBsetColorEffect(String colorEffect);
void RGBclear();
void RGBsetBrightness(uint8_t b);
uint32_t RGBgetColor();
unsigned long RGBhex2int(String col);
unsigned long RGBhue2int(uint8_t hue);
String RGBcolor2Hex(unsigned long color);
String RGBcolor2Hex(uint8_t r, uint8_t g, uint8_t b);
String RGBhue2Hex(uint8_t hue);
String RGB82Hex(uint8_t c);
//==== LED MATRIX functions ==================================================
void MATRIXwrite(String text);
void MATRIXwriteOnce(String text);
void MATRIXwriteAndWait(String text);
void MATRIXwrite(String text, int count, boolean wait);
void MATRIXdrawIcon(String iconString);
void MATRIXdrawIcon(uint32_t iconLong);
void MATRIXsetPixel(uint8_t x, uint8_t y, boolean on);
boolean MATRIXgetPixel(uint8_t x, uint8_t y);
void MATRIXsetBrightness(uint8_t brightness);
void MATRIXsetBlinkRate(uint8_t rate);
void MATRIXsetScrollSpeed(uint8_t b);
void MATRIXclear();
void MATRIXshowID();
boolean MATRIXtextDone();
void MATRIXsetStatus(uint8_t p);
void setRotation(uint8_t rotation);
//==== Onboard Button functions ==============================================
boolean BUTTONAclicked();
boolean BUTTONBclicked();
boolean BUTTONABclicked();
boolean BUTTONAdown();
boolean BUTTONBdown();
//==== IOT functions ==============================================
boolean WIFIsetup(boolean wifiMgr, boolean fast, boolean reconnecting);
MQTTClient mqtt;
boolean MQTTsetup(const char broker[], int port, const char user[],
const char password[]);
boolean MQTTconnect();
boolean MQTTconnect(boolean silent);
boolean MQTTpublish(const char topic[], String message);
boolean MQTTpublish(String topic, String message);
boolean MQTTsubscribe(const char topic[]);
boolean MQTTsubscribe(String topic);
boolean MQTTunsubscribe(const char topic[]);
boolean MQTTsubscribepublic(const char topic[]);
boolean MQTTsubscribepublic(String topic);
boolean MQTTunsubscribepublic(const char topic[]);
void MQTTsetGroup(String group);
void MQTTonMessage(void (*)(String &topic, String &message));
void MQTTconnectRGB();
void MQTTconnectMATRIX();
void PLATFORMprintConf();
//==== FS functions ==============================================
String FILEread(String fileName);
void FILEwrite(String fileName, String content);
//==== Tone functions ==============================================
void playNote(uint8_t pin, unsigned int note, uint8_t noteDuration);
void playTone(uint8_t pin, unsigned int note);
void toneOff(uint8_t pin);
//==== Date Time functions ==============================================
String getTime();
//==== Private functions =====================================================
private:
static void _baseTick();
void _PINhandle();
void _RGBblink();
void drawPixel(int16_t x, int16_t y, uint16_t color); // Draw a specific pixel
void _MATRIXupdate();
void _Buttonsread();
static void _MQTTmessageReceived(String &topic, String &payload);
void _MQTTupdateStatus(boolean force);
boolean PLATFORMcheckFWUpdate();
boolean PLATFORMcheckConfUpdate();
boolean PLATFORMupdateConf(String confJSON);
void _initNTP();
time_t _getNtpTime();
static KniwwelinoLib *getNTPTimeObject;
static time_t _globalGetNTPTime() {
return getNTPTimeObject->_getNtpTime();
}
//==== Private Members =====================================================
// general
char fwVersion[20];
char nodename[40];
// always updateable - for now...
boolean updateMode = true;
// silent mode
boolean silent = false;
// background i2c operations active
boolean bgI2C = true;
// IO
byte ioPinNumers[4] = { D0, D5, D6, D7 };
int ioPinStatus[4] = { PIN_UNUSED, PIN_UNUSED, PIN_UNUSED, PIN_UNUSED };
boolean ioPinclicked[4] = { false, false, false, false };
int pinBlinkCount = 1;
// MATRIX
boolean redrawMatrix = true;
String matrixText;
int matrixCount = -1;
int matrixPos = 0;
int iconcount = 0;
int iconnum = 0;
uint8_t displaybuffer[8];
uint8_t rotation = 0;
boolean idShowing = false;
uint8_t matrixScrollDiv = MATRIX_SCROLL_DIV;
// RGB
Adafruit_NeoPixel RGB;
uint32_t rgbColor = 0;
int rgbEffect = RGB_ON;
int rgbEffectCount = -1;
int rgbEffectBrightness = RGB_BRIGHTNESS;
int rgbEffectModifier = 1;
int rgbBlinkCount = 1;
int rgbBrightness = RGB_BRIGHTNESS;
// BUTTONS
boolean buttonsPressed;
boolean buttonA;
boolean buttonB;
boolean buttonAClicked;
boolean buttonBClicked;
boolean buttonABClicked;
// TICKER
Ticker baseTicker;
// Wifi
boolean wifiEnabled = true;
WiFiClient wifi;
// mqtt
boolean mqttEnabled = false;
char updateServer[20];
char mqttServer[20];
int mqttPort = DEF_MQTTPORT;
char mqttUser[20];
char mqttPW[20];
int mqttPublishDelay = DEF_MQTTPUBLICDELAY;
String mqttSubscriptions[10] = { "", "", "", "", "", "", "", "", "", "" };
String mqttTopicReqPwd = "/management/to/" + WiFi.macAddress()+ "/reqBrokerPwd";
String mqttTopicUpdate = "/management/to/" + WiFi.macAddress()+ "/update";
String mqttTopicLogEnabled = "/management/to/" + WiFi.macAddress()+ "/enableMQTTLog";
String mqttTopicSentPwd = "/management/from/" + WiFi.macAddress()+ "/resBrokerPwd";
String mqttTopicStatus = "/management/from/" + WiFi.macAddress()+ "/status";
String mqttGroup = "";
uint32_t mqttLastPublished = 0;
boolean mqttRGB = false;
boolean mqttMATRIX = false;
// plattform / conf
char platformPW[20];
char confPersonalParameters[256];
JsonObject* myParameters;
// DateTime / NTP Stuff
WiFiUDP ntpUdp;
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; //Central European Summer Time
TimeChangeRule CET = {"CET", Last, Sun, Oct, 3, 60}; //Central European Standard Time
Timezone timeZone = Timezone(CEST, CET);
};
extern KniwwelinoLib Kniwwelino;
#endif