Class: Neotrellis::Keypad
- Inherits:
-
Object
- Object
- Neotrellis::Keypad
- Defined in:
- lib/neotrellis/keypad.rb
Overview
Driver for the Neotrellis 4x4 keypad.
}
loop do
sleep(1)
puts "Processing pending events"
keypad.sync
end
puts event.edge == Neotrellis::Keypad::KEY_PRESSED ? “pressed” : “released” }
keypad.enable_interrupt(22)
keypad.wait_for_event
}
keypad.wait_for_event
puts "loop ended"
Defined Under Namespace
Classes: KeyEvent
Constant Summary collapse
- KEY_HIGH =
Key is pressed
0
- KEY_LOW =
Key is released
1
- KEY_RELEASED =
Key is falling edge
2
- KEY_PRESSED =
Key is rising edge
3
- KEY_BOTH =
Key is rising edge or falling edge
4
Instance Method Summary collapse
-
#count_events ⇒ Integer
Get the number of events (key pressed or released) waiting to be processed in the Seesaw buffer.
-
#disable_interrupt ⇒ Object
Disable interruption mode and release the GPIO pin.
-
#enable_interrupt(pin) ⇒ Object
Enable the interruption mode.
-
#initialize(seesaw, interrupt_pin: false, debug: false) ⇒ Keypad
constructor
Initialize the keypad driven by a Seesaw chip.
-
#interrupt? ⇒ Boolean
Tell if the interruption mode is enabled.
-
#resume ⇒ Object
Stop waiting for an event in the interruption mode.
-
#set_event(key, event: KEY_BOTH, enabled: true, &block) ⇒ Object
Register a callback to execute when a key’s event is processed.
-
#sync ⇒ Object
Trigger the callback for each event waiting to be processed.
-
#wait_for_event ⇒ Object
Wait for an interruption and process all pending event.
Constructor Details
#initialize(seesaw, interrupt_pin: false, debug: false) ⇒ Keypad
Initialize the keypad driven by a Seesaw chip.
82 83 84 85 86 87 88 |
# File 'lib/neotrellis/keypad.rb', line 82 def initialize(seesaw, interrupt_pin: false, debug: false) @seesaw = seesaw @debug = debug @callbacks = {} enable_interrupt(interrupt_pin) if interrupt_pin end |
Instance Method Details
#count_events ⇒ Integer
Get the number of events (key pressed or released) waiting to be processed in the Seesaw buffer.
93 94 95 96 97 98 99 |
# File 'lib/neotrellis/keypad.rb', line 93 def count_events begin @seesaw.read_byte(KEYPAD_BASE, KEYPAD_COUNT) rescue ReadError 0 end end |
#disable_interrupt ⇒ Object
Disable interruption mode and release the GPIO pin.
161 162 163 164 165 |
# File 'lib/neotrellis/keypad.rb', line 161 def disable_interrupt @interrupt_enabled=false @seesaw.write(KEYPAD_BASE, KEYPAD_INTENCLR, 0x01) @gpio.close end |
#enable_interrupt(pin) ⇒ Object
Enable the interruption mode. In this mode, the ‘sync()` method will be automatically called when an interruption is triggered by the Seesaw device. The INT ligne of the keypad need to be connected to this GPIO pin.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/neotrellis/keypad.rb', line 140 def enable_interrupt(pin) raise "pin must be an integer" unless pin.is_a? Integer @interrupt_enabled=true @seesaw.write(KEYPAD_BASE, KEYPAD_INTENSET, 0x01) @gpio = YaGPIO.new(pin, YaGPIO::INPUT) @gpio.set_interrupt(YaGPIO::EDGE_FALLING) do puts "DEBUG Interrupt received." if @debug sync end end |
#interrupt? ⇒ Boolean
Tell if the interruption mode is enabled.
156 157 158 |
# File 'lib/neotrellis/keypad.rb', line 156 def interrupt? @interrupt_enabled end |
#resume ⇒ Object
Stop waiting for an event in the interruption mode. This need to be run from a callback triggered by ‘wait_for_event()`.
177 178 179 |
# File 'lib/neotrellis/keypad.rb', line 177 def resume YaGPIO::resume end |
#set_event(key, event: KEY_BOTH, enabled: true, &block) ⇒ Object
Register a callback to execute when a key’s event is processed.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/neotrellis/keypad.rb', line 107 def set_event(key, event: KEY_BOTH, enabled: true, &block) if event == KEY_BOTH set_event(key, event: KEY_PRESSED, enabled: enabled, &block) set_event(key, event: KEY_RELEASED, enabled: enabled, &block) else raise "event must be one of KEY_PRESSED, KEY_RELEASED" unless [KEY_PRESSED, KEY_RELEASED].include? event raise "enabled must be a boolean" unless [true, false].include? enabled # Convert data to SeeSaw's binary registers key_b = (key/4)*8 + (key%4) edge_b = (1 << (event+1)) | ( enabled ? 1 : 0 ) @seesaw.write(KEYPAD_BASE, KEYPAD_EVENT, key_b, edge_b) @callbacks[KeyEvent.new(key, event)] = block end end |
#sync ⇒ Object
Trigger the callback for each event waiting to be processed. This method will be automatically called when the interruption mode is enabled.
126 127 128 129 130 131 132 133 |
# File 'lib/neotrellis/keypad.rb', line 126 def sync count = count_events() if count >0 read_events(count).each do |event| trigger_event(event) end end end |
#wait_for_event ⇒ Object
Wait for an interruption and process all pending event. The interruption mode must be enabled for this method to work. This is a blocking method. Use ‘resume()` inside a callback to stop waiting.
170 171 172 173 |
# File 'lib/neotrellis/keypad.rb', line 170 def wait_for_event raise "Interrupt is not enabled. Setup enable_interrupt() first" unless interrupt? YaGPIO::wait([@gpio]) end |