-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstream.ino
230 lines (199 loc) · 6.01 KB
/
stream.ino
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/////////////////////// STREAM SIMPLE ////////////////////////////////
#if defined (STREAM_SIMPLE)
int serindex =0;
int trame = 0;
// Decode Simple Pixel Stream, STREAM_BITPLANES bitplane (defailt = 1)
boolean checkSerial() {
int STREAM_BITPLANES = 1; // nb de bitplanes
int ret = false;
if (Serial.available()) {
virtualdisplay[trame][serindex++] = (byte)Serial.read();
if(serindex == 96) {
serindex = 0;
trame++;
if(trame == STREAM_BITPLANES) {
ret = true;
trame = 0;
}
}
} // end Serial
return ret;
}
#endif
/////////////////////// STREAM GENERIC ////////////////////////////////
// Decode Composite Pixel Stream with a number of "imageBitplanes" bitplanes (default = 1)
#if defined (STREAM_GENERIC)
byte virtualdisplay_buffer[3][XMAX];
int serindex =0;
int trame = 0;
int SOF[4] = {
10, 20, 30, 40};
int sofindex = 0;
boolean letsgo = false;
int structstart = 0;
int structindex = 0;
int imageSize = 0;
boolean isCompressed = false;
int imageBitplanes = 1; // nb de bitplanes
int Compressedsize = 0;
byte crc = 0;
int getUsedBitplanes() {
return imageBitplanes;
}
//void serialEvent() {
boolean checkSerial() {
boolean dodisplay = false;
while( Serial.available() ) {
int rec = Serial.read();
///// HEADER //
if( !letsgo) {
if( rec == SOF[structstart] ) {
structstart++;
#ifdef STREAMDEBUG
Serial.println(".");
#endif
} // START OF FRAME
else {
structstart = 0;
#ifdef STREAMDEBUG
Serial.print("_");
Serial.print(rec);
#endif
}
if( structstart == 4 ) {
letsgo = true;
structstart = 0;
structindex = 0;
serindex = 0;
trame = 0;
setvirtualdisplay(0);
crc = 0;
#ifdef STREAMDEBUG
Serial.println("H OK");
#endif
}
}
///// SETTINGS //
else {
// PARAM 0
if( structindex == 0 ) { // SIZE of image ...
imageSize = rec;
if( imageSize > XMAX ) imageSize = XMAX; // error!
structindex++;
crc += rec;
#ifdef STREAMDEBUG
Serial.print("S: ");
Serial.println(imageSize);
#endif
}
// PARAM 1
else if( structindex == 1 ){ // COMPRESSION used ?
if(rec != 0 ) isCompressed = true;
else isCompressed = false;
structindex++;
crc += isCompressed?1:0;
#ifdef STREAMDEBUG
Serial.println(isCompressed?"comp":"plain");
#endif
}
// PARAM 2
else if( structindex == 2 ){ // HOW MANY BITPLANES ?
imageBitplanes = rec;
if( imageBitplanes > BITPLANES ) imageBitplanes = BITPLANES; // error !
if( imageBitplanes < 1 ) imageBitplanes = 1; // error !
structindex++;
crc += rec;
#ifdef STREAMDEBUG
Serial.print("B: ");
Serial.println(imageBitplanes);
#endif
}
// PARAM 3
else if( structindex == 3 ){ // FRAME SIZE
Compressedsize = rec;
structindex++;
crc += rec;
#ifdef STREAMDEBUG
Serial.print("L: ");
Serial.println( Compressedsize );
#endif
}
// PARAM 4
else if( structindex == 4 ){ // CRC for the HEADER
//if( rec != crc ) letsgo = false; // reset if not the good crc
structindex++;
#ifdef STREAMDEBUG
Serial.print("C: ");
Serial.println(rec);
if( rec != crc ) Serial.println("CRC NOK");
#endif
}
///// TRAME NOT COMPRESSED /////
// PARAMs : TRAME
else if( structindex == 5 ){
if( ! isCompressed ){
//virtualdisplay_buffer[trame][serindex++] = rec; // double buffering transfer for each bitplane
virtualdisplay[trame][serindex++] = rec;
if( serindex >= imageSize ) { // 1 Trame received
#ifdef STREAMDEBUG
Serial.println("trm");
#endif
serindex = 0;
if( trame >= imageBitplanes - 1 ) { // all Trames received
//dirty = true;
//memcpy( &virtualdisplay[trame][0], &virtualdisplay_buffer[trame][0], imageSize ); // double buffering transfer for each bitplane
//dirty = false;
////// exp : memcpy( virtualdisplay, virtualdisplay_buffer, imageSize ); //
trame = 0;
structstart = 0;
structindex = 0;
serindex = 0;
letsgo = false;
dodisplay = true;
#ifdef STREAMDEBUG
Serial.println("/");
#endif
}
else trame++;
}
}
///// TRAME COMPRESSED ...WIP ! /////
else {
virtualdisplay_buffer[0][serindex++] = rec;
if( serindex >= Compressedsize ) { // Trame received
#ifdef STREAMDEBUG
Serial.println("trm");
#endif
// decoding 1 TRAME
int indexpix=0;
int pixval, count;
for (int idx = 0; idx < Compressedsize; idx = idx + 2 ) {
pixval = virtualdisplay_buffer[0][ idx ];
count = virtualdisplay_buffer[0][ idx+1];
for( int k = 0; k < count; k++ ) {
int x = indexpix % imageSize;
int y = indexpix / (imageSize);
if( pixval != 255 ) pointvirtual16( x, y, pixval );
indexpix++;
}
}
// end decoding
serindex = 0;
//memcpy( &virtualdisplay[trame][0], &virtualdisplay_buffer[trame][0], imageSize ); // double buffering transfer for each bitplane
//memcpy( virtualdisplay, virtualdisplay_buffer, imageSize ); //
trame = 0;
structstart = 0;
structindex = 0;
letsgo = false;
dodisplay = true;
#ifdef STREAMDEBUG
Serial.println("/");
#endif
} // end trame received
} // trame
} // end trame
}
return dodisplay;
} // end Serial
}
#endif