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
106template <typename T, typename N>
107bool setFromJSON (T& OutValue, JsonObject & Json, N Name)
108{
109 bool HasBeenModified = false;
110
111 if (Json[(char*)Name].template is<T>())
112 {
113 T temp = Json[(char*)Name];
114 if (temp != OutValue)
115 {
116 OutValue = temp;
117 HasBeenModified = true;
118 }
119 }
120 else
121 {
122 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
123 PrettyPrint (Json, Name);
124 }
125
126 return HasBeenModified;
127};
128
129template <typename N, size_t S>
130bool setFromJSON (char (&OutValue)[S], JsonObject & Json, N Name)
131{
132 bool HasBeenModified = false;
133
134 if (Json[(char*)Name].template is<String>())
135 {
136 String temp = Json[(char*)Name];
137 if (!temp.equals(String(OutValue)))
138 {
139 strncpy(OutValue, temp.c_str(), S);
140 HasBeenModified = true;
141 }
142 }
143 else
144 {
145 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
146 PrettyPrint (Json, Name);
147 }
148
149 return HasBeenModified;
150};
151
152template <typename T, typename N>
153bool setFromJSON (T& OutValue, JsonVariant & Json, N Name)
154{
155 bool HasBeenModified = false;
156
157 if (Json[(char*)Name].template is<T>())
158 {
159 T temp = Json[(char*)Name];
160 if (temp != OutValue)
161 {
162 OutValue = temp;
163 HasBeenModified = true;
164 }
165 }
166 else
167 {
168 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
169 PrettyPrint (Json, Name);
170 }
171
172 return HasBeenModified;
173};
174
175#if defined(ARDUINO_ARCH_ESP8266)
176# define JsonWrite(j, n, v) (j)[String(n)] = (v)
177void inline ResetGpio(const gpio_num_t pinId)
178{
179 if(gpio_num_t(33) > pinId)
180 {
181 pinMode(pinId, INPUT);
182 }
183}
184#else // defined(ARDUINO_ARCH_ESP32)
185# define JsonWrite(j, n, v) (j)[(char*)(n)] = (v)
186void inline ResetGpio(const gpio_num_t pinId)
187{
188 if(GPIO_IS_VALID_OUTPUT_GPIO(pinId))
189 {
190 pinMatrixOutDetach(pinId, false, false);
191 gpio_reset_pin(pinId);
192 pinMode(pinId, INPUT);
193 }
194}
195#endif
196
197extern bool ConsoleUartIsActive;
198#define logcon(msg) \
199{ \
200 String DN; \
201 GetDriverName (DN); \
202 extern void _logcon (String & DriverName, String Message); \
203 _logcon (DN, msg); \
204}
205
206extern config_t config;
207extern bool ConfigSaveNeeded;
208
209#define LOAD_CONFIG_DELAY 4
210// #define DEBUG_GPIO gpio_num_t::GPIO_NUM_25
211// #define DEBUG_GPIO1 gpio_num_t::GPIO_NUM_14
bool deserializeCore(JsonObject &json)
Definition main.cpp:323
bool dsNetwork(JsonObject &json)
void deserializeCoreHandler(JsonDocument &jsonDoc)
Definition main.cpp:404
config_t config
Definition main.cpp:98
void DelayReboot(uint32_t MinDelay)
Definition main.cpp:517
void PrettyPrint(JsonObject &jsonStuff, String Name)
Definition WebMgr.cpp:74
String serializeCore(bool pretty=false)
Definition main.cpp:488
uint32_t DiscardedRxData
Definition main.cpp:107
bool ResetWiFi
Definition main.cpp:102
bool setFromJSON(T &OutValue, JsonObject &Json, N Name)
Definition ESPixelStick.h:107
void ResetGpio(const gpio_num_t pinId)
Definition ESPixelStick.h:186
void RequestReboot(String &Reason, uint32_t LoopDelay, bool SkipDisable=false)
Definition main.cpp:611
bool IsBooting
Definition main.cpp:103
ConstConfig_t ConstConfig
Definition main.cpp:83
void FeedWDT()
Definition main.cpp:645
bool dsDevice(JsonObject &json)
Deserialize device configuration JSON to config structure - returns true if config change detected.
Definition main.cpp:281
bool ConfigSaveNeeded
Definition main.cpp:105
bool RebootInProgress()
Definition main.cpp:606
bool ConsoleUartIsActive
Definition main.cpp:623
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