Wordclock/displayword.lua

373 lines
11 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
local data={}
-- 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
2022-01-23 14:01:25 +01:00
-- @fn updateColor
-- Module displaying of the words
-- @param data struct with the following paramter:
2021-03-21 14:13:48 +01:00
-- aoC amount of characters for the complete message
-- mC amout of minutes to show
-- dC drawn characters
2019-05-10 23:42:20 +02:00
local updateColor = function (data)
if (data.aoC > 0) and (data.mC ~= nil) then
local specialChar = data.dC
if (data.mC < 1) then
specialChar = 0
elseif (data.dC > data.mC) then
specialChar = 0
end
if (specialChar < 1) then
2020-12-10 22:15:27 +01:00
return data.colorFg
elseif (specialChar < 2) then
2021-02-10 19:45:08 +01:00
return data.colorM1
elseif (specialChar < 3) then
2021-02-10 19:45:08 +01:00
return data.colorM2
elseif (specialChar < 4) then
2021-02-10 19:45:08 +01:00
return data.colorM3
elseif (specialChar < 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
2021-03-20 17:24:54 +01:00
local drawLEDs = function(data, offset, numberNewChars)
2018-12-27 14:25:51 +01:00
if (numberNewChars == nil) then
numberNewChars=0
end
2021-03-14 21:52:46 +01:00
if (data.rgbBuffer == nil) then
return
end
2017-03-01 21:39:05 +01:00
for i=1,numberNewChars do
data.dC=data.dC+1
2021-03-21 17:30:50 +01:00
data.rgbBuffer:set(tonumber(offset + i - 1), updateColor(data))
2017-03-01 21:39:05 +01:00
end
end
2021-03-19 18:21:41 +01:00
-- @fn swapLine
-- @param lineOffset offset (starting at 1) where the line is located to be swapped
-- works on the rgbBuffer, defined in data struct
-- @return <code>false</code> on errors, else <code>true</code>
2021-03-19 18:44:41 +01:00
local swapLine = function(data, lineOffset)
2021-03-19 18:21:41 +01:00
if (data.rgbBuffer == nil) then
return false
end
2021-03-21 14:13:48 +01:00
for i = 0,4 do
2021-03-20 16:07:05 +01:00
local num=tonumber(lineOffset)+i
2021-03-21 17:30:50 +01:00
local num2=tonumber(tonumber(lineOffset)+10-i)
local tmpC1, tmpC2, tmpC3=data.rgbBuffer:get(num)
local c1, c2, c3 =data.rgbBuffer:get(num2)
data.rgbBuffer:set(num, c1, c2, c3)
data.rgbBuffer:set(num2, tmpC1, tmpC2, tmpC3)
2021-03-19 18:21:41 +01:00
end
return true
end
2021-02-10 19:45:08 +01:00
-- @fn generateLEDs
2016-06-15 21:24:40 +02:00
-- Module displaying of the words
2021-03-14 21:52:46 +01:00
-- @param rgbBuffer OutputBuffer with 114 LEDs
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
2021-03-21 14:13:48 +01:00
-- @param aoC Amount of characters to be displayed
2021-03-14 21:52:46 +01:00
local generateLEDs = function(rgbBuffer, 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
local minutes=0
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
2021-03-21 14:13:48 +01:00
data.aoC = aoC
data.mC = minutes
else
data.aoC = 0
end
2021-03-14 21:52:46 +01:00
data.rgbBuffer = rgbBuffer
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
2021-03-20 16:19:03 +01:00
data.dC=0 -- drawn characters
2018-05-02 20:40:20 +02:00
local charsPerLine=11
2021-01-14 22:14:59 +01:00
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
2021-03-21 17:30:50 +01:00
rgbBuffer:fill(string.byte(colorBg,1), string.byte(colorBg,2), string.byte(colorBg,3)) -- draw the background
2021-01-14 22:14:59 +01:00
end
2021-03-19 18:21:41 +01:00
local lineIdx=1
2016-06-15 21:24:40 +02:00
-- line 1----------------------------------------------
2021-01-23 19:17:30 +01:00
if (rowbgColor[1] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[1]) end
2021-01-23 09:44:15 +01:00
end
2018-01-30 20:52:40 +01:00
if (words.it==1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx, 2) -- ES
2018-01-30 20:52:40 +01:00
end
2021-03-20 16:19:03 +01:00
-- K fill character
2018-01-30 20:52:40 +01:00
if (words.is == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx+3, 3) -- IST
2016-06-15 21:24:40 +02:00
end
2018-01-30 20:52:40 +01:00
-- L fill character
2021-03-20 16:19:03 +01:00
if (words.m5== 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx+7, 4) -- FUENF
2016-06-15 21:24:40 +02:00
end
-- line 2-- even row (so inverted) --------------------
2021-03-19 18:21:41 +01:00
lineIdx=12
2021-01-23 19:17:30 +01:00
if (rowbgColor[2] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[2]) end
2021-01-23 09:44:15 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.m10 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx, 4) -- ZEHN
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m20 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 4, 7) -- ZWANZIG
2016-06-15 21:24:40 +02:00
end
2021-03-19 18:21:41 +01:00
-- swap line
2021-03-19 18:44:41 +01:00
swapLine(data,lineIdx)
2016-06-15 21:24:40 +02:00
-- line3----------------------------------------------
2021-03-19 18:21:41 +01:00
lineIdx=23
2021-01-23 19:17:30 +01:00
if (rowbgColor[3] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[3]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.h3q == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data,lineIdx, 11) -- DREIVIERTEL
2021-02-10 19:45:08 +01:00
elseif (words.hq == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 4, 7) -- VIERTEL
2016-06-15 21:24:40 +02:00
end
--line 4-------- even row (so inverted) -------------
2021-03-19 18:21:41 +01:00
lineIdx=34
if (rowbgColor[4] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[4]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.ha == 1) then
2021-03-20 17:24:54 +01:00
-- TG
drawLEDs(data, lineIdx + 2, 4) -- NACH
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.hb == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 6, 3) -- VOR
2019-05-10 22:58:42 +02:00
end
2021-03-14 21:52:46 +01:00
if (invertRows ~= true) then
2021-03-19 18:44:41 +01:00
swapLine(data,lineIdx)
2016-06-15 21:24:40 +02:00
end
2021-03-20 17:24:54 +01:00
-- line 5 ----------------------------------------------
2021-03-19 18:21:41 +01:00
lineIdx=45
if (rowbgColor[5] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[5]) end
2021-01-23 19:17:30 +01:00
end
2016-06-15 21:24:40 +02:00
if (words.half == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx, 4) -- HALB
-- X
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.h12 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 5,5) -- ZWOELF
-- P
2016-06-15 21:24:40 +02:00
end
2019-05-11 12:28:23 +02:00
if (invertRows == true) then
2021-03-19 18:44:41 +01:00
swapLine(data,lineIdx)
2019-05-11 12:28:23 +02:00
end
2016-06-15 21:24:40 +02:00
------------even row (so inverted) ---------------------
2021-03-19 18:21:41 +01:00
lineIdx=56
if (rowbgColor[6] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[6]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.h7 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 5, 6) -- SIEBEN
2021-02-10 19:45:08 +01:00
elseif (words.h1l == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 2,4) -- EINS
2021-02-10 19:45:08 +01:00
elseif (words.h1 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 2, 3) -- EIN
2021-02-10 19:45:08 +01:00
elseif (words.h2 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx, 4) -- ZWEI
2019-05-10 22:58:42 +02:00
end
2021-03-14 21:52:46 +01:00
if (invertRows ~= true) then
2021-03-19 18:44:41 +01:00
swapLine(data,lineIdx)
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
2021-03-19 18:21:41 +01:00
lineIdx=67
if (rowbgColor[7] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[7]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.h3 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 1,4) -- DREI
2021-02-10 19:45:08 +01:00
elseif (words.h5 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 7, 4) -- FUENF
2016-06-15 21:24:40 +02:00
end
------------even row (so inverted) ---------------------
2021-03-19 18:21:41 +01:00
lineIdx=78
if (rowbgColor[8] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[8]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.h4 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 7, 4) -- VIER
2021-02-10 19:45:08 +01:00
elseif (words.h9 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 3, 4) -- NEUN
2021-02-10 19:45:08 +01:00
elseif (words.h11 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx, 3) -- ELF
2019-05-10 22:58:42 +02:00
end
2021-03-19 18:44:41 +01:00
swapLine(data,lineIdx)
2016-06-15 21:24:40 +02:00
------------------------------------------------
2021-03-19 18:21:41 +01:00
lineIdx=89
if (rowbgColor[9] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[9]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.h8 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 1, 4) -- ACHT
2021-02-10 19:45:08 +01:00
elseif (words.h10 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 5, 4) -- ZEHN
2016-06-15 21:24:40 +02:00
end
2021-03-14 21:52:46 +01:00
2016-06-15 21:24:40 +02:00
------------even row (so inverted) ---------------------
2021-03-19 18:21:41 +01:00
lineIdx=100
if (rowbgColor[10] ~= nil) then
2021-03-23 21:32:53 +01:00
for i=lineIdx,lineIdx+10, 1 do data.rgbBuffer:set(i, rowbgColor[10]) end
2021-01-23 19:17:30 +01:00
end
2021-02-10 19:45:08 +01:00
if (words.h6 == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 1, 5) -- SECHS
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.cl == 1) then
2021-03-20 17:24:54 +01:00
drawLEDs(data, lineIdx + 8, 3) -- UHR
2019-05-10 22:58:42 +02:00
end
2021-03-19 18:44:41 +01:00
swapLine(data,lineIdx)
2019-05-10 22:58:42 +02:00
------ Minutes -----------
2021-02-10 19:45:08 +01:00
if (words.m1 == 1) then
2021-03-14 21:52:46 +01:00
data.rgbBuffer:set(111, colorFg)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m2 == 1) then
2021-03-14 21:52:46 +01:00
data.rgbBuffer:set(112, colorFg)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m3 == 1) then
2021-03-14 21:52:46 +01:00
data.rgbBuffer:set(113, colorFg)
2016-06-15 21:24:40 +02:00
end
2021-02-10 19:45:08 +01:00
if (words.m4 == 1) then
2021-03-14 21:52:46 +01:00
data.rgbBuffer:set(114, colorFg)
2016-06-15 21:24:40 +02:00
end
collectgarbage()
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