-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYFOLED2.ts
310 lines (284 loc) · 10 KB
/
YFOLED2.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* YFROBOT extension for makecode
* https://github.com/YFROBOT-TM/pxt-yfrobot-oled2
*/
// 6x8 font
const Font_5x7 = hex`000000000000005F00000007000700147F147F14242A072A12231308646237495522500005030000001C2241000041221C00082A1C2A0808083E080800503000000808080808006060000020100804023E5149453E00427F400042615149462141454B311814127F1027454545393C4A49493001710905033649494936064949291E003636000000563600000008142241141414141441221408000201510906324979413E7E1111117E7F494949363E414141227F4141221C7F494949417F090901013E414151327F0808087F00417F41002040413F017F081422417F404040407F0204027F7F0408107F3E4141413E7F090909063E4151215E7F09192946464949493101017F01013F4040403F1F2040201F7F2018207F63140814630304780403615149454300007F4141020408102041417F000004020102044040404040000102040020545454787F484444383844444420384444487F3854545418087E090102081454543C7F0804047800447D40002040443D00007F10284400417F40007C041804787C0804047838444444387C14141408081414187C7C080404084854545420043F4440203C4040207C1C2040201C3C4030403C44281028440C5050503C4464544C44000836410000007F000000413608000201020402`
//% weight=50 color=#45b787 icon="\uf26c" block="YFOLED2"
namespace YFOLED2 {
export enum DISPLAY_ONOFF {
//% block="ON"
DISPLAY_ON = 1,
//% block="OFF"
DISPLAY_OFF = 0
}
const MIN_X = 0
const MIN_Y = 0
const MAX_X = 127
const MAX_Y = 63
let _I2CAddr = 61
let _screen = pins.createBuffer(1025)
let _buf2 = pins.createBuffer(2)
let _buf3 = pins.createBuffer(3)
let _buf4 = pins.createBuffer(4)
let _buf7 = pins.createBuffer(7)
_buf7[0] = 0x40
let _DRAW = 1
let _cx = 0
let _cy = 0
function cmd1(d: number) {
let n = d % 256;
pins.i2cWriteNumber(_I2CAddr, n, NumberFormat.UInt16BE);
}
function cmd2(d1: number, d2: number) {
_buf3[0] = 0;
_buf3[1] = d1;
_buf3[2] = d2;
pins.i2cWriteBuffer(_I2CAddr, _buf3);
}
function cmd3(d1: number, d2: number, d3: number) {
_buf4[0] = 0;
_buf4[1] = d1;
_buf4[2] = d2;
_buf4[3] = d3;
pins.i2cWriteBuffer(_I2CAddr, _buf4);
}
function set_pos(col: number = 0, page: number = 0) {
cmd1(0xb0 | page) // page number
cmd1(0x00 | (col % 16)) // lower start column address
cmd1(0x10 | (col >> 4)) // upper start column address
}
// clear bit
function clrbit(d: number, b: number): number {
if (d & (1 << b))
d -= (1 << b)
return d
}
/**
* draw / refresh screen
*/
function draw(d: number) {
if (d > 0) {
set_pos()
pins.i2cWriteBuffer(_I2CAddr, _screen)
}
}
/**
* set pixel in OLED
*/
//% blockId="YFOLED2_PIXEL" block="set pixel at x %x|y %y|color %color"
//% x.max=128 x.min=0 x.defl=0
//% y.max=64 y.min=0 y.defl=0
//% color.max=1 color.min=0 color.defl=1
//% weight=65 blockGap=8
export function pixel(x: number, y: number, color: number = 1) {
let page = y >> 3
let shift_page = y % 8
let ind = x + page * 128 + 1
let b = (color) ? (_screen[ind] | (1 << shift_page)) : clrbit(_screen[ind], shift_page)
_screen[ind] = b
if (_DRAW) {
set_pos(x, page)
_buf2[0] = 0x40
_buf2[1] = b
pins.i2cWriteBuffer(_I2CAddr, _buf2)
}
}
function char(c: string, col: number, row: number, color: number = 1) {
let p = (Math.min(127, Math.max(c.charCodeAt(0), 32)) - 32) * 5
let ind = col + row * 128 + 1
for (let i = 0; i < 5; i++) {
_screen[ind + i] = (color > 0) ? Font_5x7[p + i] : Font_5x7[p + i] ^ 0xFF
_buf7[i + 1] = _screen[ind + i]
}
_screen[ind + 5] = (color > 0) ? 0 : 0xFF
_buf7[6] = _screen[ind + 5]
set_pos(col, row)
pins.i2cWriteBuffer(_I2CAddr, _buf7)
}
/**
* show text in OLED
*/
//% blockId="YFOLED2_SHOWSTRING" block="show string %s|at col %col|row %row|color %color"
//% s.defl='Hello'
//% col.max=120 col.min=0 col.defl=0
//% row.max=7 row.min=0 row.defl=0
//% color.max=1 color.min=0 color.defl=1
//% weight=80 blockGap=8 inlineInputMode=inline
export function String(s: string, col: number, row: number, color: number = 1) {
for (let n = 0; n < s.length; n++) {
char(s.charAt(n), col, row, color)
col += 6
if (col > (MAX_X - 6)) return
}
}
/**
* show a number in OLED
*/
//% blockId="YFOLED2_NUMBER" block="show Number %num|at col %col|row %row|color %color"
//% num.defl=100
//% col.max=120 col.min=0 col.defl=0
//% row.max=7 row.min=0 row.defl=0
//% color.max=1 color.min=0 color.defl=1
//% weight=80 blockGap=8 inlineInputMode=inline
export function Number(num: number, col: number, row: number, color: number = 1) {
String(num.toString(), col, row, color)
}
function scroll() {
_cx = 0
_cy++
if (_cy > 7) {
_cy = 7
_screen.shift(128)
_screen[0] = 0x40
draw(1)
}
}
/**
* print a text in OLED
*/
//% blockId="YFOLED2_PRINTSTRING" block="print %s|color %color|newline %newline"
//% s.defl="string"
//% color.max=1 color.min=0 color.defl=1
//% newline.defl=true
//% weight=80 blockGap=8 inlineInputMode=inline
export function printString(s: string, color: number, newline: boolean = true) {
for (let n = 0; n < s.length; n++) {
char(s.charAt(n), _cx, _cy, color)
_cx += 6
if (_cx > 120) {
scroll()
}
}
if (newline) {
scroll()
}
}
/**
* print a Number in OLED
*/
//% blockId="YFOLED2_PRINTNUMBER" block="print number %num|color %color|newline %newline"
//% s.defl="0"
//% color.max=1 color.min=0 color.defl=1
//% newline.defl=true
//% weight=80 blockGap=8 inlineInputMode=inline
export function printNumber(num: number, color: number, newline: boolean = true) {
printString(num.toString(), color, newline)
}
/**
* draw a horizontal line
*/
//% blockId="YFOLED2_HLINE" block="draw a horizontal line at x %x|y %y|length %len|color %color"
//% x.max=127 x.min=0 x.defl=0
//% y.max=63 y.min=0 y.defl=0
//% len.max=128 len.min=1 len.defl=16
//% color.max=1 color.min=0 color.defl=1
//% weight=71 blockGap=8 inlineInputMode=inline
export function hline(x: number, y: number, len: number, color: number = 1) {
let _sav = _DRAW
if ((y < MIN_Y) || (y > MAX_Y)) return
_DRAW = 0
for (let i = x; i < (x + len); i++)
if ((i >= MIN_X) && (i <= MAX_X))
pixel(i, y, color)
_DRAW = _sav
draw(_DRAW)
}
/**
* draw a vertical line
*/
//% blockId="YFOLED2_VLINE" block="draw a vertical line at x %x|y %y|length %len|color %color"
//% x.max=127 x.min=0 x.defl=0
//% y.max=63 y.min=0 y.defl=0
//% len.max=128 len.min=1 len.defl=16
//% color.max=1 color.min=0 color.defl=1
//% weight=71 blockGap=8 inlineInputMode=inline
export function vline(x: number, y: number, len: number, color: number = 1) {
let _sav = _DRAW
_DRAW = 0
if ((x < MIN_X) || (x > MAX_X)) return
for (let i = y; i < (y + len); i++)
if ((i >= MIN_Y) && (i <= MAX_Y))
pixel(x, i, color)
_DRAW = _sav
draw(_DRAW)
}
/**
* draw a rectangle
*/
//% blockId="YFOLED2_RECT" block="draw a rectangle at x1 %x1|y1 %y1|x2 %x2|y2 %y2|color %color"
//% color.defl=1
//% weight=70 blockGap=8 inlineInputMode=inline
export function rect(x1: number, y1: number, x2: number, y2: number, color: number = 1) {
if (x1 > x2)
x1 = [x2, x2 = x1][0];
if (y1 > y2)
y1 = [y2, y2 = y1][0];
_DRAW = 0
hline(x1, y1, x2 - x1 + 1, color)
hline(x1, y2, x2 - x1 + 1, color)
vline(x1, y1, y2 - y1 + 1, color)
vline(x2, y1, y2 - y1 + 1, color)
_DRAW = 1
draw(1)
}
/**
* invert display
* @param d true: invert / false: normal, eg: true
*/
//% blockId="YFOLED2_INVERT" block="Invert display %d"
//% weight=62 blockGap=8
export function invert(d: boolean = true) {
let n = (d) ? 0xA7 : 0xA6
cmd1(n)
}
/**
* clear screen
*/
//% blockId="YFOLED2_CLEAR" block="Clear screen"
//% weight=30 blockGap=8
export function clear() {
_cx = _cy = 0
_screen.fill(0)
_screen[0] = 0x40
draw(1)
}
/**
* turn on/off screen
*/
//% blockId="YFOLED2_ON" block="Display %on"
//% on.defl=1
//% weight=62 blockGap=8
export function display(on: DISPLAY_ONOFF=DISPLAY_ONOFF.DISPLAY_ON) {
let d = (on == DISPLAY_ONOFF.DISPLAY_ON) ? 0xAF : 0xAE;
cmd1(d)
}
/**
* OLED initialize
*/
//% blockId="YFOLED2_init" block="Initial OLED"
//% weight=100 blockGap=8
export function init() {
cmd1(0xAE) // SSD1306_DISPLAYOFF
cmd1(0xA4) // SSD1306_DISPLAYALLON_RESUME
cmd2(0xD5, 0xF0) // SSD1306_SETDISPLAYCLOCKDIV
cmd2(0xA8, 0x3F) // SSD1306_SETMULTIPLEX
cmd2(0xD3, 0x00) // SSD1306_SETDISPLAYOFFSET
cmd1(0 | 0x0) // line #SSD1306_SETSTARTLINE
cmd2(0x8D, 0x14) // SSD1306_CHARGEPUMP
cmd2(0x20, 0x00) // SSD1306_MEMORYMODE
cmd3(0x21, 0, 127) // SSD1306_COLUMNADDR
cmd3(0x22, 0, 63) // SSD1306_PAGEADDR
cmd1(0xa0 | 0x1) // SSD1306_SEGREMAP
cmd1(0xc8) // SSD1306_COMSCANDEC
cmd2(0xDA, 0x12) // SSD1306_SETCOMPINS
cmd2(0x81, 0xCF) // SSD1306_SETCONTRAST
cmd2(0xd9, 0xF1) // SSD1306_SETPRECHARGE
cmd2(0xDB, 0x40) // SSD1306_SETVCOMDETECT
cmd1(0xA6) // SSD1306_NORMALDISPLAY
cmd2(0xD6, 0) // zoom off
cmd1(0xAF) // SSD1306_DISPLAYON
clear()
}
init();
}