JuliaBerry / SenseHat.jl

Julia package for the Raspberry Pi Sense HAT
Other
15 stars 12 forks source link

Setting callback function for joystick #2

Open simonbyrne opened 7 years ago

simonbyrne commented 7 years ago

Currently, the recommended way to define a callback is via:

@schedule for e in sticktask()
    foo(e)
end
  1. Should we define a function to make this easier? e.g. setcallback(foo).
  2. If so, how should it behave when invoked multiple times? We could keep a global array of functions, but I worry about the performance overhead.

cc: @aviks

aviks commented 7 years ago

The way python does this is sense.stick.direction_up = my_move_up_fn, in that the framework does the work of despatching on event types. This is the kind of API pioneered in Ben's GPIOZero, which allows writing code like button.when_pressed = led.on which I find very easy to reason about.

simonbyrne commented 7 years ago

That is nice, we should be able to do something like that. Let me think about it a bit.

aviks commented 7 years ago

Cool. As I said elsewhere, this kind of thing probably leads to spaghetti code eventually, but it's good for teaching.