-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
406 lines (345 loc) · 11.5 KB
/
main.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
* Custom functions and blocks for interfacing with TFT display
* and most code is from http://www.obliquely.org.uk/connecting-a-microbit-and-adafruit-1-44-display/
*
* For documentation on the AdaFruit 1.44" TFT display
* see: https://learn.adafruit.com/adafruit-1-44-color-tft-with-micro-sd-socket
*
* The code to drive the display has been adapted from the code provided (in C++)
* by AdaFruit.
*
* For syntax that makes functions and enums available in the blocks editor
* see https://makecode.microbit.org/blocks/custom
*/
/**
* TFT display commands
* Only the commands actually used are included here. See the ST7735R
* data sheet for the full set of commands.
*/
enum TftCom {
NOOP = 0x00,
SWRESET = 0x01,
SLPOUT = 0x11,
NORON = 0x13,
INVOFF = 0x20,
DISPON = 0x29,
CASET = 0x2A,
RASET = 0x2B,
RAMWR = 0x2C,
MADCTL = 0x36,
COLMOD = 0x3A,
FRMCTR1 = 0xB1,
FRMCTR2 = 0xB2,
FRMCTR3 = 0xB3,
INVCTR = 0xB4,
PWCTR1 = 0xC0,
PWCTR2 = 0xC1,
PWCTR3 = 0xC2,
PWCTR4 = 0xC3,
PWCTR5 = 0xC4,
VMCTR1 = 0xC5,
GMCTRP1 = 0xE0,
GMCTRN1 = 0xE1,
DELAY = 0xFFFF
}
enum COLOR {
//% block="Black"
Black = 0x0000,
//% block="Navy"
Navy = 0x000F,
//% block="DarkGreen"
DarkGreen = 0x03E0,
//% block="DarkCyan"
DarkCyan = 0x03EF,
//% block="Maroon"
Maroon = 0x7800,
//% block="Purple"
Purple = 0x780F,
//% block="Olive"
Olive = 0x7BE0,
//% block="LightGrey"
LightGrey = 0xC618,
//% block="DarkGrey"
DarkGrey = 0x7BEF,
//% block="Blue"
Blue = 0x001F,
//% block="Green"
Green = 0x07E0,
//% block="Cyan"
Cyan = 0x07FF,
//% block="Red"
Red = 0xF800,
//% block="Magenta"
Magenta = 0xF81F,
//% block="Yellow"
Yellow = 0xFFE0,
//% block="White"
White = 0xFFFF,
//% block="Orange"
Orange = 0xFD20,
//% block="GreenYellow"
GreenYellow = 0xAFE5,
//% block="Pink"
Pink = 0xF81F
};
/**
* TFT LCD SPI
*/
//% weight=20 color=#0000ff icon="\uf10b" block="TFT LCD SPI"
namespace TFTDisplay {
let screen_x = 0
let screen_y = 0
function displayScale(): number {
return 1
}
/**
* The display width in ‘working coordinates’. These are pixel values * displayScale()
*/
function displayWidth(): number {
return screen_x * displayScale()
}
/**
* The display height in ‘working coordinates’. These are pixel values * displayScale()
*/
function displayHeight(): number {
return screen_y * displayScale()
}
/**
* Convert a working coordinate to an actual pixel coordinate.
* Don’t expose this in final code. It should be internal.
*/
function roundedPixel(value: number): number {
let adjusted = value / displayScale();
adjusted = adjusted + (value & (displayScale() - 1) ? 1 : 0)
return adjusted;
}
function outOfBounds(v1: number, v2 = 0, v3 = 0, v4 = 0): boolean {
if (v1 < 0 || v1 > screen_x - 1) {
return true
}
if (v2 < 0 || v2 > screen_y - 1) {
return true
}
if (v3 < 0 || v3 > screen_x - 1) {
return true
}
if (v4 < 0 || v4 > screen_y - 1) {
return true
}
return false
}
/**
* Set the address window
*/
function setAddrWindow(x0: number, y0: number, x1: number, y1: number) : void {
if (outOfBounds(x0, y0, x1, y1)) {
return
}
// set the column
//tftCom(TftCom.CASET, [0x00, x0 + 2, 0x00, x1 + 2]) // 2 is an adjust for thr AdaFruit 1.44 display
tftCom(TftCom.CASET, [0x00, x0, 0x00, x1])
//tftCom(TftCom.CASET, [0x00, 0x00, 0x00, 0x7F])
// set the row
//tftCom(TftCom.RASET, [0x00, y0 + 3, 0x00, y1 + 3]) // 3 is an adjust for thr AdaFruit 1.44 display
tftCom(TftCom.RASET, [0x00, y0, 0x00, y1])
//tftCom(TftCom.RASET, [0x00, 0x00, 0x00, 0x9f])
}
/**
* Write a command to the TFT display
*/
function tftCom(command: TftCom, params: Array<number>) : void {
// handle the pseudo ‘DELAY’ command - provides a delay in milliseconds
if (command == TftCom.DELAY) {
let waitTime: number = 500
if (params.length == 1) {
waitTime = params[0]
}
basic.pause(waitTime)
return
}
// let the TFT know we’re sending a command (rather than data)
pins.digitalWritePin(DigitalPin.P1, 0) // command/data = command
// select the TFT controller
pins.digitalWritePin(DigitalPin.P16, 0) // select the TFT as SPI target
pins.spiWrite(command)
// let the TFT know we’re sending data bytes (rather than a command)
pins.digitalWritePin(DigitalPin.P1, 1) // command/data = data
for (let dataItem of params) {
pins.spiWrite(dataItem)
}
// de-select the TFT controller
pins.digitalWritePin(DigitalPin.P16, 1) // de-elect the TFT as SPI target
// restore pin to zero (for tidiness - not required)
pins.digitalWritePin(DigitalPin.P1, 0) // command/data = command
}
/**
* Do initial set up for display. (Required before any drawing begins.)
*/
function tftSetup() : void {
// General Setup (for various display types)
// 1. Software Reset
tftCom(TftCom.SWRESET, [1])
tftCom(TftCom.DELAY, [1])
tftCom(TftCom.SWRESET, [0])
tftCom(TftCom.DELAY, [1])
tftCom(TftCom.SWRESET, [1])
tftCom(TftCom.DELAY, [120]) // we need ot wait at least 120ms
// 2. Exit Sleep Mode
tftCom(TftCom.SLPOUT, [])
tftCom(TftCom.DELAY, [120]) // we need to wait at least 120ms
// 3. Frame rate ctrl - normal mode
tftCom(TftCom.FRMCTR1, [0x01, 0x2C, 0x2D])
// 4. Frame rate ctrl - idle mode
tftCom(TftCom.FRMCTR2, [0x01, 0x2C, 0x2D])
// 5. Frame rate ctrl - dot inversion mode
tftCom(TftCom.FRMCTR3, [0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D])
// 6. Display inversion ctrl - no inversion
tftCom(TftCom.INVCTR, [0x07])
// 7. Power Control -4.6v, Auto Mode
tftCom(TftCom.PWCTR1, [0xA2, 0x02, 0x84])
// 8. Power control,VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
tftCom(TftCom.PWCTR2, [0xC5])
// 9: Power control, Opamp current small + Boost Frequency
tftCom(TftCom.PWCTR3, [0x0A, 0x00])
// 10: Power control, BCLK/2, Opamp current small & Medium low
tftCom(TftCom.PWCTR4, [0x8A, 0x2A])
// 11: Power control
tftCom(TftCom.PWCTR5, [0x8A, 0xEE])
// 12: Power control
tftCom(TftCom.VMCTR1, [0x0E])
// 13. Don’t invert display
//tftCom(TftCom.INVOFF, [])
// 14: Memory access control (directions)
tftCom(TftCom.MADCTL, [0xC0])
// Further General Setup (for various display types)
//1: Gamma Correction
tftCom(TftCom.GMCTRP1, [0x0F, 0x1A, 0x0F, 0x18, 0x2F, 0x28, 0x20, 0x22, 0x1F, 0x1B, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
//2: Gamma Correction
tftCom(TftCom.GMCTRN1, [0x0F, 0x1B, 0x0F, 0x17, 0x33, 0x2C, 0x29, 0x2E, 0x30, 0x30, 0x39, 0x3F, 0x00, 0x07, 0x03, 0x10])
// 3: set color mode, 16-bit colour
tftCom(TftCom.COLMOD, [0x05])
// 4: Main screen turn on
tftCom(TftCom.DISPON, [])
}
/**
* Draw a line of a given colour
*/
//% blockId="TFT_drawLine" block="drawLine on x0:%x0|y0:%y0|x1:%x1|y1:%y1|colour:%colour"
//% weight=97
export function drawLine(x0: number, y0: number, x1: number, y1: number, colour: number) : void {
let xDelta = x1 - x0
let yDelta = y1 - y0
if (Math.abs(yDelta) > Math.abs(xDelta)) {
let ySteps = Math.abs(yDelta / displayScale())
let xIncrement = xDelta == 0 ? 0 : xDelta / ySteps
let yIncrement = yDelta > 0 ? displayScale() : -1 * displayScale()
let x = x0
let y = y0;
for (let steps = 0; steps <= ySteps; steps++) {
drawPixel(roundedPixel(x), roundedPixel(y), colour)
x = x + xIncrement
y = y + yIncrement
}
return
}
let xSteps = Math.abs(xDelta / displayScale())
let yIncrement = yDelta == 0 ? 0 : yDelta / xSteps;
let xIncrement = xDelta > 0 ? displayScale() : -1 * displayScale()
let y = y0;
let x = x0
for (let steps = 0; steps <= xSteps; steps++) {
drawPixel(roundedPixel(x), roundedPixel(y), colour)
y = y + yIncrement
x = x + xIncrement
}
}
/**
* Draw a single pixel of a given colour
*/
//% blockId="TFT_drawPixel" block="drawPixel on x:%x|y:%y|colour:%colour"
//% weight=98
export function drawPixel(x: number, y: number, colour: number) : void {
if (outOfBounds(x, y)) {
return
}
setAddrWindow(x, y, x + 1, y + 1);
// send data (16 bits in two bytes)
tftCom(TftCom.RAMWR, [colour >> 8, colour])
}
/**
* Fill a rectangle with a given colour
*/
//% blockId="TFT_fillRect" block="fillRect on x:%x|y:%y|width:%width|height:%height|colour:%colour"
//% weight=96
export function fillRect(x: number, y: number, width: number, height: number, colour: number) : void {
if (outOfBounds(x, y)) {
return;
}
if ((x + width) > screen_x) {
width = screen_x - x;
}
if ((y + height) > screen_y) {
height = screen_y - y;
}
let hiColour = (colour >> 8) % 256;
let loColour = colour % 256;
setAddrWindow(x, y, x + width - 1, y + height - 1);
// we are going to manually implement the RAMWR command here because
// we have custom parameters. See comments in tftCom for details
// of what’s going on here.
pins.digitalWritePin(DigitalPin.P1, 0); // command/data = command
pins.digitalWritePin(DigitalPin.P16, 0); // select the TFT as SPI target
pins.spiWrite(TftCom.RAMWR);
pins.digitalWritePin(DigitalPin.P1, 1); // command/data = data
for (let indexY = height; indexY > 0; indexY--) {
for (let indexX = width; indexX > 0; indexX--) {
pins.spiWrite(hiColour)
pins.spiWrite(loColour)
}
}
pins.digitalWritePin(DigitalPin.P16, 1) // de-elect the TFT as SPI target
pins.digitalWritePin(DigitalPin.P1, 0) // command/data = command
}
/**
* Setup and clear screen ready for used
*/
//% blockId="TFT_setupScreen" block="setupScreen on x:%x|y:%y"
//% weight=99
export function setupScreen(x: number = 128, y: number = 160) : void {
screen_x = x
screen_y = y
pins.spiFrequency(4000000) // try a fast rate for serial bus
tftSetup()
fillRect(
0,
0,
x,
y,
0
)
}
/**
* Clear screen
*/
//% blockId="TFT_clearScreen" block="clearScreen"
//% weight=95
export function clearScreen() : void {
fillRect(
0,
0,
screen_x,
screen_y,
0
)
}
/**
* Get Color
*/
//% blockId=Get_Color
//% blockGap=8
//% block="%color"
//% weight=94
export function Get_Color(color: COLOR): number{
return color;
}
}