Class: EventHandler
- Inherits:
-
Object
- Object
- EventHandler
- Defined in:
- lib/trixter/handler.rb
Overview
This handler, by default, does no more than log
Direct Known Subclasses
Instance Method Summary collapse
- #crankPositionChanged(event) ⇒ Object
- #handleEvent(event) ⇒ Object
-
#initialize ⇒ EventHandler
constructor
A new instance of EventHandler.
- #leftBrakeChanged(strength) ⇒ Object
- #leftControlDown ⇒ Object
- #leftControlLeft ⇒ Object
- #leftControlRight ⇒ Object
- #leftControlUp ⇒ Object
- #leftGearDown ⇒ Object
- #leftGearUp ⇒ Object
- #rightBrakeChanged(strength) ⇒ Object
- #rightControlLeft ⇒ Object
- #rightControlRight ⇒ Object
- #rightControlUp ⇒ Object
- #rightGearDown ⇒ Object
- #rightGearUp ⇒ Object
- #seatingChanged(seated) ⇒ Object
Constructor Details
#initialize ⇒ EventHandler
Returns a new instance of EventHandler.
3 4 5 6 |
# File 'lib/trixter/handler.rb', line 3 def initialize @current_event = nil @crank_changes = [] end |
Instance Method Details
#crankPositionChanged(event) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/trixter/handler.rb', line 77 def crankPositionChanged(event) if @last_crank_change == nil @last_crank_change = event.time return end forward = false ticks = 0 past_position = @current_event.crankPosition.hex current_position = event.crankPosition.hex if past_position < current_position ticks = current_position - past_position if ticks < 30 forward = true else ticks = 60 - ticks end else ticks = past_position - current_position if ticks > 30 ticks = 60 - ticks forward = true end end change = CrankChange.new(ticks, event.time, event.time - @last_crank_change) @crank_changes << change to_delete = [] while (event.time - @crank_changes.first.time) > 0.4 @crank_changes.delete_at(0) end smoothed_ticks = 0 for change in @crank_changes smoothed_ticks += change.ticks end first_change = @crank_changes.first if event.time - first_change.time == 0 puts "Crank RPM: 0" else rpms = (smoothed_ticks/60.0) * (60.0/(event.time - first_change.time)) puts "Crank RPM: #{rpms}" end @last_crank_change = event.time end |
#handleEvent(event) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 |
# File 'lib/trixter/handler.rb', line 8 def handleEvent(event) # We throw out initial state if @current_event == nil @current_event = event return end # Process boolean-on events (we only care when they are 'on') if event.leftGearUp and not @current_event.leftGearUp leftGearUp end if event.leftGearDown and not @current_event.leftGearDown leftGearDown end if event.rightGearUp and not @current_event.rightGearUp rightGearUp end if event.rightGearDown and not @current_event.rightGearDown rightGearDown end if event.leftControlUp and not @current_event.leftControlUp leftControlUp end if event.leftControlDown and not @current_event.leftControlDown leftControlDown end if event.leftControlLeft and not @current_event.leftControlLeft leftControlLeft end if event.leftControlRight and not @current_event.leftControlRight leftControlRight end if event.rightControlLeft and not @current_event.rightControlLeft rightControlLeft end if event.rightControlRight and not @current_event.rightControlRight rightControlRight end if event.rightControlUp and not @current_event.rightControlUp rightControlUp end # Process scaled events if event.leftBrakePressure != @current_event.leftBrakePressure leftBrakeChanged(event.leftBrakePressure) end if event.rightBrakePressure != @current_event.rightBrakePressure rightBrakeChanged(event.rightBrakePressure) end if event.crankPosition != @current_event.crankPosition crankPositionChanged(event) end # Seated or not if event.seated != @current_event.seated seatingChanged(event.seated) end @current_event = event end |
#leftBrakeChanged(strength) ⇒ Object
126 127 128 |
# File 'lib/trixter/handler.rb', line 126 def leftBrakeChanged(strength) puts "Left brake engaged at #{strength}" end |
#leftControlDown ⇒ Object
162 163 164 |
# File 'lib/trixter/handler.rb', line 162 def leftControlDown puts "Left control down pushed" end |
#leftControlLeft ⇒ Object
150 151 152 |
# File 'lib/trixter/handler.rb', line 150 def leftControlLeft puts "Left control left pushed" end |
#leftControlRight ⇒ Object
154 155 156 |
# File 'lib/trixter/handler.rb', line 154 def leftControlRight puts "Left control right pushed" end |
#leftControlUp ⇒ Object
158 159 160 |
# File 'lib/trixter/handler.rb', line 158 def leftControlUp puts "Left control up pushed" end |
#leftGearDown ⇒ Object
138 139 140 |
# File 'lib/trixter/handler.rb', line 138 def leftGearDown puts "Left gear down!" end |
#leftGearUp ⇒ Object
134 135 136 |
# File 'lib/trixter/handler.rb', line 134 def leftGearUp puts "Left gear up!" end |
#rightBrakeChanged(strength) ⇒ Object
130 131 132 |
# File 'lib/trixter/handler.rb', line 130 def rightBrakeChanged(strength) puts "Right brake engaged at #{strength}" end |
#rightControlLeft ⇒ Object
166 167 168 |
# File 'lib/trixter/handler.rb', line 166 def rightControlLeft puts "Right control left pushed" end |
#rightControlRight ⇒ Object
170 171 172 |
# File 'lib/trixter/handler.rb', line 170 def rightControlRight puts "Right control right pushed" end |
#rightControlUp ⇒ Object
174 175 176 |
# File 'lib/trixter/handler.rb', line 174 def rightControlUp puts "Right control up pushed" end |
#rightGearDown ⇒ Object
146 147 148 |
# File 'lib/trixter/handler.rb', line 146 def rightGearDown puts "Right gear down!" end |
#rightGearUp ⇒ Object
142 143 144 |
# File 'lib/trixter/handler.rb', line 142 def rightGearUp puts "Right gear up!" end |
#seatingChanged(seated) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/trixter/handler.rb', line 69 def seatingChanged(seated) if seated puts "Rider is seated" else puts "Rider is standing" end end |