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
45#include "memdebug.h"
46#include "ConstNames.hpp"
47#include "GPIO_Defs.hpp"
48#include "FastTimer.hpp"
49
50#define REBOOT_DELAY 100
51#define LOG_PORT Serial
52#define CLIENT_TIMEOUT 15
53#define AP_TIMEOUT 120
54
55#define MilliSecondsInASecond 1000
56#define MicroSecondsInAmilliSecond 1000
57#define MicroSecondsInASecond (MicroSecondsInAmilliSecond * MilliSecondsInASecond)
58#define NanoSecondsInAMicroSecond 1000
59#define NanoSecondsInASecond (MicroSecondsInASecond * NanoSecondsInAMicroSecond)
60#define NanoSecondsInAMilliSecond (NanoSecondsInAMicroSecond * MicroSecondsInAmilliSecond)
61
62#define CPU_ClockTimeNS ((1.0 / float(F_CPU)) * float(NanoSecondsInASecond))
63
64// Macro strings
65#define STRINGIFY(X) #X
66#define STRING(X) STRINGIFY(X)
67
68extern void RequestReboot(String & Reason, uint32_t LoopDelay, bool SkipDisable = false);
69extern bool RebootInProgress();
70extern void DelayReboot(uint32_t MinDelay);
71
74{
75 // Device
76 char id[65];
77 uint32_t BlankDelay = uint32_t(5);
78};
79
80String serializeCore (bool pretty = false);
81void deserializeCoreHandler (JsonDocument& jsonDoc);
82bool deserializeCore (JsonObject & json);
83bool dsDevice (JsonObject & json);
84bool dsNetwork (JsonObject & json);
85
86struct __attribute__((packed)) ConstConfig_t
87{
88 const char key[32];
89 const char Version[32];
90 const char BuildDate[32];
91 const char ConfigFileName[32];
92 const uint8_t CurrentConfigVersion;
93};
94
95extern ConstConfig_t ConstConfig;
96
97extern bool IsBooting;
98extern bool ResetWiFi;
99extern void FeedWDT ();
100extern uint32_t DiscardedRxData;
101
102extern void PrettyPrint (JsonObject& jsonStuff, String Name);
103extern void PrettyPrint (JsonArray& jsonStuff, String Name);
104extern void PrettyPrint(JsonDocument &jsonStuff, String Name);
105
106void inline SafeStrncpy(char* dest, const char* src, uint destSize)
107{
108 memset(dest, 0x00, destSize);
109 size_t cpyLen = min(destSize-1, strlen(src));
110 memcpy(dest, src, cpyLen);
111} // SafeStrncpy
112
113template <typename T, typename N>
114bool setFromJSON (T& OutValue, JsonObject & Json, N Name)
115{
116 bool HasBeenModified = false;
117
118 if (Json[(char*)Name].template is<T>())
119 {
120 T temp = Json[(char*)Name];
121 if (temp != OutValue)
122 {
123 OutValue = temp;
124 HasBeenModified = true;
125 }
126 }
127 else
128 {
129 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
130 PrettyPrint (Json, Name);
131 }
132
133 return HasBeenModified;
134};
135
136template <typename N, size_t S>
137bool setFromJSON (char (&OutValue)[S], JsonObject & Json, N Name)
138{
139 bool HasBeenModified = false;
140
141 if (Json[(char*)Name].template is<String>())
142 {
143 String temp = Json[(char*)Name];
144 if (!temp.equals(String(OutValue)))
145 {
146 SafeStrncpy(OutValue, temp.c_str(), S);
147 HasBeenModified = true;
148 }
149 }
150 else
151 {
152 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
153 PrettyPrint (Json, Name);
154 }
155
156 return HasBeenModified;
157};
158
159template <typename T, typename N>
160bool setFromJSON (T& OutValue, JsonVariant & Json, N Name)
161{
162 bool HasBeenModified = false;
163
164 if (Json[(char*)Name].template is<T>())
165 {
166 T temp = Json[(char*)Name];
167 if (temp != OutValue)
168 {
169 OutValue = temp;
170 HasBeenModified = true;
171 }
172 }
173 else
174 {
175 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
176 PrettyPrint (Json, Name);
177 }
178
179 return HasBeenModified;
180};
181
182#if defined(ARDUINO_ARCH_ESP8266)
183# define JsonWrite(j, n, v) (j)[String(n)] = (v)
184void inline ResetGpio(const gpio_num_t pinId)
185{
186 if(gpio_num_t(33) > pinId)
187 {
188 pinMode(pinId, INPUT);
189 }
190}
191#else // defined(ARDUINO_ARCH_ESP32)
192# define JsonWrite(j, n, v) (j)[(char*)(n)] = (v)
193void inline ResetGpio(const gpio_num_t pinId)
194{
195 if(GPIO_IS_VALID_OUTPUT_GPIO(pinId))
196 {
197 pinMatrixOutDetach(pinId, false, false);
198 gpio_reset_pin(pinId);
199 pinMode(pinId, INPUT);
200 }
201}
202#endif
203
204extern bool ConsoleUartIsActive;
205#define logcon(msg) \
206{ \
207 String DN; \
208 GetDriverName (DN); \
209 extern void _logcon (String & DriverName, String Message); \
210 _logcon (DN, msg); \
211}
212
213extern config_t config;
214extern bool ConfigSaveNeeded;
215
216#define LOAD_CONFIG_DELAY 4
217// #define DEBUG_GPIO gpio_num_t::GPIO_NUM_25
218// #define DEBUG_GPIO1 gpio_num_t::GPIO_NUM_14
bool deserializeCore(JsonObject &json)
Definition main.cpp:336
bool dsNetwork(JsonObject &json)
void deserializeCoreHandler(JsonDocument &jsonDoc)
Definition main.cpp:418
config_t config
Definition main.cpp:98
void DelayReboot(uint32_t MinDelay)
Definition main.cpp:549
void PrettyPrint(JsonObject &jsonStuff, String Name)
Definition WebMgr.cpp:74
String serializeCore(bool pretty=false)
Definition main.cpp:520
uint32_t DiscardedRxData
Definition main.cpp:108
bool ResetWiFi
Definition main.cpp:102
bool setFromJSON(T &OutValue, JsonObject &Json, N Name)
Definition ESPixelStick.h:114
void SafeStrncpy(char *dest, const char *src, uint destSize)
Definition ESPixelStick.h:106
void ResetGpio(const gpio_num_t pinId)
Definition ESPixelStick.h:193
void RequestReboot(String &Reason, uint32_t LoopDelay, bool SkipDisable=false)
Definition main.cpp:650
bool IsBooting
Definition main.cpp:103
ConstConfig_t ConstConfig
Definition main.cpp:83
void FeedWDT()
Definition main.cpp:684
bool dsDevice(JsonObject &json)
Deserialize device configuration JSON to config structure - returns true if config change detected.
Definition main.cpp:294
bool ConfigSaveNeeded
Definition main.cpp:105
bool RebootInProgress()
Definition main.cpp:645
bool ConsoleUartIsActive
Definition main.cpp:662
struct FSEQParsedRangeEntry __attribute__
#define DEBUG_V(v)
Definition memdebug.h:18
Core configuration structure.
Definition ESPixelStick.h:74
uint32_t BlankDelay
Definition ESPixelStick.h:77