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, 2022 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(uint32_t LoopDelay, bool SkipDisable = false);
69extern bool RebootInProgress();
70
73{
74 // Device
75 String id;
76 uint32_t BlankDelay = uint32_t(5);
77};
78
79String serializeCore (bool pretty = false);
80void deserializeCoreHandler (JsonDocument& jsonDoc);
81bool deserializeCore (JsonObject & json);
82bool dsDevice (JsonObject & json);
83bool dsNetwork (JsonObject & json);
84
85extern bool IsBooting;
86extern bool ResetWiFi;
87extern const String ConfigFileName;
88extern void FeedWDT ();
89extern uint32_t DiscardedRxData;
90
91extern void PrettyPrint (JsonObject& jsonStuff, String Name);
92extern void PrettyPrint (JsonArray& jsonStuff, String Name);
93extern void PrettyPrint(JsonDocument &jsonStuff, String Name);
94
95template <typename T, typename N>
96bool setFromJSON (T& OutValue, JsonObject & Json, N Name)
97{
98 bool HasBeenModified = false;
99
100 if (Json[(char*)Name].template is<T>())
101 {
102 T temp = Json[(char*)Name];
103 if (temp != OutValue)
104 {
105 OutValue = temp;
106 HasBeenModified = true;
107 }
108 }
109 else
110 {
111 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
112 PrettyPrint (Json, Name);
113 }
114
115 return HasBeenModified;
116};
117
118template <typename T, typename N>
119bool setFromJSON (T& OutValue, JsonVariant & Json, N Name)
120{
121 bool HasBeenModified = false;
122
123 if (Json[(char*)Name].template is<T>())
124 {
125 T temp = Json[(char*)Name];
126 if (temp != OutValue)
127 {
128 OutValue = temp;
129 HasBeenModified = true;
130 }
131 }
132 else
133 {
134 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
135 PrettyPrint (Json, Name);
136 }
137
138 return HasBeenModified;
139};
140
141#if defined(ARDUINO_ARCH_ESP8266)
142# define JsonWrite(j, n, v) (j)[String(n)] = (v)
143void inline ResetGpio(const gpio_num_t pinId)
144{
145 if(gpio_num_t(33) > pinId)
146 {
147 pinMode(pinId, INPUT);
148 }
149}
150#else // defined(ARDUINO_ARCH_ESP32)
151# define JsonWrite(j, n, v) (j)[(char*)(n)] = (v)
152void inline ResetGpio(const gpio_num_t pinId)
153{
154 if(GPIO_IS_VALID_OUTPUT_GPIO(pinId))
155 {
156 pinMatrixOutDetach(pinId, false, false);
157 gpio_reset_pin(pinId);
158 pinMode(pinId, INPUT);
159 }
160}
161#endif
162
163extern bool ConsoleUartIsActive;
164#define logcon(msg) \
165{ \
166 String DN; \
167 GetDriverName (DN); \
168 extern void _logcon (String & DriverName, String Message); \
169 _logcon (DN, msg); \
170}
171
172extern config_t config;
173extern bool ConfigSaveNeeded;
174
175extern const uint8_t CurrentConfigVersion;
176#define LOAD_CONFIG_DELAY 4
177// #define DEBUG_GPIO gpio_num_t::GPIO_NUM_25
178// #define DEBUG_GPIO1 gpio_num_t::GPIO_NUM_14
bool deserializeCore(JsonObject &json)
Definition main.cpp:304
bool dsNetwork(JsonObject &json)
void deserializeCoreHandler(JsonDocument &jsonDoc)
Definition main.cpp:385
config_t config
Definition main.cpp:93
void RequestReboot(uint32_t LoopDelay, bool SkipDisable=false)
Definition main.cpp:576
void PrettyPrint(JsonObject &jsonStuff, String Name)
Definition WebMgr.cpp:70
String serializeCore(bool pretty=false)
Definition main.cpp:469
uint32_t DiscardedRxData
Definition main.cpp:102
bool ResetWiFi
Definition main.cpp:97
const uint8_t CurrentConfigVersion
Definition main.cpp:91
bool setFromJSON(T &OutValue, JsonObject &Json, N Name)
Definition ESPixelStick.h:96
void ResetGpio(const gpio_num_t pinId)
Definition ESPixelStick.h:152
bool IsBooting
Definition main.cpp:98
void FeedWDT()
Definition main.cpp:610
bool dsDevice(JsonObject &json)
Deserialize device configuration JSON to config structure - returns true if config change detected.
Definition main.cpp:262
bool ConfigSaveNeeded
Definition main.cpp:100
bool RebootInProgress()
Definition main.cpp:571
const String ConfigFileName
Definition main.cpp:89
bool ConsoleUartIsActive
Definition main.cpp:588
#define DEBUG_V(v)
Definition memdebug.h:18
Core configuration structure.
Definition ESPixelStick.h:73
String id
Definition ESPixelStick.h:75
uint32_t BlankDelay
Definition ESPixelStick.h:76