-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlcd_device.js
182 lines (134 loc) · 4.91 KB
/
lcd_device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
/*!
* s1panel - lcd_device
* Copyright (c) 2024 Tomasz Jaworski
* GPL-3 Licensed
*/
const BUFFER_SIZE = 4104;
const HEADER_SIZE = 8;
const DATA_SIZE = 4096;
const REPORT_SIZE = 1;
const LCD_SIGNATURE = 0x55;
const LCD_CONFIG = 0xA1;
const LCD_REFRESH = 0xA2;
const LCD_REDRAW = 0xA3;
const LCD_ORIENTATION = 0xF1;
const LCD_SET_TIME = 0xF2;
const LCD_LANDSCAPE = 0x01;
const LCD_PORTRAIT = 0x02;
const LCD_REDRAW_START = 0xF0;
const LCD_REDRAW_CONTINUE = 0xF1;
const LCD_REDRAW_END = 0xF2;
function printBytesInHex(array) {
var _hexString = "";
for (var i = 1; i < Math.min(array.length, REPORT_SIZE + HEADER_SIZE); i++) {
_hexString += ('0' + array[i].toString(16)).slice(-2) + ' ';
}
console.log(_hexString);
}
function set_orientation(handle, portrait) {
return new Promise((fulfill, reject) => {
const _buffer = new Uint8ClampedArray(REPORT_SIZE + BUFFER_SIZE);
const _header = new DataView(_buffer.buffer, REPORT_SIZE);
_header.setUint8(0, LCD_SIGNATURE);
_header.setUint8(1, LCD_CONFIG);
_header.setUint8(2, LCD_ORIENTATION);
_header.setUint8(3, portrait ? LCD_PORTRAIT : LCD_LANDSCAPE);
//console.log('set orientation');
//printBytesInHex(_buffer);
handle.write(_buffer).then(fulfill, reject);
});
}
function heartbeat(handle) {
return new Promise((fulfill, reject) => {
const _buffer = new Uint8ClampedArray(REPORT_SIZE + BUFFER_SIZE);
const _header = new DataView(_buffer.buffer, REPORT_SIZE);
const _date = new Date();
_header.setUint8(0, LCD_SIGNATURE);
_header.setUint8(1, LCD_CONFIG);
_header.setUint8(2, LCD_SET_TIME);
_header.setUint8(3, _date.getHours());
_header.setUint8(4, _date.getMinutes());
_header.setUint8(5, _date.getSeconds());
//console.log('heartbeat');
//printBytesInHex(_buffer);
handle.write(_buffer).then(fulfill, reject);
});
}
function redraw_next(handle, header, image, buffer, index, fulfill, reject) {
if (index < 27) {
switch (index) {
case 0:
header.setUint8(2, LCD_REDRAW_START);
break;
case 26:
header.setUint8(2, LCD_REDRAW_END);
break;
default:
header.setUint8(2, LCD_REDRAW_CONTINUE);
break;
}
const _length = (index < 26) ? DATA_SIZE : 2304; // hard coded for now
header.setUint8(3, 1 + index); // sequence, 1, 2, 3...
header.setUint16(5, index * DATA_SIZE); // offset into image
header.setUint16(7, _length); // chunk size
// copy from part of the image to xmit buffer
{
const _data = new DataView(buffer.buffer, REPORT_SIZE + HEADER_SIZE);
const _pixel_start = (index * DATA_SIZE) / 2;
const _pixel_length = _length / 2;
var _offset = 0;
for (var i = 0; i < _pixel_length; i++) {
_data.setUint16(_offset, image.data[_pixel_start + i]);
_offset += 2;
}
}
//printBytesInHex(buffer);
handle.write(buffer).then(() => {
redraw_next(handle, header, image, buffer, ++index, fulfill, reject);
}, reject);
}
else {
fulfill();
}
}
function redraw(handle, image) {
return new Promise((fulfill, reject) => {
const _buffer = new Uint8ClampedArray(REPORT_SIZE + BUFFER_SIZE);
const _header = new DataView(_buffer.buffer, REPORT_SIZE);
_header.setUint8(0, LCD_SIGNATURE);
_header.setUint8(1, LCD_REDRAW);
//console.log('lcd_redraw');
redraw_next(handle, _header, image, _buffer, 0, fulfill, reject);
});
}
function refresh(handle, x, y, width, height, image) {
return new Promise((fulfill, reject) => {
const _buffer = new Uint8ClampedArray(REPORT_SIZE + BUFFER_SIZE);
const _header = new DataView(_buffer.buffer, REPORT_SIZE);
_header.setUint8(0, LCD_SIGNATURE);
_header.setUint8(1, LCD_REFRESH);
_header.setUint16(2, x, true);
_header.setUint16(4, y, true);
_header.setUint8(6, width);
_header.setUint8(7, height);
{
const _data = new DataView(_buffer.buffer, REPORT_SIZE + HEADER_SIZE);
const _length = width * height;
var _offset = 0;
for (var i = 0; i < _length; i++) {
_data.setUint16(_offset, image.data[i]);
_offset += 2;
}
}
//console.log('lcd_refresh');
//printBytesInHex(_buffer);
handle.write(_buffer).then(fulfill, reject);
});
}
module.exports = {
set_orientation,
heartbeat,
redraw,
refresh
};