for layer in layers.keys():
            keys[layer].led_off()

        # Give some visual feedback for the modifier key
        keys[0].set_led(0, 255, 25)

    # Loop through all of the keys in the layer and if they're pressed, get the
    # key code from the layer's key map
    for k in layers[current_layer].keys():
        if keys[k].pressed:
            key_press = layers[current_layer][k]

            # If the key hasn't just fired (prevents refiring)
            if not fired:
                fired = True

                # Send the right sort of key press and set debounce for each
                # layer accordingly (layer 2 needs a long debounce)
                if current_layer == 1:
                    debounce = short_debounce
                    keyboard.send(key_press)
                elif current_layer == 2:
                    debounce = long_debounce
                    layout.write(key_press)
                elif current_layer == 3:
                    debounce = short_debounce
                    consumer_control.send(key_press)

    # If enough time has passed, reset the fired variable
    if fired and time.monotonic() - keybow.time_of_last_press > debounce:
        fired = False
Exemple #2
0
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import usb_hid
from adafruit_circuitplayground.express import cpx
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

cc = ConsumerControl(usb_hid.devices)

while True:
    if cpx.button_a:
        cc.send(ConsumerControlCode.VOLUME_INCREMENT)
        while cpx.button_a:
            pass
    if cpx.button_b:
        cc.send(ConsumerControlCode.VOLUME_DECREMENT)
        while cpx.button_b:
            pass
                                        _cur_actions = [_cur_actions]

                                    # loop over the actions
                                    for _action in _cur_actions:
                                        # HID keyboard keys
                                        if _action[0] == KEY:
                                            kbd.press(*_action[1])
                                            kbd.release(*_action[1])

                                        # String to write from layout
                                        elif _action[0] == STRING:
                                            kbd_layout.write(_action[1])

                                        # Consumer control code
                                        elif _action[0] == MEDIA:
                                            cc.send(_action[1])

                                        # Key press
                                        elif _action[0] == KEY_PRESS:
                                            kbd.press(*_action[1])

                                        # Key release
                                        elif _action[0] == KEY_RELEASE:
                                            kbd.release(*_action[1])

                                        # Change Layer
                                        elif _action[0] == CHANGE_LAYER:
                                            if isinstance(
                                                    _action[1], int
                                            ) and 0 <= _action[1] < len(
                                                    touch_deck_config["layers"]
Exemple #4
0
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
control_key = Keycode.SHIFT
cc = ConsumerControl(usb_hid.devices)

cols = [digitalio.DigitalInOut(x) for x in (board.D11, board.D12, board.D13)]
rows = [digitalio.DigitalInOut(x) for x in (board.D9, board.D10)]
keys = (
    (0,1,2),
    (3,4,5),
)
keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys)

KEYS = [
    ConsumerControlCode.SCAN_NEXT_TRACK,
    ConsumerControlCode.VOLUME_INCREMENT,
    ConsumerControlCode.PLAY_PAUSE,
    ConsumerControlCode.SCAN_PREVIOUS_TRACK,
    ConsumerControlCode.VOLUME_DECREMENT,
    ConsumerControlCode.MUTE,
]

while True:
    keys = keypad.pressed_keys
    if keys:
        for key in keys:
            cc.send(KEYS[key])
        time.sleep(0.1)
Exemple #5
0
    delta = pos - last_pos
    last_pos = pos
    direction = 0

    if delta > 0:
        command = ConsumerControlCode.VOLUME_INCREMENT
        direction = -1
    elif delta < 0:
        command = ConsumerControlCode.VOLUME_DECREMENT
        direction = 1

    if direction:
        muted = False
        for _ in range(abs(delta)):
            cc.send(command)
            # spin neopixel LED around in the correct direction!
            dot_location = (dot_location + direction) % len(cp.pixels)
            draw()

    if cp.button_b and not button_b_pressed:
        if not muted:
            print("Muting")
            cc.send(ConsumerControlCode.MUTE)
            muted = True
        else:
            print("Unmuting")
            cc.send(ConsumerControlCode.MUTE)
            muted = False
        draw()
        # while not button_B.value:  # debounce
Exemple #6
0
        (r, g, b) = (0, int(pos * 3), int(255 - pos * 3))
    return (r, g, b)


last_encoder_val = encoder.position
ring_pos = 0
rainbow_pos = 0

while True:
    diff = last_encoder_val - encoder.position  # encoder clicks since last read
    last_encoder_val = encoder.position
    ring_pos = (ring_pos + diff) % len(ring)  # position on LED ring
    hue = colorwheel(encoder.position * 4 % 255)  # fun hue change based on pos

    if button.value == False:  # button pressed
        cc.send(ConsumerControlCode.MUTE)  # toggle mute
        while not button.value:  # wait for release
            time.sleep(0.05)
            for i in range(len(ring)):  # spin the rainbow while held
                pixel_index = (i * 256 // len(ring)) + rainbow_pos
                ring[i] = colorwheel(pixel_index & 255)
                ring.show()
                rainbow_pos = (rainbow_pos + 1) % 256
    else:
        if diff > 0:
            for i in range(diff):
                cc.send(ConsumerControlCode.VOLUME_DECREMENT)
                time.sleep(0.01)
        elif diff < 0:
            for i in range(-diff):
                cc.send(ConsumerControlCode.VOLUME_INCREMENT)
Exemple #7
0
while True:
    pressed = set(trellis.pressed_keys)
    now = time.monotonic()
    sleep_time = now - last_press
    sleeping = sleep_time > TIMEOUT
    for down in pressed - current_press:
        if down in keymap and not sleeping:
            print("down", down)
            # Lower the brightness so that we don't draw too much current when we turn all of
            # the LEDs on.
            trellis.pixels.brightness = 0.2
            trellis.pixels.fill(keymap[down][0])
            if keymap[down][1] == KEY:
                kbd.press(*keymap[down][2]) if type(keymap[down][2]) == tuple else kbd.press(keymap[down][2])
            else:
                cc.send(keymap[down][2])
            # else if the entry starts with 'l' for layout.write
        last_press = now
    for up in current_press - pressed:
        if up in keymap:
            print("up", up)
            if keymap[up][1] == KEY:
                if type(keymap[down][2]) == tuple:
                    kbd.release(*keymap[down][2])
                else:
                    kbd.release(keymap[down][2])

    # Reset the LEDs when there was something previously pressed (current_press) but nothing now
    # (pressed).
    if not pressed and current_press:
        trellis.pixels.brightness = 1