def __init__(self,
                 joy_x_pin=board.A4,
                 joy_y_pin=board.A3,
                 joy_z_pin=board.D2,
                 joy_gnd_pin=board.A5,
                 dpad_l_pin=board.D3,
                 dpad_r_pin=board.D4,
                 dpad_u_pin=board.D1,
                 dpad_d_pin=board.D0,
                 mc_top_pin=board.SCK,
                 mc_middle_pin=board.MOSI,
                 mc_bottom_pin=board.MISO,
                 outputScale=20.0,
                 deadbandCutoff=0.1,
                 weight=0.2):
        self.x_axis = PiperJoystickAxis(joy_x_pin,
                                        outputScale=outputScale,
                                        deadbandCutoff=deadbandCutoff,
                                        weight=weight)
        self.y_axis = PiperJoystickAxis(joy_y_pin,
                                        outputScale=outputScale,
                                        deadbandCutoff=deadbandCutoff,
                                        weight=weight)
        self.joy_z = PiperJoystickZ(joy_z_pin)
        self.dpad = PiperDpad(dpad_l_pin, dpad_r_pin, dpad_u_pin, dpad_d_pin)
        self.minecraftbuttons = PiperMineCraftButtons(mc_top_pin,
                                                      mc_middle_pin,
                                                      mc_bottom_pin)

        # Drive pin low if requested for easier joystick wiring
        if joy_gnd_pin is not None:
            # Provide a ground for the joystick - this is to facilitate
            # easier wiring
            self.joystick_gnd = DigitalInOut(joy_gnd_pin)
            self.joystick_gnd.direction = Direction.OUTPUT
            self.joystick_gnd.value = 0

        self.keyboard = Keyboard(usb_hid.devices)
        self.keyboard_layout = KeyboardLayoutUS(
            self.keyboard)  # Change for non-US
        self.mouse = Mouse(usb_hid.devices)

        # State
        #
        self.state = _UNWIRED
        self.timer = time.monotonic()
        self.last_mouse_wheel = time.monotonic()
        self.last_mouse = time.monotonic()
        self.dotstar_led = adafruit_dotstar.DotStar(board.APA102_SCK,
                                                    board.APA102_MOSI, 1)
        self.dotstar_led.brightness = 0.2
        self.up_pressed = False
        self.down_pressed = False
        self.left_pressed = False
        self.right_pressed = False
        self.mc_mode = _MC_DEFAULT
        self.mc_flyingdown_req = False
        self.mc_sprinting_req = False
        self.mc_crouching_req = False
        self.mc_utility_req = False
Example #2
0
    def __init__(self,
                 name="Generic Keyboard",
                 gpio_row=None,
                 gpio_col=None,
                 layout=None,
                 layers=None,
                 macro_layer=None):
        self.name = name
        self.kbd = Keyboard(usb_hid.devices)  # Setup keyboard HID device
        self.kbd_layout = KeyboardLayoutUS(self.kbd)
        self.keymap = KeyboardHwLayout(layout)  # map layout to hw layout

        self.key_row = []  # write
        self.key_col = []  # read

        self.last_scan_matrix = {
        }  # used to determine diff between scans (if a release needs to be signalled)

        # Layers 0, 1, 2, 3 where 0 is the default layer 1 & 2 is the extended layers and 3 is the macro layer
        self.current_layer = 0

        # Setup write pins
        for pin in gpio_row:
            key_pin = digitalio.DigitalInOut(pin)
            key_pin.direction = digitalio.Direction.OUTPUT
            self.key_row.append(key_pin)

        # Setup read pins
        for pin in gpio_col:
            key_pin = digitalio.DigitalInOut(pin)
            key_pin.direction = digitalio.Direction.INPUT
            key_pin.pull = digitalio.Pull.UP
            self.key_col.append(key_pin)
    def paste(self, text):
        if self.keyboard is None:
            if usb_hid:
                self.keyboard = Keyboard(usb_hid.devices)
                self.keyboard_layout = KeyboardLayoutUS(self.keyboard)
            else:
                return

        if self.keyboard_layout is None:
            raise ValueError("USB HID not available")
        text = str(text)
        self.keyboard_layout.write(text)
        raise RuntimeError("Pasted")
Example #4
0
    def __init__(self, config):
        """
        Init and binds the H/W
        """
        # Pimoroni's RGB Keypad - Default wiring
        self.KEYPAD = RgbKeypad()
        self.KEYS = self.KEYPAD.keys
        # DS3231 module, i2c1, SCL=GP11 and SDA=GP10
        i2c = busio.I2C(board.GP11, board.GP10)
        self.DS = DS3231(i2c)
        print(self.DS.datetime)  # Just to check time at boot when dev

        self.CONFIG = config

        # USB HID
        keyboard = Keyboard(usb_hid.devices)
        if self.CONFIG.get("layout", "us") == "fr":
            # More to come
            from adafruit_hid.keyboard_layout_fr import KeyboardLayoutFR
            self.LAYOUT = KeyboardLayoutFR(keyboard)
        else:
            # Default US layout
            from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
            self.LAYOUT = KeyboardLayoutUS(keyboard)

        # Pico display
        self.DISPLAY = pico_dio.get_display()
        self.SCREENS = dict()
        self.SCREENS["splash"] = pico_dio.get_splash()

        self.DISPLAY.show(self.SCREENS["splash"])

        self.UPDATE_INDEX = 0
        self.LOCKED = False
        self.LAST_CODE = ""
        self.OTP = None
        self.MODE = 0
        self.PAGE = 0
        self.INDEX = None
        self.LAST_COUNTER = 0  # time // 30, OTP counter

        self.SCREENS["OTP"] = pico_dio.get_otp_group()
        self.SCREENS["PAGE"] = pico_dio.get_page_group()

        self.display_page(self.PAGE)

        for key in self.KEYS:
            @self.KEYPAD.on_press(key)
            def press_handler(a_key):
                self.handle_numpad(button_to_numpad(a_key.number))
Example #5
0
    def paste(self, text):
        if self.keyboard is None:
            if usb_hid:
                self.keyboard = Keyboard(usb_hid.devices)
                self.keyboard_layout = KeyboardLayoutUS(self.keyboard)
            else:
                return

        if self.keyboard_layout is None:
            self.add_trail("No USB")
        else:
            text = str(text)
            self.keyboard_layout.write(text)

            self.add_trail(f"Pasted {text}")
Example #6
0
# Use default HID descriptor
hid = HIDService()
device_info = DeviceInfoService(
    software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries"
)
advertisement = ProvideServicesAdvertisement(hid)
advertisement.appearance = 961
scan_response = Advertisement()

ble = adafruit_ble.BLERadio()
if ble.connected:
    for c in ble.connections:
        c.disconnect()

print("advertising")
ble.start_advertising(advertisement, scan_response)

k = Keyboard(hid.devices)
kl = KeyboardLayoutUS(k)
while True:
    while not ble.connected:
        pass
    print("Start typing:")
    while ble.connected:
        c = sys.stdin.read(1)
        sys.stdout.write(c)
        kl.write(c)
        # print("sleeping")
        time.sleep(0.1)
    ble.start_advertising(advertisement)
Example #7
0
from adafruit_hid.keyboard import Keyboard
gc.collect()
from adafruit_hid.keycode import Keycode
gc.collect()
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
gc.collect()
import board
import pulseio
import adafruit_dotstar
import adafruit_irremote
import time

# The keyboard object!
time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard()
keyboard_layout = KeyboardLayoutUS(keyboard)  # We're in the US :)

led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
decoder = adafruit_irremote.GenericDecode()
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=100, idle_state=True)

# Expected pulse, pasted in from previous recording REPL session:
key1_pulses = [0]

key2_pulses = [1]

print('IR listener')


# Fuzzy pulse comparison function:
def fuzzy_pulse_compare(pulse1, pulse2, fuzzyness=0.2):
Example #8
0
from digitalio import DigitalInOut, Direction, Pull

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

switch = DigitalInOut(board.GP20)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

switch2 = DigitalInOut(board.GP21)
switch2.direction = Direction.INPUT
switch2.pull = Pull.UP

keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)

while True:
    if not switch.value:
        print('y')
        keyboard.press(Keycode.LEFT_ALT, Keycode.F1)
        keyboard.release_all()

    if not switch2.value:
        print('y')
        keyboard.press(Keycode.LEFT_ALT, Keycode.F2)
        keyboard.release_all()

    print('---')
    time.sleep(0.2)
Example #9
0
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

from digitalio import DigitalInOut, Direction, Pull
cs = DigitalInOut(board.GP17)
cs.direction = Direction.OUTPUT
cs.value = 0
num_pixels = 16
pixels = adafruit_dotstar.DotStar(board.GP18, board.GP19, num_pixels, brightness=0.1, auto_write=True)
i2c = busio.I2C(board.GP5, board.GP4)
device = I2CDevice(i2c, 0x20)
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
def colourwheel(pos):
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)
def read_button_states(x, y):
    pressed = [0] * 16
    with device:
        device.write(bytes([0x0]))
        result = bytearray(2)
Example #10
0
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
import adafruit_thermistor

# Switch to quickly enable/disable

# light level

# temperature

# Set the keyboard object!
# Sleep for a bit to avoid a race condition on some systems
time.sleep(1)
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd)  # US is only current option...

print("Time\tLight\tTemperature\tX\tY\tZ")  # Print column headers


def slow_write(string):  # Typing should not be too fast for
    for c in string:  # the computer to be able to accept
        layout.write(c)
        time.sleep(0.2)  # use 1/5 second pause between characters


while True:
    if cpx.switch:  # If the slide switch is on, don't log
        continue

    # Turn on the LED to show we're logging
Example #11
0
    def __init__(self, debug=False):
        self.debug = debug
        #self.mouse = Mouse(usb_hid.devices)
        self.keyboard = Keyboard(usb_hid.devices)
        self.keyboard_layout = KeyboardLayoutUS(
            self.keyboard)  # Change for non-US
        self.default_delay = 0
        self.string_delay = 0
        self.last_cmd = ""
        self.last_arg = ""

        # Keycode only has US mappings, not sure if AdaFruit has others
        #
        # TODO - consider a data structure which consumes less RAM for
        # constrained platforms.
        #
        # Keep a separate structure for multi-key combos to lower memory
        # usage.
        #
        self.tuple_keymap = {
            "CTRL-ALT": (Keycode.CONTROL, Keycode.ALT),
            "CTRL-SHIFT": (Keycode.CONTROL, Keycode.SHIFT),
            "ALT-SHIFT": (Keycode.ALT, Keycode.SHIFT)
        }

        self.keymap = {
            "ALT": Keycode.ALT,
            "APP": Keycode.APPLICATION,
            "BREAK": Keycode.PAUSE,
            "CAPSLOCK": Keycode.CAPS_LOCK,
            "CONTROL": Keycode.CONTROL,
            "CTRL": Keycode.CONTROL,
            "DELETE": Keycode.DELETE,
            "DOWNARROW": Keycode.DOWN_ARROW,
            "DOWN": Keycode.DOWN_ARROW,
            "END": Keycode.END,
            "ENTER": Keycode.ENTER,
            "ESC": Keycode.ESCAPE,
            "ESCAPE": Keycode.ESCAPE,
            "GUI": Keycode.GUI,
            "HOME": Keycode.HOME,
            "INSERT": Keycode.INSERT,
            "LEFTARROW": Keycode.LEFT_ARROW,
            "LEFT": Keycode.LEFT_ARROW,
            "MENU": Keycode.APPLICATION,
            "NUMLOCK": Keycode.KEYPAD_NUMLOCK,
            "PAGEUP": Keycode.PAGE_UP,
            "PAGEDOWN": Keycode.PAGE_DOWN,
            "PAUSE": Keycode.PAUSE,
            "PRINTSCREEN": Keycode.PRINT_SCREEN,
            "RIGHTARROW": Keycode.RIGHT_ARROW,
            "RIGHT": Keycode.RIGHT_ARROW,
            "SCROLLLOCK": Keycode.SCROLL_LOCK,
            "SHIFT": Keycode.SHIFT,
            "SPACE": Keycode.SPACE,
            "TAB": Keycode.TAB,
            "UPARROW": Keycode.UP_ARROW,
            "UP": Keycode.UP_ARROW,
            "WINDOWS": Keycode.WINDOWS,
            "a": Keycode.A,
            "A": Keycode.A,
            "b": Keycode.B,
            "B": Keycode.B,
            "c": Keycode.C,
            "C": Keycode.C,
            "d": Keycode.D,
            "D": Keycode.D,
            "e": Keycode.E,
            "E": Keycode.E,
            "f": Keycode.F,
            "F": Keycode.F,
            "g": Keycode.G,
            "G": Keycode.G,
            "h": Keycode.H,
            "H": Keycode.H,
            "i": Keycode.I,
            "I": Keycode.I,
            "j": Keycode.J,
            "J": Keycode.J,
            "k": Keycode.K,
            "K": Keycode.K,
            "l": Keycode.L,
            "L": Keycode.L,
            "m": Keycode.M,
            "M": Keycode.M,
            "n": Keycode.N,
            "N": Keycode.N,
            "o": Keycode.O,
            "O": Keycode.O,
            "p": Keycode.P,
            "P": Keycode.P,
            "q": Keycode.Q,
            "Q": Keycode.Q,
            "r": Keycode.R,
            "R": Keycode.R,
            "s": Keycode.S,
            "S": Keycode.S,
            "t": Keycode.T,
            "T": Keycode.T,
            "u": Keycode.U,
            "U": Keycode.U,
            "v": Keycode.V,
            "V": Keycode.V,
            "w": Keycode.W,
            "W": Keycode.W,
            "x": Keycode.X,
            "X": Keycode.X,
            "y": Keycode.Y,
            "Y": Keycode.Y,
            "z": Keycode.Z,
            "Z": Keycode.Z,
            #
            # The DuckyScript encoder didn't appear to have the following codes and
            # probably used the STRING command to deal with some of them. Adding them shouldn't
            # hurt but does consume RAM. Some are Mac specific.
            #
            "F1": Keycode.F1,
            "F2": Keycode.F2,
            "F3": Keycode.F3,
            "F4": Keycode.F4,
            "F5": Keycode.F5,
            "F6": Keycode.F6,
            "F7": Keycode.F7,
            "F8": Keycode.F8,
            "F9": Keycode.F9,
            "F10": Keycode.F10,
            "F11": Keycode.F11,
            "F12": Keycode.F12,
            "F13": Keycode.F13,
            "F14": Keycode.F14,
            "F15": Keycode.F15,
            "F16": Keycode.F16,
            "F17": Keycode.F17,
            "F18": Keycode.F18,
            "F19": Keycode.F19,
            "BACKSLASH": Keycode.BACKSLASH,
            "COMMA": Keycode.COMMA,
            "COMMAND": Keycode.COMMAND,
            "FORWARD_SLASH": Keycode.FORWARD_SLASH,
            "GRAVE_ACCENT": Keycode.GRAVE_ACCENT,
            "LEFT_BRACKET": Keycode.LEFT_BRACKET,
            "OPTION": Keycode.ALT,
            "PERIOD": Keycode.PERIOD,
            "POUND": Keycode.POUND,
            "QUOTE": Keycode.QUOTE
        }
Example #12
0
def keyboard_config(key_to_be_pressed):
    kbd = Keyboard(usb_hid.devices)
    layout = KeyboardLayoutUS(kbd)
    kbd.send(key_to_be_pressed)
# neopixel.mpy

import board
import time
import usb_hid
import neopixel

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)

time.sleep(1)  # sleep for a bit to avoid a race condition on some systems
kbd = Keyboard(usb_hid.devices)  # the keyboard object!
layout = KeyboardLayoutUS(kbd)  # you're americans :)

num_lock = False  # NUM LOCK
caps_lock = False  # CAPS LOCK
scroll_lock = False  # SCROLL LOCK


def update_lock():
    global num_lock, caps_lock, scroll_lock
    report = int.from_bytes(usb_hid.devices[0].last_received_report, "big")
    num_lock = (report & 0x01) == 0x01  # NUM LOCK
    caps_lock = (report & 0x02) == 0x02  # CAPS LOCK
    scroll_lock = (report & 0x04) == 0x04  # SCROLL LOCK


update_lock()
Example #14
0
"""programme 6-8-2 : saisie dans un tableur"""
# importation des modules natifs utiles
from time import *
from board import *
from digitalio import *
# importation de modules supplémentaires
from adafruit_lsm6ds import *
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

# Instanciation du clavier
clavier = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(clavier)

# Instanciation du bus de communication I2C sur le module Feather
bus_i2c = I2C()
# Instanciation de l'accéléromètre LSM6DS33
accelerometre = LSM6DS33(bus_i2c)

# Instanciation de la LED interne et du bouton interne
led = DigitalInOut(D13)
led.direction = Direction.OUTPUT
led.value = False
bouton = DigitalInOut(SWITCH)
bouton.pull = Pull.UP

# Attente d'appui sur le bouton permettant à l'utilisateur de se placer
# sur la feuille de son tableur
while bouton.value:
Example #15
0
 def __init__(self):
     self.keyboard = adafruit_hid.keyboard.Keyboard(usb_hid.devices)
     self.keyboard_layout = KeyboardLayoutUS(self.keyboard)
Example #16
0
x = AnalogIn(board.A2)
y = AnalogIn(board.A3)

# Touch pins
touch_right = touchio.TouchIn(board.D11)  # Requires 1 Mohm pulldown
touch_left = touchio.TouchIn(board.D12)  # Requires 1 Mohm pulldown

# We'll use these to detect button/joystick presses/movement
up_state = False
down_state = False
left_state = False
right_state = False
space_state = False

keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)  # Change for non-US

while True:
    # Placeholder
    #if touch_left.value:
    #    print("Left touched")
    #if touch_right.value:
    #    print("Right touched")

    # Call the debouncing library frequently
    right.update()
    left.update()
    up.update()
    down.update()
    joy_press.update()