Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Newbie problem with usb_hid.devices
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Newbie problem with usb_hid.devices

by MKB11 on Fri Aug 14, 2020 10:51 pm

Hello all, first post. I am having a problem with a new Gemma M0 and circuitpython, and using the usb hid functions. In several different projects, I keep running into the following issue:

raceback (most recent call last):
File "main.py", line 24, in <module>
TypeError: function takes 1 positional arguments but 2 were given

Line 24: keyboard = Keyboard(usb_hid.devices)

I've run into this with several different demo codes I've downloaded, and to my knowledge the Gemma has been updated with the latest libraries and circuitpython. Any pointers on what could be going on here? I've searched quite a bit with no luck so far, and have eve tried different computers. It seems to be such a common pervasive problem that someone else must have run into it.

Thanks for all the help, my background is in C and I have a lot of time with arduinos, but want to branch into python.

Here's the latest code I've tried, but it's a download off the adafruit web site, with no changes other than an added comment.
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.A1, board.A2]
# 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, "Hello World!\n"]
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)  # This is line 24 where the error occurs
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)

MKB11
 
Posts: 2
Joined: Fri Aug 14, 2020 10:34 pm

Re: Newbie problem with usb_hid.devices

by adafruit_support_carter on Tue Aug 18, 2020 1:45 pm

Let's start by checking your CP and library versions. What version of CircuitPython are you running? You can look in the boot_out.txt file in your CIRCUITPY folder.

Also, how are you installing the HID library?

adafruit_support_carter
 
Posts: 16921
Joined: Tue Nov 29, 2016 2:45 pm

Re: Newbie problem with usb_hid.devices

by MKB11 on Wed Aug 19, 2020 5:55 am

adafruit_support_carter wrote:Let's start by checking your CP and library versions. What version of CircuitPython are you running? You can look in the boot_out.txt file in your CIRCUITPY folder.

Also, how are you installing the HID library?

Hello, thanks for the response, however I did find a solution yesterday and just now can post it. I found that the code worked properly on a new Circuit Playground Express that was upgraded according to a tutorial video on youtube (basically upgrade to the latest stable circuitpython release and install the latest library bundle for that release). I then did the same thing on the Gemma, using the proper latest circuitpython download and the matching version adafruit_hid library folder from the library bundle, and the code is working on the Gemma now.

It would be helpful if these steps would be clearly and simply indicated, along with links, as was in the instructional video. However I'm sure that my past experience made me try things that were counterproductive. I did spend almost a day hammering away at the issue, with some of the info I found pulling me in wrong directions (such as reading the hid library being included in the circuitpython release). But seeing the code work correctly in the Playground Express provided the key to getting the issue fixed. Also, the Gemma M0 might not be the best board to start with for a newbie, as you can't load all of the libraries on it (no external flash), and it gives more chances to make mistakes with libraries. There are several different releases of circuitpython available, and apparently different library releases as well, and they have to match up to work correctly.

Thanks for the reply however!

MKB11
 
Posts: 2
Joined: Fri Aug 14, 2020 10:34 pm

Please be positive and constructive with your questions and comments.