By registering loop functions, you can receive periodic callbacks from the OS.
on_online_loop() / on_offline_loop()
There are two types of loop functions: one called while the device is offline and another while it is online. The system automatically switches between these functions based on the current connection status.
-- Called approximately every 1 msec while online.
function on_online_loop()
-- os.log("online_loop");
end
-- Called approximately every 1 msec while offline.
-- Note: This function runs on a different thread;
-- please manage communication and data handling carefully.
function on_offline_loop()
-- os.log("offline_loop");
end
A typical use case is handling UART data: you might write logic to buffer incoming UART signals while offline, and then both buffer and transmit them once the device is online.
If you wish to execute the same logic regardless of the connection status, define a custom function (e.g., my_loop()) and call it from both loop handlers.
function my_loop()
-- Common logic here
end
function on_online_loop()
my_loop()
end
function on_offline_loop()
my_loop()
end
Note: The Online loop and Offline loop are called from different threads. Please ensure your implementation accounts for thread safety if necessary.