Wordclock/displayword.lua

307 lines
8.9 KiB
Lua
Raw Normal View History

2017-03-01 21:39:05 +01:00
-- Module filling a buffer, sent to the LEDs
2018-02-18 15:40:54 +01:00
function updateColor(data, inverseRow, characters2draw)
2018-02-18 14:08:56 +01:00
if (inverseRow == nil) then
inverseRow=false
end
2018-01-28 15:08:08 +01:00
-- special case, and there are exactly 4 words to display (so each word for each minute)
2018-02-18 15:40:54 +01:00
if (not inverseRow) then -- nomral row
if (data.drawnCharacters < data.charsPerMinute) then
if (data.words.min1 == 1 or data.words.min2 == 1 or data.words.min3 == 1 or data.words.min4 == 1) then
return data.colorMin1
else
return data.colorFg
end
elseif (data.drawnCharacters < data.charsPerMinute*2) then
if (data.words.min2 == 1 or data.words.min3 == 1 or data.words.min4 == 1) then
return data.colorMin2
else
return data.colorFg
end
elseif (data.drawnCharacters < data.charsPerMinute*3) then
if (data.words.min3 == 1 or data.words.min4 == 1) then
return data.colorMin3
else
return data.colorFg
end
elseif (data.drawnCharacters > data.charsPerMinute*3) then
if (data.words.min4 == 1) then
return data.colorMin4
else
return data.colorFg
end
2017-03-01 21:39:05 +01:00
else
return data.colorFg
end
2018-02-18 15:40:54 +01:00
else -- inverse row
--FIXME magic missing
2017-03-01 21:39:05 +01:00
return data.colorFg
end
end
2018-02-18 14:08:56 +01:00
function drawLEDs(data, numberNewChars, inverseRow)
if (inverseRow == nil) then
inverseRow=false
end
2018-02-18 15:40:54 +01:00
print("charactes " .. tostring(data.charsPerMinute) .. " per minute; " .. tonumber(data.drawnCharacters) .. " used characters")
2017-03-01 21:39:05 +01:00
local tmpBuf=nil
for i=1,numberNewChars do
if (tmpBuf == nil) then
2018-02-18 15:40:54 +01:00
tmpBuf = updateColor(data, inverseRow, numberNewChars)
2017-03-01 21:39:05 +01:00
else
2018-02-18 15:40:54 +01:00
tmpBuf=tmpBuf .. updateColor(data, inverseRow, numberNewChars)
2017-03-01 21:39:05 +01:00
end
2018-01-28 15:08:08 +01:00
data.drawnCharacters=data.drawnCharacters+1
2017-03-01 21:39:05 +01:00
end
2018-01-28 15:08:08 +01:00
data.drawnWords=data.drawnWords+1
2017-03-01 21:39:05 +01:00
return tmpBuf
end
2016-06-15 21:24:40 +02:00
-- Module displaying of the words
2018-05-02 20:40:20 +02:00
function generateLEDs(words, colorForground, colorMin1, colorMin2, colorMin3, colorMin4, characters)
2017-03-01 21:39:05 +01:00
-- Set the local variables needed for the colored progress bar
data={}
2018-12-26 21:41:13 +01:00
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
print("Minutes : " .. tostring(minutes) )
-- data.charsPerMinute=characters - math.floor(characters / minutes) * minutes -- modulo
data.charsPerMinute = math.ceil(characters / minutes)
-- devide by five (Minute 0, Minute 1 to Minute 4 takes the last chars)
2017-03-01 21:39:05 +01:00
data.words=words
data.colorFg=colorFg
data.colorMin1=colorMin1
data.colorMin2=colorMin2
data.colorMin3=colorMin3
data.colorMin4=colorMin4
2018-01-28 15:08:08 +01:00
data.drawnCharacters=0
data.drawnWords=0
data.amountWords=display_countwords_de(words)
2018-05-02 20:40:20 +02:00
local charsPerLine=11
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)
2018-05-02 19:52:08 +02:00
-- set FG to fix value:
2018-05-02 20:40:20 +02:00
colorFg = string.char(255,255,255)
2017-01-03 14:46:09 +01:00
2017-03-01 21:39:05 +01:00
-- Set the foreground color as the default color
local buf=colorFg
2016-06-15 21:24:40 +02:00
-- line 1----------------------------------------------
2018-01-30 20:52:40 +01:00
if (words.it==1) then
2018-05-02 19:52:08 +02:00
buf=colorFg:rep(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-05-02 19:52:08 +02:00
buf=buf .. colorFg:rep(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)
if (words.fiveMin== 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(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) --------------------
if (words.twenty == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(7,true) -- ZWANZIG
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(7)
2016-06-15 21:24:40 +02:00
end
if (words.tenMin == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4,true) -- ZEHN
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
-- line3----------------------------------------------
if (words.threequater == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(11) -- Dreiviertel
2016-06-15 21:24:40 +02:00
elseif (words.quater == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(4)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(7) -- VIERTEL
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(11)
2016-06-15 21:24:40 +02:00
end
--line 4-------- even row (so inverted) -------------
if (words.before == 1) then
2017-01-03 14:46:09 +01:00
buf=buf .. space:rep(2)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(3,true) -- VOR
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(5)
2016-06-15 21:24:40 +02:00
end
if (words.after == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4,true) -- NACH
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(2) -- TG
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(6)
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
if (words.half == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4) -- HALB
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1) -- X
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(5)
2016-06-15 21:24:40 +02:00
end
if (words.twelve == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(5) -- ZWOELF
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1) -- P
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(6)
2016-06-15 21:24:40 +02:00
end
------------even row (so inverted) ---------------------
if (words.seven == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(6,true) -- SIEBEN
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(5)
elseif (words.oneLong == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(5)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4,true) -- EINS
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(2)
elseif (words.one == 1) then
buf= buf .. space:rep(6)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(3,true) -- EIN
buf= buf .. space:rep(2)
2016-06-15 21:24:40 +02:00
elseif (words.two == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(7)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4,true) -- ZWEI
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(11)
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
if (words.three == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4) -- DREI
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(6)
2016-06-15 21:24:40 +02:00
elseif (words.five == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(7)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4) -- FUENF
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(11)
2016-06-15 21:24:40 +02:00
end
------------even row (so inverted) ---------------------
if (words.four == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4,true) -- VIER
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(7)
2016-06-15 21:24:40 +02:00
elseif (words.nine == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(4)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4,true) -- NEUN
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(3)
2016-06-15 21:24:40 +02:00
elseif (words.eleven == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(8)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(3,true) -- ELEVEN
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(11)
2016-06-15 21:24:40 +02:00
end
------------------------------------------------
if (words.eight == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4) -- ACHT
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(6)
2016-06-15 21:24:40 +02:00
elseif (words.ten == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(5)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(4) -- ZEHN
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(2)
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(11)
2016-06-15 21:24:40 +02:00
end
------------even row (so inverted) ---------------------
if (words.clock == 1) then
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(3,true) -- UHR
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(3)
2016-06-15 21:24:40 +02:00
end
2016-06-18 19:12:31 +02:00
if (words.six == 1) then
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(2)
2018-05-02 19:52:08 +02:00
buf= buf .. colorFg:rep(5,true) -- SECHS
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(1)
2016-06-15 21:24:40 +02:00
else
2017-01-03 14:46:09 +01:00
buf= buf .. space:rep(8)
2016-06-15 21:24:40 +02:00
end
if (words.min1 == 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
if (words.min2 == 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
if (words.min3 == 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
if (words.min4 == 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-05-02 19:52:08 +02:00
2018-05-02 20:40:20 +02:00
local bufColored = string.char()
--function to set some color to the LEDs
2018-05-02 21:03:43 +02:00
local word=0
local firstCharAfterSpace=true
2018-05-02 20:40:20 +02:00
for x=0,9 do
for y=0, (charsPerLine-1) do
local start = ((x * charsPerLine) + y)*3 + 1
item=string.byte(buf, start)
2018-05-02 21:03:43 +02:00
-- Color the visible words
2018-05-02 20:40:20 +02:00
if (item > 0) then
2018-05-02 21:03:43 +02:00
if (firstCharAfterSpace == true) then
word = word + 1
end
firstCharAfterSpace=false
if (characters == 4) then -- we have a word for each minute to color differently
if (words.min4 == 1 and word == 4) then
bufColored = bufColored .. colorMin4
elseif (words.min3 == 1 and word == 3) then
bufColored = bufColored .. colorMin3
elseif (words.min2 == 1 and word == 2) then
bufColored = bufColored .. colorMin2
elseif (words.min1 == 1 and word == 1) then
bufColored = bufColored .. colorMin1
else
bufColored = bufColored .. colorForground
end
else -- FIXME some more magic should be added here
if (words.min4 == 1) then
bufColored = bufColored .. colorMin4
elseif (words.min3 == 1) then
bufColored = bufColored .. colorMin3
elseif (words.min2 == 1) then
bufColored = bufColored .. colorMin2
elseif (words.min1 == 1) then
bufColored = bufColored .. colorMin1
else
bufColored = bufColored .. colorForground
end
end
2018-05-02 20:40:20 +02:00
else
2018-05-02 21:03:43 +02:00
firstCharAfterSpace=true
2018-05-02 20:40:20 +02:00
-- update the background color, if set
if (colorBg ~= nil) then
bufColored = bufColored .. colorBg
end
end
2018-05-02 21:13:44 +02:00
print (x .. "x" .. y .. " : " .. start .. " color " .. tostring(item) .. " len " .. string.len(buf) .. " word " .. tostring(word))
2018-05-02 20:40:20 +02:00
end
end
collectgarbage()
2018-05-02 19:52:08 +02:00
2018-05-02 20:40:20 +02:00
return bufColored
2016-06-18 17:07:49 +02:00
end