@@ -39,7 +39,7 @@ async function sendCommand(cmd) {
3939}
4040
4141async function sendcmd ( cmdTXT ) {
42- addLog ( `发送命令: ${ cmdTXT } ` ) ;
42+ addLog ( `<span class="action">⇑</span> ${ cmdTXT } ` ) ;
4343 await sendCommand ( hexToBytes ( cmdTXT ) ) ;
4444}
4545
@@ -71,9 +71,6 @@ async function sendCmWithData(cmd, data){
7171}
7272
7373async function send4GrayLut ( ) {
74- await sendcmd ( "0300" ) ;
75- await sendcmd ( "043F" ) ; // Load LUT from register
76-
7774 await sendCmWithData ( "20" , "000A0000000160141400000100140000000100130A010001000000000000000000000000000000000000" ) ; // vcom
7875 await sendCmWithData ( "21" , "400A0000000190141400000110140A000001A01301000001000000000000000000000000000000000000" ) ; // red not use
7976 await sendCmWithData ( "22" , "400A0000000190141400000100140A000001990C01030401000000000000000000000000000000000000" ) ; // bw r
@@ -82,11 +79,29 @@ async function send4GrayLut() {
8279 await sendCmWithData ( "25" , "400A0000000190141400000110140A000001A01301000001000000000000000000000000000000000000" ) ; // vcom
8380}
8481
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 ( ) {
86100 startTime = new Date ( ) . getTime ( ) ;
101+ const canvas = document . getElementById ( "canvas" ) ;
87102 const driver = document . getElementById ( "epddriver" ) . value ;
88103 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 ) ;
90105 const bwArrLen = ( canvas . width / 8 ) * canvas . height * 2 ;
91106
92107 if ( imgArray . length == bwArrLen * 2 ) {
@@ -95,10 +110,14 @@ async function sendimg(cmdIMG) {
95110 } else {
96111 await sendCmWithData ( driver === "03" ? "10" : "13" , imgArray ) ;
97112 }
113+
98114 if ( mode === "4gray" ) {
115+ await sendcmd ( "0300" ) ;
116+ await sendcmd ( "043F" ) ; // Load LUT from register
99117 await send4GrayLut ( ) ;
100118 await sendcmd ( "05" ) ;
101- await sendcmd ( `01${ driver } ` ) ; // restore lut
119+ await sendcmd ( "0300" ) ;
120+ await sendcmd ( "041F" ) ; // Load LUT from OTP
102121 } else {
103122 await sendcmd ( "05" ) ;
104123 }
@@ -111,17 +130,18 @@ async function sendimg(cmdIMG) {
111130function updateButtonStatus ( ) {
112131 const connected = gattServer != null && gattServer . connected ;
113132 const status = connected ? null : 'disabled' ;
133+ document . getElementById ( "reconnectbutton" ) . disabled = ( gattServer == null || gattServer . connected ) ? 'disabled' : null ;
114134 document . getElementById ( "sendcmdbutton" ) . disabled = status ;
115135 document . getElementById ( "clearscreenbutton" ) . disabled = status ;
116136 document . getElementById ( "sendimgbutton" ) . disabled = status ;
117137 document . getElementById ( "setDriverbutton" ) . disabled = status ;
118138}
119139
120140function disconnect ( ) {
141+ updateButtonStatus ( ) ;
121142 resetVariables ( ) ;
122143 addLog ( '已断开连接.' ) ;
123144 document . getElementById ( "connectbutton" ) . innerHTML = '连接' ;
124- updateButtonStatus ( ) ;
125145}
126146
127147async function preConnect ( ) {
@@ -133,10 +153,16 @@ async function preConnect() {
133153 }
134154 else {
135155 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+
140166 await bleDevice . addEventListener ( 'gattserverdisconnected' , disconnect ) ;
141167 try {
142168 await connect ( ) ;
@@ -156,21 +182,21 @@ async function reConnect() {
156182}
157183
158184async function connect ( ) {
159- if ( epdCharacteristic == null ) {
185+ if ( epdCharacteristic == null && bleDevice != null ) {
160186 addLog ( "正在连接: " + bleDevice . name ) ;
161187
162188 gattServer = await bleDevice . gatt . connect ( ) ;
163- addLog ( '> 找到 GATT Server' ) ;
189+ addLog ( ' 找到 GATT Server' ) ;
164190
165191 epdService = await gattServer . getPrimaryService ( '62750001-d828-918d-fb46-b6c11c675aec' ) ;
166- addLog ( '> 找到 EPD Service' ) ;
192+ addLog ( ' 找到 EPD Service' ) ;
167193
168194 epdCharacteristic = await epdService . getCharacteristic ( '62750002-d828-918d-fb46-b6c11c675aec' ) ;
169- addLog ( '> 找到 Characteristic' ) ;
195+ addLog ( ' 找到 Characteristic' ) ;
170196
171197 await epdCharacteristic . startNotifications ( ) ;
172198 epdCharacteristic . addEventListener ( 'characteristicvaluechanged' , ( event ) => {
173- addLog ( `> 收到配置: ${ bytesToHex ( event . target . value . buffer ) } ` ) ;
199+ addLog ( `<span class="action">⇓</span> ${ bytesToHex ( event . target . value . buffer ) } ` ) ;
174200 document . getElementById ( "epdpins" ) . value = bytesToHex ( event . target . value . buffer . slice ( 0 , 7 ) ) ;
175201 document . getElementById ( "epddriver" ) . value = bytesToHex ( event . target . value . buffer . slice ( 7 , 8 ) ) ;
176202 } ) ;
@@ -187,13 +213,17 @@ function setStatus(statusText) {
187213}
188214
189215function 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 ;
197227 }
198228}
199229
@@ -215,24 +245,6 @@ function intToHex(intIn) {
215245 return stringOut . substring ( 2 , 4 ) + stringOut . substring ( 0 , 2 ) ;
216246}
217247
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-
236248async function update_image ( ) {
237249 const canvas = document . getElementById ( "canvas" ) ;
238250 const ctx = canvas . getContext ( "2d" ) ;
@@ -258,7 +270,6 @@ function clear_canvas() {
258270 const ctx = canvas . getContext ( "2d" ) ;
259271 ctx . fillStyle = 'white' ;
260272 ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
261- document . getElementById ( 'cmdIMAGE' ) . value = '' ;
262273 }
263274}
264275
@@ -272,7 +283,6 @@ function convert_dithering() {
272283 } else {
273284 dithering ( ctx , canvas . width , canvas . height , parseInt ( document . getElementById ( 'threshold' ) . value ) , mode ) ;
274285 }
275- updateImageData ( canvas ) ;
276286}
277287
278288document . body . onload = ( ) => {
0 commit comments