ESPixelStick Firmware
Firmware for the ESPixelStick
Loading...
Searching...
No Matches
OutputSerial.hpp
Go to the documentation of this file.
1#pragma once
2/*
3* OutputSerial.h - Pixel driver code for ESPixelStick
4*
5* Project: ESPixelStick - An ESP8266 / ESP32 and E1.31 based pixel driver
6* Copyright (c) 2015, 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* This is a derived class that converts data in the output buffer into
20* pixel intensities and then transmits them through the configured serial
21* interface.
22*
23*/
24
25#include "OutputCommon.hpp"
26#if defined(SUPPORT_OutputType_FireGod) || defined(SUPPORT_OutputType_DMX) || defined(SUPPORT_OutputType_Serial) || defined(SUPPORT_OutputType_Renard)
27
28class c_OutputSerial : public c_OutputCommon
29{
30public:
31 // These functions are inherited from c_OutputCommon
32 c_OutputSerial (c_OutputMgr::e_OutputChannelIds OutputChannelId,
33 gpio_num_t outputGpio,
34 uart_port_t uart,
35 c_OutputMgr::e_OutputType outputType);
36 virtual ~c_OutputSerial ();
37
38 // functions to be provided by the derived class
39 virtual void Begin ();
40 virtual bool SetConfig (ArduinoJson::JsonObject & jsonConfig);
41 virtual void GetConfig (ArduinoJson::JsonObject & jsonConfig);
42 void GetDriverName (String& sDriverName);
43 virtual void GetStatus (ArduinoJson::JsonObject & jsonStatus);
44 uint32_t GetNumOutputBufferBytesNeeded () { return OutputBufferSize; };
46 void SetOutputBufferSize (uint32_t NumChannelsAvailable);
47 uint32_t Poll = 0;
48 void StartNewFrame();
49
50 bool IRAM_ATTR ISR_GetNextIntensityToSend(uint32_t &DataToSend);
51 bool IRAM_ATTR ISR_MoreDataToSend() { return (SerialFrameState_t::SerialIdle != SerialFrameState); }
52
53protected:
54 void SetFrameDurration();
55
56#define GS_CHANNEL_LIMIT 2048
57
58 enum class BaudRate
59 {
60 BR_MIN = 38400,
61 BR_FIREGOD = 115200,
62 BR_DMX = 250000,
63 BR_MAX = 460800,
64 BR_DEF = 57600,
65 };
66
67 uint32_t CurrentBaudrate = uint32_t(BaudRate::BR_DEF); // current transmit rate
68
69 /* DMX minimum timings per E1.11 */
70 const uint32_t DMX_BREAK_US = uint32_t(((1.0 / float(BaudRate::BR_DMX)) * 23.0) * float(MicroSecondsInASecond)); // 23 bits = 92us
71 const uint32_t DMX_MAB_US = uint32_t(((1.0 / float(BaudRate::BR_DMX)) * 3.0) * float(MicroSecondsInASecond)); // 3 bits = 12us
72 uint32_t InterFrameGapInMicroSec = DMX_BREAK_US + DMX_MAB_US;
73
74private:
75
76 const uint32_t MAX_HDR_SIZE = 10; // Max generic serial header size
77 const uint32_t MAX_FOOTER_SIZE = 10; // max generic serial footer size
78 const uint32_t MAX_CHANNELS = 1024;
79 const uint16_t DEFAULT_NUM_CHANNELS = 64;
80 const uint32_t BUF_SIZE = (MAX_CHANNELS + MAX_HDR_SIZE + MAX_FOOTER_SIZE);
81 const uint32_t DMX_BITS_PER_BYTE = (1.0 + 8.0 + 2.0);
82 const uint32_t DMX_MaxFrameSize = 512;
83
84 uint32_t Num_Channels = DEFAULT_NUM_CHANNELS; // Number of data channels to transmit
85
86 uint8_t* NextIntensityToSend = nullptr;
87 uint32_t intensity_count = 0;
88 uint32_t SentIntensityCount = 0;
89
90 float IntensityBitTimeInUs = 0.0;
91 uint32_t NumBitsPerIntensity = 1 + 8 + 2; // Start. 8 Data, Stop
92
93 char GenericSerialHeader[65];
94 uint32_t SerialHeaderSize = 0;
95 uint32_t SerialHeaderIndex = 0;
96
97 char GenericSerialFooter[65];
98 uint32_t SerialFooterSize = 0;
99 uint32_t SerialFooterIndex = 0;
100
101#if defined(SUPPORT_OutputType_FireGod)
102 uint16_t FireGodCurrentController = 0;
103 uint8_t FireGodBytesInFrameCount = 0;
104 const uint8_t FireGodNumMaxControllers = 4;
105 const uint8_t FireGodNumChanPerController = 32;
106#endif // defined(SUPPORT_OutputType_FireGod)
107
108#ifdef USE_SERIAL_DEBUG_COUNTERS
109 uint32_t IntensityBytesSent = 0;
110 uint32_t IntensityBytesSentLastFrame = 0;
111 uint32_t FrameStartCounter = 0;
112 uint32_t FrameEndCounter = 0;
113 uint32_t AbortFrameCounter = 0;
114 uint32_t LastDataSent = 0;
115 uint32_t DmxFrameStart = 0;
116 uint32_t DmxSendData = 0;
117 uint32_t Serialidle = 0;
118#define SERIAL_DEBUG_COUNTER(p) p
119
120#else
121
122#define SERIAL_DEBUG_COUNTER(p)
123
124#endif // def USE_SERIAL_DEBUG_COUNTERS
125
126 bool validate ();
127
128 enum RenardFrameDefinitions_t
129 {
130 CMD_DATA_START = 0x80,
131 ESC_CHAR = 0x7F,
132 FRAME_START_CHAR = 0x7E,
133 FRAME_PAD_CHAR = 0x7D,
134 ESCAPED_OFFSET = 0x4E,
135 MIN_VAL_TO_ESC = FRAME_PAD_CHAR,
136 MAX_VAL_TO_ESC = ESC_CHAR
137 };
138
139 enum FireGodFrameDefinitions_t
140 {
141 FRAME_START = 0x55,
142 DATA_BASE = 100,
143 DATA_MAX = 200,
144 };
145
146 enum SerialFrameState_t
147 {
148 RenardFrameStart,
149 RenardDataStart,
150 RenardSendData,
151 RenardSendEscapedData,
152 DMXSendFrameStart,
153 DMXSendData,
154 GenSerSendHeader,
155 GenSerSendData,
156 GenSerSendFooter,
157 FireGodFrameStart,
158 FireGodSendControllerId,
159 FireGodSendData,
160 FireGodSendFill,
161 SerialIdle
162 };
163 SerialFrameState_t SerialFrameState = SerialIdle;
164
165}; // c_OutputSerial
166
167#endif // defined(SUPPORT_OutputType_FireGod) || defined(SUPPORT_OutputType_DMX) || defined(SUPPORT_OutputType_Serial) || defined(SUPPORT_OutputType_Renard)
#define MicroSecondsInASecond
Definition ESPixelStick.h:57
Definition OutputCommon.hpp:31
virtual void GetDriverName(String &sDriverName)=0
get the name for the instantiated driver
virtual void Begin()
set up the operating environment based on the current config (or defaults)
Definition OutputCommon.hpp:43
virtual uint32_t GetNumOutputBufferChannelsServiced()=0
uint32_t OutputBufferSize
Definition OutputCommon.hpp:81
virtual void GetStatus(ArduinoJson::JsonObject &jsonStatus)=0
virtual uint32_t GetNumOutputBufferBytesNeeded()=0
virtual bool SetConfig(ArduinoJson::JsonObject &jsonConfig)
Set a new config in the driver.
Definition OutputCommon.cpp:82
virtual void GetConfig(ArduinoJson::JsonObject &jsonConfig)
Get the current config used by the driver.
Definition OutputCommon.cpp:99
virtual void SetOutputBufferSize(uint32_t NewOutputBufferSize)
Definition OutputCommon.hpp:59
e_OutputChannelIds
Definition OutputMgr.hpp:69
e_OutputType
Definition OutputMgr.hpp:128