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#define ARDUINOJSON_USE_LONG_LONG 1
36#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 15
37
38#include <Ticker.h>
39#include <ArduinoJson.h>
40
41#include "memdebug.h"
42#include "ConstNames.hpp"
43#include "GPIO_Defs.hpp"
44#include "FastTimer.hpp"
45
46#define REBOOT_DELAY 100
47#define LOG_PORT Serial
48#define CLIENT_TIMEOUT 15
49#define AP_TIMEOUT 120
50
51#define MilliSecondsInASecond 1000
52#define MicroSecondsInAmilliSecond 1000
53#define MicroSecondsInASecond (MicroSecondsInAmilliSecond * MilliSecondsInASecond)
54#define NanoSecondsInAMicroSecond 1000
55#define NanoSecondsInASecond (MicroSecondsInASecond * NanoSecondsInAMicroSecond)
56#define NanoSecondsInAMilliSecond (NanoSecondsInAMicroSecond * MicroSecondsInAmilliSecond)
57
58#define CPU_ClockTimeNS ((1.0 / float(F_CPU)) * float(NanoSecondsInASecond))
59
60// Macro strings
61#define STRINGIFY(X) #X
62#define STRING(X) STRINGIFY(X)
63
64extern void RequestReboot(uint32_t LoopDelay, bool SkipDisable = false);
65extern bool RebootInProgress();
66
69{
70 // Device
71 String id;
72 uint32_t BlankDelay = uint32_t(5);
73};
74
75String serializeCore (bool pretty = false);
76void deserializeCoreHandler (JsonDocument& jsonDoc);
77bool deserializeCore (JsonObject & json);
78bool dsDevice (JsonObject & json);
79bool dsNetwork (JsonObject & json);
80
81extern bool IsBooting;
82extern bool ResetWiFi;
83extern const String ConfigFileName;
84extern void FeedWDT ();
85extern uint32_t DiscardedRxData;
86
87extern void PrettyPrint (JsonObject& jsonStuff, String Name);
88extern void PrettyPrint (JsonArray& jsonStuff, String Name);
89extern void PrettyPrint(JsonDocument &jsonStuff, String Name);
90
91template <typename T, typename N>
92bool setFromJSON (T& OutValue, JsonObject & Json, N Name)
93{
94 bool HasBeenModified = false;
95
96 if (Json[(char*)Name].template is<T>())
97 {
98 T temp = Json[(char*)Name];
99 if (temp != OutValue)
100 {
101 OutValue = temp;
102 HasBeenModified = true;
103 }
104 }
105 else
106 {
107 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
108 PrettyPrint (Json, Name);
109 }
110
111 return HasBeenModified;
112};
113
114template <typename T, typename N>
115bool setFromJSON (T& OutValue, JsonVariant & Json, N Name)
116{
117 bool HasBeenModified = false;
118
119 if (Json[(char*)Name].template is<T>())
120 {
121 T temp = Json[(char*)Name];
122 if (temp != OutValue)
123 {
124 OutValue = temp;
125 HasBeenModified = true;
126 }
127 }
128 else
129 {
130 DEBUG_V(String("Could not find field '") + Name + "' in the json record");
131 PrettyPrint (Json, Name);
132 }
133
134 return HasBeenModified;
135};
136
137#if defined(ARDUINO_ARCH_ESP8266)
138# define JsonWrite(j, n, v) (j)[String(n)] = (v)
139void inline ResetGpio(const gpio_num_t pinId)
140{
141 if(gpio_num_t(33) > pinId)
142 {
143 pinMode(pinId, INPUT);
144 }
145}
146#else // defined(ARDUINO_ARCH_ESP32)
147# define JsonWrite(j, n, v) (j)[(char*)(n)] = (v)
148void inline ResetGpio(const gpio_num_t pinId)
149{
150 if(GPIO_IS_VALID_OUTPUT_GPIO(pinId))
151 {
152 gpio_reset_pin(pinId);
153 pinMode(pinId, INPUT);
154 }
155}
156#endif
157
158extern bool ConsoleUartIsActive;
159#define logcon(msg) \
160{ \
161 String DN; \
162 GetDriverName (DN); \
163 extern void _logcon (String & DriverName, String Message); \
164 _logcon (DN, msg); \
165}
166
167extern config_t config;
168extern bool ConfigSaveNeeded;
169
170extern const uint8_t CurrentConfigVersion;
171#define LOAD_CONFIG_DELAY 4
172// #define DEBUG_GPIO gpio_num_t::GPIO_NUM_25
173// #define DEBUG_GPIO1 gpio_num_t::GPIO_NUM_14
bool deserializeCore(JsonObject &json)
Definition main.cpp:301
bool dsNetwork(JsonObject &json)
void deserializeCoreHandler(JsonDocument &jsonDoc)
Definition main.cpp:382
config_t config
Definition main.cpp:93
void RequestReboot(uint32_t LoopDelay, bool SkipDisable=false)
Definition main.cpp:573
void PrettyPrint(JsonObject &jsonStuff, String Name)
Definition WebMgr.cpp:70
String serializeCore(bool pretty=false)
Definition main.cpp:466
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:92
void ResetGpio(const gpio_num_t pinId)
Definition ESPixelStick.h:148
bool IsBooting
Definition main.cpp:98
void FeedWDT()
Definition main.cpp:607
bool dsDevice(JsonObject &json)
Deserialize device configuration JSON to config structure - returns true if config change detected.
Definition main.cpp:259
bool ConfigSaveNeeded
Definition main.cpp:100
bool RebootInProgress()
Definition main.cpp:568
const String ConfigFileName
Definition main.cpp:89
bool ConsoleUartIsActive
Definition main.cpp:585
#define DEBUG_V(v)
Definition memdebug.h:18
Core configuration structure.
Definition ESPixelStick.h:69
String id
Definition ESPixelStick.h:71
uint32_t BlankDelay
Definition ESPixelStick.h:72