diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2022-06-19 19:47:51 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2022-06-19 19:47:51 +0530 |
commit | 4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch) | |
tree | 65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/frozen/Adafruit_CircuitPython_HID/examples | |
parent | 0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff) |
add circuitpython code
Diffstat (limited to 'circuitpython/frozen/Adafruit_CircuitPython_HID/examples')
6 files changed, 385 insertions, 0 deletions
diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_consumer_control_brightness.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_consumer_control_brightness.py new file mode 100644 index 0000000..1cf922c --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_consumer_control_brightness.py @@ -0,0 +1,32 @@ +# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import digitalio +import usb_hid +from adafruit_hid.consumer_control import ConsumerControl +from adafruit_hid.consumer_control_code import ConsumerControlCode + +cc = ConsumerControl(usb_hid.devices) + +# define buttons. these can be any physical switches/buttons, but the values +# here work out-of-the-box with a FunHouse UP and DOWN buttons. +button_up = digitalio.DigitalInOut(board.BUTTON_UP) +button_up.switch_to_input(pull=digitalio.Pull.DOWN) + +button_down = digitalio.DigitalInOut(board.BUTTON_DOWN) +button_down.switch_to_input(pull=digitalio.Pull.DOWN) + +while True: + if button_up.value: + print("Button up pressed!") + # send brightness up button press + cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT) + + if button_down.value: + print("Button down pressed!") + # send brightness down button press + cc.send(ConsumerControlCode.BRIGHTNESS_DECREMENT) + + time.sleep(0.1) diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_gamepad.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_gamepad.py new file mode 100644 index 0000000..01a9653 --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_gamepad.py @@ -0,0 +1,157 @@ +# SPDX-FileCopyrightText: 2018 Dan Halbert for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +""" +`Gamepad` +==================================================== + +* Author(s): Dan Halbert +""" + +import struct +import time + +from adafruit_hid import find_device + + +class Gamepad: + """Emulate a generic gamepad controller with 16 buttons, + numbered 1-16, and two joysticks, one controlling + ``x` and ``y`` values, and the other controlling ``z`` and + ``r_z`` (z rotation or ``Rz``) values. + + The joystick values could be interpreted + differently by the receiving program: those are just the names used here. + The joystick values are in the range -127 to 127.""" + + def __init__(self, devices): + """Create a Gamepad object that will send USB gamepad HID reports. + + Devices can be a list of devices that includes a gamepad device or a gamepad device + itself. A device is any object that implements ``send_report()``, ``usage_page`` and + ``usage``. + """ + self._gamepad_device = find_device(devices, usage_page=0x1, usage=0x05) + + # Reuse this bytearray to send mouse reports. + # Typically controllers start numbering buttons at 1 rather than 0. + # report[0] buttons 1-8 (LSB is button 1) + # report[1] buttons 9-16 + # report[2] joystick 0 x: -127 to 127 + # report[3] joystick 0 y: -127 to 127 + # report[4] joystick 1 x: -127 to 127 + # report[5] joystick 1 y: -127 to 127 + self._report = bytearray(6) + + # Remember the last report as well, so we can avoid sending + # duplicate reports. + self._last_report = bytearray(6) + + # Store settings separately before putting into report. Saves code + # especially for buttons. + self._buttons_state = 0 + self._joy_x = 0 + self._joy_y = 0 + self._joy_z = 0 + self._joy_r_z = 0 + + # Send an initial report to test if HID device is ready. + # If not, wait a bit and try once more. + try: + self.reset_all() + except OSError: + time.sleep(1) + self.reset_all() + + def press_buttons(self, *buttons): + """Press and hold the given buttons.""" + for button in buttons: + self._buttons_state |= 1 << self._validate_button_number(button) - 1 + self._send() + + def release_buttons(self, *buttons): + """Release the given buttons.""" + for button in buttons: + self._buttons_state &= ~(1 << self._validate_button_number(button) - 1) + self._send() + + def release_all_buttons(self): + """Release all the buttons.""" + + self._buttons_state = 0 + self._send() + + def click_buttons(self, *buttons): + """Press and release the given buttons.""" + self.press_buttons(*buttons) + self.release_buttons(*buttons) + + def move_joysticks(self, x=None, y=None, z=None, r_z=None): + """Set and send the given joystick values. + The joysticks will remain set with the given values until changed + + One joystick provides ``x`` and ``y`` values, + and the other provides ``z`` and ``r_z`` (z rotation). + Any values left as ``None`` will not be changed. + + All values must be in the range -127 to 127 inclusive. + + Examples:: + + # Change x and y values only. + gp.move_joysticks(x=100, y=-50) + + # Reset all joystick values to center position. + gp.move_joysticks(0, 0, 0, 0) + """ + if x is not None: + self._joy_x = self._validate_joystick_value(x) + if y is not None: + self._joy_y = self._validate_joystick_value(y) + if z is not None: + self._joy_z = self._validate_joystick_value(z) + if r_z is not None: + self._joy_r_z = self._validate_joystick_value(r_z) + self._send() + + def reset_all(self): + """Release all buttons and set joysticks to zero.""" + self._buttons_state = 0 + self._joy_x = 0 + self._joy_y = 0 + self._joy_z = 0 + self._joy_r_z = 0 + self._send(always=True) + + def _send(self, always=False): + """Send a report with all the existing settings. + If ``always`` is ``False`` (the default), send only if there have been changes. + """ + struct.pack_into( + "<Hbbbb", + self._report, + 0, + self._buttons_state, + self._joy_x, + self._joy_y, + self._joy_z, + self._joy_r_z, + ) + + if always or self._last_report != self._report: + self._gamepad_device.send_report(self._report) + # Remember what we sent, without allocating new storage. + self._last_report[:] = self._report + + @staticmethod + def _validate_button_number(button): + if not 1 <= button <= 16: + raise ValueError("Button number must in range 1 to 16") + return button + + @staticmethod + def _validate_joystick_value(value): + if not -127 <= value <= 127: + raise ValueError("Joystick value must be in range -127 to 127") + return value diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_joywing_gamepad.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_joywing_gamepad.py new file mode 100644 index 0000000..2473675 --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_joywing_gamepad.py @@ -0,0 +1,76 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +# Use Joy FeatherWing to drive Gamepad. +# https://www.adafruit.com/product/3632 +# https://learn.adafruit.com/joy-featherwing + +# You must add a gamepad HID device inside your boot.py file +# in order to use this example. +# See this Learn Guide for details: +# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9 + +import time + +import board +import busio +from micropython import const +from adafruit_seesaw.seesaw import Seesaw +import usb_hid +from hid_gamepad import Gamepad + + +def range_map(value, in_min, in_max, out_min, out_max): + return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min + + +BUTTON_RIGHT = const(6) +BUTTON_DOWN = const(7) +BUTTON_LEFT = const(9) +BUTTON_UP = const(10) +BUTTON_SEL = const(14) +button_mask = const( + (1 << BUTTON_RIGHT) + | (1 << BUTTON_DOWN) + | (1 << BUTTON_LEFT) + | (1 << BUTTON_UP) + | (1 << BUTTON_SEL) +) + +i2c = busio.I2C(board.SCL, board.SDA) + +ss = Seesaw(i2c) + +ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP) + +last_game_x = 0 +last_game_y = 0 + +g = Gamepad(usb_hid.devices) + +while True: + x = ss.analog_read(2) + y = ss.analog_read(3) + + game_x = range_map(x, 0, 1023, -127, 127) + game_y = range_map(y, 0, 1023, -127, 127) + if last_game_x != game_x or last_game_y != game_y: + last_game_x = game_x + last_game_y = game_y + print(game_x, game_y) + g.move_joysticks(x=game_x, y=game_y) + + buttons = (BUTTON_RIGHT, BUTTON_DOWN, BUTTON_LEFT, BUTTON_UP, BUTTON_SEL) + button_state = [False] * len(buttons) + for i, button in enumerate(buttons): + buttons = ss.digital_read_bulk(button_mask) + if not (buttons & (1 << button) and not button_state[i]): + g.press_buttons(i + 1) + print("Press", i + 1) + button_state[i] = True + elif button_state[i]: + g.release_buttons(i + 1) + print("Release", i + 1) + button_state[i] = False + + time.sleep(0.01) diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_keyboard_shortcuts.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_keyboard_shortcuts.py new file mode 100644 index 0000000..8a25bd5 --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_keyboard_shortcuts.py @@ -0,0 +1,32 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import digitalio +import usb_hid +from adafruit_hid.keyboard import Keyboard +from adafruit_hid.keycode import Keycode + +kbd = Keyboard(usb_hid.devices) + +# define buttons. these can be any physical switches/buttons, but the values +# here work out-of-the-box with a CircuitPlayground Express' A and B buttons. +swap = digitalio.DigitalInOut(board.D4) +swap.direction = digitalio.Direction.INPUT +swap.pull = digitalio.Pull.DOWN + +search = digitalio.DigitalInOut(board.D5) +search.direction = digitalio.Direction.INPUT +search.pull = digitalio.Pull.DOWN + +while True: + # press ALT+TAB to swap windows + if swap.value: + kbd.send(Keycode.ALT, Keycode.TAB) + + # press CTRL+K, which in a web browser will open the search dialog + elif search.value: + kbd.send(Keycode.CONTROL, Keycode.K) + + time.sleep(0.1) diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py new file mode 100644 index 0000000..614cc84 --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +# You must add a gamepad HID device inside your boot.py file +# in order to use this example. +# See this Learn Guide for details: +# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9 + +import board +import digitalio +import analogio +import usb_hid + +from hid_gamepad import Gamepad + +gp = Gamepad(usb_hid.devices) + +# Create some buttons. The physical buttons are connected +# to ground on one side and these and these pins on the other. +button_pins = (board.D2, board.D3, board.D4, board.D5) + +# Map the buttons to button numbers on the Gamepad. +# gamepad_buttons[i] will send that button number when buttons[i] +# is pushed. +gamepad_buttons = (1, 2, 8, 15) + +buttons = [digitalio.DigitalInOut(pin) for pin in button_pins] +for button in buttons: + button.direction = digitalio.Direction.INPUT + button.pull = digitalio.Pull.UP + +# Connect an analog two-axis joystick to A4 and A5. +ax = analogio.AnalogIn(board.A4) +ay = analogio.AnalogIn(board.A5) + +# Equivalent of Arduino's map() function. +def range_map(x, in_min, in_max, out_min, out_max): + return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min + + +while True: + # Buttons are grounded when pressed (.value = False). + for i, button in enumerate(buttons): + gamepad_button_num = gamepad_buttons[i] + if button.value: + gp.release_buttons(gamepad_button_num) + print(" release", gamepad_button_num, end="") + else: + gp.press_buttons(gamepad_button_num) + print(" press", gamepad_button_num, end="") + + # Convert range[0, 65535] to -127 to 127 + gp.move_joysticks( + x=range_map(ax.value, 0, 65535, -127, 127), + y=range_map(ay.value, 0, 65535, -127, 127), + ) + print(" x", ax.value, "y", ay.value) diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simpletest.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simpletest.py new file mode 100644 index 0000000..3c5f651 --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simpletest.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import digitalio +import usb_hid +from adafruit_hid.mouse import Mouse + +mouse = Mouse(usb_hid.devices) + +# define buttons. these can be any physical switches/buttons, but the values +# here work out-of-the-box with a CircuitPlayground Express' A and B buttons. +up = digitalio.DigitalInOut(board.D4) +up.direction = digitalio.Direction.INPUT +up.pull = digitalio.Pull.DOWN + +down = digitalio.DigitalInOut(board.D5) +down.direction = digitalio.Direction.INPUT +down.pull = digitalio.Pull.DOWN + +while True: + # scroll up one unit (varies with host/OS) + if up.value: + mouse.move(wheel=1) + + # scroll down one unit (varies with host/OS) + elif down.value: + mouse.move(wheel=-1) + + time.sleep(0.1) |