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
# 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

  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



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

def all
  setColors(7)
end

#clearObject



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

def clear
  setColors(0)
end

#greenObject



33
34
35
# File 'lib/traffic_light.rb', line 33

def green
  setColors(1)
end

#green_orangeObject



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

def green_orange
  setColors(3)
end

#green_redObject



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

def green_red
  setColors(5)
end

#orangeObject



37
38
39
# File 'lib/traffic_light.rb', line 37

def orange
  setColors(2)
end

#orange_redObject



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

def orange_red
  setColors(6)
end

#readObject



19
20
21
# File 'lib/traffic_light.rb', line 19

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

#redObject



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

def red
  setColors(4)
end

#setColors(colors) ⇒ Object



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

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