@@ -39,7 +39,7 @@ async function sendCommand(cmd) {
39
39
}
40
40
41
41
async function sendcmd ( cmdTXT ) {
42
- addLog ( `发送命令: ${ cmdTXT } ` ) ;
42
+ addLog ( `<span class="action">⇑</span> ${ cmdTXT } ` ) ;
43
43
await sendCommand ( hexToBytes ( cmdTXT ) ) ;
44
44
}
45
45
@@ -71,9 +71,6 @@ async function sendCmWithData(cmd, data){
71
71
}
72
72
73
73
async function send4GrayLut ( ) {
74
- await sendcmd ( "0300" ) ;
75
- await sendcmd ( "043F" ) ; // Load LUT from register
76
-
77
74
await sendCmWithData ( "20" , "000A0000000160141400000100140000000100130A010001000000000000000000000000000000000000" ) ; // vcom
78
75
await sendCmWithData ( "21" , "400A0000000190141400000110140A000001A01301000001000000000000000000000000000000000000" ) ; // red not use
79
76
await sendCmWithData ( "22" , "400A0000000190141400000100140A000001990C01030401000000000000000000000000000000000000" ) ; // bw r
@@ -82,11 +79,29 @@ async function send4GrayLut() {
82
79
await sendCmWithData ( "25" , "400A0000000190141400000110140A000001A01301000001000000000000000000000000000000000000" ) ; // vcom
83
80
}
84
81
85
- async function sendimg ( cmdIMG ) {
82
+ function getImageData ( canvas , driver , mode ) {
83
+ if ( mode === '4gray' ) {
84
+ return bytesToHex ( canvas2gray ( canvas ) ) ;
85
+ } else {
86
+ let data = bytesToHex ( canvas2bytes ( canvas , 'bw' ) ) ;
87
+ if ( driver === '03' ) {
88
+ if ( mode . startsWith ( 'bwr' ) ) {
89
+ data += bytesToHex ( canvas2bytes ( canvas , 'red' ) ) ;
90
+ } else {
91
+ const count = data . length ;
92
+ data += 'F' . repeat ( count ) ;
93
+ }
94
+ }
95
+ return data ;
96
+ }
97
+ }
98
+
99
+ async function sendimg ( ) {
86
100
startTime = new Date ( ) . getTime ( ) ;
101
+ const canvas = document . getElementById ( "canvas" ) ;
87
102
const driver = document . getElementById ( "epddriver" ) . value ;
88
103
const mode = document . getElementById ( 'dithering' ) . value ;
89
- const imgArray = cmdIMG . replace ( / (?: \r \n | \r | \n | , | 0 x | ) / g , '' ) ;
104
+ const imgArray = getImageData ( canvas , driver , mode ) ;
90
105
const bwArrLen = ( canvas . width / 8 ) * canvas . height * 2 ;
91
106
92
107
if ( imgArray . length == bwArrLen * 2 ) {
@@ -95,10 +110,14 @@ async function sendimg(cmdIMG) {
95
110
} else {
96
111
await sendCmWithData ( driver === "03" ? "10" : "13" , imgArray ) ;
97
112
}
113
+
98
114
if ( mode === "4gray" ) {
115
+ await sendcmd ( "0300" ) ;
116
+ await sendcmd ( "043F" ) ; // Load LUT from register
99
117
await send4GrayLut ( ) ;
100
118
await sendcmd ( "05" ) ;
101
- await sendcmd ( `01${ driver } ` ) ; // restore lut
119
+ await sendcmd ( "0300" ) ;
120
+ await sendcmd ( "041F" ) ; // Load LUT from OTP
102
121
} else {
103
122
await sendcmd ( "05" ) ;
104
123
}
@@ -111,17 +130,18 @@ async function sendimg(cmdIMG) {
111
130
function updateButtonStatus ( ) {
112
131
const connected = gattServer != null && gattServer . connected ;
113
132
const status = connected ? null : 'disabled' ;
133
+ document . getElementById ( "reconnectbutton" ) . disabled = ( gattServer == null || gattServer . connected ) ? 'disabled' : null ;
114
134
document . getElementById ( "sendcmdbutton" ) . disabled = status ;
115
135
document . getElementById ( "clearscreenbutton" ) . disabled = status ;
116
136
document . getElementById ( "sendimgbutton" ) . disabled = status ;
117
137
document . getElementById ( "setDriverbutton" ) . disabled = status ;
118
138
}
119
139
120
140
function disconnect ( ) {
141
+ updateButtonStatus ( ) ;
121
142
resetVariables ( ) ;
122
143
addLog ( '已断开连接.' ) ;
123
144
document . getElementById ( "connectbutton" ) . innerHTML = '连接' ;
124
- updateButtonStatus ( ) ;
125
145
}
126
146
127
147
async function preConnect ( ) {
@@ -133,10 +153,16 @@ async function preConnect() {
133
153
}
134
154
else {
135
155
connectTrys = 0 ;
136
- bleDevice = await navigator . bluetooth . requestDevice ( {
137
- optionalServices : [ '62750001-d828-918d-fb46-b6c11c675aec' ] ,
138
- acceptAllDevices : true
139
- } ) ;
156
+ try {
157
+ bleDevice = await navigator . bluetooth . requestDevice ( {
158
+ optionalServices : [ '62750001-d828-918d-fb46-b6c11c675aec' ] ,
159
+ acceptAllDevices : true
160
+ } ) ;
161
+ } catch ( e ) {
162
+ if ( e . message ) addLog ( e . message ) ;
163
+ return ;
164
+ }
165
+
140
166
await bleDevice . addEventListener ( 'gattserverdisconnected' , disconnect ) ;
141
167
try {
142
168
await connect ( ) ;
@@ -156,21 +182,21 @@ async function reConnect() {
156
182
}
157
183
158
184
async function connect ( ) {
159
- if ( epdCharacteristic == null ) {
185
+ if ( epdCharacteristic == null && bleDevice != null ) {
160
186
addLog ( "正在连接: " + bleDevice . name ) ;
161
187
162
188
gattServer = await bleDevice . gatt . connect ( ) ;
163
- addLog ( '> 找到 GATT Server' ) ;
189
+ addLog ( ' 找到 GATT Server' ) ;
164
190
165
191
epdService = await gattServer . getPrimaryService ( '62750001-d828-918d-fb46-b6c11c675aec' ) ;
166
- addLog ( '> 找到 EPD Service' ) ;
192
+ addLog ( ' 找到 EPD Service' ) ;
167
193
168
194
epdCharacteristic = await epdService . getCharacteristic ( '62750002-d828-918d-fb46-b6c11c675aec' ) ;
169
- addLog ( '> 找到 Characteristic' ) ;
195
+ addLog ( ' 找到 Characteristic' ) ;
170
196
171
197
await epdCharacteristic . startNotifications ( ) ;
172
198
epdCharacteristic . addEventListener ( 'characteristicvaluechanged' , ( event ) => {
173
- addLog ( `> 收到配置: ${ bytesToHex ( event . target . value . buffer ) } ` ) ;
199
+ addLog ( `<span class="action">⇓</span> ${ bytesToHex ( event . target . value . buffer ) } ` ) ;
174
200
document . getElementById ( "epdpins" ) . value = bytesToHex ( event . target . value . buffer . slice ( 0 , 7 ) ) ;
175
201
document . getElementById ( "epddriver" ) . value = bytesToHex ( event . target . value . buffer . slice ( 7 , 8 ) ) ;
176
202
} ) ;
@@ -187,13 +213,17 @@ function setStatus(statusText) {
187
213
}
188
214
189
215
function addLog ( logTXT ) {
190
- const today = new Date ( ) ;
191
- const time = ( "0" + today . getHours ( ) ) . slice ( - 2 ) + ":" + ( "0" + today . getMinutes ( ) ) . slice ( - 2 ) + ":" + ( "0" + today . getSeconds ( ) ) . slice ( - 2 ) + " : " ;
192
- document . getElementById ( "log" ) . innerHTML += time + logTXT + '<br>' ;
193
- console . log ( time + logTXT ) ;
194
- while ( ( document . getElementById ( "log" ) . innerHTML . match ( / < b r > / g) || [ ] ) . length > 10 ) {
195
- var logs_br_position = document . getElementById ( "log" ) . innerHTML . search ( "<br>" ) ;
196
- document . getElementById ( "log" ) . innerHTML = document . getElementById ( "log" ) . innerHTML . substring ( logs_br_position + 4 ) ;
216
+ const log = document . getElementById ( "log" ) ;
217
+ const now = new Date ( ) ;
218
+ const time = String ( now . getHours ( ) ) . padStart ( 2 , '0' ) + ":" +
219
+ String ( now . getMinutes ( ) ) . padStart ( 2 , '0' ) + ":" +
220
+ String ( now . getSeconds ( ) ) . padStart ( 2 , '0' ) + " " ;
221
+ log . innerHTML += '<span class="time">' + time + '</span>' + logTXT + '<br>' ;
222
+ log . scrollTop = log . scrollHeight ;
223
+ while ( ( log . innerHTML . match ( / < b r > / g) || [ ] ) . length > 20 ) {
224
+ var logs_br_position = log . innerHTML . search ( "<br>" ) ;
225
+ log . innerHTML = log . innerHTML . substring ( logs_br_position + 4 ) ;
226
+ log . scrollTop = log . scrollHeight ;
197
227
}
198
228
}
199
229
@@ -215,24 +245,6 @@ function intToHex(intIn) {
215
245
return stringOut . substring ( 2 , 4 ) + stringOut . substring ( 0 , 2 ) ;
216
246
}
217
247
218
- function updateImageData ( canvas ) {
219
- const driver = document . getElementById ( "epddriver" ) . value ;
220
- const mode = document . getElementById ( 'dithering' ) . value ;
221
- if ( mode === '4gray' ) {
222
- document . getElementById ( 'cmdIMAGE' ) . value = bytesToHex ( canvas2gray ( canvas ) ) ;
223
- } else {
224
- document . getElementById ( 'cmdIMAGE' ) . value = bytesToHex ( canvas2bytes ( canvas , 'bw' ) ) ;
225
- if ( driver === '03' ) {
226
- if ( mode . startsWith ( 'bwr' ) ) {
227
- document . getElementById ( 'cmdIMAGE' ) . value += bytesToHex ( canvas2bytes ( canvas , 'red' ) ) ;
228
- } else {
229
- const count = document . getElementById ( 'cmdIMAGE' ) . value . length ;
230
- document . getElementById ( 'cmdIMAGE' ) . value += 'F' . repeat ( count ) ;
231
- }
232
- }
233
- }
234
- }
235
-
236
248
async function update_image ( ) {
237
249
const canvas = document . getElementById ( "canvas" ) ;
238
250
const ctx = canvas . getContext ( "2d" ) ;
@@ -258,7 +270,6 @@ function clear_canvas() {
258
270
const ctx = canvas . getContext ( "2d" ) ;
259
271
ctx . fillStyle = 'white' ;
260
272
ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
261
- document . getElementById ( 'cmdIMAGE' ) . value = '' ;
262
273
}
263
274
}
264
275
@@ -272,7 +283,6 @@ function convert_dithering() {
272
283
} else {
273
284
dithering ( ctx , canvas . width , canvas . height , parseInt ( document . getElementById ( 'threshold' ) . value ) , mode ) ;
274
285
}
275
- updateImageData ( canvas ) ;
276
286
}
277
287
278
288
document . body . onload = ( ) => {
0 commit comments