class Smalrubot::Board

Constants

HIGH
READ_COMMANDS
WRITE_COMMANDS

Attributes

analog_zero[R]

Public Class Methods

new(io) click to toggle source
# File lib/smalrubot/board.rb, line 6
def initialize(io)
  @io = io
  @mutex = Mutex.new
  handshake
end

Public Instance Methods

handshake() click to toggle source
# File lib/smalrubot/board.rb, line 12
def handshake
  @mutex.synchronize do
    @analog_zero = @io.handshake
  end
end
normalize_cmd(cmd) click to toggle source
# File lib/smalrubot/board.rb, line 111
def normalize_cmd(cmd)
  raise Exception.new('commands can only be two digits') if cmd.to_s.length > 2
  normalize(cmd, 2)
end
normalize_pin(pin) click to toggle source
# File lib/smalrubot/board.rb, line 101
def normalize_pin(pin)
  if pin.to_s.match /\Aa/i
    int_pin = @analog_zero + pin.to_s.gsub(/\Aa/i, '').to_i
  else
    int_pin = pin
  end
  raise Exception.new('pin number must be in 0-99') if int_pin.to_i > 99
  return normalize(int_pin, 2)
end
normalize_value(value) click to toggle source
# File lib/smalrubot/board.rb, line 116
def normalize_value(value)
  raise Exception.new('values are limited to three digits') if value.to_s.length > 3
  normalize(value, 3)
end
read() click to toggle source
# File lib/smalrubot/board.rb, line 23
def read
  @io.read(1)
end
set_pin_mode(pin, mode, pullup=nil) click to toggle source
# File lib/smalrubot/board.rb, line 27
def set_pin_mode(pin, mode, pullup=nil)
  pin, value = normalize_pin(pin), normalize_value(mode == :out ? 0 : 1)
  write("00#{pin}#{value}")
  set_pullup(pin, pullup) if mode == :in
end
set_pullup(pin, pullup) click to toggle source
# File lib/smalrubot/board.rb, line 33
def set_pullup(pin, pullup)
  pullup ? digital_write(pin, HIGH) : digital_write(pin, LOW)
end
write(msg, opts = {}) click to toggle source
# File lib/smalrubot/board.rb, line 18
def write(msg, opts = {})
  formatted_msg = opts.delete(:no_wrap) ? msg : "!#{msg}."
  @io.write(formatted_msg)
end

Private Instance Methods

normalize(pin, spaces) click to toggle source
# File lib/smalrubot/board.rb, line 123
def normalize(pin, spaces)
  pin.to_s.rjust(spaces, '0')
end