class Ev3dev::Button

Constants

BUF_LEN

end of stuff from linux/input.h

KEY_BACK
KEY_DOWN
KEY_ENTER
KEY_LEFT
KEY_MAX
KEY_RIGHT
KEY_UP

www.ev3dev.org/docs/tutorials/using-ev3-buttons/ from linux/input.h

PATH

Public Class Methods

new() click to toggle source
# File lib/ev3dev/button.rb, line 22
def initialize
  raise "couldn't find LED attributes" unless File.exist?(PATH)
  @buttons = {up: KEY_UP, down: KEY_DOWN, left: KEY_LEFT, right: KEY_RIGHT, enter: KEY_ENTER, back: KEY_BACK}
end

Public Instance Methods

EVIOCGKEY(length) click to toggle source
# File lib/ev3dev/button.rb, line 14
def EVIOCGKEY(length)
  2 << (14+8+8) | length << (8+8) | 'E'.ord << 8 | 0x18
end
back?() click to toggle source
# File lib/ev3dev/button.rb, line 52
def back?
  buf = get_key_buf
  key_pressed?(KEY_BACK, buf)
end
down?() click to toggle source
# File lib/ev3dev/button.rb, line 32
def down?
  buf = get_key_buf
  key_pressed?(KEY_DOWN, buf)
end
enter?() click to toggle source
# File lib/ev3dev/button.rb, line 47
def enter?
  buf = get_key_buf
  key_pressed?(KEY_ENTER, buf)
end
left?() click to toggle source
# File lib/ev3dev/button.rb, line 37
def left?
  buf = get_key_buf
  key_pressed?(KEY_LEFT, buf)
end
pressed() click to toggle source
# File lib/ev3dev/button.rb, line 57
def pressed
  pressed_buttons =[]
  buf = get_key_buf
  @buttons.each do |button, key_code|
    pressed_buttons << button.to_s if key_pressed?(key_code, buf)
  end
  pressed_buttons
end
right?() click to toggle source
# File lib/ev3dev/button.rb, line 42
def right?
  buf = get_key_buf
  key_pressed?(KEY_RIGHT, buf)
end
up?() click to toggle source
# File lib/ev3dev/button.rb, line 27
def up?
  buf = get_key_buf
  key_pressed?(KEY_UP, buf)
end

Private Instance Methods

get_key_buf() click to toggle source
# File lib/ev3dev/button.rb, line 67
def get_key_buf
  buf = Array.new(BUF_LEN){ 0 }.pack("C*")

  File.open(PATH, 'r') do |fd|
    ret = fd.ioctl(EVIOCGKEY(buf.length), buf)
    raise "couldn't find Button attributes" if ret < 0

    buf = buf.unpack("C*")
  end
end
key_pressed?(key_code, buf) click to toggle source
# File lib/ev3dev/button.rb, line 78
def key_pressed?(key_code, buf)
  test_bit(key_code, buf)
end
test_bit(bit, bytes) click to toggle source
# File lib/ev3dev/button.rb, line 82
def test_bit(bit, bytes)
  # bit in bytes is 1 when released and 0 when pressed
  if (bytes[bit / 8] & (1 << (bit % 8))) == 0
    true   # pressed
  else
    false  # released
  end
end