Class: Tamashii::Agent::Device::Keyboard::ButtonMatrix4x4

Inherits:
Base
  • Object
show all
Defined in:
lib/tamashii/agent/device/keyboard/button_matrix4x4.rb

Instance Method Summary collapse

Methods inherited from Base

#add_callback, #default_number_of_keys, #default_watch, #initialize, #initialize_watcher, #mark_key_down, #mark_key_up, #on_key_down, #on_key_pressed, #on_key_up, #poll_key, #process_callbacks, #record_key_state, #shutdown, #start_watcher_thread, #stop_watcher_thread, #watcher_loop, #watcher_stopping?

Methods inherited from DeviceBase

#fetch_option, #fetch_option!, #initialize, #shutdown, #unexport_pin

Methods included from Common::Loggable

#display_name, #logger, #progname

Constructor Details

This class inherits a constructor from Tamashii::Agent::Device::Keyboard::Base

Instance Method Details

#default_col_pinsObject



31
32
33
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 31

def default_col_pins
  [26, 19, 13, 6]
end

#default_row_pinsObject



27
28
29
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 27

def default_row_pins
  [21, 20, 16, 12]
end

#finalize_hardwareObject



35
36
37
38
39
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 35

def finalize_hardware
  (@row_pins + @col_pins).each do |pin|
    unexport_pin(pin.pin)
  end
end

#initialize_hardwareObject



10
11
12
13
14
15
16
17
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 10

def initialize_hardware
  @row_pins = fetch_option(:row_pins, default_row_pins).map do |pin_number|
    PiPiper::Pin.new(pin: pin_number, direction: :out).tap {|pin| pin.on} 
  end
  @col_pins = fetch_option(:col_pins, default_col_pins).map do |pin_number|
    PiPiper::Pin.new(pin: pin_number, direction: :in, pull: :up)
  end
end

#number_of_keysObject



19
20
21
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 19

def number_of_keys
  16
end

#polling_intervalObject



23
24
25
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 23

def polling_interval
  0.01
end

#read_keyObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tamashii/agent/device/keyboard/button_matrix4x4.rb', line 41

def read_key
  current_key = nil
  @row_pins.each_with_index do |row_pin, row_index|
    row_pin.off
    @col_pins.each_with_index do |col_pin, col_index|
      col_pin.read
      key = row_index * 4 + col_index
      if col_pin.off?
        current_key = key
        mark_key_down(key)
      else
        mark_key_up(key)
      end
    end
    row_pin.on
    sleep 0.001
  end
  current_key
end