Hi all,
I originally posted this into Programming Questions but the only response was 'how many people would have Leonardos do you think?". Well, since its also an Arduino Core question as well as a request to see if I've done this properly, I'll post it in here.
I've been playing with what is ostensibly a Leonardo clone - the Freetronics LeoStick. I've compiled the new Arduino 1.0.1 Caterina bootloader and using the new 1.0.1 USBCore.cpp/.h files etc.
The current USB_CONFIG_POWER_MA Macro used as part of the D_CONFIG macro called out of USBCore.cpp is hardcoded at 500ma.
This can cause errors to appear when plugging the device into many Keyboard based USB Ports or Hubs. I'm lead to believe particularly on MacOSX ?
So I've tried to hack in, unfortunately (I hate modifying core code ) the ability to alter this in a sketch, so you can set it in the setup() function. Is this the best way to do this?
This works, and allows you to set any arbitrary integer value to be used for the negotiation of the sketch comport.
Edit USBCore.cpp . At line 47 Add the line
int usbMaxPower = 500;
Edit USBCore.h. Edit the D_CONFIG and set line 284 to be:
#define D_CONFIG(_totalLength,_interfaces) \
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(usbMaxPower) }
By default, it will now negotiate 500ma. However in your sketch, at the top, define
"extern int usbMaxPower" Then you can do in setup(), before calling any USB descriptors (i.e, one of the first things)
"usbMaxPower = 100;"
Test script - the Example Leonardo KeyboardSerial Sketch with the above additions.:
/*
// Comments Snipped
created 21 Oct 2011
modified 27 Mar 2012
by Tom Igoe
This example code is in the public domain.
*/
extern int usbMaxPower; // Import usbMaxPower from USBCore.cpp
void setup() {
// Set USB MaxPower to be 150
usbMaxPower = 150;
// open the serial port:
Serial.begin(9600);
// initialize control over the keyboard:
Keyboard.begin();
// Print usbMaxPower int to Serial to check during setup
Serial.print("usbma ");
Serial.println(usbMaxPower);
}
void loop() {
// check for incoming serial data:
if (Serial.available() > 0) {
// read incoming serial data:
char inChar = Serial.read();
// Print usbMaxPower int to Serial to check
Serial.print("usbma ");
Serial.println(usbMaxPower);
// Type the next ASCII value from what you received:
Keyboard.write(inChar+1);
}
}
Voila! Script defineable usbMaxPower values... albeit very klunky and involving hacks to the USBCore.cpp/USBCore.h - which I was trying to avoid.
Is this the best way to accomplish this for now with 1.0.1 ? What is the long term plans around the USB Descriptors ?
Adrian