Compare commits

...

6 Commits

Author SHA1 Message Date
e7867aabcc bugfix 2023-10-29 19:39:07 +01:00
7e02ed1e06 wait for go signal 2023-10-29 19:34:35 +01:00
1b86fe8a44 connect message and go signal 2023-10-29 19:34:09 +01:00
a37dd9f605 bugfix 2023-10-29 18:51:33 +01:00
59423aa1e1 reset timer and ping on timeout 2023-10-29 18:49:17 +01:00
e119fa5bde added timeout ping 2023-10-29 18:49:05 +01:00
3 changed files with 99 additions and 36 deletions

View File

@ -69,14 +69,16 @@ function send_info()
end
function event_listener()
while true do
local _, _, senderChannel, _, message, _ = os.pullEvent('modem_message')
local timer_id = os.startTimer(options.timeout/2)
while scan_components('inductionPort').success and (not system.timeout) do
local event, _, senderChannel, _, message, _ = os.pullEvent()
-- respond to corresponding message only
if senderChannel == system.channel and message == 'print' then
if event == 'modem_message' and senderChannel == system.channel and message == 'print' then
-- send message main display message
send_info()
end
reset_timer(options, timer_id)
elseif event == 'timer' then ping_receiver(options, system, timer_id) end
end
end
@ -87,9 +89,7 @@ function main()
-- reset terminal
term.clear()
-- run while all components are detected
while scan_components('inductionPort').success and (not system.timeout) do
event_listener()
end
event_listener()
-- rescan components
wait_for_components(options, system, 'inductionPort', 'Induction Matrix')
end

View File

@ -115,7 +115,7 @@ function init_modem(options, system, name)
local channel = math.random(2, 1 + options.channels)
-- open reply channel
system.modem.open(channel)
system.modem.transmit(1, channel, name)
system.modem.transmit(1, channel, 'connect\n' .. name)
term.setCursorPos(1, 6)
print('\nTry #' .. count)
@ -153,38 +153,59 @@ function init_modem(options, system, name)
system.modem.close(1)
end
function wait_to_start(options, system)
-- double timer because waiting on purpose
local timer_id = os.startTimer(options.timeout*2)
while true do
local event, _, senderChannel, _, message, _ = os.pullEvent()
-- wait for go signal
if event == 'modem_message' and senderChannel == system.channel and message == 'go' then break
elseif event == 'timer' then return false end
end
-- cancel timer
os.cancelTimer(timer_id)
return true
end
-- scan for components and retry on fail
function wait_for_components(options, system, component, name)
system.timeout = false
local count = 1
-- fixed print out
term.clear()
term.setCursorPos(1, 1)
print('Scanning for valid configuration:')
while true do
-- reset terminal
term.setCursorPos(1, 2)
-- output retry count
print('Try #' .. count .. '\n')
count = count + 1
system.timeout = false
local count = 1
-- fixed print out
term.clear()
term.setCursorPos(1, 1)
print('Scanning for valid configuration:')
while true do
-- reset terminal
term.setCursorPos(1, 2)
-- output retry count
print('Try #' .. count .. '\n')
count = count + 1
-- scan for components
local components = scan_components(component)
if components.success then
-- output success message
print('Success!')
print('Trying to connect to receiver . . .')
-- set system components
system.main = components.main
system.modem = components.modem
init_modem(options, system, name)
print('\nSuccess!')
return
-- scan for components
local components = scan_components(component)
if components.success then
-- output success message
print('Success!')
print('Trying to connect to receiver . . .')
-- set system components
system.main = components.main
system.modem = components.modem
init_modem(options, system, name)
print('\nSuccess!')
break
end
-- components not found, wait to retry
print('Failed!')
os.sleep(options.timeout)
end
-- components not found, wait to retry
print('Failed!')
os.sleep(options.timeout)
-- only continue if server says go
if wait_to_start(options, system) then break end
end
end
@ -219,4 +240,30 @@ function transmit_messages(options, system, buffer)
return
end
end
end
function reset_timer(options, id)
os.cancelTimer(id)
id = os.startTimer(options.timeout)
end
function ping_receiver(options, system, old_timer)
system.modem.transmit(system.channel, system.channel, 'ping')
local timer_id = os.startTimer(options.timeout/2)
while true do
local event, _, senderChannel, _, _, _ = os.pullEvent()
-- respond to any message => connection active
if event == 'modem_message' and senderChannel == system.channel then
-- cancel timer on success
os.cancelTimer(timer_id)
reset_timer(options, old_timer)
return
-- cancel on timeout
elseif event == 'timer' then
system.timeout = true
return
end
end
end

View File

@ -46,6 +46,15 @@ function channel_used(channel)
return false
end
-- split string by delimiter
function split_string(text, delimiter)
local result = {}
for match in (text .. delimiter):gmatch('(.-)' .. delimiter) do
table.insert(result, match)
end
return result
end
-- wait for components to connect to receiver
function wait_for_components()
-- reset system variables
@ -68,13 +77,14 @@ function wait_for_components()
local success = false
-- reply to channel 1 (initializer) only
if event == 'modem_message' and senderChannel == 1 then
local result = split_string(message, '\n')
-- component is trying to connect
if channel_used(replyChannel) then
system.modem.transmit(replyChannel, replyChannel, 'used')
else
elseif result[1] == 'connect' then
-- save component
table.insert(system.channels, replyChannel)
table.insert(system.components, message)
table.insert(system.components, result[2])
component = message
count = count + 1
@ -87,6 +97,7 @@ function wait_for_components()
system.modem.transmit(replyChannel, replyChannel, 'ready ' .. system.width)
-- open channel for communication
system.modem.open(replyChannel)
print(message)
print(component .. ' connected!')
end
end
@ -97,6 +108,11 @@ function wait_for_components()
-- close initializer channel
system.modem.close(1)
-- notify components
for _, value in pairs(system.channels) do
system.modem.transmit(value, value, 'go')
end
end
wait_for_components()