Skip to content

Instantly share code, notes, and snippets.

@simonbyrne
Created November 1, 2016 11:15
Show Gist options
  • Save simonbyrne/70f8c944ed7a76c95b1c90a964e9d7d1 to your computer and use it in GitHub Desktop.
Save simonbyrne/70f8c944ed7a76c95b1c90a964e9d7d1 to your computer and use it in GitHub Desktop.
# read from Raspbery Pi Sense HAT joystick
function _stick_input_dev()
try
for devname in readdir("/sys/class/input")
sysfname = joinpath("/sys/class/input",devname,"device","name")
if startswith(devname, "event") && isfile(sysfname)
if startswith(readstring(sysfname),"Raspberry Pi Sense HAT Joystick")
return joinpath("/dev/input",devname)
end
end
end
catch e
end
error("Sense Hat not found.")
end
const STICK_INPUT_DEV = _stick_input_dev()
const EV_KEY = Cushort(0x01)
const KEY_UP = Cushort(103)
const KEY_LEFT = Cushort(105)
const KEY_RIGHT = Cushort(106)
const KEY_DOWN = Cushort(108)
const KEY_ENTER = Cushort(28)
const STATE_RELEASE = Cint(0)
const STATE_PRESS = Cint(1)
const STATE_HOLD = Cint(2)
immutable Stick
tv_sec::Clong
tv_usec::Clong
ev_type::Cushort
ev_code::Cushort
ev_value::Cuint
end
Base.read(io::IO, ::Type{Stick}) = read(io, Ref{Stick}())[]
@async begin
dev = open(STICK_INPUT_DEV)
while true
s = read(dev, Stick)
if s.ev_type == EV_KEY
println(s)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment