@@ -8,14 +8,31 @@ 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 ,
14
+ "bytesPerPixel" : 2
15
+ } ,
16
+ "GRAYSCALE" : {
17
+ "convert" : convertGrayScaleToRGB888 ,
18
+ "bytesPerPixel" : 1
19
+ } ,
20
+ "RGB888" : {
21
+ "convert" : convertToRGB888 ,
22
+ "bytesPerPixel" : 3
23
+ }
24
+ } ;
25
+
11
26
const imageWidth = 320 ; // Adjust this value based on your bitmap width
12
27
const imageHeight = 240 ; // Adjust this value based on your bitmap height
13
- const bytesPerPixel = 1 ; // Adjust this value based on your bitmap format
28
+ const bytesPerPixel = 2 ; // Adjust this value based on your bitmap format
14
29
// const mode = 'RGB565'; // Adjust this value based on your bitmap format
15
- const totalBytes = imageWidth * imageHeight * bytesPerPixel ;
30
+ const mode = 'GRAYSCALE' ; // Adjust this value based on your bitmap format
31
+ const totalBytes = imageWidth * imageHeight * config [ mode ] . bytesPerPixel ;
16
32
17
33
// Set the buffer size to the total bytes. This allows to read the entire bitmap in one go.
18
34
const bufferSize = Math . min ( totalBytes , 16 * 1024 * 1024 ) ; // Max buffer size is 16MB
35
+ const flowControl = 'hardware' ;
19
36
const baudRate = 115200 ; // Adjust this value based on your device's baud rate
20
37
const dataBits = 8 ; // Adjust this value based on your device's data bits
21
38
const stopBits = 2 ; // Adjust this value based on your device's stop bits
@@ -85,7 +102,7 @@ async function readBytes(port, numBytes, timeout = null){
85
102
const reader = port . readable . getReader ( ) ;
86
103
currentReader = reader ;
87
104
let timeoutID = null ;
88
- let count = 0 ;
105
+ // let count = 0;
89
106
90
107
try {
91
108
while ( bytesReadIdx < numBytes ) {
@@ -129,9 +146,26 @@ async function readBytes(port, numBytes, timeout = null){
129
146
return bytesRead ;
130
147
}
131
148
149
+ // Get the pixel value using big endian
150
+ // Big-endian: the most significant byte comes first
151
+ function getPixelValue ( data , index , bytesPerPixel ) {
152
+ if ( bytesPerPixel == 1 ) {
153
+ return data [ index ] ;
154
+ } else if ( bytesPerPixel == 2 ) {
155
+ return ( data [ index ] << 8 ) | data [ index + 1 ] ;
156
+ } else if ( bytesPerPixel == 3 ) {
157
+ return ( data [ index ] << 16 ) | ( data [ index + 1 ] << 8 ) | data [ index + 2 ] ;
158
+ } else if ( bytesPerPixel == 4 ) {
159
+ return ( data [ index ] << 24 ) | ( data [ index + 1 ] << 16 ) | ( data [ index + 2 ] << 8 ) | data [ index + 3 ] ;
160
+ }
161
+
162
+ return 0 ;
163
+ }
164
+
132
165
function renderBitmap ( bytes , width , height ) {
133
166
canvas . width = width ;
134
167
canvas . height = height ;
168
+ const bytesPerPixel = config [ mode ] . bytesPerPixel ;
135
169
const BYTES_PER_ROW = width * bytesPerPixel ;
136
170
const BYTES_PER_COL = height * bytesPerPixel ;
137
171
@@ -140,13 +174,14 @@ function renderBitmap(bytes, width, height) {
140
174
141
175
for ( let row = 0 ; row < BYTES_PER_ROW ; row ++ ) {
142
176
for ( let col = 0 ; col < BYTES_PER_COL ; col ++ ) {
143
- const byte = bytes [ row * BYTES_PER_COL + col ] ;
144
- const grayscaleValue = byte ;
145
-
146
- const idx = ( row * BYTES_PER_COL + col ) * 4 ;
147
- data [ idx ] = grayscaleValue ; // Red channel
148
- data [ idx + 1 ] = grayscaleValue ; // Green channel
149
- data [ idx + 2 ] = grayscaleValue ; // Blue channel
177
+ const dataIndex = ( row * BYTES_PER_ROW ) + ( col * bytesPerPixel ) ;
178
+ const pixelValue = getPixelValue ( bytes , dataIndex , bytesPerPixel ) ;
179
+ const [ r , g , b ] = config [ mode ] . convert ( pixelValue ) ;
180
+
181
+ const idx = ( ( row * width ) + col ) * 4 ;
182
+ data [ idx ] = r ; // Red channel
183
+ data [ idx + 1 ] = g ; // Green channel
184
+ data [ idx + 2 ] = b ; // Blue channel
150
185
data [ idx + 3 ] = 255 ; // Alpha channel (opacity)
151
186
}
152
187
}
@@ -205,7 +240,7 @@ async function disconnectSerial(port) {
205
240
startButton . addEventListener ( 'click' , renderStream ) ;
206
241
connectButton . addEventListener ( 'click' , async ( ) => {
207
242
currentPort = await requestSerialPort ( ) ;
208
- if ( await connectSerial ( currentPort , baudRate , dataBits , stopBits , bufferSize ) ) {
243
+ if ( await connectSerial ( currentPort , baudRate , dataBits , stopBits , bufferSize , flowControl ) ) {
209
244
renderStream ( ) ;
210
245
}
211
246
} ) ;
0 commit comments