Class: Neotrellis::Keypad

Inherits:
Object
  • Object
show all
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"

Examples:

Print a message when key #3 is pressed

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E)
keypad = Neotrellis::Keypad.new(seesaw)
keypad.set_event(2, event: Neotrellis::Keypad::KEY_PRESSED) { |event|
	puts "Key #{event.key} pressed"

Print a message when key #3 is released using interruption on GPIO pin 22

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E)
keypad = Neotrellis::Keypad.new(seesaw)
keypad.set_event(2, event: Neotrellis::Keypad::KEY_RELEASED) { |event|
	puts "Key #{event.key}"

Stop waiting for events when key #4 is pressed

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E)
keypad = Neotrellis::Keypad.new(seesaw, interrupt_pin: 22)
keypad.set_event(3, event: Neotrellis::Keypad::KEY_PRESSED) {
	keypad.resume

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

Constructor Details

#initialize(seesaw, interrupt_pin: false, debug: false) ⇒ Keypad

Initialize the keypad driven by a Seesaw chip.

Parameters:

  • seesaw (Neotrellis::SeeSaw)

    Seesaw driver

  • interrupt_pin (Integer) (defaults to: false)

    GPIO pin used by the interruption handler. If false, the interruption mode will be disabled.

  • debug (Boolean) (defaults to: false)

    Enable debug ouput on stdout



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_eventsInteger

Get the number of events (key pressed or released) waiting to be processed in the Seesaw buffer.

Returns:

  • (Integer)

    Number of events



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_interruptObject

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.

Parameters:

  • pin (Integer)

    GPIO pin to configure for interruption on. Pin number is in the BCM numbering, as reported by Sysfs.



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.

Returns:

  • (Boolean)

    True if interruption mode is enabled.



156
157
158
# File 'lib/neotrellis/keypad.rb', line 156

def interrupt?
	@interrupt_enabled
end

#resumeObject

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.

Parameters:

  • key (Integer)

    ID of the key. Must be between 0 and 15 (for the 4x4 keypad)

  • event (Neotrellis::Keypad::KEY_PRESSED|Neotrellis::Keypad::KEY_RELEASED|Neotrellis::Keypad::KEY_BOTH) (defaults to: KEY_BOTH)

    Type of event to react to.

  • enabled (Boolean) (defaults to: true)

    If false, the callback will be disabled.

  • block (Block)

    Code to execute when the event is trigerred. A Neotrellis::Keypad::KeyEvent will be passed as argument to the block.



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

#syncObject

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_eventObject

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