Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Rotary Trinkey - red flashing LED unless unplugged/replugged
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Rotary Trinkey - red flashing LED unless unplugged/replugged

by ChrisDpi on Thu Oct 21, 2021 4:15 pm

Hi guys,

I have a rotary trinkey on an rPi4 which was working fine as a simple volume control but now does the following:

- power on Pi - trinkey LED flashes yellow as normal when it powers up.
- then LED blinks red twice at 5 second intervals. In the documentation this is supposed to indicate an exception, but..
- if I unplug/replug the Trinkey it works as it should. However the problem returns after the Pi is shutdown/rebooted. I have tested this with nothing else plugged into the Pi USB ports to ensure it’s not a power issue. I’ve also been able to rescan the USB port using the pi terminal in the hope it would replicate physically removing/inserting the board, the port did rescan but the Trinkey still didn’t work - only physically hotplugging it works.

I have also connected it to my Win10 pc and it works perfectly. I can reboot the PC without any Trinkey issues, but on the Pi I get the red LED. I’ve also tried it with different Pi OS memory cards to make sure it’s not a wider software issue.

I have used the latest library files etc to ensure the right versions are installed. Is there a simple answer to this, or could it be a faulty Trinkey?

Many thanks,
Chris

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Sat Oct 23, 2021 11:07 am

Okay, (for now..) I seem to have solved this.

Until this afternoon I was looking at all sorts of stuff (some I tried) such as using a script to re-scan the port after boot (a virtual re-plugging) which didn't work; dropping power to the pi4 usb ports when shutdown; I reinstalled the OS (on two cards); installed and reinstalled Mu Editor on the Pi and PC, cleared the Trinkey memory and installed CP again, re-wrote the script I was using for vol up/down; and checked the code with the Mu checker. I have a feeling that using ftp to the Pi or Windows explorer click and drag both create issues with the Trinkey resulting in it working on Windows, but not on the Pi from boot - perhaps something to do with the file transfer method. I *think* I fixed this by copying the code from my ftp'd file by opening it in Mu Editor on the Pi, and copying and pasting that into the code.py Trinkey file - I know that Mu is the recommended way to go, but I thought that was more to do with not editing using notepad etc. As far as I can see, with the Trinkey now working on all USB ports as soon as the OS is ready, so I think it's very sensitive to the method used to get the code onto it. Oddly, scripts that didn't include the rotary function would work fine (e.g: blinking the LED) - but something about loading rotary modules would result in the errors. So, if the script file transfer/edit method was to blame - perhaps this would also mess with the wider function of Circuitpy on the board affecting rotaryio etc. Anyway, it's working now.

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Sun Oct 24, 2021 3:25 am

Scratch that - definitely a faulty Trinkey. Bought another, again from Pimoroni, and this one works fine. Do Adafruit do refund (or preferably replacement?) for an item bought over 30 days ago?

cheers
Chris

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Wed Oct 27, 2021 5:35 pm

Nope, now the new one does the same. This is definitely an issue with the way the Pi treats the device / vice versa.
If anyone else has this issue, I solved it in a quick and dirty way (I am a novice with this stuff) by using crontab to run a little command at reboot to sleep for a few seconds before playing a tiny wav stored on the Trinkey itself,. The wav doesn’t play (of course, as the drive is not working despite being on the ls usb list) but it does wake up the Trinkey and the circuitpy/code.py works fine.

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by adafruit_support_carter on Fri Oct 29, 2021 1:12 pm

The issue is only when rebooting with the Rotary Trinkey left plugged in?

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

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Fri Oct 29, 2021 1:35 pm

Hi, thanks for replying - this happens on a cold boot, and a reboot.
I have changed nothing in my system config that affects an normal boot (I'm running the standard recommended raspian OS with GUI for the pi4) - other than an instruction to play a video using omx player while the system boots. I am about to get rid of that to see if that is in some way interrupting the USB drivers at boot, but since the Trinkey lights up at boot and worked at first but not later on exactly the same OS I'm not sure if/why this would solve the issue.

thanks
Chris

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by adafruit_support_carter on Fri Oct 29, 2021 2:44 pm

What is the current code.py installed on the Trinkey? We'll take a look here, see if we can recreate, etc.

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

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Sat Oct 30, 2021 12:54 pm

Hi, the cody.py is below. It's a slightly modified version of the Youtube frame by frame code on the adafruit site. I added the relevant mpy files to the .lib folder on the Trinkey, and added the neopixel lines so that the led is green as an easy indicator of when the Trinkey is working correctly (e.g: green instead of the two red flashes when it's not properly initialised). (NB: I had the same problem before the green led lines were added.)

Code: Select all | TOGGLE FULL SIZE
import board
import neopixel
import rotaryio
import usb_hid
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)
switch.switch_to_input(pull=digitalio.Pull.DOWN)
keyboard = Keyboard(usb_hid.devices)

switch_state = None
last_position = encoder.position
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
while True:
    pixels.fill((0, 120, 0))
    current_position = encoder.position
    position_change = current_position - last_position
    if position_change > 0:
        for _ in range(position_change):
            keyboard.send(Keycode.ALT , Keycode.U)
        print(current_position)
    elif position_change < 0:
        for _ in range(-position_change):
            keyboard.send(Keycode.ALT , Keycode.D)
        print(current_position)
    last_position = current_position
Last edited by adafruit_support_carter on Sun Oct 31, 2021 11:05 am, edited 1 time in total.
Reason: added [code] tags

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by jnnfrcbb on Fri Nov 05, 2021 6:53 am

I think I'm having the same problem with a Trinket M0, which is intended to interface between a Raspberry Pi and a car's steering wheel controls. The Trinket is connected to the Pi via USB.

For a while it seemed to work fine. At some point in the last few weeks that changed, though neither the Raspberry Pi or the Trinket was updated in that time, nor were there changes to either the code running on the Trinket or the Pi's setup.

If it's already plugged in to the Raspberry Pi at either cold boot or reboot then it doesn't work. The yellow LEDs flash a bit then I get a repeating pattern of two red LED flashes then an interval then two red LED flashes, and the Trinket is entirely unresponsive. This happens whether or not there are other USB devices connected to the Pi; it happens regardless of which USB port on the Pi the Trinket is connected to; and it happens across multiple Micro USB leads, all of which carry data fine.

If I press the physical reset button on the Trinket it does then work, but it's intended to go into an enclosure out of reach in the car's dashboard so that's not a solution.

If the Raspberry Pi is already running and I then plug the Trinket in, it works as it should.

It also works absolutely fine on my Windows laptop (though Windows does occasionally tell me that there is a problem with the drive when I plug it in).

This is the code I'm running on the Trinket:

Code: Select all | TOGGLE FULL SIZE
import board
from analogio import AnalogIn
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import time

#setup analog inputs
analog0in = AnalogIn(board.D0)
analog1in = AnalogIn(board.D1)

#setup keyboard
kbd = Keyboard(usb_hid.devices)

#setup voltage reader
def getVoltage(pin):
    return (pin.value * 3.3) / 65536

#i = 0

while True:

  VD0 = getVoltage(analog0in) #AD
  VD1 = getVoltage(analog1in) #SHIFT

  if VD1 > 1:
    #print("SHIFT: FALSE")
    if VD0 < 0.1:
      print("BUTTON: OFF")
      kbd.send(Keycode.H)  #HOME
    elif VD0 < 0.7:
      print("BUTTON: SOURCE")
      kbd.send(Keycode.CONTROL, Keycode.F3) #SWITCH MODE
    elif VD0 < 1.1:
      print("BUTTON: ATT")
      kbd.send(Keycode.M)  #VOICE
    elif VD0 < 1.4:
      print("BUTTON: LIST")
      kbd.send(Keycode.CONTROL, Keycode.F11) #TOGGLE MUTE
    elif VD0 < 1.65:
      print("BUTTON: SEEK+")
      kbd.send(Keycode.N)  #NEXT TRACK
    elif VD0 < 1.9:
      print("BUTTON: SEEK-")
      kbd.send(Keycode.V)  #PREVIOUS TRACK
    elif VD0 < 2.15:
      print("BUTTON: VOL+")
      kbd.send(Keycode.F8) #VOLUME up
    elif VD0 < 2.4:
      print("BUTTON: VOL-")
      kbd.send(Keycode.F7) #VOLUME DOWN
    elif VD0 < 2.6:
      print("BUTTON: SEL")
      kbd.send(Keycode.B)  #PLAY/PAUSE
    elif VD0 < 2.8:
      print("BUTTON: MODE")
      kbd.send(Keycode.A) #todo: display on or off - use F19 or something?
  elif VD1 < 1:
    #print("SHIFT: TRUE")
    if VD0 < 1.6:
      print("BUTTON: SHIFTUP")
      kbd.send(Keycode.J)  #LAUNCH MEDIA
    elif VD0 < 1.9:
      print("BUTTON: SHIFTDOWN")
      kbd.send(Keycode.F) #LAUNCH NAVIGATION

  #print (i)

  #i = i + 1

  time.sleep(0.25)


The Trinket has been updated to the latest version of Circuit Python from the Adafruit website, and the latest version of the adafruid_hid library from github.

Any help with this is much appreciated.

jnnfrcbb
 
Posts: 1
Joined: Fri Nov 05, 2021 6:41 am

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Fri Nov 05, 2021 12:40 pm

jnnfrcbbm - that does sound absolutely the same as mine, I also can't reset/unplug the Trinkey as the final project is going to be sealed inside a case, it won't be easy to reach even after I open it.

Looking at your code and mine (if this is a code related thing) the most obvious common element seems to be

Code: Select all | TOGGLE FULL SIZE
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode


A while ago I ran a test by putting just a simple green LED script on the Trinkey and it worked fine. The problem returned as soon as I put a script on there which called the rotaryio lib file for the rotary encoder, but that script also included the above keyboard/keycode lines... Wonder if the issue is down to those files instead. And perhaps the corresponding files on the Windows Trinkey/Trinket software just doesn't have that bug, since both of us could use it on Windows without this issue..

Chris

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by adafruit_support_carter on Fri Nov 05, 2021 1:25 pm

I think the general issue here is a race condition. The code in code.py will run within seconds of the Trinkey being powered - regardless of what else is happening with the Raspberry Pi. Since you are trying to use HID, the code essentially needs to wait until the RPI OS has booted up enough to actually allow opening the HID connection over USB to be supported. As is, it's trying too soon, and fails.

You'll need to restructure the code to deal with two scenarios.

* Cold Boot - Waiting for operating system to boot up on initial power
* Warm Boot - Trinkey will remain powered, but code will suddenly fail since the operating system is rebooting

For the Cold Boot, can add a try/except loop at the top of code that just keeps trying to import and start the HID devices until it succeeds. Rough example
Code: Select all | TOGGLE FULL SIZE
#
# somewhere near top of code, like after the imports
#
kbd_initialized = False

while kbd_initialized is False:
    try:
        kbd = Keyboard()
        kbd_initialized = True
        time.sleep(0.75)
    except:
        kbd_initialized = False
        time.sleep(1)
#
# reset of code down here
#

It'd also help to use the built in NeoPixel to provide status feedback somehow in the above loop. So you'd have a visual cue.

For the Warm Boot, can catch the exception(s) and then initiate a reset of the Trinkey from within CP, which will cause code.py to start over and then the cold boot loop above will kick in and wait until the OS is back up again.
https://learn.adafruit.com/circuitpytho ... -resetting

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

Re: Rotary Trinkey - red flashing LED unless unplugged/replu

by ChrisDpi on Fri Nov 05, 2021 7:33 pm

Thanks Carter, that helps. I am still building my project and the crontab I set up achieves a similar thing - but less sophisticated of course. i have a feeling that when I build my final pi OS I will be referring back to this thread.

ChrisDpi
 
Posts: 8
Joined: Thu Oct 21, 2021 4:00 pm

Please be positive and constructive with your questions and comments.