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
20
# 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
  @blind = false

  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



81
82
83
# File 'lib/traffic_light.rb', line 81

def all
  setColors(7)
end

#blind=(value) ⇒ Object



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

def blind=(value)
  @blind = value
end

#blind?Boolean

Returns:

  • (Boolean)


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

def blind?
  @blind
end

#clearObject



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

def clear
  setColors(0)
end

#close!Object



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

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

#greenObject



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

def green
  setColors(1)
end

#green_orangeObject



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

def green_orange
  setColors(3)
end

#green_redObject



73
74
75
# File 'lib/traffic_light.rb', line 73

def green_red
  setColors(5)
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  @opened
end

#orangeObject



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

def orange
  setColors(2)
end

#orange_redObject



77
78
79
# File 'lib/traffic_light.rb', line 77

def orange_red
  setColors(6)
end

#readObject



39
40
41
42
43
44
45
# File 'lib/traffic_light.rb', line 39

def read
  if blind?
    @message = ""
  else
    @message = @sp.read.chomp
  end
end

#redObject



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

def red
  setColors(4)
end

#setColors(colors) ⇒ Object



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

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