Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
1d8485fe84 | |||
40eb131fce | |||
457a67af44 | |||
ac918ad47e | |||
b66b0c751a | |||
f3feb3e336 | |||
b280a31dfd | |||
b1f92ea8b2 | |||
c8eb38671e |
|
@ -55,7 +55,7 @@ function format_percent(options, number, mult)
|
|||
percent = number * 100
|
||||
end
|
||||
|
||||
return string.format('%.2f ', percent) .. '% ' .. string.rep(' ', #(options.unit or ''))
|
||||
return string.format('%.2f ', percent) .. '% ' .. string.rep(' ', #(options.unit or ' '))
|
||||
end
|
||||
|
||||
-- format temperature as number with two decimals and padded unit
|
||||
|
@ -67,7 +67,7 @@ function format_temp(options, number)
|
|||
else temperature = string.format('%.2f ', temperature) end
|
||||
|
||||
-- build string
|
||||
return temperature .. 'K ' .. string.rep(' ', #(options.unit or ''))
|
||||
return temperature .. 'K ' .. string.rep(' ', #(options.unit or ' '))
|
||||
end
|
||||
|
||||
-- format liquids as compact number and padded unit
|
||||
|
@ -260,14 +260,49 @@ function respond_print(options, name)
|
|||
if #buffer > 0 then transmit_messages(options, buffer) end
|
||||
end
|
||||
|
||||
function event_listener(options)
|
||||
while true do
|
||||
local _, _, senderChannel, _, message, _ = os.pullEvent('modem_message')
|
||||
function reset_timer(id, time)
|
||||
os.cancelTimer(id)
|
||||
return os.startTimer(time)
|
||||
end
|
||||
|
||||
function ping_status(options)
|
||||
system.modem.transmit(system.channel, system.channel, 'ping')
|
||||
print('Sent ping...')
|
||||
|
||||
local timer_id = os.startTimer(options.timeout/2)
|
||||
while not system.timeout do
|
||||
local event, _, senderChannel, _, _, _ = os.pullEvent()
|
||||
|
||||
-- respond to corresponding message only
|
||||
if senderChannel == system.channel then
|
||||
if event == 'modem_message' and senderChannel == system.channel then
|
||||
print('Received response!')
|
||||
os.cancelTimer(timer_id)
|
||||
return
|
||||
elseif event == 'timer' then
|
||||
print('Reconnecting...')
|
||||
system.timeout = true
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function event_listener(options)
|
||||
local timer_id = os.startTimer(options.timeout/2)
|
||||
while not system.timeout do
|
||||
local event, _, senderChannel, _, message, _ = os.pullEvent()
|
||||
|
||||
-- respond to corresponding message only
|
||||
if event == 'modem_message' and senderChannel == system.channel then
|
||||
-- send main display message
|
||||
timer_id = reset_timer(timer_id, options.timeout/2)
|
||||
respond_print(options, message)
|
||||
elseif event == 'timer' then
|
||||
-- send ping for server status
|
||||
ping_status(options)
|
||||
-- reset timer if response
|
||||
if not system.timeout then
|
||||
timer_id = reset_timer(timer_id, options.timeout/2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -67,18 +67,24 @@ function wait_for_components()
|
|||
local component = ''
|
||||
local success = false
|
||||
-- reply to channel 1 (initializer) only
|
||||
if event == 'modem_message' and senderChannel == 1 then
|
||||
if event == 'modem_message' then
|
||||
print(message)
|
||||
-- component is trying to connect
|
||||
if channel_used(replyChannel) then
|
||||
system.modem.transmit(replyChannel, replyChannel, 'used')
|
||||
else
|
||||
-- save component
|
||||
table.insert(system.channels, replyChannel)
|
||||
table.insert(system.components, message)
|
||||
if senderChannel == 1 then
|
||||
if channel_used(replyChannel) then
|
||||
system.modem.transmit(replyChannel, replyChannel, 'used')
|
||||
else
|
||||
-- save component
|
||||
table.insert(system.channels, replyChannel)
|
||||
table.insert(system.components, message)
|
||||
|
||||
component = message
|
||||
count = count + 1
|
||||
success = true
|
||||
component = message
|
||||
count = count + 1
|
||||
success = true
|
||||
end
|
||||
-- component asking for status
|
||||
elseif message == 'ping' and channel_used(senderChannel) then
|
||||
system.modem.transmit(replyChannel, replyChannel, 'pong')
|
||||
end
|
||||
elseif event == 'monitor_touch' and count > 0 then break end
|
||||
|
||||
|
@ -130,15 +136,18 @@ function get_messages(buffer)
|
|||
local touchExit = false
|
||||
while true do
|
||||
-- get next pull event
|
||||
local event, _, senderChannel, _, message, _ = os.pullEvent()
|
||||
local event, _, senderChannel, replyChannel, message, _ = os.pullEvent()
|
||||
if event == 'modem_message' then
|
||||
-- save message in buffer
|
||||
if senderChannel == channel then
|
||||
table.insert(buffer, message)
|
||||
system.modem.transmit(channel, channel, 'ok')
|
||||
break
|
||||
-- dont accept messages from wrong channel
|
||||
else system.modem.transmit(senderChannel, senderChannel, 'busy') end
|
||||
-- invalid channel
|
||||
elseif not channel_used(senderChannel) then
|
||||
system.modem.transmit(replyChannel, replyChannel, 'unregistered')
|
||||
-- keep busy otherwise
|
||||
else system.modem.transmit(replyChannel, replyChannel, 'busy') end
|
||||
elseif event == 'timer' then
|
||||
system.timeout = true
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue
Block a user