/
USB_CDC.h
executable file
·208 lines (190 loc) · 6.74 KB
/
USB_CDC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//-------------------------------------------------------------------------------
// TinyCircuits TinyTV Firmware
//
// Changelog:
// 05/26/2023 Initial Release for TinyTV 2/Mini
// 02/08/2023 Cross-platform base committed
//
// Written by Mason Watmough, Ben Rose, and Jason Marcum for TinyCircuits, http://TinyCircuits.com
//
//-------------------------------------------------------------------------------
uint16_t jpegBufferReadCount = 0;
uint32_t frameSize = 0;
uint32_t tempFrameSize = 0;
bool frameDeliminatorAcquired = false;
uint32_t liveTimeoutStart = 0;
uint16_t liveTimeoutLimitms = 750;
char commandBuffer[30];
uint8_t commandBufPos = 0;
uint32_t commandStartMS = 0;
uint32_t commandStartTimeoutMS = 20;
#ifdef ARDUINO_ARCH_RP2040
#define SerialInterface cdc
#else
#define SerialInterface SerialUSB
#endif
//
//{"asdf":"jkl"}
//{"SET":"channel=2"}
//{"SET":"volume=1"}
//{"GET":"channel"}
//{"FRAME":5000}
//{"SET":"alphabetize=true"}
//{"GET":"alphabetize"}
//#define cdc SerialUSB
bool setKeyValue(String);
String getKeyValue(String);
void initVideoPlayback(bool);
bool handleCDCcommand(String input) {
if ( input.length() > 5 && input.indexOf(":") >= 0) {
String key = input.substring(0, input.indexOf(":"));
String val = input.substring(input.indexOf(":") + 1);
key.trim();
val.trim();
while (key.indexOf("\"") >= 0) {
key.remove(key.indexOf("\""), 1);
}
while (val.indexOf("\"") >= 0) {
val.remove(val.indexOf("\""), 1);
}
// cdc.print("key = ");
// cdc.print(key);
// cdc.print(", val = ");
// cdc.println(val);
if (key == String("FRAME")) {
frameDeliminatorAcquired = true;
frameSize = val.toInt();
} else if (key == String("GET")) {
//cdc.println( String("{\"") + val + String("\":\"") + getKeyValue(val) + String("\"}"));
SerialInterface.println( String("{\"") + val + String("\":") + getKeyValue(val) + String("}"));
//cdc.print(val);
//cdc.print(":");
//cdc.println(getKeyValue(val));
} else if (key == String("SET")) {
if (setKeyValue(val)) {
inputFlags.settingsChanged = true;//.saveSettings();
}
} else if (key == String("FORMAT")) {
if (val == String("SDFAT")) {
FatFormatter fatFormatter;
uint8_t sectorBuffer[512];
bool rtn = fatFormatter.format(sd.card(), sectorBuffer, NULL);
if (!rtn) {
SerialInterface.println("Sd Format Error");
} else {
SerialInterface.println("Sd Format Success");
}
#ifndef TinyTVKit
delay(500);
hardwarePowerOff();
#endif
initVideoPlayback(true);
}
} else {
SerialInterface.println("Unhandled JSON key");
}
} else {
SerialInterface.print("Invalid JSON? ");
SerialInterface.println(input);
return true;
}
return false;
}
void commandSearch(uint16_t jpegBufferSize) {
while (SerialInterface.available()) {
char c = SerialInterface.read();
if (!commandStartMS) {
if (c == '{') {
//start of JSON string
commandStartMS = millis();
commandBufPos = 0;
}
} else {
if (c == '}') {
//end of JSON string
commandBuffer[commandBufPos] = 0;
if (handleCDCcommand(String(commandBuffer))) {
// Format error- clear buffer
while (SerialInterface.available()) {
SerialInterface.read();
}
}
commandStartMS = 0;
return;
} else {
//within JSON string, add bytes or wait for timeout
if (commandBufPos < sizeof(commandBuffer) - 1) {
commandBuffer[commandBufPos] = c;
commandBufPos++;
}
if (millis() - commandStartMS > commandStartTimeoutMS) {
commandStartMS = 0;
}
}
}
}
}
bool incomingCDCHandler(uint8_t *jpegBuffer, uint16_t jpegBufferSize, bool *live, uint16_t *totalBytes) {
if (SerialInterface.available() > 0) {
liveTimeoutStart = millis();
if (!frameDeliminatorAcquired) {
// Search for deliminator to get back to filling buffers or respond to commands
commandSearch(jpegBufferSize);
}
if (frameDeliminatorAcquired) {
//#ifdef ARDUINO_ARCH_RP2040
if (frameSize >= jpegBufferSize) {
frameSize = 0;
jpegBufferReadCount = 0;
frameDeliminatorAcquired = false;
//cdc.println("ERROR: Received frame size is too big");
return false;
}
*live = true;
if (frameSize) {
if (SerialInterface.available() > 0) {
// If the frame size was determined, get number of bytes to read, check if done filling, then fill if not done
uint16_t bytesToReadCount = frameSize - (jpegBufferReadCount + 1);
// Just in case, check if this read will take us out of bounds, if so, restart (shouldn't happen except for future changes forgetting about this)
if (jpegBufferReadCount + bytesToReadCount < frameSize) {
#ifdef ARDUINO_ARCH_RP2040
jpegBufferReadCount += cdc.read(jpegBuffer + jpegBufferReadCount, bytesToReadCount);
#else
while (SerialUSB.available() && bytesToReadCount) {
jpegBuffer[jpegBufferReadCount] = SerialUSB.read();
jpegBufferReadCount++;
bytesToReadCount = frameSize - (jpegBufferReadCount + 1);
}
//SerialUSB.println("received some frame data");
#endif
} else {
// Not going to get reset by decode so do it here so it doesn't get stuck out of bounds forever
jpegBufferReadCount = 0;
frameSize = 0;
frameDeliminatorAcquired = false;
SerialInterface.println("ERROR: Tried to place jpeg data out of bounds...");
}
if (bytesToReadCount <= 0) {
//cdc.println("Done receiving frame data");
//SerialUSB.println("Done receiving frame data");
frameSize = 0;
frameDeliminatorAcquired = false;
*totalBytes = jpegBufferReadCount;
jpegBufferReadCount = 0;
return true;
}
}
}
//#endif
}
} else if (*live && millis() - liveTimeoutStart >= liveTimeoutLimitms) {
// A timeout is a time to reset states of both jpeg buffers, reset everything
SerialInterface.println("Streaming timeout- outer loop");
frameSize = 0;
frameDeliminatorAcquired = false;
*live = false;
// Wait for decoding to finish and then reset incoming jpeg data read counts (otherwise may start at something other than zero next time)
}
// No buffer filled, wait for more bytes
return false;
}