Module: NewFirmware

Defined in:
lib/sensors/basic_message_arduino_sensor.rb

Defined Under Namespace

Classes: Logger

Instance Method Summary collapse

Instance Method Details

#false_startObject


105
106
107
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 105

def false_start
  @t&&@t['false_start']
end

#finish_timesObject


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

def finish_times
  @t['finish_times'] || []
end

#handshakeObject


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 115

def handshake()
  stop
  @handshake ||= 0
  @handshake = @handshake <= 2**16-1 ? @handshake+1 : 0
  begin
  Timeout.timeout(1.0){
    @f.flush
    @f.puts "!a:#{@handshake}"
    log "ping: #{@handshake}"
    handshake_status = nil
    while !handshake_status do
      line = @f.readline
      handshake_status = line if line =~ /^A:#{@handshake}/
    end
    log "pong: #{handshake_status}"
  }
  rescue Timeout::Error
    log "the arduino is unplugged!"
    raise MissingArduinoError
  end
  true
end

#racersObject


93
94
95
96
97
98
99
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 93

def racers
  if @t
    @t['racers'] || [[],[],[],[]]
  else
   [[],[],[],[]]
  end
end

#send_countdownObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 22

def send_countdown
  begin
    Timeout.timeout(1.0){
      @f.flush
      @f.puts "!c:4"
      log "setting countdown"
      @countdown_status = nil
      while !@countdown_status do
        line = @f.readline
        @countdown_status = line if line =~ /^C:4/
      end
      log "countdown status: #{@countdown_status}"
    }
  rescue Timeout::Error
    log "Timeout setting countdown"
    raise ErrorSettingLength
  end
end

#send_length(ticks) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 2

def send_length(ticks)
  begin
    Timeout.timeout(1.0){
      @f.flush
      @f.puts "!l:#{ticks}"
      log "setting length"
      @length_status = nil
      while !@length_status do
        line = @f.readline
        @length_status = line if line =~ /^L:#{ticks}/
      end
      log "length status: #{@length_status}"
    }
  rescue Timeout::Error
    log "Timeout setting length"
    raise ErrorSettingLength
  end
  send_countdown
end

#startObject


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 41

def start
  @t.kill if @t
  @t = Thread.new(Thread.current) do |parent|
    @f.puts "!g"
    Thread.current["racers"] = [[],[],[],[]]
    Thread.current["finish_times"] = []
    Thread.current["false_start"] = nil
    @f.flush
    while true do
      l = @f.readline
      if l=~/:/
        if l =~ /0:/
          Thread.current["racers"][0] =  [0] * l.gsub(/0: /,'').to_i
        end
        if l =~ /1:/
          Thread.current["racers"][1] =  [1] * l.gsub(/1: /,'').to_i
        end
        if l =~ /2:/
          Thread.current["racers"][2] =  [2] * l.gsub(/2: /,'').to_i
        end
        if l =~ /3:/
          Thread.current["racers"][3] =  [3] * l.gsub(/3: /,'').to_i
        end
        if l =~ /0f:/
          Thread.current["finish_times"][0] = l.gsub(/0f: /,'').to_i
        end
        if l =~ /1f:/
          Thread.current["finish_times"][1] = l.gsub(/1f: /,'').to_i
        end
        if l =~ /2f:/
          Thread.current["finish_times"][2] = l.gsub(/2f: /,'').to_i
        end
        if l =~ /3f:/
          Thread.current["finish_times"][3] = l.gsub(/3f: /,'').to_i
        end
        if l =~ /t:/
          Thread.current["time"] = l.gsub(/t: /,'').to_i
        end
        if l =~ /F:/
          Thread.current["false_start"] = l.gsub(/F:/,'').to_i
        end
      end
      log l
    end
  end
  self
end

#stopObject


109
110
111
112
113
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 109

def stop
  @f.puts '!s'
  @f.flush
  @t.kill if @t
end

#timeObject


101
102
103
# File 'lib/sensors/basic_message_arduino_sensor.rb', line 101

def time
  @t['time'] || 0
end