libzedmd
ZeDMDComm.h
1#pragma once
2
3#if defined(__APPLE__)
4#include <TargetConditionals.h>
5#endif
6
7#if !( \
8 (defined(__APPLE__) && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_TV) && TARGET_OS_TV))) || \
9 defined(__ANDROID__))
10#include "libserialport.h"
11#endif
12
13#include <inttypes.h>
14#include <stdarg.h>
15
16#include <cstdio>
17#include <mutex>
18#include <queue>
19#include <string>
20#include <thread>
21
22#ifdef _MSC_VER
23#define ZEDMDCALLBACK __stdcall
24#else
25#define ZEDMDCALLBACK
26#endif
27
28typedef enum
29{
30 FrameSize = 0x02,
31 Handshake = 0x0c,
32 Chunk = 0x0d,
33 Compression = 0x0e,
34 EnableCompression = 0x0e,
35 DisableCompression = 0x0f,
36 LEDTest = 0x10,
37 EnableUpscaling = 0x15,
38 DisableUpscaling = 0x14,
39 Brightness = 0x16,
40 RGBOrder = 0x17,
41 GetBrightness = 0x18,
42 GetRGBOrder = 0x19,
43 EnableFlowControlV2 = 0x1a,
44 SetWiFiSSID = 0x1b,
45 SetWiFiPassword = 0x1c,
46 SetWiFiPort = 0x1d,
47 SaveSettings = 0x1e,
48 Reset = 0x1f,
49 GetVersionBytes = 0x20,
50 GetResolution = 0x21,
51
52 RGB24 = 0x03,
53 RGB24ZonesStream = 0x04,
54 RGB565ZonesStream = 0x05,
55 Gray2 = 0x08,
56 ColGray4 = 0x09,
57 ColGray6 = 0x0b,
58 ClearScreen = 0x0a,
59
60 DisableDebug = 0x62,
61 EnableDebug = 0x63,
62} ZEDMD_COMM_COMMAND;
63
65{
66 uint8_t command;
67 uint8_t* data;
68 int size;
69 int8_t streamId;
70};
71
72#define ZEDMD_COMM_BAUD_RATE 921600
73#define ZEDMD_S3_COMM_BAUD_RATE 921600 * 8
74
75#if defined(_WIN32) || defined(_WIN64)
76#define ZEDMD_COMM_MAX_SERIAL_WRITE_AT_ONCE 1888
77#define ZEDMD_COMM_SERIAL_READ_TIMEOUT 16
78#define ZEDMD_COMM_SERIAL_WRITE_TIMEOUT 16
79#else
80#define ZEDMD_COMM_MAX_SERIAL_WRITE_AT_ONCE 1888
81#define ZEDMD_COMM_SERIAL_READ_TIMEOUT 32
82#define ZEDMD_COMM_SERIAL_WRITE_TIMEOUT 16
83#endif
84
85#define ZEDMD_COMM_FRAME_SIZE_COMMAND_LIMIT 10
86#define ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX 8
87#define ZEDMD_COMM_FRAME_QUEUE_SIZE_MAX_DELAYED 2
88
89typedef void(ZEDMDCALLBACK* ZeDMD_LogCallback)(const char* format, va_list args, const void* userData);
90
92{
93 public:
94 static const int CTRL_CHARS_HEADER_SIZE = 6;
95 static constexpr uint8_t CTRL_CHARS_HEADER[] = {0x5a, 0x65, 0x64, 0x72, 0x75, 0x6d};
96
97 public:
98 ZeDMDComm();
99 ~ZeDMDComm();
100
101 void SetLogCallback(ZeDMD_LogCallback callback, const void* userData);
102
103 void IgnoreDevice(const char* ignore_device);
104 void SetDevice(const char* device);
105
106 virtual bool Connect();
107 virtual void Disconnect();
108 virtual bool IsConnected();
109
110 void Run();
111 void QueueCommand(char command, uint8_t* buffer, int size, uint16_t width, uint16_t height, uint8_t bytes = 3);
112 void QueueCommand(char command, uint8_t* buffer, int size, int8_t streamId = -1, bool delayed = false);
113 void QueueCommand(char command);
114 void QueueCommand(char command, uint8_t value);
115 bool FillDelayed();
116
117 uint16_t const GetWidth();
118 uint16_t const GetHeight();
119 bool const IsS3();
120
121 protected:
122 virtual bool StreamBytes(ZeDMDFrame* pFrame);
123 virtual void Reset();
124
125 uint16_t m_zonesBytesLimit = 0;
126 uint8_t m_zoneWidth = 8;
127 uint8_t m_zoneHeight = 4;
128
129 private:
130 void Log(const char* format, ...);
131
132 bool Connect(char* pName);
133
134 ZeDMD_LogCallback m_logCallback = nullptr;
135 const void* m_logUserData = nullptr;
136 uint64_t m_zoneHashes[128] = {0};
137 uint16_t m_width = 128;
138 uint16_t m_height = 32;
139 int8_t m_streamId = -1;
140 int8_t m_lastStreamId = -1;
141 uint8_t m_flowControlCounter = 0;
142 char m_ignoredDevices[10][32] = {0};
143 uint8_t m_ignoredDevicesCounter = 0;
144 char m_device[32] = {0};
145#if !( \
146 (defined(__APPLE__) && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_TV) && TARGET_OS_TV))) || \
147 defined(__ANDROID__))
148 struct sp_port* m_pSerialPort;
149 struct sp_port_config* m_pSerialPortConfig;
150#endif
151 std::queue<ZeDMDFrame> m_frames;
152 std::thread* m_pThread;
153 std::mutex m_frameQueueMutex;
154 uint8_t m_frameCounter = 0;
155 std::queue<ZeDMDFrame> m_delayedFrames;
156 std::mutex m_delayedFrameMutex;
157 bool m_delayedFrameReady = false;
158 bool m_s3 = false;
159};
Definition: ZeDMDComm.h:92
Definition: ZeDMDComm.h:65