Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Trinket M0 Push to talk button
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trinket M0 Push to talk button

by Theatreguy on Wed May 06, 2020 8:53 pm

Hi.

Similar issues have come up in my searches here, but they're somewhat older so the versions don't match, or the solutions don't fit my problem.

I have zero experience with any kind of programming of this nature, so I'm having quite a hard time with this.

I'm trying to create a Push to talk switch using a push button from D0 to GND on my Trinket m0 to emulate Scroll Lock being held down with the button pressed, then released when the button is released. Just like a regular keyboard.

I'm basically using the example posted here https://learn.adafruit.com/circuitpytho ... -and-mouse and I figure I can just change the "A" key to "scroll_lock" when I have it working right.

However, this example waits for the the button to be released before typing the "A" as well as only prints it once. If i remove "Keyboard.release_all(), it does a runaway A press and I have to unplug it. I need it to be pressed on the button press, and stay held as long as the button is down.

I'm using the D0 pin for this, and CircuitPython 5.1.0

I'm sure this is a super simple thing to code, but my inexperience is getting the best of me.

Can someone point me in the right direction, or tell me how to fix this?

Code: Select all | TOGGLE FULL SIZE
# CircuitPython demo - Keyboard emulator

import time

import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

# A simple neat keyboard demo in CircuitPython

# The pins we'll use, each will have an internal pullup
keypress_pins = [board.D0]
# Our array of key objects
key_pin_array = []
# The Keycode sent for each button, will be paired with a control key
keys_pressed = [Keycode.A]
control_key = Keycode.SHIFT

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

# Make all pin objects inputs with pullups
for pin in keypress_pins:
    key_pin = digitalio.DigitalInOut(pin)
    key_pin.direction = digitalio.Direction.INPUT
    key_pin.pull = digitalio.Pull.UP
    key_pin_array.append(key_pin)

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

print("Waiting for key pin...")

while True:
    # Check each pin
    for key_pin in key_pin_array:
        if not key_pin.value:  # Is it grounded?
            i = key_pin_array.index(key_pin)
            print("Pin #%d is grounded." % i)

            # Turn on the red LED
            led.value = True

            while not key_pin.value:
                pass  # Wait for it to be ungrounded!
            # "Type" the Keycode or string
            key = keys_pressed[i]  # Get the corresponding Keycode or string
            if isinstance(key, str):  # If it's a string...
                keyboard_layout.write(key)  # ...Print the string
            else:  # If it's not a string...
                keyboard.press(control_key, key)  # "Press"...
                keyboard.release_all()  # ..."Release"!

            # Turn off the red LED
            led.value = False

    time.sleep(0.01)

Theatreguy
 
Posts: 2
Joined: Wed May 06, 2020 8:13 pm

Re: Trinket M0 Push to talk button

by dastels on Wed May 06, 2020 9:29 pm

At the point where the LED gets turned on, you would call keyboard.press, and where it is turned off, call keyboard.release_all.

For your purpose you can get rid of the check for it being a string, since it's a single key press/release you need. And you won't need the control key either.

That should get you further.

Dave

dastels
 
Posts: 3023
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinket M0 Push to talk button

by Theatreguy on Wed May 06, 2020 10:39 pm

Thank you.

That definitely helped.

This is what I have now. Testing it in a keyboard testing program, shows the scroll lock key just being pressed on and off , about twice a second, instead of being held down. On my real keyboard, holding scroll lock works correctly.


Removing the " keyboard.release_all()" just lets the scroll lock runaway pressed constantly.



Code: Select all | TOGGLE FULL SIZE
# CircuitPython demo - Keyboard emulator

import time

import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

# A simple neat keyboard demo in CircuitPython

# The pins we'll use, each will have an internal pullup
keypress_pins = [board.D0]
# Our array of key objects
key_pin_array = []

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

# Make all pin objects inputs with pullups
for pin in keypress_pins:
    key_pin = digitalio.DigitalInOut(pin)
    key_pin.direction = digitalio.Direction.INPUT
    key_pin.pull = digitalio.Pull.UP
    key_pin_array.append(key_pin)

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

print("Waiting for key pin...")

while True:
    # Check each pin
    for key_pin in key_pin_array:
        if not key_pin.value:  # Is it grounded?
            i = key_pin_array.index(key_pin)
            print("Pin #%d is grounded." % i)

            # Turn on the red LED
            led.value = True

            keyboard.press(71)  # "Press"...
            keyboard.release_all()  # ..."Release"!

            # Turn off the red LED
            led.value = False

    time.sleep(0.1)

Theatreguy
 
Posts: 2
Joined: Wed May 06, 2020 8:13 pm

Re: Trinket M0 Push to talk button

by dastels on Thu May 07, 2020 9:57 am

The code looks at the button about several times a second. If it sees the button pressed it sends a press and release. You want it to send a press when the button is first seen as pressed, and a release when it's first seen released. The debounder module will make that a lot easier. See https://learn.adafruit.com/debouncer-library-python-circuitpython-buttons-sensors for details.

Dave

dastels
 
Posts: 3023
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.