Class: Switchy

Inherits:
Object
  • Object
show all
Defined in:
lib/switchy.rb

Defined Under Namespace

Modules: PINS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modem = '/dev/tty.usbmodem12341', baud = 38400) ⇒ Switchy

Returns a new instance of Switchy.



92
93
94
95
96
97
# File 'lib/switchy.rb', line 92

def initialize(modem = '/dev/tty.usbmodem12341', baud=38400)
  @borked = !File.exists?(modem)
  puts "**** Could not find Switchy device on #{modem}" if borked?
  @modem, @baud = modem, baud
  connect
end

Instance Attribute Details

#baudObject

Returns the value of attribute baud.



88
89
90
# File 'lib/switchy.rb', line 88

def baud
  @baud
end

#borkedObject

true when the device was not found at startup. Makes switchy-inclusive apps noncrashy when they find themselves unexpectedly switchy-excluded



89
90
91
# File 'lib/switchy.rb', line 89

def borked
  @borked
end

#modemObject

Returns the value of attribute modem.



88
89
90
# File 'lib/switchy.rb', line 88

def modem
  @modem
end

Instance Method Details

#borked?Boolean

Returns:

  • (Boolean)


90
# File 'lib/switchy.rb', line 90

def borked?; @borked; end

#connectObject



99
100
101
102
# File 'lib/switchy.rb', line 99

def connect
  return if borked?
  @sp = SerialPort.new @modem, @baud
end

#disconnectObject



104
105
106
107
# File 'lib/switchy.rb', line 104

def disconnect
  return if borked?
  # ???
end

#light1=(v) ⇒ Object



122
123
124
125
# File 'lib/switchy.rb', line 122

def light1=(v)
  return if borked?
  set_light 1, v
end

#light2=(v) ⇒ Object



127
128
129
130
# File 'lib/switchy.rb', line 127

def light2=(v)
  return if borked?
  set_light 2, v
end

#light3=(v) ⇒ Object



132
133
134
135
# File 'lib/switchy.rb', line 132

def light3=(v)
  return if borked?
  set_light 3, v
end

#light4=(v) ⇒ Object



137
138
139
140
# File 'lib/switchy.rb', line 137

def light4=(v)
  return if borked?
  set_light 4, v
end

#set_light(l, v) ⇒ Object



116
117
118
119
120
# File 'lib/switchy.rb', line 116

def set_light(l, v)
  return if borked?
  cmd = "C#{l+3}=#{v}\r\n"
  @sp.write cmd
end

#set_pin(p, v) ⇒ Object

Set pin “b4”, 1 # turn on pin b4



110
111
112
113
114
# File 'lib/switchy.rb', line 110

def set_pin(p, v)
  return if borked?
  cmd = "#{p.upcase}=#{v}\r\n"
  @sp.write cmd
end