Skip to content

Instantly share code, notes, and snippets.

@hasumikin
Created April 19, 2023 23:46
Show Gist options
  • Save hasumikin/1c4cb0e48aa36b6d1fe41277501e2fa0 to your computer and use it in GitHub Desktop.
Save hasumikin/1c4cb0e48aa36b6d1fe41277501e2fa0 to your computer and use it in GitHub Desktop.
PRK Firmware ADC example
require "adc"
require "mouse"
kbd = Keyboard.new
kbd.init_direct_pins( [8] )
kbd.add_layer :default, %i(BOOTSEL)
kbd.define_mode_key :BOOTSEL, [ Proc.new { kbd.bootsel! }, nil, 300, nil ]
adc_x = ADC.new(26)
adc_y = ADC.new(27)
# Adjust center offset
x_offset = 0
y_offset = 0
3.times do
sleep_ms 20
x_offset += adc_x.read
y_offset += adc_y.read
end
x_offset /= 3
y_offset /= 3
mouse = Mouse.new(driver: [adc_x, adc_y])
mouse.task do |mouse|
# Note: `driver.map(&:read)` doesn't work in mruby/c VM yet
x, y = mouse.driver.map { |adc| adc.read}
x -= x_offset
y -= y_offset
USB.merge_mouse_report(0,
x.abs < 300 ? 0 : x/100,#.clamp(-100, 100),
y.abs < 300 ? 0 : y/-100,#.clamp(-100, 100),
0,
0)
end
kbd.append mouse
kbd.start!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment