2011-01-09 11:00:50 +00:00
|
|
|
#! /usr/bin/env lua
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
2011-01-30 09:18:19 +00:00
|
|
|
spid.lua - Lua part of the Flukso daemon
|
2011-01-09 11:00:50 +00:00
|
|
|
|
|
|
|
Copyright (C) 2011 Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2011-01-21 19:31:18 +00:00
|
|
|
local dbg = require 'dbg'
|
|
|
|
local spi = require 'flukso.spi'
|
|
|
|
local nixio = require 'nixio'
|
|
|
|
nixio.fs = require 'nixio.fs'
|
2011-01-09 11:00:50 +00:00
|
|
|
|
2011-01-28 09:14:17 +00:00
|
|
|
local arg = arg or {} -- needed when this code is not loaded via the interpreter
|
|
|
|
|
2011-01-25 14:10:18 +00:00
|
|
|
local DEBUG = (arg[1] == '-d')
|
2011-01-25 09:32:47 +00:00
|
|
|
|
2011-01-21 19:31:18 +00:00
|
|
|
local SPI_DEV = '/dev/spidev0.0'
|
2011-02-12 10:47:31 +00:00
|
|
|
local SPI_MAX_CLK_SPEED_HZ = 1e6
|
2011-01-21 19:31:18 +00:00
|
|
|
local SPI_MIN_BYTE_DELAY_US = 250
|
2011-02-12 10:47:31 +00:00
|
|
|
local SPI_TX_RX_DELAY_NS = 1e7
|
|
|
|
local SPI_CT_DELAY_NS = 5e8
|
2011-01-21 19:31:18 +00:00
|
|
|
local POLL_TIMEOUT_MS = 100
|
|
|
|
|
2011-01-24 13:40:52 +00:00
|
|
|
local TIMERFD_ENABLE = 1
|
|
|
|
local TIMERFD_SEC = 1
|
|
|
|
local TIMERFD_NS = 0
|
|
|
|
|
|
|
|
local GET_DELTA = 'gd'
|
|
|
|
|
2011-01-30 09:18:19 +00:00
|
|
|
local DAEMON = os.getenv('DAEMON') or 'spid'
|
2011-01-21 19:31:18 +00:00
|
|
|
local DAEMON_PATH = os.getenv('DAEMON_PATH') or '/var/run/' .. DAEMON
|
|
|
|
|
|
|
|
local O_RDWR_NONBLOCK = nixio.open_flags('rdwr', 'nonblock')
|
|
|
|
local POLLIN = nixio.poll_flags('in')
|
2011-01-09 11:00:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
function mkfifos(input)
|
|
|
|
local path = string.format('%s/%s/', DAEMON_PATH, input)
|
|
|
|
|
|
|
|
nixio.fs.mkdirr(path)
|
2011-01-30 09:18:19 +00:00
|
|
|
-- nixio.fs.unlink(path .. 'in') -- clean up mess from previous run
|
|
|
|
-- nixio.fs.unlink(path .. 'out') -- idem
|
2011-01-09 11:00:50 +00:00
|
|
|
nixio.fs.mkfifo(path .. 'in', '644')
|
|
|
|
nixio.fs.mkfifo(path .. 'out', '644')
|
|
|
|
|
|
|
|
local fdin = nixio.open(path .. 'in', O_RDWR_NONBLOCK)
|
|
|
|
local fdout = nixio.open(path .. 'out', O_RDWR_NONBLOCK)
|
|
|
|
|
2011-01-21 19:31:18 +00:00
|
|
|
return { fd = fdin, -- need this entry for nixio.poll
|
2011-01-09 11:00:50 +00:00
|
|
|
fdin = fdin,
|
|
|
|
fdout = fdout,
|
|
|
|
events = POLLIN,
|
|
|
|
revents = 0,
|
|
|
|
line = fdin:linesource() }
|
|
|
|
end
|
|
|
|
|
|
|
|
local uart = mkfifos('uart')
|
|
|
|
local ctrl = mkfifos('ctrl')
|
|
|
|
local delta = mkfifos('delta')
|
|
|
|
|
2011-01-24 13:40:52 +00:00
|
|
|
if TIMERFD_ENABLE == 1 then
|
|
|
|
delta.fd = nixio.timerfd(TIMERFD_SEC, TIMERFD_NS, TIMERFD_SEC, TIMERFD_NS)
|
|
|
|
end
|
|
|
|
|
2011-01-09 11:00:50 +00:00
|
|
|
local fds = { uart, ctrl, delta }
|
|
|
|
|
2011-01-21 19:31:18 +00:00
|
|
|
local spidev = nixio.open(SPI_DEV, O_RDWR_NONBLOCK)
|
|
|
|
nixio.spi.setspeed(spidev, SPI_MAX_CLK_SPEED_HZ, SPI_MIN_BYTE_DELAY_US)
|
2011-02-12 14:27:01 +00:00
|
|
|
spidev:lock('lock') -- blocks until it can place a write lock on the spidev device
|
2011-01-09 11:00:50 +00:00
|
|
|
|
|
|
|
while true do
|
2011-01-21 19:31:18 +00:00
|
|
|
local msg
|
|
|
|
local poll = nixio.poll(fds, POLL_TIMEOUT_MS)
|
|
|
|
|
|
|
|
if poll == 0 then -- poll timed out, so time to 'poll' the spi bus
|
|
|
|
msg = spi.new_msg('', '')
|
|
|
|
elseif poll > 0 then
|
|
|
|
if ctrl.revents == POLLIN then
|
|
|
|
msg = spi.new_msg('ctrl', ctrl.line())
|
|
|
|
msg:parse()
|
|
|
|
|
|
|
|
elseif delta.revents == POLLIN then
|
2011-01-24 13:40:52 +00:00
|
|
|
if TIMERFD_ENABLE == 1 then
|
|
|
|
delta.fd:numexp() -- reset the numexp counter
|
|
|
|
else
|
|
|
|
delta.line()
|
|
|
|
end
|
|
|
|
|
|
|
|
msg = spi.new_msg('delta', GET_DELTA)
|
2011-01-09 11:00:50 +00:00
|
|
|
msg:parse()
|
2011-01-21 19:31:18 +00:00
|
|
|
|
|
|
|
elseif uart.revents == POLLIN then
|
|
|
|
msg = spi.new_msg('uart', uart.line())
|
|
|
|
end
|
|
|
|
|
|
|
|
msg:encode()
|
|
|
|
msg:tx(spidev)
|
2011-02-12 11:36:53 +00:00
|
|
|
msg:wait(SPI_TX_RX_DELAY_NS, SPI_CT_DELAY_NS)
|
2011-01-21 19:31:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if poll >= 0 then
|
|
|
|
msg:rx(spidev)
|
|
|
|
local dispatch = msg:decode()
|
2011-01-25 14:10:18 +00:00
|
|
|
if DEBUG then dbg.vardump(msg) end
|
2011-01-21 19:31:18 +00:00
|
|
|
|
|
|
|
if dispatch.ctrl then
|
|
|
|
ctrl.fdout:write(dispatch.ctrl .. '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
if dispatch.delta then
|
|
|
|
delta.fdout:write(dispatch.delta .. '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
if dispatch.uart then
|
|
|
|
uart.fdout:write(dispatch.uart .. '\n')
|
2011-01-09 11:00:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|