The same problem occurs of course with a mouse on the native USB port (USBHost).
And the same code cures the problem.
Put the following code in the setup() function:
void setup(){
// --------------------------------------------------------------------------------------
// This will set 5V to the native USB port even if an external power supply is connected.
PIOB->PIO_PER |= 1<<10; // Never do this once Serial.begin() is called,
PIOB->PIO_OER |= 1<<10; // it will disconnect the programming USB port from the PC.
PIOB->PIO_SODR |= 1<<10; // UOTGVBOF HIGH
// --------------------------------------------------------------------------------------
Serial.begin(9600);
}
I use this mouse: http://www.conrad.at/ce/de/product/916448
The mouse should be a straight forward Microsoft compatible USB mouse with three buttons, or two buttons and a wheel which can act as a button. The wheel function can not be used with the USBHost library.
The mouse I got has a LED (near the wheel) which will indicate whether there is 5V at the USB or not.
I hope that this problem (5V at the native USB port while on an external power supply) will be solved in the USBHost and Serial Class. Who will do that? Whom shall I contact?
Greetings, Conrad
This code works:
/*
Mouse Controller Example
Shows the output of a USB Mouse connected to
the Native USB port on an Arduino Due Board.
*/
// Require mouse control library
#include <address.h>
#include <adk.h>
#include <confdescparser.h>
#include <hid.h>
#include <hidboot.h>
#include <hidusagestr.h>
#include <KeyboardController.h>
#include <MouseController.h>
#include <parsetools.h>
#include <Usb.h>
#include <usb_ch9.h>
USBHost usb; // Initialize USB Controller
MouseController mouse(usb); // Attach mouse controller to USB
boolean leftButton = false; // variables for mouse button states
boolean middleButton = false;
boolean rightButton = false;
char inByte = 0; // incoming character from PC
void setup()
{
// --------------------------------------------------------------------------------------
// This will set 5V to the native USB port even if an external power supply is connected.
PIOB->PIO_PER |= 1<<10; // Never do this once Serial.begin() is called,
PIOB->PIO_OER |= 1<<10; // it will disconnect the programming USB port from the PC.
PIOB->PIO_SODR |= 1<<10; // UOTGVBOF HIGH
// --------------------------------------------------------------------------------------
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("from PC : ");
Serial.println(inByte);
if (inByte == 'h') {
PIOB->PIO_PER |= 1<<27;
PIOB->PIO_OER |= 1<<27;
PIOB->PIO_SODR |= 1<<27; // LED HIGH, direct access to Pin 27 at I/O PortB
Serial.println("from PC LED ON");
}
if (inByte == 'l') {
PIOB->PIO_PER |= 1<<27;
PIOB->PIO_OER |= 1<<27;
PIOB->PIO_CODR |= 1<<27; // LED LOW, direct access to Pin 27 at I/O PortB
Serial.println("from PC LED OUT");
}
}
usb.Task(); // Process USB tasks
}
// This function intercepts mouse movements
void mouseMoved() {
Serial.print("Move: ");
Serial.print(mouse.getXChange());
Serial.print(", ");
Serial.println(mouse.getYChange());
}
// This function intercepts mouse movements while a button is pressed
void mouseDragged() {
Serial.print("DRAG: ");
Serial.print(mouse.getXChange());
Serial.print(", ");
Serial.println(mouse.getYChange());
}
// This function intercepts mouse button press
void mousePressed() {
Serial.print("Pressed: ");
if (mouse.getButton(LEFT_BUTTON)){
Serial.print("L");
leftButton = true;
}
if (mouse.getButton(MIDDLE_BUTTON)){
Serial.print("M");
middleButton = true;
}
if (mouse.getButton(RIGHT_BUTTON)){
Serial.print("R");
Serial.println();
rightButton = true;
}
}
// This function intercepts mouse button release
void mouseReleased() {
Serial.print("Released: ");
if (!mouse.getButton(LEFT_BUTTON) && leftButton==true) {
Serial.print("L");
leftButton = false;
}
if (!mouse.getButton(MIDDLE_BUTTON) && middleButton==true) {
Serial.print("M");
middleButton = false;
}
if (!mouse.getButton(RIGHT_BUTTON) && rightButton==true) {
Serial.print("R");
rightButton = false;
}
Serial.println();
}