Sha256: ff6eddc6298c35871738d8da358b56460b9ee023db4aabc5a5cc42730f7916af

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Wifly
  class Control
    include Calculations
    
    attr_accessor :connection

    ##
    # address    - the hostname or IP address 
    # port       - defaults to 2000
    # version    - the firmware version (a string)
    # connection - the object to use for talking to the wifly. Responds to "send_command."
    #              Defaults to Wifly::Connection. 
    #
    def initialize(address, port, version, connection=nil)
      self.connection   = connection || Connection.new(address, port, version)
    end

    ##
    # Toggle the blinking lights on the device
    #
    def lites
      connection.send_command("lites")
    end

    ##
    # set a pin high
    #
    def set_high(pin)
      hex = pin_to_hex(pin)
      connection.send_command "set sys output #{hex} #{hex}", AOK.length
    end

    ##
    # set a pin low
    #
    def set_low(pin)
      hex = pin_to_hex(pin)
      connection.send_command "set sys output 0x00 #{hex}", AOK.length
    end

    ##
    # given a pin number, return 1 if high or 0 if low
    #
    def read_pin(pin)
      high_pins.include?(pin) ? 1 : 0
    end

    ##
    # Return an array of all the pins which are currently high 
    #
    def high_pins
      parse_io(read_io)
    end

    private

    ##
    # Gets the status of all IO pins on wifly. Returns a hex string.
    #
    def read_io
      cmd = "show io\r"

      # wifly spits back something like 'show io\r\r\n8d08\r\n<2.32> '
      # crucially, the middle part "8d08\r\n" is always the same length
      str = connection.send_command("show io", "8d08\r\n".length)

      # Return only the middle part, which is the io state
      str.gsub(cmd,'').strip
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wifly-0.0.5 lib/wifly/control.rb