@@ -8,17 +8,14 @@ const ctx = canvas.getContext('2d');
8
8
const UserActionAbortError = 8 ;
9
9
const ArduinoUSBVendorId = 0x2341 ;
10
10
11
- let config = {
12
- "RGB565" : {
13
- "convert" : convertRGB565ToRGB888 ,
11
+ config = {
12
+ "RGB565" : {
14
13
"bytesPerPixel" : 2
15
14
} ,
16
- "GRAYSCALE" : {
17
- "convert" : convertGrayScaleToRGB888 ,
15
+ "GRAYSCALE" : {
18
16
"bytesPerPixel" : 1
19
17
} ,
20
- "RGB888" : {
21
- "convert" : convertToRGB888 ,
18
+ "RGB888" : {
22
19
"bytesPerPixel" : 3
23
20
}
24
21
} ;
@@ -37,6 +34,7 @@ const dataBits = 8; // Adjust this value based on your device's data bits
37
34
const stopBits = 2 ; // Adjust this value based on your device's stop bits
38
35
39
36
let currentPort , currentReader ;
37
+ const imageDataProcessor = new ImageDataProcessor ( ctx , mode , imageWidth , imageHeight ) ;
40
38
41
39
async function requestSerialPort ( ) {
42
40
try {
@@ -83,7 +81,6 @@ async function connectSerial(port, baudRate = 115200, dataBits = 8, stopBits = 2
83
81
}
84
82
}
85
83
86
-
87
84
async function readBytes ( port , numBytes , timeout = null ) {
88
85
if ( port . readable . locked ) {
89
86
console . log ( '🔒 Stream is already locked. Ignoring request...' ) ;
@@ -145,44 +142,10 @@ async function readBytes(port, numBytes, timeout = null){
145
142
return bytesRead ;
146
143
}
147
144
148
- // Get the pixel value using big endian
149
- // Big-endian: the most significant byte comes first
150
- function getPixelValue ( data , index , bytesPerPixel ) {
151
- if ( bytesPerPixel == 1 ) {
152
- return data [ index ] ;
153
- } else if ( bytesPerPixel == 2 ) {
154
- return ( data [ index ] << 8 ) | data [ index + 1 ] ;
155
- } else if ( bytesPerPixel == 3 ) {
156
- return ( data [ index ] << 16 ) | ( data [ index + 1 ] << 8 ) | data [ index + 2 ] ;
157
- } else if ( bytesPerPixel == 4 ) {
158
- return ( data [ index ] << 24 ) | ( data [ index + 1 ] << 16 ) | ( data [ index + 2 ] << 8 ) | data [ index + 3 ] ;
159
- }
160
-
161
- return 0 ;
162
- }
163
-
164
145
function renderBitmap ( bytes , width , height ) {
165
146
canvas . width = width ;
166
147
canvas . height = height ;
167
- const bytesPerPixel = config [ mode ] . bytesPerPixel ;
168
- const BYTES_PER_ROW = width * bytesPerPixel ;
169
-
170
- const imageData = ctx . createImageData ( canvas . width , canvas . height ) ;
171
- const dataContainer = imageData . data ;
172
-
173
- for ( let row = 0 ; row < height ; row ++ ) {
174
- for ( let col = 0 ; col < width ; col ++ ) {
175
- const sourceDataIndex = ( row * BYTES_PER_ROW ) + ( col * bytesPerPixel ) ;
176
- const pixelValue = getPixelValue ( bytes , sourceDataIndex , bytesPerPixel ) ;
177
- const [ r , g , b ] = config [ mode ] . convert ( pixelValue ) ;
178
-
179
- const pixelIndex = ( ( row * width ) + col ) * 4 ;
180
- dataContainer [ pixelIndex ] = r ; // Red channel
181
- dataContainer [ pixelIndex + 1 ] = g ; // Green channel
182
- dataContainer [ pixelIndex + 2 ] = b ; // Blue channel
183
- dataContainer [ pixelIndex + 3 ] = 255 ; // Alpha channel (opacity)
184
- }
185
- }
148
+ const imageData = imageDataProcessor . getImageDataBytes ( bytes , width , height ) ;
186
149
ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
187
150
ctx . putImageData ( imageData , 0 , 0 ) ;
188
151
}
@@ -198,6 +161,7 @@ async function requestFrame(port){
198
161
await writer . write ( new Uint8Array ( [ 1 ] ) ) ;
199
162
await writer . close ( ) ;
200
163
}
164
+
201
165
async function renderStream ( ) {
202
166
while ( true && currentPort ) {
203
167
await renderFrame ( currentPort ) ;
0 commit comments