-
-
Save creationix/8af27d5ab5535b01f40b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Bit = require('bit') | |
local FS = require('fs') | |
local Emitter = require('emitter') | |
local Buffer = require('buffer') | |
-- http://www.mjmwired.net/kernel/Documentation/input/joystick-api.txt | |
function parse(buffer) | |
p("parse", buffer) | |
local event = { | |
time = buffer:readUInt32LE(1), | |
number = buffer:readUInt8(8), | |
value = buffer:readUInt16LE(5), | |
} | |
local type = buffer:readUInt8(7) | |
if Bit.band(type, 0x80) > 0 then event.init = true end | |
if Bit.band(type, 0x01) > 0 then event.type = "button" end | |
if Bit.band(type, 0x02) > 0 then event.type = "axis" end | |
return event | |
end | |
-- Expose as a nice Lua API | |
local Joystick = Emitter:extend() | |
function Joystick.prototype:initialize(id) | |
self:wrap("on_open") | |
self:wrap("on_read") | |
self.id = id | |
FS.open("/dev/input/js" .. id, "r", "0644", self.on_open) | |
end | |
function Joystick.prototype:on_open(fd) | |
self.fd = fd | |
self:start_read() | |
end | |
function Joystick.prototype:start_read() | |
p("start_read") | |
FS.read(self.fd, nil, 8, self.on_read) | |
end | |
function Joystick.prototype:on_read(chunk) | |
local event = parse(Buffer:new(chunk)) | |
event.id = self.id | |
self:emit(event.type, event) | |
if self.fd then self:start_read() end | |
end | |
function Joystick.prototype:close(callback) | |
local fd = self.fd | |
self.fd = nil | |
FS.close(fd, callback) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment