Wordclock/main.lua

248 lines
7.0 KiB
Lua
Raw Normal View History

-- Main Module
2021-02-13 14:17:58 +01:00
mlt = tmr.create() -- Main loop timer
2021-02-09 19:58:59 +01:00
rowbgColor= {}
2021-03-14 14:13:46 +01:00
-- Buffer of the clock
rgbBuffer = ws2812.newBuffer(114, 3)
-- 110 Character plus one LED for each minute,
-- that cannot be displayed, as the clock as only a resolution of 5 minutes
2016-06-19 00:08:21 +02:00
function syncTimeFromInternet()
2020-12-08 21:51:14 +01:00
if (syncRunning == nil) then
syncRunning=true
2016-06-19 00:08:21 +02:00
sntp.sync(sntpserverhostname,
function(sec,usec,server)
2020-12-08 21:51:14 +01:00
syncRunning=nil
2016-06-19 00:08:21 +02:00
end,
function()
2021-02-11 21:23:32 +01:00
print('NTP failed!')
2020-12-08 21:51:14 +01:00
syncRunning=nil
2016-06-19 00:08:21 +02:00
end
)
2020-12-08 21:51:14 +01:00
end
2016-06-19 00:08:21 +02:00
end
2020-12-07 21:26:02 +01:00
function displayTime()
2021-02-09 20:18:24 +01:00
collectgarbage()
2019-04-27 18:34:57 +02:00
local sec, usec = rtctime.get()
2016-12-30 14:16:19 +01:00
-- Handle lazy programmer:
if (timezoneoffset == nil) then
timezoneoffset=0
end
2022-01-23 18:30:46 +01:00
local tc = require("timecore_diet")
2021-02-11 21:23:32 +01:00
if (tc == nil) then
return
end
2021-02-11 21:23:32 +01:00
local time = tc.getTime(sec, timezoneoffset)
tc = nil
2022-01-23 18:30:46 +01:00
timecore_diet=nil
package.loaded["timecore_diet"]=nil
2021-02-11 21:23:32 +01:00
collectgarbage()
2022-01-23 18:30:46 +01:00
local wc = require("wordclock_diet")
2021-02-11 21:23:32 +01:00
if (wc ~= nil) then
2021-11-01 19:02:12 +01:00
words = wc.timestat(time.hour, time.minute)
if ((dim ~= nil) and (dim == "on")) then
2021-02-11 21:48:05 +01:00
words.briPer=briPer
if (words.briPer ~= nil and words.briPer < 3) then
words.briPer=3
end
2021-11-01 19:02:12 +01:00
else
2021-02-11 21:48:05 +01:00
words.briPer=nil
2021-11-01 19:02:12 +01:00
end
end
2021-02-11 21:23:32 +01:00
wc = nil
2022-01-23 18:30:46 +01:00
wordclock_diet=nil
package.loaded["wordclock_diet"]=nil
2021-02-11 21:23:32 +01:00
collectgarbage()
2021-02-11 21:33:06 +01:00
print("wc: " .. tostring(node.heap()))
2022-01-23 18:30:46 +01:00
local dw = require("displayword_diet")
2021-02-11 21:23:32 +01:00
if (dw ~= nil) then
2019-05-11 12:28:23 +02:00
--if lines 4 to 6 are inverted due to hardware-fuckup, unfuck it here
local invertRows=false
if ((inv46 ~= nil) and (inv46 == "on")) then
2019-05-11 12:28:23 +02:00
invertRows=true
end
2021-02-11 21:23:32 +01:00
local c = dw.countChars(words)
2021-03-19 19:55:15 +01:00
dw.generateLEDs(rgbBuffer, words, colorBg, color, color1, color2, color3, color4, invertRows, c)
2019-05-03 20:19:12 +02:00
end
if ( (tw ~= nil) and (tcol ~= nil) ) then
local c1 = dw.countChars(tw)
dw.generateLEDs(rgbBuffer, tw, nil, tcol, nil, nil, nil, nil, invertRows, c1)
2022-01-23 15:14:30 +01:00
print("Show number")
end
2021-02-11 21:23:32 +01:00
dw = nil
2022-01-23 18:30:46 +01:00
displayword_diet=nil
package.loaded["displayword_diet"]=nil
2021-02-11 21:33:06 +01:00
collectgarbage()
2021-03-21 15:31:05 +01:00
-- cleanup
2021-03-14 14:13:46 +01:00
i=nil
2021-02-10 20:04:22 +01:00
briPer=words.briPer
words=nil
time=nil
collectgarbage()
end
2016-06-19 00:30:09 +02:00
function normalOperation()
2016-06-19 22:48:44 +02:00
-- use default color, if nothing is defined
if (color == nil) then
-- Color is defined as GREEN, RED, BLUE
color=string.char(0,0,250)
end
2021-02-11 21:33:06 +01:00
print("start: " , node.heap())
2021-02-11 21:48:05 +01:00
-------------------------------------------------------------
-- Define the main loop
local setupCounter=5
local alive=0
2021-04-04 19:17:15 +02:00
mlt:register(1000, tmr.ALARM_AUTO, function (lt)
2021-02-11 21:48:05 +01:00
if (setupCounter > 4) then
2021-03-21 17:30:50 +01:00
if (colorBg ~= nil) then
rgbBuffer:fill(string.byte(colorBg,1), string.byte(colorBg,2), string.byte(colorBg,3)) -- disable all LEDs
else
rgbBuffer:fill(0,0,0) -- disable all LEDs
end
2021-02-11 21:48:05 +01:00
syncTimeFromInternet()
setupCounter=setupCounter-1
alive = 1
2021-03-21 15:31:05 +01:00
rgbBuffer:set(19, color) -- N
rgbBuffer:set(31, color) -- T
if ((inv46 ~= nil) and (inv46 == "on")) then
rgbBuffer:set(45, color) -- P
else
rgbBuffer:set(55, color) -- P
end
2021-02-11 21:48:05 +01:00
elseif (setupCounter > 3) then
-- Here the WLAN is found, and something is done
mydofile("mqtt")
2021-03-21 15:31:05 +01:00
rgbBuffer:fill(0,0,0) -- disable all LEDs
2021-02-11 21:48:05 +01:00
if (startMqttClient ~= nil) then
2021-03-21 15:31:05 +01:00
if ((inv46 ~= nil) and (inv46 == "on")) then
rgbBuffer:set(34, color) -- M
else
rgbBuffer:set(44, color) -- M
end
rgbBuffer:set(82, color) -- T
startMqttClient()
2021-02-11 21:48:05 +01:00
else
print("NO Mqtt found")
mydofile("telnet")
end
setupCounter=setupCounter-1
elseif (setupCounter > 2) then
if (startTelnetServer ~= nil) then
startTelnetServer()
else
displayTime()
end
setupCounter=setupCounter-1
2021-03-14 13:23:14 +01:00
elseif ( (alive % 120) == 0) then
2021-12-04 13:04:12 +01:00
-- sync the time every 5 minutes
local heapusage = node.heap()
if (heapusage > 12000) then
syncTimeFromInternet()
end
heapusage=nil
2021-03-21 15:31:05 +01:00
alive = alive + 1
2021-02-11 21:48:05 +01:00
else
displayTime()
alive = alive + 1
end
2021-03-21 15:31:05 +01:00
if (rgbBuffer ~= nil) then
2021-03-21 18:00:07 +01:00
-- show Mqtt status
if (startMqttClient ~= nil) then
if (not connectedMqtt()) then
2021-03-21 21:05:27 +01:00
rgbBuffer:set(103, 0, 64,0)
-- check every thirty seconds, if reconnecting is necessary
if (((tmr.now() / 1000000) % 100) == 30) then
print("MQTT reconnecting... ")
reConnectMqtt()
end
2021-03-21 18:00:07 +01:00
end
end
2021-03-21 15:31:05 +01:00
ws2812.write(rgbBuffer)
else
-- set FG to fix value: RED
local color = string.char(255,0,0)
rgbBuffer:fill(0,0,0) -- disable all LEDs
for i=108,110, 1 do rgbBuffer:set(i, color) end
ws2812.write(rgbBuffer)
print("Fallback no time displayed")
end
2021-12-04 13:04:12 +01:00
collectgarbage()
2021-02-11 21:48:05 +01:00
-- Feed the system watchdog.
tmr.wdclr()
end)
-------------------------------------------------------------
-- Connect to Wifi
2021-02-10 19:45:28 +01:00
local connect_counter=0
2016-06-19 00:30:09 +02:00
-- Wait to be connect to the WiFi access point.
2021-01-31 20:22:32 +01:00
local wifitimer = tmr.create()
2021-03-21 18:00:07 +01:00
wifitimer:register(500, tmr.ALARM_AUTO, function (timer)
2016-06-19 00:30:09 +02:00
connect_counter=connect_counter+1
if wifi.sta.status() ~= 5 then
2016-06-19 17:48:39 +02:00
print(connect_counter .. "/60 Connecting to AP...")
2021-03-14 16:52:53 +01:00
rgbBuffer:fill(0,0,0) -- clear all LEDs
2019-05-03 23:18:27 +02:00
if (connect_counter % 5 ~= 4) then
2017-03-01 19:53:55 +01:00
local wlanColor=string.char((connect_counter % 6)*20,(connect_counter % 5)*20,(connect_counter % 3)*20)
2019-05-03 23:18:27 +02:00
if ((connect_counter % 5) >= 1) then
2021-03-14 16:52:53 +01:00
rgbBuffer:set(7, wlanColor)
2019-05-03 23:18:27 +02:00
end
if ((connect_counter % 5) >= 3) then
2021-03-14 16:52:53 +01:00
rgbBuffer:set(15, wlanColor)
2019-05-03 23:18:27 +02:00
end
if ((connect_counter % 5) >= 2) then
2021-03-14 16:52:53 +01:00
rgbBuffer:set(16, wlanColor)
2019-05-03 23:18:27 +02:00
end
if ((connect_counter % 5) >= 0) then
2021-03-14 16:52:53 +01:00
rgbBuffer:set(17, wlanColor)
2019-05-03 23:18:27 +02:00
end
2016-06-19 00:30:09 +02:00
end
2021-03-14 14:13:46 +01:00
ws2812.write(rgbBuffer)
2016-06-19 00:30:09 +02:00
else
2021-03-18 21:38:10 +01:00
wifitimer:unregister()
2021-02-11 21:48:05 +01:00
wifitimer=nil
connect_counter=nil
2021-02-11 21:33:06 +01:00
print('IP: ',wifi.sta.getip(), " heap: ", node.heap())
2021-03-19 19:55:15 +01:00
rgbBuffer:fill(0,0,0) -- clear all LEDs
2021-03-20 15:49:54 +01:00
rgbBuffer:set(13, color) -- I
if ((inv46 ~= nil) and (inv46 == "on")) then
rgbBuffer:set(45, color) -- P
else
rgbBuffer:set(55, color) -- P
end
2021-03-19 19:55:15 +01:00
ws2812.write(rgbBuffer)
2021-02-13 14:17:58 +01:00
mlt:start()
2016-06-19 00:30:09 +02:00
end
end)
2021-01-31 20:22:32 +01:00
wifitimer:start()
2020-12-08 20:45:28 +01:00
end
-------------------main program -----------------------------
2021-02-10 20:04:22 +01:00
briPer = 50 -- Default brightness is set to 50%
ws2812.init() -- WS2812 LEDs initialized on GPIO2
2016-05-04 23:23:49 +02:00
2019-04-19 22:05:00 +02:00
----------- button ---------
gpio.mode(3, gpio.INPUT)
2021-01-31 20:22:32 +01:00
local btnCounter=0
-- Start the time Thread handling the button
local btntimer = tmr.create()
btntimer:register(5000, tmr.ALARM_AUTO, function (t)
2019-04-19 22:05:00 +02:00
if (gpio.read(3) == 0) then
2021-02-13 14:17:58 +01:00
mlt:unregister()
2019-04-19 22:05:00 +02:00
print("Button pressed " .. tostring(btnCounter))
btnCounter = btnCounter + 5
2021-03-14 16:52:53 +01:00
for i=1,btnCounter do rgbBuffer:set(i, 128, 0, 0) end
2021-03-14 14:13:46 +01:00
ws2812.write(rgbBuffer)
2019-04-19 22:05:00 +02:00
if (btnCounter >= 110) then
file.remove("config.lua")
2020-12-08 21:51:14 +01:00
file.remove("config.lc")
2019-04-19 22:05:00 +02:00
node.restart()
end
end
end)
2021-01-31 20:22:32 +01:00
btntimer:start()