Class: TrafficLight

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = "/dev/tty.usbmodemfd121") ⇒ TrafficLight

Returns a new instance of TrafficLight.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/traffic_light.rb', line 6

def initialize(port="/dev/tty.usbmodemfd121")
  @port_str = port
  @baud_rate = 9600
  @data_bits = 8
  @stop_bits = 1
  @parity = SerialPort::NONE

  @sp = SerialPort.new(@port_str, @baud_rate, @data_bits, @stop_bits, @parity)
  @sp.read_timeout = 1000

  @opened = true

  clear
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/traffic_light.rb', line 4

def message
  @message
end

Instance Method Details

#allObject



68
69
70
# File 'lib/traffic_light.rb', line 68

def all
  setColors(7)
end

#clearObject



40
41
42
# File 'lib/traffic_light.rb', line 40

def clear
  setColors(0)
end

#close!Object



25
26
27
28
# File 'lib/traffic_light.rb', line 25

def close!
  @sp.close
  @opened = false
end

#greenObject



44
45
46
# File 'lib/traffic_light.rb', line 44

def green
  setColors(1)
end

#green_orangeObject



52
53
54
# File 'lib/traffic_light.rb', line 52

def green_orange
  setColors(3)
end

#green_redObject



60
61
62
# File 'lib/traffic_light.rb', line 60

def green_red
  setColors(5)
end

#open?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/traffic_light.rb', line 21

def open?
  @opened
end

#orangeObject



48
49
50
# File 'lib/traffic_light.rb', line 48

def orange
  setColors(2)
end

#orange_redObject



64
65
66
# File 'lib/traffic_light.rb', line 64

def orange_red
  setColors(6)
end

#readObject



30
31
32
# File 'lib/traffic_light.rb', line 30

def read
  @message = @sp.read.chomp
end

#redObject



56
57
58
# File 'lib/traffic_light.rb', line 56

def red
  setColors(4)
end

#setColors(colors) ⇒ Object



34
35
36
37
38
# File 'lib/traffic_light.rb', line 34

def setColors(colors)
  read
  @sp.write colors
  read
end