@@ -42,10 +42,11 @@ uint16_t imageHeight = 0;
4242uint16_t imageX = 0 ;
4343uint16_t imageY = 0 ;
4444
45+ uint32_t totalPixel = 0 ; // received pixel
46+
4547uint16_t color_buf[512 ];
4648
4749// Statistics for speed testing
48- uint32_t rxCount = 0 ;
4950uint32_t rxStartTime = 0 ;
5051uint32_t rxLastTime = 0 ;
5152
@@ -133,47 +134,45 @@ void loop()
133134 if ( !bleuart.notifyEnabled () ) return ;
134135 if ( !bleuart.available () ) return ;
135136
136- // PRINT_INT(bleuart.available());
137-
138137 // all pixel data is received
139- if ( rxCount == 1 + 5 + imageWidth*imageHeight* 3 )
138+ if ( totalPixel == imageWidth*imageHeight )
140139 {
141140 uint8_t crc = bleuart.read ();
142- rxCount++;
143141 // do checksum later
144142
145- print_speed (rxCount, rxLastTime-rxStartTime);
146- rxCount = 0 ;
143+ print_speed (totalPixel*3 + 7 , rxLastTime-rxStartTime);
144+
145+ // reset and waiting for new image
146+ totalPixel = imageWidth = imageHeight = 0 ;
147147 }
148148
149149 // extract pixel data and display on TFT
150150 uint16_t pixelNum = bleuart.available () / 3 ;
151151
152- // draw up to 512 pixels since color_buf array size is 512
152+ // Draw up to color_buf size ( 512 pixel)
153153 // the rest will be drawn in the next invocation of loop()
154154 pixelNum = min (pixelNum, sizeof (color_buf)/2 );
155155
156+ // Chop pixel number to multiple of image width
157+ pixelNum = (pixelNum / imageWidth) * imageWidth;
158+
156159 if ( pixelNum )
157160 {
158- PRINT_INT (pixelNum);
159- PRINT_INT (imageX);
160- PRINT_INT (imageY);
161-
162161 for ( uint16_t i=0 ; i < pixelNum; i++)
163162 {
164163 uint8_t red = bleuart.read ();
165164 uint8_t green = bleuart.read ();
166165 uint8_t blue = bleuart.read ();
167166
168167 color_buf[i] = ((red & 0xF8 ) << 8 ) | ((green & 0xFC ) << 3 ) | ( blue >> 3 );
169-
170- rxCount += 3 ;
171168 }
172169
173- tft.drawRGBBitmap (imageX, imageY, color_buf, imageWidth, imageHeight);
170+ tft.drawRGBBitmap (imageX, imageY, color_buf, imageWidth, imageHeight - imageY );
174171
175- imageX += (imageX + pixelNum) % imageWidth;
176- imageY += pixelNum/imageHeight;
172+ totalPixel += pixelNum;
173+
174+ imageX = totalPixel % imageWidth;
175+ imageY = totalPixel / imageWidth;
177176 }
178177}
179178
@@ -214,29 +213,23 @@ void bleuart_rx_callback(uint16_t conn_hdl)
214213
215214 rxLastTime = millis ();
216215
217- // first packet: reset time, extract Image Width & Height
218- if ( rxCount == 0 )
216+ // Received new Image
217+ if ( (imageWidth == 0 ) && (imageHeight == 0 ) )
219218 {
220219 rxStartTime = millis ();
221220
222- // Incorrect format, possibly corrupted data
223- if ( bleuart.read () != ' !' )
224- {
225- PRINT_LOCATION ();
226- bleuart.flush ();
227- return ;
228- }
221+ // Skip all data until '!' is found
222+ while ( bleuart.available () && bleuart.read () != ' !' ) { }
223+ if ( !bleuart.available () ) return ;
229224
230- bleuart.read (); rxCount++; // skip unicode extra byte following '!'
225+ bleuart.read (); // skip unicode extra byte following '!'
231226
232227 imageWidth = bleuart.read16 ();
233228 imageHeight = bleuart.read16 ();
234229
235230 PRINT_INT (imageWidth);
236231 PRINT_INT (imageHeight);
237232
238- rxCount += 5 ;
239-
240233 tft.fillScreen (ILI9341_BLACK);
241234 tft.setCursor (0 , 0 );
242235 imageX = imageY = 0 ;
@@ -255,4 +248,6 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
255248 tft.fillScreen (ILI9341_BLACK);
256249 tft.setCursor (0 , 0 );
257250 tft.println (" Advertising ..." );
251+
252+ totalPixel = 0 ;
258253}
0 commit comments