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, 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* 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_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_DMX = 250000,
62 BR_MAX = 460800,
63 BR_DEF = 57600,
64 };
65
66 uint32_t CurrentBaudrate = uint32_t(BaudRate::BR_DEF); // current transmit rate
67
68 /* DMX minimum timings per E1.11 */
69 const uint32_t DMX_BREAK_US = uint32_t(((1.0 / float(BaudRate::BR_DMX)) * 23.0) * float(MicroSecondsInASecond)); // 23 bits = 92us
70 const uint32_t DMX_MAB_US = uint32_t(((1.0 / float(BaudRate::BR_DMX)) * 3.0) * float(MicroSecondsInASecond)); // 3 bits = 12us
71 uint32_t InterFrameGapInMicroSec = DMX_BREAK_US + DMX_MAB_US;
72
73private:
74
75 const uint32_t MAX_HDR_SIZE = 10; // Max generic serial header size
76 const uint32_t MAX_FOOTER_SIZE = 10; // max generic serial footer size
77 const uint32_t MAX_CHANNELS = 1024;
78 const uint16_t DEFAULT_NUM_CHANNELS = 64;
79 const uint32_t BUF_SIZE = (MAX_CHANNELS + MAX_HDR_SIZE + MAX_FOOTER_SIZE);
80 const uint32_t DMX_BITS_PER_BYTE = (1.0 + 8.0 + 2.0);
81 const uint32_t DMX_MaxFrameSize = 512;
82
83 uint32_t Num_Channels = DEFAULT_NUM_CHANNELS; // Number of data channels to transmit
84
85 uint8_t* NextIntensityToSend = nullptr;
86 uint32_t intensity_count = 0;
87 uint32_t SentIntensityCount = 0;
88
89 float IntensityBitTimeInUs = 0.0;
90 uint32_t NumBitsPerIntensity = 1 + 8 + 2; // Start. 8 Data, Stop
91
92 String GenericSerialHeader;
93 uint32_t SerialHeaderSize = 0;
94 uint32_t SerialHeaderIndex = 0;
95
96 String GenericSerialFooter;
97 uint32_t SerialFooterSize = 0;
98 uint32_t SerialFooterIndex = 0;
99
100#ifdef USE_SERIAL_DEBUG_COUNTERS
101 uint32_t IntensityBytesSent = 0;
102 uint32_t IntensityBytesSentLastFrame = 0;
103 uint32_t FrameStartCounter = 0;
104 uint32_t FrameEndCounter = 0;
105 uint32_t AbortFrameCounter = 0;
106 uint32_t LastDataSent = 0;
107 uint32_t DmxFrameStart = 0;
108 uint32_t DmxSendData = 0;
109 uint32_t Serialidle = 0;
110#define SERIAL_DEBUG_COUNTER(p) p
111
112#else
113
114#define SERIAL_DEBUG_COUNTER(p)
115
116#endif // def USE_SERIAL_DEBUG_COUNTERS
117
118 bool validate ();
119
120 enum RenardFrameDefinitions_t
121 {
122 CMD_DATA_START = 0x80,
123 ESC_CHAR = 0x7F,
124 FRAME_START_CHAR = 0x7E,
125 FRAME_PAD_CHAR = 0x7D,
126 ESCAPED_OFFSET = 0x4E,
127 MIN_VAL_TO_ESC = FRAME_PAD_CHAR,
128 MAX_VAL_TO_ESC = ESC_CHAR
129 };
130
131 enum SerialFrameState_t
132 {
133 RenardFrameStart,
134 RenardDataStart,
135 RenardSendData,
136 RenardSendEscapedData,
137 DMXSendFrameStart,
138 DMXSendData,
139 GenSerSendHeader,
140 GenSerSendData,
141 GenSerSendFooter,
142 SerialIdle
143 };
144 SerialFrameState_t SerialFrameState = SerialIdle;
145
146}; // c_OutputSerial
147
148#endif // 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:80
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:84
virtual void GetConfig(ArduinoJson::JsonObject &jsonConfig)
Get the current config used by the driver.
Definition OutputCommon.cpp:101
virtual void SetOutputBufferSize(uint32_t NewOutputBufferSize)
Definition OutputCommon.hpp:59
e_OutputChannelIds
Definition OutputMgr.hpp:68
e_OutputType
Definition OutputMgr.hpp:127