Skip to content

Instantly share code, notes, and snippets.

@jsnyder
Created September 6, 2012 18:48
Show Gist options
  • Save jsnyder/3659392 to your computer and use it in GitHub Desktop.
Save jsnyder/3659392 to your computer and use it in GitHub Desktop.
example for writing file in looping luarpc
stm32.enc.init(3)
stm32.enc.setidxtrig(cpu.INT_GPIO_POSEDGE, pio.PB_5, 3, 32768)
local state = {}
local stime
cycletime = 20000
tdiff = 0
function control()
end
iter = 1
handle = rpc.listen(0,0)
stime = tmr.start(tmr.SYS_TIMER)
while true do
-- Check for pending RPC request
if rpc.peek( handle ) then
rpc.dispatch( handle )
end
control()
tdiff = tmr.gettimediff( tmr.SYS_TIMER, stime, tmr.read(tmr.SYS_TIMER) )
tmr.delay( 1, cycletime - (tmr.read(tmr.SYS_TIMER) % cycletime ) )
stime = tmr.read(tmr.SYS_TIMER)
iter = iter+1
end
function error_handler (message)
io.write ("MY ERROR: " .. message .. "\n");
end
rpc.on_error (error_handler);
slave,err = rpc.connect ("/dev/tty.usbserial-FTDXSDBS");
state_init = {fd = nil, fname='/mmc/testfile4.txt', iter = 1}
function control_open_write()
if state.fd == nil then
state.fd = io.open(state.fname, "w+")
else
state.fd:write(string.format("%d, %d\n", state.iter, tdiff))
state.fd:flush()
end
state.iter = state.iter+1
end
function control_close()
if state.fd ~= nil then
state.fd:flush()
state.fd:close()
state.fd = nil
end
end
slave.state = state_init
slave.control = control_open_write
-- wait for some time to pass, then close with
slave.control = control_close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment