RGB Keypad not working on boot up?

I have the following code that works just fine from Thonny, saved as code.py to my RGB Keypad.

from pmk import PMK
from pmk.platform.rgbkeypadbase import RGBKeypadBase as Hardware

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

pmk = PMK(Hardware())
keys = pmk.keys
#pmk.set_all(255, 0, 255)

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

key0 = keys[0] # Camera 1
key1 = keys[1] # Camera 2
key2 = keys[2] # Camera 3
key3 = keys[3] # Camera 4
key4 = keys[4] # Camera 5
key5 = keys[5] # Camera 6
key6 = keys[6] # Camera 7
key7 = keys[7] # Outlook 
key8 = keys[8] # Enviroment Canada
key9 = keys[9] 
key10 = keys[10] 
key11 = keys[11] 
key12 = keys[12] 
key13 = keys[13] 
key14 = keys[14] 
key15 = keys[15] 


@pmk.on_press(key0)
def press_handler(key0):
    pmk.set_led(0, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.ONE)
    
@pmk.on_press(key1)
def press_handler(key1):
    pmk.set_led(1, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.TWO)
    
@pmk.on_press(key2)
def press_handler(key2):
    pmk.set_led(2, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.THREE)

@pmk.on_press(key3)
def press_handler(key3):
    pmk.set_led(3, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.FOUR)
    
@pmk.on_press(key4)
def press_handler(key4):
    pmk.set_led(4, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.Five)    
    
@pmk.on_press(key5)
def press_handler(key5):
    pmk.set_led(5, 0, 0, 255) 
    keyboard.send(Keycode.GUI, Keycode.SIX)    
    
@pmk.on_press(key6)
def press_handler(key6):
    pmk.set_led(6, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.SEVEN)
    
@pmk.on_press(key7)
def press_handler(key7):
    pmk.set_led(7, 0, 0, 255) 
    keyboard.send(Keycode.GUI, Keycode.EIGHT)
    
@pmk.on_press(key8)
def press_handler(key8):
    pmk.set_led(8, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.NINE)
   
@pmk.on_press(key12)
def press_handler(key12):
    pmk.set_led(12, 0, 0, 255)
    keyboard.send(Keycode.GUI, Keycode.E)
    
@pmk.on_press(key15)
def press_handler(key15):
    pmk.set_led(15, 0, 0, 255) 
    keyboard.send(Keycode.GUI, Keycode.M)    
 
while True:
    # Always remember to call keybow.update()!
    pmk.update()

On boot up Device Manager shows USB Serial Device (COM20)
A second USB HID Keyboard Device and a second USB HID-compliant mouse.
And CIRCUITPY drive is visible in File explorer.
Nothing happens when I press the keys on the RGB Keypad though?

If I press the reset button COM20 etc disappear and reappear. Now pressing the buttons on the Keypad works?

What if anything am I doing wrong?

Is this from a fresh boot, or when coming out of hibernation or sleep? My Keybow starts up fine when my PC is powered on, but it needs a ā€˜resetā€™ to start up if itā€™s waking up from hibernate. I do have some fairly aggressive Windows power saving settings for USB devices though.

Thereā€™s some interesting customisation you can do with adafruit_hid devices in boot.py that might help: Overview | Customizing USB Devices in CircuitPython | Adafruit Learning System

Cold start, it did it several times in a row. This morning though, it worked without having to reset it? Going to try a different USB cable. This one is a really long one, 3M. It was for my XBOX Game controller. Nice beefy power wires, probably not the best choice data wise though.
I donā€™t use sleep or hibernate, just a personal preference thing. I press PC power button, make cup of coffee, come back and its all booted up. Iā€™m retired so no need for instant on etc. ;)
That being said, its a lot more fun just pressing one button to open one of my Motion Eye Camera feeds than messing with my mouse. I have a triple monitor setup.

Iā€™m going to swap the USB cable for something of a normal length and see how it goes.
Thanks for that link, going to have a good look see at that info.

Swapping to a shorter cable didnā€™t help. It looks like it working without having to reset it was just luck? I tried my SparkFun Cerberus USB Hub Cable, and a stock Red Micro USB to USB cable that Pimoroni sell.
If I open Thonny I have access without having to press stop? No ā€œDevice is busyā€ message, itā€™s just sitting there doing nothing? My file is saved as code.py? And runs without error from Thonny? Also runs just fine after pressing the captain resetti reset button.

The examples, rainbow, and reactive-press run on boot. It lights up as soon as I press the power button on the PC.

I found a solution / work around. This PC takes a 40 seconds to boot too the logon screen. Iā€™m guessing my code is erroring out waiting that long? I put a time.sleep(60) in at the top / start and now it works every time. When all the keys light up white I know its ready to go.

import time
time.sleep (60)

import usb_hid

from pmk import PMK
from pmk.platform.rgbkeypadbase import RGBKeypadBase as Hardware
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

pmk = PMK(Hardware())
keys = pmk.keys
pmk.set_all(28, 28, 28)

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

key0 = keys[0] # Camera 1
key1 = keys[1] # Camera 2
key2 = keys[2] # Camera 3
key3 = keys[3] # Camera 4
key4 = keys[4] # Camera 5
key5 = keys[5] # Camera 6
key6 = keys[6] # Camera 7
key7 = keys[7] # Outlook 
key8 = keys[8] # Environment Canada
key9 = keys[9] # Edge
#key10 = keys[10]  
#key11 = keys[11] 
#key12 = keys[12]  
#key13 = keys[13]  
key14 = keys[14] 
key15 = keys[15] 

@pmk.on_press(key0)
def press_handler(key0):
    pmk.set_led(0, 85, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.ONE)
    
@pmk.on_press(key1)
def press_handler(key1):
    pmk.set_led(1, 85, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.TWO)
    
@pmk.on_press(key2)
def press_handler(key2):
    pmk.set_led(2, 85, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.THREE)

@pmk.on_press(key3)
def press_handler(key3):
    pmk.set_led(3, 85, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.FOUR)
    
@pmk.on_press(key4)
def press_handler(key4):
    pmk.set_led(4, 85, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.FIVE)    
    
@pmk.on_press(key5)
def press_handler(key5):
    pmk.set_led(5, 85, 85, 0) 
    keyboard.send(Keycode.GUI, Keycode.SIX)    
    
@pmk.on_press(key6)
def press_handler(key6):
    pmk.set_led(6, 85, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.SEVEN)
    
@pmk.on_press(key7)
def press_handler(key7):
    pmk.set_led(7, 0, 0, 85) 
    keyboard.send(Keycode.GUI, Keycode.EIGHT)
    
@pmk.on_press(key8)
def press_handler(key8):
    pmk.set_led(8, 85, 0, 0)
    keyboard.send(Keycode.GUI, Keycode.NINE)
    
@pmk.on_press(key9)
def press_handler(key9):
    pmk.set_led(9, 0, 85, 0)
    keyboard.send(Keycode.GUI, Keycode.ZERO)
    
#@pmk.on_press(key10)
#def press_handler(key10):
    #pmk.set_led(10, 0, 0, 255)
    #keyboard.send(Keycode.GUI, Keycode.NINE)
    
#@pmk.on_press(key11)
#def press_handler(key11):
    #pmk.set_led(11, 0, 0, 255)
    #keyboard.send(Keycode.GUI, Keycode.E)
    
#@pmk.on_press(key12)
#def press_handler(key12):
#    pmk.set_led(12, 0, 0, 255)
#    keyboard.send(Keycode.GUI, Keycode.E)

#@pmk.on_press(key13)
#def press_handler(key13):
    #pmk.set_led(13, 0, 0, 255)
    #keyboard.send(Keycode.GUI, Keycode.M)
    
@pmk.on_press(key14)
def press_handler(key14):
    pmk.set_all(28, 28, 28)
    keyboard.send(Keycode.GUI, Keycode.M)    
    
@pmk.on_press(key15)
def press_handler(key15):
    pmk.set_all(0, 0, 0) 

while True:
    # Always remember to call keybow.update()!
    pmk.update()
1 Like

Aha, good to know! I think Iā€™ve seen some of the Adafruit macropad code has a time.sleep() at the beginning, presumably to stop similar USB race conditions.

I think Iā€™d have to use the Debug pins to actually see where the code fails. More an issue with this getting old desktop PC than the Pico though, Iā€™m thinking.
I reduced the delay to 40 seconds and it lights up just before the logon screen shows, and still works. Works for me, lol. ;)
Itā€™s a little after 5 AM here and I have a rainbow of color sitting here beside me on my desk. Starting to remember what button does what too. Color coding them helps. I used RED for the ones I donā€™t want mix up and accidentally push.