ESPixelStick Firmware
Firmware for the ESPixelStick
Loading...
Searching...
No Matches
ESPixelStick.h
Go to the documentation of this file.
1#pragma once
2/*
3* ESPixelStick.h
4*
5* Project: ESPixelStick - An ESP8266 / ESP32 and E1.31 based pixel driver
6* Copyright (c) 2016, 2025 Shelby Merrick
7* http://www.forkineye.com
8*
9* This program is provided free for you to use in any way that you wish,
10* subject to the laws and regulations where you are using it. Due diligence
11* is strongly suggested before using this code. Please give credit where due.
12*
13* The Author makes no warranty of any kind, express or implied, with regard
14* to this program or the documentation contained in this document. The
15* Author shall not be liable in any event for incidental or consequential
16* damages in connection with, or arising out of, the furnishing, performance
17* or use of these programs.
18*
19*/
20
21#include <Arduino.h>
22
23#if defined(ARDUINO_ARCH_ESP8266)
24# include <ESP8266WiFi.h>
25# include <ESPAsyncTCP.h>
26# include <ESPAsyncUDP.h>
27#elif defined(ARDUINO_ARCH_ESP32)
28# include <AsyncTCP.h>
29# include <AsyncUDP.h>
30# include <WiFi.h>
31#else
32# error "Unsupported CPU type"
33#endif
34
35#ifdef BOARD_HAS_PSRAM
36# error "PSRAM is not supported by ESPixelStick"
37#endif // def BOARD_HAS_PSRAM
38
39#define ARDUINOJSON_USE_LONG_LONG 1
40#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 15
41
42#include <Ticker.h>
43#include <ArduinoJson.h>
44#ifdef ARDUINO_ARCH_ESP8266
45# include <coredecls.h> // can_yield()
46#endif // def ARDUINO_ARCH_ESP8266
47
48#include "memdebug.h"
49#include "ConstNames.hpp"
50#include "GPIO_Defs.hpp"
51#include "FastTimer.hpp"
52
53#define LOG_PORT Serial
54#define CLIENT_TIMEOUT 15
55#define AP_TIMEOUT 120
56
57#define MilliSecondsInASecond 1000
58#define MicroSecondsInAmilliSecond 1000
59#define MicroSecondsInASecond (MicroSecondsInAmilliSecond * MilliSecondsInASecond)
60#define NanoSecondsInAMicroSecond 1000
61#define NanoSecondsInASecond (MicroSecondsInASecond * NanoSecondsInAMicroSecond)
62#define NanoSecondsInAMilliSecond (NanoSecondsInAMicroSecond * MicroSecondsInAmilliSecond)
63
64#define CPU_ClockTimeNS ((1.0 / float(F_CPU)) * float(NanoSecondsInASecond))
65
66// Macro strings
67#define STRINGIFY(X) #X
68#define STRING(X) STRINGIFY(X)
69
70extern void RequestReboot(String & Reason, uint32_t LoopDelay, bool SkipDisable = false);
71extern bool RebootInProgress();
72extern void DelayReboot(uint32_t MinDelay);
73
76{
77 // Device
78 char id[65];
79 uint32_t BlankDelay = uint32_t(5);
80};
81
82String serializeCore (bool pretty = false);
83void deserializeCoreHandler (JsonDocument& jsonDoc);
84bool deserializeCore (JsonObject & json);
85bool dsDevice (JsonObject & json);
86bool dsNetwork (JsonObject & json);
87
88struct __attribute__((packed)) ConstConfig_t
89{
90 const char key[32];
91 const char Version[32];
92 const char BuildDate[32];
93 const char ConfigFileName[32];
94 const uint8_t CurrentConfigVersion;
95};
96
97extern ConstConfig_t ConstConfig;
98
99extern bool IsBooting;
100extern bool ResetWiFi;
101extern void FeedWDT ();
102
103// Use this instead of delay() in code that the SDK runs outside of loop(): UDP, web and timer callbacks.
104// On the ESP8266, delay() cannot wait there and it corrupts the SDK timer list when loop() is in delay()
105// at the same time. This only waits when called from the loop() context.
106inline void SafeDelay (uint32_t ms)
107{
108#ifdef ARDUINO_ARCH_ESP8266
109 if (!can_yield ()) { return; }
110#endif // def ARDUINO_ARCH_ESP8266
111 delay (ms);
112} // SafeDelay
113extern uint32_t DiscardedRxData;
114
115extern void PrettyPrint (JsonObject& jsonStuff, String Name);
116extern void PrettyPrint (JsonArray& jsonStuff, String Name);
117extern void PrettyPrint(JsonDocument &jsonStuff, String Name);
118
119void inline SafeStrncpy(char* dest, const char* src, uint destSize)
120{
121 memset(dest, 0x00, destSize);
122 size_t cpyLen = min(destSize-1, strlen(src));
123 memcpy(dest, src, cpyLen);
124} // SafeStrncpy
125
126template <typename T, typename N>
127bool setFromJSON (T& OutValue, JsonObject & Json, N Name)
128{
129 bool HasBeenModified = false;
130
131 if (Json[(char*)Name].template is<T>())
132 {
133 T temp = Json[(char*)Name];
134 if (temp != OutValue)
135 {
136 OutValue = temp;
137 HasBeenModified = true;
138 }
139 }
140 else
141 {
142 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
143 PrettyPrint (Json, Name);
144 }
145
146 return HasBeenModified;
147};
148
149template <typename N, size_t S>
150bool setFromJSON (char (&OutValue)[S], JsonObject & Json, N Name)
151{
152 bool HasBeenModified = false;
153
154 if (Json[(char*)Name].template is<String>())
155 {
156 String temp = Json[(char*)Name];
157 if (!temp.equals(String(OutValue)))
158 {
159 SafeStrncpy(OutValue, temp.c_str(), S);
160 HasBeenModified = true;
161 }
162 }
163 else
164 {
165 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
166 PrettyPrint (Json, Name);
167 }
168
169 return HasBeenModified;
170};
171
172template <typename T, typename N>
173bool setFromJSON (T& OutValue, JsonVariant & Json, N Name)
174{
175 bool HasBeenModified = false;
176
177 if (Json[(char*)Name].template is<T>())
178 {
179 T temp = Json[(char*)Name];
180 if (temp != OutValue)
181 {
182 OutValue = temp;
183 HasBeenModified = true;
184 }
185 }
186 else
187 {
188 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
189 PrettyPrint (Json, Name);
190 }
191
192 return HasBeenModified;
193};
194
195#if defined(ARDUINO_ARCH_ESP8266)
196# define JsonWrite(j, n, v) (j)[String(n)] = (v)
197void inline ResetGpio(const gpio_num_t pinId)
198{
199 if(gpio_num_t(33) > pinId)
200 {
201 pinMode(pinId, INPUT);
202 }
203}
204#else // defined(ARDUINO_ARCH_ESP32)
205# define JsonWrite(j, n, v) (j)[(char*)(n)] = (v)
206void inline ResetGpio(const gpio_num_t pinId)
207{
208 if(GPIO_IS_VALID_OUTPUT_GPIO(pinId))
209 {
210 pinMatrixOutDetach(pinId, false, false);
211 gpio_reset_pin(pinId);
212 pinMode(pinId, INPUT);
213 }
214 // else
215 // {
216 // DEBUG_V("GPIO Is not a valid output");
217 // }
218}
219#endif
220
221extern bool ConsoleUartIsActive;
222#define logcon(msg) \
223{ \
224 String DN; \
225 GetDriverName (DN); \
226 extern void _logcon (String & DriverName, String Message); \
227 _logcon (DN, msg); \
228}
229
230extern config_t config;
231extern bool ConfigSaveNeeded;
232
233#define LOAD_CONFIG_DELAY 4
234// #define DEBUG_GPIO gpio_num_t::GPIO_NUM_25
235// #define DEBUG_GPIO1 gpio_num_t::GPIO_NUM_14
bool deserializeCore(JsonObject &json)
Definition main.cpp:344
bool dsNetwork(JsonObject &json)
void deserializeCoreHandler(JsonDocument &jsonDoc)
Definition main.cpp:431
config_t config
Definition main.cpp:98
void DelayReboot(uint32_t MinDelay)
Definition main.cpp:562
void PrettyPrint(JsonObject &jsonStuff, String Name)
Definition WebMgr.cpp:74
String serializeCore(bool pretty=false)
Definition main.cpp:533
uint32_t DiscardedRxData
Definition main.cpp:108
bool ResetWiFi
Definition main.cpp:102
bool setFromJSON(T &OutValue, JsonObject &Json, N Name)
Definition ESPixelStick.h:127
void SafeDelay(uint32_t ms)
Definition ESPixelStick.h:106
void SafeStrncpy(char *dest, const char *src, uint destSize)
Definition ESPixelStick.h:119
void ResetGpio(const gpio_num_t pinId)
Definition ESPixelStick.h:206
void RequestReboot(String &Reason, uint32_t LoopDelay, bool SkipDisable=false)
Definition main.cpp:665
bool IsBooting
Definition main.cpp:103
ConstConfig_t ConstConfig
Definition main.cpp:83
void FeedWDT()
Definition main.cpp:705
bool dsDevice(JsonObject &json)
Deserialize device configuration JSON to config structure - returns true if config change detected.
Definition main.cpp:302
bool ConfigSaveNeeded
Definition main.cpp:105
bool RebootInProgress()
Definition main.cpp:660
bool ConsoleUartIsActive
Definition main.cpp:677
struct FSEQParsedRangeEntry __attribute__
#define DEBUG_V(v)
Definition memdebug.h:18
Core configuration structure.
Definition ESPixelStick.h:76
uint32_t BlankDelay
Definition ESPixelStick.h:79