Wordclock/main.lua

193 lines
5.4 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)
print('sync', 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
2021-02-11 21:23:32 +01:00
mydofile("timecore")
if (tc == nil) then
return
end
2021-02-11 21:23:32 +01:00
local time = tc.getTime(sec, timezoneoffset)
tc = nil
collectgarbage()
mydofile("wordclock")
if (wc ~= nil) then
words = wc.timestat(time.hour, time.minute)
2021-02-11 21:48:05 +01:00
if ((dim ~= nil) and (dim == "on")) then
words.briPer=briPer
if (words.briPer ~= nil and words.briPer < 3) then
words.briPer=3
end
else
words.briPer=nil
end
2021-02-11 21:23:32 +01:00
end
wc = nil
collectgarbage()
2021-02-11 21:33:06 +01:00
print("wc: " .. tostring(node.heap()))
2021-02-09 19:58:59 +01:00
mydofile("displayword")
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-18 21:55:10 +01:00
ledBuf = dw.generateLEDs(rgbBuffer, words, colorBg, color, color1, color2, color3, color4, invertRows, c)
2019-05-03 20:19:12 +02:00
end
2021-02-11 21:23:32 +01:00
dw = nil
2021-02-11 21:33:06 +01:00
collectgarbage()
print("dw: " .. tostring(node.heap()))
2019-05-03 20:19:12 +02:00
if (ledBuf ~= nil) then
2019-05-11 12:28:23 +02:00
ws2812.write(ledBuf)
else
-- set FG to fix value: RED
local color = string.char(255,0,0)
2021-03-14 16:52:53 +01:00
rgbBuffer:fill(0,0,0) -- disable all LEDs
for i=108,110, 1 do rgbBuffer:set(i, color) end
2021-03-14 14:13:46 +01:00
ws2812.write(rgbBuffer)
end
-- 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-02-13 14:17:58 +01:00
mlt:register(2500, tmr.ALARM_AUTO, function (lt)
2021-02-11 21:48:05 +01:00
if (setupCounter > 4) then
syncTimeFromInternet()
setupCounter=setupCounter-1
alive = 1
elseif (setupCounter > 3) then
-- Here the WLAN is found, and something is done
mydofile("mqtt")
if (startMqttClient ~= nil) then
startMqttClient()
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-02-11 21:48:05 +01:00
-- sync the time every 5 minutes
syncTimeFromInternet()
alive = alive + 1
collectgarbage()
else
displayTime()
alive = alive + 1
end
-- 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-02-11 21:48:05 +01:00
wifitimer:register(2000, 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-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()