Class: Rnes::Keypad

Inherits:
Object
  • Object
show all
Defined in:
lib/rnes/keypad.rb

Constant Summary collapse

KEY_MAP =
{
  '.' => 0,
  ',' => 1,
  'n' => 2,
  'm' => 3,
  'w' => 4,
  's' => 5,
  'a' => 6,
  'd' => 7,
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeKeypad

Returns a new instance of Keypad.



14
15
16
17
18
# File 'lib/rnes/keypad.rb', line 14

def initialize
  @buffer = 0
  @copy = 0
  @index = 0
end

Instance Method Details

#checkObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rnes/keypad.rb', line 20

def check
  character = ::STDIN.read_nonblock(1)
  index = KEY_MAP[character]
  if index
    @buffer |= 1 << index
  end
rescue ::EOFError
  # Rescue on no STDIN environment (e.g. CircleCI).
rescue ::IO::WaitReadable
  # Rescue on no data in STDIN buffer.
end

#readInteger

Returns:

  • (Integer)


33
34
35
36
37
# File 'lib/rnes/keypad.rb', line 33

def read
  value = @copy[@index]
  @index = (@index + 1) % 0x10
  value
end

#write(value) ⇒ Object

Parameters:

  • value (Integer)


40
41
42
43
44
45
46
47
48
49
# File 'lib/rnes/keypad.rb', line 40

def write(value)
  if value[0] == 1
    @set = true
  elsif @set
    @set = false
    @copy = @buffer
    @buffer = 0
    @index = 0
  end
end