forked from 0110/Wordclock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplayword.lua
437 lines (422 loc) · 11.6 KB
/
displayword.lua
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
-- Module filling a buffer, sent to the LEDs
local M
do
local updateColor = function (data)
if (data.amountOfChars > 0) then
local div = tonumber(data.drawnCharacters/data.amountOfChars)
if (div < 1) then
return data.colorFg
elseif (div < 2) then
return data.colorMin1
elseif (div < 3) then
return data.colorMin2
elseif (div < 4) then
return data.colorMin3
elseif (div < 5) then
return data.colorMin4
else
return data.colorFg
end
else
return data.colorFg
end
end
local drawLEDs = function(data, numberNewChars)
if (numberNewChars == nil) then
numberNewChars=0
end
local tmpBuf=nil
for i=1,numberNewChars do
if (tmpBuf == nil) then
tmpBuf = updateColor(data)
else
tmpBuf=tmpBuf .. updateColor(data)
end
data.drawnCharacters=data.drawnCharacters+1
end
return tmpBuf
end
-- Utility function for round
local round = function(num)
under = math.floor(num)
upper = math.floor(num) + 1
underV = -(under - num)
upperV = upper - num
if (upperV > underV) then
return under
else
return upper
end
end
local data={}
-- Module displaying of the words
local generateLEDs = function(words, colorBg, colorFg, colorMin1, colorMin2, colorMin3, colorMin4, invertRows, amountOfChars)
-- Set the local variables needed for the colored progress bar
if (words == nil) then
return nil
end
if (invertRows == nil) then
invertRows=false
end
local minutes=1
if (words.min1 == 1) then
minutes = minutes + 1
elseif (words.min2 == 1) then
minutes = minutes + 2
elseif (words.min3 == 1) then
minutes = minutes + 3
elseif (words.min4 == 1) then
minutes = minutes + 4
end
-- always set a foreground value
if (colorFg == nil) then
colorFg = string.char(255,255,255)
end
if (amountOfChars ~= nil) then
data.amountOfChars = amountOfChars/minutes
else
data.amountOfChars = 0
end
if ( (adc ~= nil) and (words.briPercent ~= nil) ) then
local per = math.floor(100*adc.read(0)/1000)
words.briPercent = tonumber( ((words.briPercent * 4) + per) / 5)
print("Minutes : " .. tostring(minutes) .. " bright: " .. tostring(words.briPercent) .. "% current: " .. tostring(per) .. "%")
data.colorFg = string.char(string.byte(colorFg,1) * briPercent / 100, string.byte(colorFg,2) * briPercent / 100, string.byte(colorFg,3) * briPercent / 100)
data.colorMin1 = string.char(string.byte(colorMin1,1) * briPercent / 100, string.byte(colorMin1,2) * briPercent / 100, string.byte(colorMin1,3) * briPercent / 100)
data.colorMin2 = string.char(string.byte(colorMin2,1) * briPercent / 100, string.byte(colorMin2,2) * briPercent / 100, string.byte(colorMin2,3) * briPercent / 100)
data.colorMin3 = string.char(string.byte(colorMin3,1) * briPercent / 100, string.byte(colorMin3,2) * briPercent / 100, string.byte(colorMin3,3) * briPercent / 100)
data.colorMin4 = string.char(string.byte(colorMin4,1) * briPercent / 100, string.byte(colorMin4,2) * briPercent / 100, string.byte(colorMin4,3) * briPercent / 100)
else
-- devide by five (Minute 0, Minute 1 to Minute 4 takes the last chars)
data.colorFg=colorFg
data.colorMin1=colorMin1
data.colorMin2=colorMin2
data.colorMin3=colorMin3
data.colorMin4=colorMin4
end
data.drawnCharacters=0
local charsPerLine=11
-- Space / background has no color by default
local space=string.char(0,0,0)
-- Background color must always be set
if (colorBg ~= nil) then
space = colorBg
else
colorBg = space
end
-- Set the foreground color as the default color
local buf=data.colorFg
local line=space
-- line 1----------------------------------------------
if (rowbgColor[1] ~= nil) then
space = rowbgColor[1]
end
if (words.it==1) then
buf=drawLEDs(data,2) -- ES
else
buf=space:rep(2)
end
-- K fill character
buf=buf .. space:rep(1)
if (words.is == 1) then
buf=buf .. drawLEDs(data,3) -- IST
else
buf=buf .. space:rep(3)
end
-- L fill character
buf=buf .. space:rep(1)
if (words.fiveMin== 1) then
buf= buf .. drawLEDs(data,4) -- FUENF
else
buf= buf .. space:rep(4)
end
-- line 2-- even row (so inverted) --------------------
if (rowbgColor[2] ~= nil) then
space = rowbgColor[2]
else
space = colorBg
end
if (words.tenMin == 1) then
line= drawLEDs(data,4) -- ZEHN
else
line= space:rep(4)
end
if (words.twenty == 1) then
line= line .. drawLEDs(data,7) -- ZWANZIG
else
line= line .. space:rep(7)
end
-- fill, the buffer
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
end
-- line3----------------------------------------------
if (rowbgColor[3] ~= nil) then
space = rowbgColor[3]
else
space = colorBg
end
if (words.threequater == 1) then
line= drawLEDs(data,11) -- Dreiviertel
elseif (words.quater == 1) then
line= space:rep(4)
line= line .. drawLEDs(data,7) -- VIERTEL
else
line= space:rep(11)
end
-- fill, the buffer
buf = buf .. line
--line 4-------- even row (so inverted) -------------
if (rowbgColor[4] ~= nil) then
space = rowbgColor[4]
else
space = colorBg
end
if (words.after == 1) then
line= space:rep(2) -- TG
line= line .. drawLEDs(data,4) -- NACH
else
line= space:rep(6)
end
if (words.before == 1) then
line= line .. drawLEDs(data,3) -- VOR
line= line .. space:rep(2)
else
line= line .. space:rep(5)
end
if (invertRows == true) then
buf = buf .. line
else
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
end
end
------------------------------------------------
if (rowbgColor[5] ~= nil) then
space = rowbgColor[5]
else
space = colorBg
end
if (words.half == 1) then
line= drawLEDs(data,4) -- HALB
line= line .. space:rep(1) -- X
else
line= space:rep(5)
end
if (words.twelve == 1) then
line= line .. drawLEDs(data,5) -- ZWOELF
line= line .. space:rep(1) -- P
else
line= line .. space:rep(6)
end
if (invertRows == true) then
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
end
else
buf=buf .. line
end
------------even row (so inverted) ---------------------
if (rowbgColor[6] ~= nil) then
space = rowbgColor[6]
else
space = colorBg
end
if (words.seven == 1) then
line= space:rep(5)
line= line .. drawLEDs(data,6) -- SIEBEN
elseif (words.oneLong == 1) then
line= space:rep(2)
line= line .. drawLEDs(data,4) -- EINS
line= line .. space:rep(5)
elseif (words.one == 1) then
line= space:rep(2)
line= line .. drawLEDs(data,3) -- EIN
line= line .. space:rep(6)
elseif (words.two == 1) then
line= drawLEDs(data,4) -- ZWEI
line= line .. space:rep(7)
else
line= space:rep(11)
end
if (invertRows == true) then
buf = buf .. line
else
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
end
end
------------------------------------------------
if (rowbgColor[7] ~= nil) then
space = rowbgColor[7]
else
space = colorBg
end
if (words.three == 1) then
line= space:rep(1)
line= line .. drawLEDs(data,4) -- DREI
line= line .. space:rep(6)
elseif (words.five == 1) then
line= space:rep(7)
line= line .. drawLEDs(data,4) -- FUENF
else
line= space:rep(11)
end
buf = buf .. line
------------even row (so inverted) ---------------------
if (rowbgColor[8] ~= nil) then
space = rowbgColor[8]
else
space = colorBg
end
if (words.four == 1) then
line= space:rep(7)
line= line .. drawLEDs(data,4) -- VIER
elseif (words.nine == 1) then
line= space:rep(3)
line= line .. drawLEDs(data,4) -- NEUN
line= line .. space:rep(4)
elseif (words.eleven == 1) then
line= drawLEDs(data,3) -- ELEVEN
line= line .. space:rep(8)
else
line= space:rep(11)
end
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
end
------------------------------------------------
if (rowbgColor[9] ~= nil) then
space = rowbgColor[9]
else
space = colorBg
end
if (words.eight == 1) then
line= space:rep(1)
line= line .. drawLEDs(data,4) -- ACHT
line= line .. space:rep(6)
elseif (words.ten == 1) then
line= space:rep(5)
line= line .. drawLEDs(data,4) -- ZEHN
line= line .. space:rep(2)
else
line= space:rep(11)
end
buf = buf .. line
------------even row (so inverted) ---------------------
if (rowbgColor[10] ~= nil) then
space = rowbgColor[10]
else
space = colorBg
end
if (words.six == 1) then
line= space:rep(1)
line= line .. drawLEDs(data,5) -- SECHS
line= line .. space:rep(2)
else
line= space:rep(8)
end
if (words.clock == 1) then
line= line .. drawLEDs(data,3) -- UHR
else
line= line .. space:rep(3)
end
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
end
------ Minutes -----------
if (words.min1 == 1) then
buf= buf .. colorFg
else
buf= buf .. space:rep(1)
end
if (words.min2 == 1) then
buf= buf .. colorFg
else
buf= buf .. space:rep(1)
end
if (words.min3 == 1) then
buf= buf .. colorFg
else
buf= buf .. space:rep(1)
end
if (words.min4 == 1) then
buf= buf .. colorFg
else
buf= buf .. space:rep(1)
end
collectgarbage()
return buf
end
-- Count amount of characters to display
local countChars = function(words)
local characters = 0
for key,value in pairs(words) do
if (value > 0) then
if (key == "it") then
characters = characters + 2
elseif (key == "is") then
characters = characters + 3
elseif (key == "fiveMin") then
characters = characters + 4
elseif (key == "tenMin") then
characters = characters + 4
elseif (key == "after") then
characters = characters + 4
elseif (key == "before") then
characters = characters + 3
elseif (key == "threeHour") then
characters = characters + 4
elseif (key == "quater") then
characters = characters + 7
elseif (key == "threequater") then
characters = characters + 11
elseif (key == "half") then
characters = characters + 4
elseif (key == "one") then
characters = characters + 3
elseif (key == "oneLong") then
characters = characters + 4
elseif (key == "two") then
characters = characters + 4
elseif (key == "three") then
characters = characters + 4
elseif (key == "four") then
characters = characters + 4
elseif (key == "five") then
characters = characters + 4
elseif (key == "six") then
characters = characters + 4
elseif (key == "seven") then
characters = characters + 6
elseif (key == "eight") then
characters = characters + 4
elseif (key == "nine") then
characters = characters + 4
elseif (key == "ten") then
characters = characters + 4
elseif (key == "eleven") then
characters = characters + 3
elseif (key == "twelve") then
characters = characters + 5
elseif (key == "twenty") then
characters = characters + 7
elseif (key == "clock") then
characters = characters + 3
elseif (key == "sr_nc") then
characters = characters + 3
end
end
end
return characters
end
M = {
generateLEDs = generateLEDs,
round = round,
drawLEDs = drawLEDs,
updateColor = updateColor,
data = data,
countChars = countChars
}
end
displayword = M