Skip to content

Instantly share code, notes, and snippets.

@nixterrimus
Last active December 11, 2015 05:48
Show Gist options
  • Save nixterrimus/4554154 to your computer and use it in GitHub Desktop.
Save nixterrimus/4554154 to your computer and use it in GitHub Desktop.
A spirit Arduino blinky adapter
require 'dino'
class ArduinoBlinky
attr_accessor :led, :current_state
# implements :light
def initialize
board = Dino::Board.new(Dino::TxRx.new)
@led = Dino::Components::Led.new(pin: 13, board: board)
set_default_state
end
def set_current_state(params = {})
on if params[:binary_state] == :on
off if params[:binary_state] == :on
end
# the Arduino adapter doesn't support reading state from the device
# so the adapter assumes that it's the only one talking to arduino
# and it always knows the real state of the device, in other devices
# this method might perform an HTTP to ask the device what state it's
# currently in
def current_state
{
binary_state: current_state
}
end
private
def set_default_state
off
current_state = :off
end
def on
@led.send(:on)
end
def off
@led.send(:off)
end
end
@nixterrimus
Copy link
Author

This is still kind of leaky because it relies on the adapter understanding that the key for on/off is stored as binary_state. Feedback?

@parkr
Copy link

parkr commented Jan 22, 2013

Shouldn't ::initialize accept a pin param? (In the future, perhaps.)

Shouldn't #on and #off handle setting current_state?

If you set a convention (using :binary_state to represent the state of a binary thingy), then you should be fine. The key is just to be consistent. You could also specify a default key for state for each class: ArduinoBlinky::STATE_KEY = :binary_state or something. That way the adapters could just use that when fetching different states. Could expand for RGB stuff / dimmable stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment