Sha256: aaa7ee6298d117977983d5b3c352c3bceedbc389e01464adb4a9d3b2803d0ac8
Contents?: true
Size: 1.97 KB
Versions: 2
Compression:
Stored size: 1.97 KB
Contents
begin require 'pi_piper' rescue LoadError require 'whipped-cream/pi_piper' end module WhippedCream # Actor that manages all interaction with a plugin class Runner def self.instance @instance end def self.create_instance(plugin) @instance = new(plugin) end attr_reader :plugin def initialize(plugin) @plugin = plugin configure end def name plugin.name end def pins @pins ||= {} end private def configure configure_buttons configure_sensors configure_switches end def configure_buttons plugin.buttons.each do |button| create_pin button, direction: :out define_singleton_method button.id do tap_pin(pins[button.id]) end end end def configure_sensors plugin.sensors.each do |sensor| if sensor.pin define_sensor_method_with_pin sensor else define_sensor_method_with_block sensor end end end def define_sensor_method_with_pin(sensor) create_pin sensor, direction: :in define_singleton_method sensor.id do pin = pins[sensor.id] pin.read == 1 ? sensor.high : sensor.low end end def define_sensor_method_with_block(sensor) define_singleton_method sensor.id do sensor.block.call end end def configure_switches plugin.switches.each do |switch| create_pin switch, direction: :out define_singleton_method switch.id do toggle_pin(pins[switch.id]) end end end def create_pin(control, options = {}) return unless control.pin options[:pin] = control.pin pins[control.id] = PiPiper::Pin.new(options) end def tap_pin(pin) pin.on Thread.new { sleep 0.25 pin.off } end def toggle_pin(pin) if pin.read.nonzero? pin.off else pin.on end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
whipped-cream-0.1.1 | lib/whipped-cream/runner.rb |
whipped-cream-0.1.0 | lib/whipped-cream/runner.rb |