diff options
Diffstat (limited to 'apps/plugins/lua/include_lua/draw_num.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/draw_num.lua | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/draw_num.lua b/apps/plugins/lua/include_lua/draw_num.lua new file mode 100644 index 0000000000..0f42c1f9f5 --- /dev/null +++ b/apps/plugins/lua/include_lua/draw_num.lua | |||
@@ -0,0 +1,115 @@ | |||
1 | --[[ Lua draw number function | ||
2 | /*************************************************************************** | ||
3 | * __________ __ ___. | ||
4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
8 | * \/ \/ \/ \/ \/ | ||
9 | * $Id$ | ||
10 | * | ||
11 | * Copyright (C) 2019 William Wilgus | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | ]] | ||
23 | |||
24 | --[[ Exposed Functions | ||
25 | _draw_nums.print; binary (base = 2) , octal (base = 8), hexadecimal (base = 16) | ||
26 | _draw_nums.nums; table of number characters | ||
27 | ]] | ||
28 | |||
29 | if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end | ||
30 | |||
31 | local _draw_nums = {} do | ||
32 | local _poly = require "draw_poly" | ||
33 | |||
34 | -- every 2 elements is an x, y coord pair | ||
35 | -- n[?] = {x,y,x,y,x,y} | ||
36 | local nums = { | ||
37 | ["b"] = {0,1,0,7,0,5,1,4,1,4,2,4,3,5,3,6,2,7,1,7,0,6,0,5}, | ||
38 | ["o"] = {1,4,0,5,0,6,1,7,2,7,3,6,3,5,2,4,1,4}, | ||
39 | ["x"] = {0,3,4,7,2,5,4,3,0,7}, | ||
40 | [-1] = {1,4, 3,4}, | ||
41 | [0] = {1,2,1,6,2,7,3,7,4,6,4,2,3,1,2,1,1,2}, | ||
42 | [1] = {3,1,3,7}, | ||
43 | [2] = {1,1,3,1,4,2,4,3,3,4,1,5,1,7,4,7}, | ||
44 | [3] = {1,1,3,1,4,2,4,3,3,4,2,4,3,4,4,5,4,6,3,7,1,7}, | ||
45 | [4] = {1,1,1,3,2,4,4,4,4,1,4,7}, | ||
46 | [5] = {1,1,4,1,1,1,1,4,3,4,4,5,4,7,1,7}, | ||
47 | [6] = {1,2,1,4,1,6,2,7,3,7,4,6,4,4,1,4,1,2,2,1,4,1}, | ||
48 | [7] = {1,1,4,1,4,2,1,7}, | ||
49 | [8] = {1,2,1,6,2,7,3,7,4,6,4,4,1,4,4,4,4,2,3,1,2,1,1,2}, | ||
50 | [9] = {4,6,4,4,4,2,3,1,2,1,1,2,1,4,4,4,4,6,3,7,1,7}, | ||
51 | [10] = {1,7,1,4,4,4,4,7,4,2,3,1,2,1,1,2,1,4}, | ||
52 | [11] = {1,1,1,7,3,7,4,6,4,5,3,4,1,4,3,4,4,3,4,2,3,1,1,1}, | ||
53 | [12] = {4,2,3,1,2,1,1,2,1,6,2,7,3,7,4,6}, | ||
54 | [13] = {1,1,1,7,3,7,4,6,4,2,3,1,1,1}, | ||
55 | [14] = {4,1,1,1,1,4,3,4,1,4,1,7,4,7}, | ||
56 | [15] = {4,1,1,1,1,4,3,4,1,4,1,7}, | ||
57 | } | ||
58 | _draw_nums.nums = nums | ||
59 | |||
60 | |||
61 | _draw_nums.print = function(img, num, x, y, chrw, color, base, prefix, bClip, scale_x, scale_y, t_nums) | ||
62 | scale_x = scale_x or 1 | ||
63 | scale_y = scale_y or 1 | ||
64 | chrw = chrw * scale_x | ||
65 | prefix = (prefix == nil or prefix == true) and true or false | ||
66 | t_nums = t_nums or nums | ||
67 | local max_x, max_y, digits = 0, 0, {} | ||
68 | |||
69 | if num <= 0 then | ||
70 | if num < 0 then | ||
71 | digits[-3] = -1 | ||
72 | num = -num | ||
73 | else | ||
74 | digits[0] = 0 | ||
75 | end | ||
76 | end | ||
77 | |||
78 | if not prefix and (base == 2 or base == 8 or base == 16) then | ||
79 | -- no prefix | ||
80 | elseif base == 2 then | ||
81 | digits[-1] = "b" | ||
82 | elseif base == 8 then | ||
83 | digits[-1] = "o" | ||
84 | elseif base == 10 then | ||
85 | -- no prefix | ||
86 | elseif base == 16 then | ||
87 | digits[-2] = 0 | ||
88 | digits[-1] = "x" | ||
89 | elseif base == nil then -- default | ||
90 | base = 10 | ||
91 | else | ||
92 | error("unknown number base: " .. base) | ||
93 | return nil | ||
94 | end | ||
95 | |||
96 | while num > 0 do -- get each digit (LeastSignificant) | ||
97 | digits[#digits + 1] = num % base; | ||
98 | num=num/base; | ||
99 | end | ||
100 | |||
101 | digits[#digits + 1] = digits[0] -- zero | ||
102 | digits[#digits + 1] = digits[-1] -- base prefix | ||
103 | digits[#digits + 1] = digits[-2] -- base prefix (hex) | ||
104 | digits[#digits + 1] = digits[-3] -- neg sign | ||
105 | |||
106 | for i = #digits, 1, -1 do | ||
107 | max_x, max_y = _poly.polyline(img, x, y, t_nums[digits[i]], | ||
108 | color, false, bClip, scale_x, scale_y) | ||
109 | x = x + chrw | ||
110 | end | ||
111 | |||
112 | return x, y + max_y, chrw | ||
113 | end | ||
114 | end | ||
115 | return _draw_nums | ||