Class: Trixter

Inherits:
EventHandler show all
Defined in:
lib/trixter/trixter.rb

Instance Method Summary collapse

Methods inherited from EventHandler

#handleEvent, #leftControlDown, #leftControlLeft, #leftControlRight, #leftControlUp, #leftGearDown, #leftGearUp, #rightControlLeft, #rightControlRight, #rightControlUp, #seatingChanged

Constructor Details

#initialize(port) ⇒ Trixter

Returns a new instance of Trixter.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/trixter/trixter.rb', line 9

def initialize(port)
  super()

  @port = SerialPort.new(port, 115200, 8, 1)
  @status_thread = nil
  @control_thread = nil
  @current_difficulty = 0
  @saved_difficulty = 0
  @right_brake = false
  @left_brake = false

  @should_run = false
end

Instance Method Details

#applyBrake(pressure) ⇒ Object



100
101
102
103
104
105
# File 'lib/trixter/trixter.rb', line 100

def applyBrake(pressure)
  span = Difficulties.size - @saved_difficulty - 1
  units = 100.0/span
  new_difficulty = (pressure/units).to_i + @saved_difficulty
  @current_difficulty = [new_difficulty, Difficulties.size - 1].min
end

#controlObject



129
130
131
132
133
134
# File 'lib/trixter/trixter.rb', line 129

def control
  while @should_run
    sleep 0.01
    @port.write([Difficulties[@current_difficulty]].pack("H*"))
  end
end

#crankPositionChanged(event) ⇒ Object



107
108
109
# File 'lib/trixter/trixter.rb', line 107

def crankPositionChanged(event)
  # pass
end

#joinObject



136
137
138
139
140
141
142
143
# File 'lib/trixter/trixter.rb', line 136

def join
  if @status_thread != nil
    @status_thread.join
  end
  if @control_thread != nil
    @control_thread.join
  end
end

#leftBrakeChanged(strength) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/trixter/trixter.rb', line 56

def leftBrakeChanged(strength)
  pressure = 240 - strength.hex
  if pressure > 10
    if !@left_brake and !@right_brake
      puts "Left brake engaged"
      @saved_difficulty = @current_difficulty
    end

    @left_brake = true
    applyBrake(pressure)
  else
    if @left_brake
      puts "Left brake disengaged"
      # restore selected difficulty
      @left_brake = false
      if !@right_brake
        @current_difficulty = @saved_difficulty
      end
    end
  end
end

#rightBrakeChanged(strength) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/trixter/trixter.rb', line 78

def rightBrakeChanged(strength)
  pressure = 240 - strength.hex
  if pressure > 10
    if !@left_brake and !@right_brake
      puts "Right brake engaged"
      @saved_difficulty = @current_difficulty
    end

    @right_brake = true
    applyBrake(pressure)
  else
    if @right_brake
      puts "Right brake disengaged"
      # restore selected difficulty
      @right_brake = false
      if !@left_brake
        @current_difficulty = @saved_difficulty
      end
    end
  end
end

#rightGearDownObject



48
49
50
51
52
53
54
# File 'lib/trixter/trixter.rb', line 48

def rightGearDown
  @current_difficulty -= 1
  if @current_difficulty < 0
    @current_difficulty = 0
  end
  puts "Decreased difficulty to level #{@current_difficulty}"
end

#rightGearUpObject



40
41
42
43
44
45
46
# File 'lib/trixter/trixter.rb', line 40

def rightGearUp
  @current_difficulty += 1
  if @current_difficulty >= Difficulties.size
    @current_difficulty = Difficulties.size - 1
  end
  puts "Increased difficulty to level #{@current_difficulty}"
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trixter/trixter.rb', line 23

def run
  return if @should_run or @status_thread != nil
  return if @should_run or @control_thread != nil

  @should_run = true
  @status_thread = Thread.new do
    status
  end
  @control_thread = Thread.new do
    control
  end
end

#statusObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/trixter/trixter.rb', line 111

def status
  while @should_run
    raw_event = ""
    while true
      raw_event << @port.read(1).strip
      if raw_event.size == 1
        raw_event = "" unless raw_event[0] == '6'
      elsif raw_event.size == 2
        raw_event == "" unless raw_event[1] == 'a'
      elsif raw_event.size > 2
        raw_event == "" unless raw_event[0..1] == '6a'
      end
      break if raw_event.size == 32
    end
    handleEvent(Event.new(raw_event))
  end
end

#stopObject



36
37
38
# File 'lib/trixter/trixter.rb', line 36

def stop
  @should_run = false
end