Wordclock/displayword.lua

452 lines
12 KiB
Lua
Raw Normal View History

2017-03-01 21:39:05 +01:00
-- Module filling a buffer, sent to the LEDs
2019-05-03 20:19:12 +02:00
local M
do
-- @fn generateLEDs
-- Module displaying of the words
-- @param data struct with the following paramter:
-- aoC amount of characters to be used
-- dC drawn characters
2019-05-10 23:42:20 +02:00
local updateColor = function (data)
if (data.aoC > 0) then
local div = tonumber(data.dC/data.aoC)
2020-12-10 22:15:27 +01:00
if (div < 1) then
return data.colorFg
elseif (div < 2) then
2021-02-10 19:45:08 +01:00
return data.colorM1
2020-12-10 22:15:27 +01:00
elseif (div < 3) then
2021-02-10 19:45:08 +01:00
return data.colorM2
2020-12-10 22:15:27 +01:00
elseif (div < 4) then
2021-02-10 19:45:08 +01:00
return data.colorM3
2020-12-10 22:15:27 +01:00
elseif (div < 5) then
2021-02-10 19:45:08 +01:00
return data.colorM4
2020-12-10 22:15:27 +01:00
else
return data.colorFg
end
2019-05-22 21:25:33 +02:00
else
return data.colorFg
end
2017-03-01 21:39:05 +01:00
end
2019-05-10 22:58:42 +02:00
local drawLEDs = function(data, numberNewChars)
2018-12-27 14:25:51 +01:00
if (numberNewChars == nil) then
numberNewChars=0
end
2017-03-01 21:39:05 +01:00
local tmpBuf=nil
for i=1,numberNewChars do
if (tmpBuf == nil) then
2019-05-10 23:42:20 +02:00
tmpBuf = updateColor(data)
2017-03-01 21:39:05 +01:00
else
2019-05-10 23:42:20 +02:00
tmpBuf=tmpBuf .. updateColor(data)
2017-03-01 21:39:05 +01:00
end
data.dC=data.dC+1
2017-03-01 21:39:05 +01:00
end
return tmpBuf
end
-- Utility function for round
2019-05-03 20:19:12 +02:00
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
2019-04-27 18:34:57 +02:00
local data={}
2019-04-19 23:38:46 +02:00
2021-02-10 19:45:08 +01:00
-- @fn generateLEDs
2016-06-15 21:24:40 +02:00
-- Module displaying of the words
2021-02-10 19:45:08 +01:00
-- @param words
-- @param colorBg background color
-- @param colorFg foreground color
-- @param colorM1 foreground color if one minute after a displayable time is present
-- @param colorM2 foreground color if two minutes after a displayable time is present
-- @param colorM3 foreground color if three minutes after a displayable time is present
-- @param colorM4 foreground color if four minutes after a displayable time is present
-- @param invertRows wheather line 4,5 and 6 shall be inverted or not
-- @param aoC Amount of characters to be displayed
local generateLEDs = function(words, colorBg, colorFg, colorM1, colorM2, colorM3, colorM4, invertRows, aoC)
2017-03-01 21:39:05 +01:00
-- Set the local variables needed for the colored progress bar
2019-05-03 20:45:16 +02:00
if (words == nil) then
return nil
end
2019-05-11 12:28:23 +02:00
if (invertRows == nil) then
invertRows=false
end
2019-05-22 21:25:33 +02:00
local minutes=1
2021-02-10 19:45:08 +01:00
if (words.m1 == 1) then
2018-12-26 21:41:13 +01:00
minutes = minutes + 1
2021-02-10 19:45:08 +01:00
elseif (words.m2 == 1) then
2018-12-26 21:41:13 +01:00
minutes = minutes + 2
2021-02-10 19:45:08 +01:00
elseif (words.m3 == 1) then
2018-12-26 21:41:13 +01:00
minutes = minutes + 3
2021-02-10 19:45:08 +01:00
elseif (words.m4 == 1) then
2018-12-26 21:41:13 +01:00
minutes = minutes + 4
end
2019-05-15 20:40:41 +02:00
-- always set a foreground value
2019-05-22 21:25:33 +02:00
if (colorFg == nil) then
colorFg = string.char(255,255,255)
2019-05-15 20:40:41 +02:00
end
if (aoC ~= nil) then
data.aoC = aoC/minutes
else
data.aoC = 0
end
2021-02-10 20:04:22 +01:00
if ( (adc ~= nil) and (words.briPer ~= nil) ) then
2019-04-27 00:22:29 +02:00
local per = math.floor(100*adc.read(0)/1000)
2021-02-10 20:04:22 +01:00
words.briPer = tonumber( ((words.briPer * 4) + per) / 5)
print("Minutes : " .. tostring(minutes) .. " bright: " .. tostring(words.briPer) .. "% current: " .. tostring(per) .. "%")
data.colorFg = string.char(string.byte(colorFg,1) * briPer / 100, string.byte(colorFg,2) * briPer / 100, string.byte(colorFg,3) * briPer / 100)
data.colorM1 = string.char(string.byte(colorM1,1) * briPer / 100, string.byte(colorM1,2) * briPer / 100, string.byte(colorM1,3) * briPer / 100)
data.colorM2 = string.char(string.byte(colorM2,1) * briPer / 100, string.byte(colorM2,2) * briPer / 100, string.byte(colorM2,3) * briPer / 100)
data.colorM3 = string.char(string.byte(colorM3,1) * briPer / 100, string.byte(colorM3,2) * briPer / 100, string.byte(colorM3,3) * briPer / 100)
data.colorM4 = string.char(string.byte(colorM4,1) * briPer / 100, string.byte(colorM4,2) * briPer / 100, string.byte(colorM4,3) * briPer / 100)
2019-03-22 23:52:25 +01:00
else
-- devide by five (Minute 0, Minute 1 to Minute 4 takes the last chars)
2019-05-15 20:40:41 +02:00
data.colorFg=colorFg
2021-02-10 19:45:08 +01:00
data.colorM1=colorM1
data.colorM2=colorM2
data.colorM3=colorM3
data.colorM4=colorM4
2019-03-22 23:52:25 +01:00
end
data.dC=0
2018-05-02 20:40:20 +02:00
local charsPerLine=11
2021-01-14 22:14:59 +01:00
2018-05-02 19:52:08 +02:00
-- Space / background has no color by default
2017-01-03 14:46:09 +01:00
local space=string.char(0,0,0)
2021-01-23 09:44:15 +01:00
-- Background color must always be set
2021-01-14 22:14:59 +01:00
if (colorBg ~= nil) then
space = colorBg
2021-01-23 09:44:15 +01:00
else
colorBg = space
2021-01-14 22:14:59 +01:00
end
2017-03-01 21:39:05 +01:00
-- Set the foreground color as the default color
2019-05-15 20:40:41 +02:00
local buf=data.colorFg
2019-05-10 22:58:42 +02:00
local line=space
2016-06-15 21:24:40 +02:00
-- line 1----------------------------------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[1] ~= nil) then
space = rowbgColor[1]
2021-01-23 09:44:15 +01:00
end
2018-01-30 20:52:40 +01:00
if (words.it==1) then
2018-12-27 14:25:51 +01:00
buf=drawLEDs(data,2) -- ES
2018-01-30 20:52:40 +01:00
else
buf=space:rep(2)
end
-- K fill character
buf=buf .. space:rep(1)
if (words.is == 1) then
2018-12-27 14:25:51 +01:00
buf=buf .. drawLEDs(data,3) -- IST
2016-06-15 21:24:40 +02:00
else
2018-01-30 20:52:40 +01:00
buf=buf .. space:rep(3)
2016-06-15 21:24:40 +02:00
end
2018-01-30 20:52:40 +01:00
-- L fill character
buf=buf .. space:rep(1)
2021-02-10 19:45:08 +01:00
if (words.m5== 1) then
2018-12-27 14:25:51 +01:00
buf= buf .. drawLEDs(data,4) -- FUENF
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(4)
2016-06-15 21:24:40 +02:00
end
-- line 2-- even row (so inverted) --------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[2] ~= nil) then
space = rowbgColor[2]
2021-01-23 09:44:15 +01:00
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.m10 == 1) then
2019-05-10 22:58:42 +02:00
line= drawLEDs(data,4) -- ZEHN
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(4)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m20 == 1) then
2019-05-10 22:58:42 +02:00
line= line .. drawLEDs(data,7) -- ZWANZIG
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
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)
2016-06-15 21:24:40 +02:00
end
2019-05-10 22:58:42 +02:00
2016-06-15 21:24:40 +02:00
-- line3----------------------------------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[3] ~= nil) then
space = rowbgColor[3]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.h3q == 1) then
line= drawLEDs(data,11) -- DREIVIERTEL
elseif (words.hq == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(4)
line= line .. drawLEDs(data,7) -- VIERTEL
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(11)
2016-06-15 21:24:40 +02:00
end
2019-05-10 22:58:42 +02:00
-- fill, the buffer
buf = buf .. line
2016-06-15 21:24:40 +02:00
--line 4-------- even row (so inverted) -------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[4] ~= nil) then
space = rowbgColor[4]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.ha == 1) then
2019-05-10 23:42:20 +02:00
line= space:rep(2) -- TG
line= line .. drawLEDs(data,4) -- NACH
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(6)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.hb == 1) then
2019-05-10 22:58:42 +02:00
line= line .. drawLEDs(data,3) -- VOR
line= line .. space:rep(2)
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= line .. space:rep(5)
end
2019-05-11 12:28:23 +02:00
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
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[5] ~= nil) then
space = rowbgColor[5]
else
space = colorBg
end
2016-06-15 21:24:40 +02:00
if (words.half == 1) then
2019-05-10 22:58:42 +02:00
line= drawLEDs(data,4) -- HALB
line= line .. space:rep(1) -- X
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(5)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.h12 == 1) then
2019-05-10 22:58:42 +02:00
line= line .. drawLEDs(data,5) -- ZWOELF
line= line .. space:rep(1) -- P
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= line .. space:rep(6)
2016-06-15 21:24:40 +02:00
end
2019-05-11 12:28:23 +02:00
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
2016-06-15 21:24:40 +02:00
------------even row (so inverted) ---------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[6] ~= nil) then
space = rowbgColor[6]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.h7 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(5)
line= line .. drawLEDs(data,6) -- SIEBEN
2021-02-10 19:45:08 +01:00
elseif (words.h1l == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(2)
line= line .. drawLEDs(data,4) -- EINS
line= line .. space:rep(5)
2021-02-10 19:45:08 +01:00
elseif (words.h1 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(2)
line= line .. drawLEDs(data,3) -- EIN
line= line .. space:rep(6)
2021-02-10 19:45:08 +01:00
elseif (words.h2 == 1) then
2019-05-10 22:58:42 +02:00
line= drawLEDs(data,4) -- ZWEI
line= line .. space:rep(7)
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(11)
end
2019-05-11 12:28:23 +02:00
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
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[7] ~= nil) then
space = rowbgColor[7]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.h3 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(1)
line= line .. drawLEDs(data,4) -- DREI
line= line .. space:rep(6)
2021-02-10 19:45:08 +01:00
elseif (words.h5 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(7)
line= line .. drawLEDs(data,4) -- FUENF
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(11)
2016-06-15 21:24:40 +02:00
end
2019-05-10 22:58:42 +02:00
buf = buf .. line
2016-06-15 21:24:40 +02:00
------------even row (so inverted) ---------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[8] ~= nil) then
space = rowbgColor[8]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.h4 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(7)
line= line .. drawLEDs(data,4) -- VIER
2021-02-10 19:45:08 +01:00
elseif (words.h9 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(3)
line= line .. drawLEDs(data,4) -- NEUN
line= line .. space:rep(4)
2021-02-10 19:45:08 +01:00
elseif (words.h11 == 1) then
line= drawLEDs(data,3) -- ELF
2019-05-10 22:58:42 +02:00
line= line .. space:rep(8)
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(11)
end
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[9] ~= nil) then
space = rowbgColor[9]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.h8 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(1)
line= line .. drawLEDs(data,4) -- ACHT
line= line .. space:rep(6)
2021-02-10 19:45:08 +01:00
elseif (words.h10 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(5)
line= line .. drawLEDs(data,4) -- ZEHN
line= line .. space:rep(2)
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(11)
2016-06-15 21:24:40 +02:00
end
2019-05-10 22:58:42 +02:00
buf = buf .. line
2016-06-15 21:24:40 +02:00
------------even row (so inverted) ---------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[10] ~= nil) then
space = rowbgColor[10]
else
space = colorBg
end
2021-02-10 19:45:08 +01:00
if (words.h6 == 1) then
2019-05-10 22:58:42 +02:00
line= space:rep(1)
line= line .. drawLEDs(data,5) -- SECHS
line= line .. space:rep(2)
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= space:rep(8)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.cl == 1) then
2019-05-10 22:58:42 +02:00
line= line .. drawLEDs(data,3) -- UHR
2016-06-15 21:24:40 +02:00
else
2019-05-10 22:58:42 +02:00
line= line .. space:rep(3)
end
for i = 0,10 do
buf = buf .. line:sub((11-i)*3-2,(11-i)*3)
2016-06-15 21:24:40 +02:00
end
2019-05-10 22:58:42 +02:00
------ Minutes -----------
2021-02-10 19:45:08 +01:00
if (words.m1 == 1) then
2017-03-01 21:39:05 +01:00
buf= buf .. colorFg
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m2 == 1) then
2017-03-01 21:39:05 +01:00
buf= buf .. colorFg
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m3 == 1) then
2017-03-01 21:39:05 +01:00
buf= buf .. colorFg
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m4 == 1) then
2017-03-01 21:39:05 +01:00
buf= buf .. colorFg
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2016-06-15 21:24:40 +02:00
end
collectgarbage()
2018-12-27 14:25:51 +01:00
return buf
2016-06-18 17:07:49 +02:00
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
2021-02-10 19:45:08 +01:00
elseif (key == "m5") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "m10") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "ha") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "hb") then
characters = characters + 3
2021-02-10 19:45:08 +01:00
elseif (key == "h3") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "hq") then
characters = characters + 7
2021-02-10 19:45:08 +01:00
elseif (key == "h3q") then
characters = characters + 11
elseif (key == "half") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h1") then
characters = characters + 3
2021-02-10 19:45:08 +01:00
elseif (key == "h1l") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h2") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h3") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h4") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h5") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h6") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h7") then
characters = characters + 6
2021-02-10 19:45:08 +01:00
elseif (key == "h8") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h9") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h10") then
characters = characters + 4
2021-02-10 19:45:08 +01:00
elseif (key == "h11") then
characters = characters + 3
2021-02-10 19:45:08 +01:00
elseif (key == "h12") then
characters = characters + 5
2021-02-10 19:45:08 +01:00
elseif (key == "m20") then
characters = characters + 7
2021-02-10 19:45:08 +01:00
elseif (key == "cl") then
characters = characters + 3
end
end
end
return characters
end
2019-05-03 20:19:12 +02:00
M = {
generateLEDs = generateLEDs,
round = round,
drawLEDs = drawLEDs,
updateColor = updateColor,
2019-05-03 20:45:16 +02:00
data = data,
countChars = countChars
2019-05-03 20:19:12 +02:00
}
end
2021-02-11 21:23:32 +01:00
dw = M