Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d54dba8

Browse files
committedMay 24, 2023
Working version for RGB
1 parent 70f6b23 commit d54dba8

File tree

4 files changed

+123
-19
lines changed

4 files changed

+123
-19
lines changed
 

‎libraries/Camera/extras/WebSerialCamera/app.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,31 @@ const ctx = canvas.getContext('2d');
88
const UserActionAbortError = 8;
99
const ArduinoUSBVendorId = 0x2341;
1010

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+
1126
const imageWidth = 320; // Adjust this value based on your bitmap width
1227
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
1429
// 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;
1632

1733
// Set the buffer size to the total bytes. This allows to read the entire bitmap in one go.
1834
const bufferSize = Math.min(totalBytes, 16 * 1024 * 1024); // Max buffer size is 16MB
35+
const flowControl = 'hardware';
1936
const baudRate = 115200; // Adjust this value based on your device's baud rate
2037
const dataBits = 8; // Adjust this value based on your device's data bits
2138
const stopBits = 2; // Adjust this value based on your device's stop bits
@@ -85,7 +102,7 @@ async function readBytes(port, numBytes, timeout = null){
85102
const reader = port.readable.getReader();
86103
currentReader = reader;
87104
let timeoutID = null;
88-
let count = 0;
105+
// let count = 0;
89106

90107
try {
91108
while (bytesReadIdx < numBytes) {
@@ -129,9 +146,26 @@ async function readBytes(port, numBytes, timeout = null){
129146
return bytesRead;
130147
}
131148

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+
132165
function renderBitmap(bytes, width, height) {
133166
canvas.width = width;
134167
canvas.height = height;
168+
const bytesPerPixel = config[mode].bytesPerPixel;
135169
const BYTES_PER_ROW = width * bytesPerPixel;
136170
const BYTES_PER_COL = height * bytesPerPixel;
137171

@@ -140,13 +174,14 @@ function renderBitmap(bytes, width, height) {
140174

141175
for (let row = 0; row < BYTES_PER_ROW; row++) {
142176
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
150185
data[idx + 3] = 255; // Alpha channel (opacity)
151186
}
152187
}
@@ -205,7 +240,7 @@ async function disconnectSerial(port) {
205240
startButton.addEventListener('click', renderStream);
206241
connectButton.addEventListener('click', async () => {
207242
currentPort = await requestSerialPort();
208-
if(await connectSerial(currentPort, baudRate, dataBits, stopBits, bufferSize)){
243+
if(await connectSerial(currentPort, baudRate, dataBits, stopBits, bufferSize, flowControl)){
209244
renderStream();
210245
}
211246
});

‎libraries/Camera/extras/WebSerialCamera/conversion.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ function convertRGB565ToRGB888(pixelValue) {
99
b <<= 3;
1010
return [r, g, b];
1111
}
12+
13+
function convertGrayScaleToRGB888(pixelValue) {
14+
return [pixelValue, pixelValue, pixelValue];
15+
}
16+
17+
function convertToRGB888(pixelValue){
18+
return [pixelValue[0], pixelValue[1], pixelValue[2]];
19+
}

‎libraries/Camera/extras/WebSerialCamera/index.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Web Serial Bitmap Reader</title>
7-
<style>
8-
#main-container {
9-
display: flex;
10-
flex-direction: column;
11-
align-items: center;
12-
gap: 1rem;
13-
}
14-
</style>
7+
<link rel="stylesheet" href="style.css">
158
</head>
169
<body>
1710
<div id="main-container">
@@ -23,6 +16,7 @@
2316
<button id="start">Start</button>
2417
</div>
2518
</div>
19+
<script src="conversion.js"></script>
2620
<script src="app.js"></script>
2721
</body>
2822
</html>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
:root {
2+
--main-control-color: #008184;
3+
--main-control-color-hover: #005c5f;
4+
--main-flexbox-gap: 16px;
5+
--secondary-text-color: #87898b;
6+
}
7+
8+
html {
9+
font-size: 14px;
10+
}
11+
12+
body {
13+
font-family: 'Open Sans', sans-serif;
14+
text-align: center;
15+
}
16+
17+
#main-container {
18+
display: flex;
19+
flex-direction: column;
20+
align-items: center;
21+
gap: 1rem;
22+
margin-top: 20px;
23+
}
24+
25+
#controls {
26+
display: flex;
27+
flex-direction: row;
28+
align-items: center;
29+
gap: 1rem;
30+
margin-top: 20px;
31+
}
32+
33+
canvas {
34+
border-radius: 5px;
35+
}
36+
37+
button {
38+
font-family: 'Open Sans', sans-serif;
39+
font-weight: 700;
40+
font-size: 1rem;
41+
justify-content: center;
42+
background-color: var(--main-control-color);
43+
color: #fff;
44+
cursor: pointer;
45+
letter-spacing: 1.28px;
46+
line-height: normal;
47+
outline: none;
48+
padding: 8px 18px;
49+
text-align: center;
50+
text-decoration: none;
51+
border: 2px solid transparent;
52+
border-radius: 32px;
53+
text-transform: uppercase;
54+
box-sizing: border-box;
55+
}
56+
57+
button:hover {
58+
background-color: var(--main-control-color-hover);
59+
}
60+
61+
#refresh {
62+
display: none;
63+
}
64+
65+
#start {
66+
display: none;
67+
}

0 commit comments

Comments
 (0)
Please sign in to comment.