-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
api_stdlcd.cpp
645 lines (505 loc) · 16.2 KB
/
api_stdlcd.cpp
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <cctype>
#include <cstdio>
#include "opentx.h"
#include "lua_api.h"
/*luadoc
@function lcd.refresh()
Refresh the LCD screen
@status current Introduced in 2.2.0
@notice This function only works in stand-alone and telemetry scripts.
*/
static int luaLcdRefresh(lua_State *L)
{
if (luaLcdAllowed) lcdRefresh();
return 0;
}
/*luadoc
@function lcd.clear([color])
Clear the LCD screen
@param color (optional, only on color screens)
@status current Introduced in 2.0.0, `color` parameter introduced in 2.2.0 RC12
@notice This function only works in stand-alone and telemetry scripts.
*/
static int luaLcdClear(lua_State *L)
{
if (luaLcdAllowed) {
lcdClear();
}
return 0;
}
/*luadoc
@function lcd.resetBacklightTimeout()
Reset the backlight timeout
@status current Introduced in 2.3.6
*/
static int luaLcdResetBacklightTimeout(lua_State * L)
{
if (!luaLcdAllowed)
return 0;
resetBacklightTimeout();
return 0;
}
/*luadoc
@function lcd.drawPoint(x, y)
Draw a single pixel at (x,y) position
@param x (positive number) x position
@param y (positive number) y position
@param flags (optional) lcdflags
@notice Taranis has an LCD display width of 212 pixels and height of 64 pixels.
Position (0,0) is at top left. Y axis is negative, top line is 0,
bottom line is 63. Drawing on an existing black pixel produces white pixel (TODO check this!)
@status current Introduced in 2.0.0
*/
static int luaLcdDrawPoint(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
LcdFlags att = luaL_optunsigned(L, 3, 0);
lcdDrawPoint(x, y, att);
return 0;
}
/*luadoc
@function lcd.drawLine(x1, y1, x2, y2, pattern, flags)
Draw a straight line on LCD
@param x1,y1 (positive numbers) starting coordinate
@param x2,y2 (positive numbers) end coordinate
@param pattern SOLID or DOTTED
@param flags lcdflags
@notice If the start or the end of the line is outside the LCD dimensions, then the
whole line will not be drawn (starting from OpenTX 2.1.5)
@status current Introduced in 2.0.0, flags introduced in 2.3.6
*/
static int luaLcdDrawLine(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
coord_t x1 = luaL_checkunsigned(L, 1);
coord_t y1 = luaL_checkunsigned(L, 2);
coord_t x2 = luaL_checkunsigned(L, 3);
coord_t y2 = luaL_checkunsigned(L, 4);
uint8_t pat = luaL_checkunsigned(L, 5);
LcdFlags flags = luaL_checkunsigned(L, 6);
if (x1 > LCD_W || y1 > LCD_H || x2 > LCD_W || y2 > LCD_H)
return 0;
if (pat == SOLID) {
if (x1 == x2) {
lcdDrawSolidVerticalLine(x1, y1<y2 ? y1 : y2, y1<y2 ? (y2-y1)+1 : (y1-y2)+1, flags);
return 0;
}
else if (y1 == y2) {
lcdDrawSolidHorizontalLine(x1<x2 ? x1 : x2, y1, x1<x2 ? (x2-x1)+1 : (x1-x2)+1, flags);
return 0;
}
}
lcdDrawLine(x1, y1, x2, y2, pat, flags);
return 0;
}
/*luadoc
@function lcd.getLastPos()
Returns the rightmost x position from previous output
@retval number (integer) x position
@notice Only available on Taranis
@notice For added clarity, it is recommended to use lcd.getLastRightPos()
@status current Introduced in 2.0.0
*/
/*luadoc
@function lcd.getLastRightPos()
Returns the rightest x position from previous drawtext or drawNumber output
@retval number (integer) x position
@notice Only available on Taranis
@notice This is strictly equivalent to former lcd.getLastPos()
@status current Introduced in 2.2.0
*/
static int luaLcdGetLastPos(lua_State *L)
{
lua_pushinteger(L, lcdLastRightPos);
return 1;
}
/*luadoc
@function lcd.getLastLeftPos()
Returns the leftmost x position from previous drawtext or drawNumber output
@retval number (integer) x position
@notice Only available on Taranis
@status current Introduced in 2.2.0
*/
static int luaLcdGetLeftPos(lua_State *L)
{
lua_pushinteger(L, lcdLastLeftPos);
return 1;
}
/*luadoc
@function lcd.drawText(x, y, text [, flags])
Draw a text beginning at (x,y)
@param x,y (positive numbers) starting coordinate
@param text (string) text to display
@param flags (unsigned number) drawing flags. All values can be
combined together using the + character. ie BLINK + DBLSIZE.
See the [Appendix](../appendix/fonts.md) for available characters in each font set.
* `0 or not specified` normal font
* `XXLSIZE` jumbo sized font
* `DBLSIZE` double size font
* `MIDSIZE` mid sized font
* `SMLSIZE` small font
* `INVERS` inverted display
* `BLINK` blinking text
* `SHADOWED` Horus only, apply a shadow effect
@status current Introduced in 2.0.0, `SHADOWED` introduced in 2.2.1
*/
static int luaLcdDrawText(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
const char * s = luaL_checkstring(L, 3);
unsigned int att = luaL_optunsigned(L, 4, 0);
lcdDrawText(x, y, s, att);
return 0;
}
/*luadoc
@function lcd.drawTimer(x, y, value [, flags])
Display a value formatted as time at (x,y)
@param x,y (positive numbers) starting coordinate
@param value (number) time in seconds
@param flags (unsigned number) drawing flags:
* `0 or not specified` normal representation (minutes and seconds)
* `TIMEHOUR` display hours
* other general LCD flag also apply
* `SHADOWED` Horus only, apply a shadow effect
@status current Introduced in 2.0.0, `SHADOWED` introduced in 2.2.1
*/
static int luaLcdDrawTimer(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int seconds = luaL_checkinteger(L, 3);
unsigned int att = luaL_optunsigned(L, 4, 0);
drawTimer(x, y, seconds, att|LEFT, att);
return 0;
}
/*luadoc
@function lcd.drawNumber(x, y, value [, flags])
Display a number at (x,y)
@param x,y (positive numbers) starting coordinate
@param value (number) value to display
@param flags (unsigned number) drawing flags:
* `0 or not specified` display with no decimal (like abs())
* `PREC1` display with one decimal place (number 386 is displayed as 38.6)
* `PREC2` display with tow decimal places (number 386 is displayed as 3.86)
* other general LCD flag also apply
* `SHADOWED` Horus only, apply a shadow effect
@status current Introduced in 2.0.0, `SHADOWED` introduced in 2.2.1
*/
static int luaLcdDrawNumber(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int val = luaL_checkinteger(L, 3);
unsigned int att = luaL_optunsigned(L, 4, 0);
lcdDrawNumber(x, y, val, att);
return 0;
}
/*luadoc
@function lcd.drawChannel(x, y, source, flags)
Display a telemetry value at (x,y)
@param x,y (positive numbers) starting coordinate
@param source can be a source identifier (number) or a source name (string).
See getValue()
@param flags (unsigned number) drawing flags
@status current Introduced in 2.0.6, changed in 2.1.0 (only telemetry sources are valid)
*/
static int luaLcdDrawChannel(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int channel = -1;
if (lua_isnumber(L, 3)) {
channel = luaL_checkinteger(L, 3);
}
else {
const char * what = luaL_checkstring(L, 3);
LuaField field;
bool found = luaFindFieldByName(what, field);
if (found) {
channel = field.id;
}
}
unsigned int att = luaL_optunsigned(L, 4, 0);
getvalue_t value = getValue(channel);
drawSensorCustomValue(x, y, (channel-MIXSRC_FIRST_TELEM)/3, value, att);
return 0;
}
/*luadoc
@function lcd.drawSwitch(x, y, switch, flags)
Draw a text representation of switch at (x,y)
@param x,y (positive numbers) starting coordinate
@param switch (number) number of switch to display, negative number
displays negated switch
@param flags (unsigned number) drawing flags, only SMLSIZE, BLINK and INVERS.
@status current Introduced in 2.0.0
*/
static int luaLcdDrawSwitch(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int s = luaL_checkinteger(L, 3);
unsigned int att = luaL_optunsigned(L, 4, 0);
drawSwitch(x, y, s, att);
return 0;
}
/*luadoc
@function lcd.drawSource(x, y, source [, flags])
Displays the name of the corresponding input as defined by the source at (x,y)
@param x,y (positive numbers) starting coordinate
@param source (number) source index
@param flags (unsigned number) drawing flags
@status current Introduced in 2.0.0
*/
static int luaLcdDrawSource(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int s = luaL_checkinteger(L, 3);
unsigned int att = luaL_optunsigned(L, 4, 0);
drawSource(x, y, s, att);
return 0;
}
/*luadoc
@function lcd.drawPixmap(x, y, name)
Draw a bitmap at (x,y)
@param x,y (positive numbers) starting coordinates
@param name (string) full path to the bitmap on SD card (i.e. “/IMAGES/test.bmp”)
@notice Maximum image size is [display width / 2] x [display height] pixels.
@status current Introduced in 2.0.0
*/
static int luaLcdDrawPixmap(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
const char * filename = luaL_checkstring(L, 3);
uint8_t bitmap[BITMAP_BUFFER_SIZE(LCD_W/2, LCD_H)]; // width max is LCD_W/2 pixels for saving stack and avoid a malloc here
if (lcdLoadBitmap(bitmap, filename, LCD_W/2, LCD_H)) {
lcdDrawBitmap(x, y, bitmap);
}
return 0;
}
/*luadoc
@function lcd.drawRectangle(x, y, w, h [, flags [, t]])
Draw a rectangle from top left corner (x,y) of specified width and height
@param x,y (positive numbers) top left corner position
@param w (number) width in pixels
@param h (number) height in pixels
@param flags (unsigned number) drawing flags
@param t (number) thickness in pixels, defaults to 1 (only on Horus)
@status current Introduced in 2.0.0, changed in 2.2.0
*/
static int luaLcdDrawRectangle(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int w = luaL_checkinteger(L, 3);
int h = luaL_checkinteger(L, 4);
unsigned int flags = luaL_optunsigned(L, 5, 0);
#if defined(PCBHORUS)
unsigned int t = luaL_optunsigned(L, 6, 1);
lcdDrawRect(x, y, w, h, t, 0xff, flags);
#else
lcdDrawRect(x, y, w, h, 0xff, flags);
#endif
return 0;
}
/*luadoc
@function lcd.drawFilledRectangle(x, y, w, h [, flags])
Draw a solid rectangle from top left corner (x,y) of specified width and height
@param x,y (positive numbers) top left corner position
@param w (number) width in pixels
@param h (number) height in pixels
@param flags (unsigned number) drawing flags
@status current Introduced in 2.0.0
*/
static int luaLcdDrawFilledRectangle(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int w = luaL_checkinteger(L, 3);
int h = luaL_checkinteger(L, 4);
unsigned int flags = luaL_optunsigned(L, 5, 0);
lcdDrawFilledRect(x, y, w, h, SOLID, flags);
return 0;
}
/*luadoc
@function lcd.drawGauge(x, y, w, h, fill, maxfill [, flags])
Draw a simple gauge that is filled based upon fill value
@param x,y (positive numbers) top left corner position
@param w (number) width in pixels
@param h (number) height in pixels
@param fill (number) amount of fill to apply
@param maxfill (number) total value of fill
@param flags (unsigned number) drawing flags
@status current Introduced in 2.0.0, changed in 2.2.0
*/
static int luaLcdDrawGauge(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int w = luaL_checkinteger(L, 3);
int h = luaL_checkinteger(L, 4);
int num = luaL_checkinteger(L, 5);
int den = luaL_checkinteger(L, 6);
unsigned int flags = luaL_optunsigned(L, 7, 0);
#if defined(PCBHORUS)
lcdDrawRect(x, y, w, h, 1, 0xff, flags);
#else
lcdDrawRect(x, y, w, h, 0xff, flags);
#endif
uint8_t len = limit((uint8_t)1, uint8_t(w*num/den), uint8_t(w));
lcdDrawSolidFilledRect(x+1, y+1, len, h-2, flags);
return 0;
}
/*luadoc
@function lcd.drawScreenTitle(title, page, pages)
Draw a title bar
@param title (string) text for the title
@param page (number) page number
@param pages (number) total number of pages. Only used as indicator on
the right side of title bar. (i.e. idx=2, cnt=5, display `2/5`)
@notice Only available on Taranis
@status current Introduced in 2.0.0
*/
static int luaLcdDrawScreenTitle(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
const char * str = luaL_checkstring(L, 1);
int idx = luaL_checkinteger(L, 2);
int cnt = luaL_checkinteger(L, 3);
if (cnt) drawScreenIndex(idx-1, cnt, 0);
#if LCD_DEPTH > 1
lcdDrawFilledRect(0, 0, LCD_W, FH, SOLID, FILL_WHITE|GREY_DEFAULT);
#endif
title(str);
return 0;
}
/*luadoc
@function lcd.drawCombobox(x, y, w, list, idx [, flags])
Draw a combo box
@param x,y (positive numbers) top left corner position
@param w (number) width of combo box in pixels
@param list (table) combo box elements, each element is a string
@param idx (integer) index of entry to highlight
@param flags (unsigned number) drawing flags, the flags can not be combined:
* `BLINK` combo box is expanded
* `INVERS` combo box collapsed, text inversed
* `0 or not present` combo box collapsed, text normal
@notice Only available on Taranis
@status current Introduced in 2.0.0
*/
static int luaLcdDrawCombobox(lua_State *L)
{
if (!luaLcdAllowed)
return 0;
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int w = luaL_checkinteger(L, 3);
luaL_checktype(L, 4, LUA_TTABLE);
int count = luaL_len(L, 4); /* get size of table */
int idx = luaL_checkinteger(L, 5);
unsigned int flags = luaL_optunsigned(L, 6, 0);
if (idx >= count) {
// TODO error
}
if (flags & BLINK) {
lcdDrawFilledRect(x, y, w-9, count*9+2, SOLID, ERASE);
lcdDrawRect(x, y, w-9, count*9+2);
for (int i=0; i<count; i++) {
lua_rawgeti(L, 4, i+1);
const char * item = luaL_checkstring(L, -1);
lcdDrawText(x+2, y+2+9*i, item, 0);
}
lcdDrawFilledRect(x+1, y+1+9*idx, w-11, 9);
lcdDrawFilledRect(x+w-10, y, 10, 11, SOLID, ERASE);
lcdDrawRect(x+w-10, y, 10, 11);
}
else if (flags & INVERS) {
lcdDrawFilledRect(x, y, w, 11);
lcdDrawFilledRect(x+w-9, y+1, 8, 9, SOLID, ERASE);
lua_rawgeti(L, 4, idx+1);
const char * item = luaL_checkstring(L, -1);
lcdDrawText(x+2, y+2, item, INVERS);
}
else {
lcdDrawFilledRect(x, y, w, 11, SOLID, ERASE);
lcdDrawRect(x, y, w, 11);
lcdDrawFilledRect(x+w-10, y+1, 9, 9, SOLID);
lua_rawgeti(L, 4, idx+1);
const char * item = luaL_checkstring(L, -1);
lcdDrawText(x+2, y+2, item, 0);
}
lcdDrawSolidHorizontalLine(x+w-8, y+3, 6);
lcdDrawSolidHorizontalLine(x+w-8, y+5, 6);
lcdDrawSolidHorizontalLine(x+w-8, y+7, 6);
return 0;
}
const luaL_Reg lcdLib[] = {
{ "refresh", luaLcdRefresh },
{ "clear", luaLcdClear },
{ "resetBacklightTimeout", luaLcdResetBacklightTimeout },
{ "drawPoint", luaLcdDrawPoint },
{ "drawLine", luaLcdDrawLine },
{ "drawRectangle", luaLcdDrawRectangle },
{ "drawFilledRectangle", luaLcdDrawFilledRectangle },
{ "drawText", luaLcdDrawText },
{ "drawTimer", luaLcdDrawTimer },
{ "drawNumber", luaLcdDrawNumber },
{ "drawChannel", luaLcdDrawChannel },
{ "drawSwitch", luaLcdDrawSwitch },
{ "drawSource", luaLcdDrawSource },
{ "drawGauge", luaLcdDrawGauge },
{ "getLastPos", luaLcdGetLastPos },
{ "getLastRightPos", luaLcdGetLastPos },
{ "getLastLeftPos", luaLcdGetLeftPos },
{ "drawPixmap", luaLcdDrawPixmap },
{ "drawScreenTitle", luaLcdDrawScreenTitle },
{ "drawCombobox", luaLcdDrawCombobox },
{ NULL, NULL } /* sentinel */
};