06-10-2022, 03:17 AM
(This post was last modified: 06-11-2022, 01:04 PM by wdyn1.
Edit Reason: Be explicit about microcontrollers
)
This is a How To guide on making a USB Consumer Controller for Moode Audio (or any other purpose) using CircuitPython >= 7.1.0 , framework, and a compatible microcontroller board. This guide assumes that you have some working knowledge of CircuitPython. At the end of this How To, you will have a USB volume and playback control device working with Moode Audio.
This has been tested on RP2040, SAMD51, and SAMD21 based microcontroller boards. It may work on other microcontrollers, but I don't have the hardware to test. It also has not been tried on SBC Linux computers (eg. RPi zero, 4B, etc., again I don't have any laying around right now...) but in theory could work with some effort.
Step 1:
Download or clone the USB Consumer Controller project repo and copy the contents of the 'CIRCUITPY' folder to your CIRCUITPY drive.
Step 2:
Edit 'CIRCUITPY/usb_cc/usb_cc.json' configurations file on your CIRCUITPY drive to reflect your desired functionality and pin connections.
For example:
The messages are derived from these consumer control messages and the pin names are the pin names that you have connected pushbuttons to on your board (eg. "GP0", "D0", etc).
There are also options for a rotary encoder and led. Connect the encoder and LED to your board as desired and edit those pin names as well. There are also a number of settings that can be changed. For example, you may find that your rotary encoder operates in reverse. In that case, set the "reverse" option to true.
If you want to change the functionality of the rotary encoder, change the messages in the 'volume_up' and 'volume_down' callback functions in 'CIRCUITPY/usb_cc/usb_cc.py'
Step 3:
Set up triggerhappy. There are several guides to setting up triggerhappy on this forum. If you need help, let me know.
Step 4:
Determine and edit the boot delay.
When using this with my Moode setup, I found that I needed to get the timing right to be automatically recognized as a consumer control device. It seems to be after RPi boot starts but before triggerhappy starts. 10 seconds works for me, but you may need a different delay. Power off your Raspberry Pi and plug in your microcontroller board. Power up your RPi and wait for Moode to start. Try the controls. If it works, great!
If the controls do not work, try adjusting the delay value in 'CIRCUITPY/boot.py'
Optional:
The default consumer control library provides only the very basic CC messages to get started. There is an extended CC module available and usable by this project. Download the consumer_control_extended module and copy it to your 'CIRCUITPY/lib' folder. This will override the default library and only use the extended module.
Note that consumer_control_extended is not an exact superset of the default library. For example, "VOLUME_DOWN" vs "VOLUME_DECREMENT" and "VOLUME_UP" vs "VOLUME_INCREMENT". I haven't done a full inventory of differences, so refer to the appropriate documentation for the library/module you are using.
This has been tested on RP2040, SAMD51, and SAMD21 based microcontroller boards. It may work on other microcontrollers, but I don't have the hardware to test. It also has not been tried on SBC Linux computers (eg. RPi zero, 4B, etc., again I don't have any laying around right now...) but in theory could work with some effort.
Step 1:
Download or clone the USB Consumer Controller project repo and copy the contents of the 'CIRCUITPY' folder to your CIRCUITPY drive.
Step 2:
Edit 'CIRCUITPY/usb_cc/usb_cc.json' configurations file on your CIRCUITPY drive to reflect your desired functionality and pin connections.
For example:
Code:
"buttons": {
"peripheral_type": "Buttons",
"value_when_pressed": true,
"pull": true,
"pins": {
### EDIT THESE MESSAGES AND PIN NAMES ###
"SCAN_PREVIOUS_TRACK": "GP0",
"PLAY_PAUSE": "GP1",
"SCAN_NEXT_TRACK": "GP2",
"STOP": "GP3",
"MUTE": "GP4"
}
},
The messages are derived from these consumer control messages and the pin names are the pin names that you have connected pushbuttons to on your board (eg. "GP0", "D0", etc).
There are also options for a rotary encoder and led. Connect the encoder and LED to your board as desired and edit those pin names as well. There are also a number of settings that can be changed. For example, you may find that your rotary encoder operates in reverse. In that case, set the "reverse" option to true.
If you want to change the functionality of the rotary encoder, change the messages in the 'volume_up' and 'volume_down' callback functions in 'CIRCUITPY/usb_cc/usb_cc.py'
Code:
def volume_up():
usb_cc.message = 'VOLUME_INCREMENT'
def volume_down():
usb_cc.message = 'VOLUME_DECREMENT'
Step 3:
Set up triggerhappy. There are several guides to setting up triggerhappy on this forum. If you need help, let me know.
Step 4:
Determine and edit the boot delay.
When using this with my Moode setup, I found that I needed to get the timing right to be automatically recognized as a consumer control device. It seems to be after RPi boot starts but before triggerhappy starts. 10 seconds works for me, but you may need a different delay. Power off your Raspberry Pi and plug in your microcontroller board. Power up your RPi and wait for Moode to start. Try the controls. If it works, great!
If the controls do not work, try adjusting the delay value in 'CIRCUITPY/boot.py'
Code:
import time
# CHANGE THIS VALUE TO CHANGE THE BOOT DELAY
time.sleep(10)
Optional:
The default consumer control library provides only the very basic CC messages to get started. There is an extended CC module available and usable by this project. Download the consumer_control_extended module and copy it to your 'CIRCUITPY/lib' folder. This will override the default library and only use the extended module.
Note that consumer_control_extended is not an exact superset of the default library. For example, "VOLUME_DOWN" vs "VOLUME_DECREMENT" and "VOLUME_UP" vs "VOLUME_INCREMENT". I haven't done a full inventory of differences, so refer to the appropriate documentation for the library/module you are using.