USB Host

Dependencies:   C12832_lcd DebounceInterrupts USBHost mbed

Fork of USBHostMouse_HelloWorld by Samuel Mokrani

USB Mouse

Goal

To build a USB Mouse (Host and device) using two MBEDs and test the communication between them.

Details

This is a host part of the USB mouse Project. The USB mouse project uses two MBED boards.

There is a bubble on the LCD of the Host MBED. The position of this bubble can be controlled using the potentiometers on the device MBED.

Pin Mappings(Device side)

  • Pot1 is used to control Y position of the bubble
  • Pot2 is used to control X position of the bubble
  • Right button on joystick is used to emulate the right click
  • Left button on joystick is used to emulate the left click
  • Center button of joystick is used to emulate the pressing of the wheel

Pin Mapping (Host side)

  • LED1 : Left Click
  • LED2 : Right Click
  • LED3 : Middle Click

Block Diagram/Connection diagram

/media/uploads/bhakti08/block_diagram.jpg

Design Constraints

The output of the USB mouse(device) is adjusted according to the limits of the LCD on the other MBED board(the host). Due to this this mouse positions may be incorrect for any other sizes of the LCD or the Host computer.

Complete Testing(For both device and host)

Case DescriptionOutput ExpectedActual OutputStatus
ConnectionThe USB device should be properly enumerated when connected to the hostConnection recognized by the hostPASS
If the device is disconnected from the host, there should be some error messageWhen the device is disconnected, the LED4 on Host turns OFF and "Connect the USB mouse" is displayed on the LCDPass
Move Pot1 on the device and check the position of bubble on the Host.The pot should be able to control the bubble for the entire range of the LCD.Using Pot1 the Y position is controlled and the bubble moves from topmost position to bottom most positionPASS
Move Pot2 on the device and check the position of bubble on the Host.The pot should be able to control the bubble for the entire range of the LCD.Using Pot2 the X position is controlled and the bubble moves from leftmost position to the rightmost positionPASS
Press the left switch of the joystick on the deviceThe left click should be detected and the LED on the host should turn ONLED2 on the host turns ON. Thus, a left click on the device is detected by the host.PASS
Press the right switch of the joystick on the deviceThe right click should be detected and the LED on the host should turn ONLED1 on the host turns ON. Thus, a right click on the device is detected by the host.PASS
Press the center switch of the joystick on the deviceThe click of the wheel should be detected and the LED on the host should turn ONLED3 on the host turns ON. Thus, a wheel the wheel being clicked the device is detected by the host.PASS
Disconnect the host from the deviceThe LED4 on the host should turn OFFThe LED4 turns OFF along with the message on the LCD "Connect the USB mouse".PASS

Testing (Host Specific)

Test DescriptionExpected OutputActual OutputStatus
PowerThe host should be able to supply power to the host.The device gets power from the host. No external supply is needed for the devicePASS
Load the USB bus and check if required power is supplied to the deviceThe host should be capable of supplying the power required by the device even under full load conditionNot TestedNA
Check the host outputs before connecting the device to the hostThe host LED4 (Connection LED) should be OFF and the error message should be displayed on the LCD.LED4 on host turns OFF and the error is displayedPASS

USB Host Program

Import programUSB_Project_Host

USB Host

main.cpp

/*****************************************************************************/
/*                                   USB MOUSE HOST                          */
/*Function: This program is used to connect the MBED USB mouse. The bubble   */
/*          on the LCD moves according to the input from the USB device.     */
/*          The program for the MBED USB Device can be found at following    */
/*          location:                                                        */
/*          <http://mbed.org/users/bhakti08/code/USB_Project_Device/>        */
/*Author:   Bhakti Kulkarni                                                  */
/*Date:     03/27/2014                                                       */
/*****************************************************************************/

#include "mbed.h"
#include "USBHostMouse.h"
#include "C12832_lcd.h"
#include "DebouncedInterrupt.h"
#include "DebouncedIn.h"

DigitalOut connect(LED4);     //LED to indicate USB Mouse connected
C12832_LCD lcd;
Serial pc(USBTX,USBRX);
BusOut button (LED1,LED2,LED3);   //LEDs for the Button pressed on Device

/*Global Variables to process the X abd Y values*/
int x_lcd=0,y_lcd=0;
int Xpos=0,Ypos=0;
uint8_t buttons; 
int8_t x; 
int8_t y; 
int8_t z;
uint8_t b;

/*****************************************************************************/
/*Function: OnMouseEvent()                                                   */
/*          This function is called whenever there is change in mouse data   */
/*          It copies the values of x,y and buttons from the device into     */
/*          variables that can be accessed by the main logic to move the     */
/*          bubble on the LCD                                                */
/*Inputs:   The x,y and button values from the Mouse.                        */
/*Outputs:  None.                                                            */
/*****************************************************************************/
void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
    pc.printf("Mouse values: x= %d, y= %d\r\n",x,y);
    //pc.printf("Buttons are: %d/r/n",buttons);
    Xpos = x;
    Ypos = y;
    b = buttons;// & 0x03;
}
/*****************************************************************************/

/*****************************************************************************/
/*This thread is the mouse_task thread which will check for the USB Device   */
/*connectivity. If the device is not connected it will issue an error message*/
/*Once the device is connected, this thread will attach the mouse event      */
/*thread.                                                                    */
/*****************************************************************************/
void mouse_task(void const *) {
    
    USBHostMouse mouse;
    
    while(1) {
        
        // try to connect a USB mouse
        while(!mouse.connect()){
            lcd.cls();
            lcd.locate(0,0);
            lcd.printf("Connect the USB Mouse\n");
            Thread::wait(500);
        }
    
        // when connected, attach handler called on mouse event
        mouse.attachEvent(onMouseEvent);
        
        // wait until the mouse is disconnected
        while(mouse.connected()){
            lcd.cls();
            connect = 1;
            lcd.fillcircle(Xpos,Ypos, 2, 1); 
            wait(.05);
            lcd.fillcircle(Xpos,Ypos, 2, 1);
            pc.printf("%d\r\n",b); 
            button = b;
            Thread::wait(100);
        }
         connect= 0;
    }
}
/*****************************************************************************/

/*****************************************************************************/
/*This is the main thread. This will do nothing but initiate the mouse task  */
/*thread and then wait forever in the loop                                   */
/*****************************************************************************/ 
int main() {
    Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
    while(1) {
        Thread::wait(osWaitForever);
    }
}
/*****************************************************************************/

USB Device code

Import programUSB_Project_Device

This is a USB mouse(Device). The mouse positions can be changed with the potentiometers on the MBD

main.cpp

/*****************************************************************************/
/*Function: This program is used to emulate a USB Mouse. The mouse position  */
/*          can be changed by the potentiometers. The output values of x and */
/*          y are modified to move the bubble on the LCD on another MBED     */
/*          acting as USB Host. The program for MBED USB Host can be found at*/
/*          http://mbed.org/users/bhakti08/code/USB_Project_Host/            */
/*Author:   Bhakti Kulkarni                                                  */
/*Date:     03/27/2014                                                       */
/*****************************************************************************/
#include <mbed.h>
#include <USBMouse.h>
#include "DebouncedIn.h"
#include "C12832_lcd.h"
#include "DebouncedInterrupt.h"

#define RANGE 0.02
#define INTERRUPT
#define X_MAX 120
#define Y_MAX 28
#define x_move 7
#define y_move 3

Serial pc(USBTX,USBRX);

DebouncedIn right_click(p13);      //Button enulating right click
DebouncedIn left_click(p16);       //Button emulating left click
DebouncedIn wheel(p14);            //Button emulating the wheel button
C12832_LCD lcd;
AnalogIn X_in(p20);               //Analog input to move bubble in x position
AnalogIn Y_in(p19);               //Analog input to move bubble in y position
 
USBMouse  Mouse;

int Xpos = 0;
int Ypos = 0;

/*****************************************************************************/ 
/*This is the main program. This reads the input from the potentiometers,    */
/*converts them into the values that are required for the lcd display        */
/*and then sends them as the mouse outputs.                                  */
/*****************************************************************************/ 
int main()
{
   float AinX_old = 0;
   float AinX_new;
   float AinY_old = 0;
   float AinY_new;
   while (true){
        if(left_click)
        {
            Mouse.click(MOUSE_LEFT);  //if left click is pressed,
                                      // emulate mouse left
        }
        if (right_click)
        {
            Mouse.click(MOUSE_RIGHT);  //if right click is pressed,
                                      // emulate mouse right
        }
        if (wheel)
        {
            Mouse.click(MOUSE_MIDDLE); //if wheel button is pressed,
                                      // emulate wheel button pressed
        }
        AinX_new = X_in.read();
        //check if the values are between the LCD values
        if (AinX_new >= AinX_old + RANGE){         
            if (Xpos < X_MAX)
                Xpos += x_move;
            else
                Xpos = Xpos;
            AinX_old = AinX_new;        
        }
        else if (AinX_new < AinX_old - RANGE){
            if (Xpos > 0)
                Xpos -= x_move;
            else
                Xpos = Xpos;
            AinX_old = AinX_new;        
        }
        else
            Xpos = Xpos;
            
        AinY_new = Y_in.read();
        if (AinY_new >= AinY_old + RANGE){
            if (Ypos < Y_MAX)
                Ypos += y_move;
            else
                Ypos = Ypos;
            AinY_old = AinY_new;        
        }
        else if (AinY_new < AinY_old - RANGE){
            if (Ypos > 0)
                Ypos -= y_move;
            else
                Ypos = Ypos;
            AinY_old = AinY_new;        
        }
        else
            Ypos = Ypos;
        Mouse.move(Xpos,Ypos);      //Send the values as the mouse output
    }
}

/*****************************************************************************/
Committer:
bhakti08
Date:
Thu Mar 27 18:11:50 2014 +0000
Revision:
8:1b64fa37f082
Parent:
7:f740e1a3890f
Modified to add comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhakti08 8:1b64fa37f082 1 /*****************************************************************************/
bhakti08 8:1b64fa37f082 2 /* USB MOUSE HOST */
bhakti08 8:1b64fa37f082 3 /*Function: This program is used to connect the MBED USB mouse. The bubble */
bhakti08 8:1b64fa37f082 4 /* on the LCD moves according to the input from the USB device. */
bhakti08 8:1b64fa37f082 5 /* The program for the MBED USB Device can be found at following */
bhakti08 8:1b64fa37f082 6 /* location: */
bhakti08 8:1b64fa37f082 7 /* <http://mbed.org/users/bhakti08/code/USB_Project_Device/> */
bhakti08 8:1b64fa37f082 8 /*Author: Bhakti Kulkarni */
bhakti08 8:1b64fa37f082 9 /*Date: 03/27/2014 */
bhakti08 8:1b64fa37f082 10 /*****************************************************************************/
bhakti08 8:1b64fa37f082 11
samux 0:c14069b9487e 12 #include "mbed.h"
samux 0:c14069b9487e 13 #include "USBHostMouse.h"
bhakti08 7:f740e1a3890f 14 #include "C12832_lcd.h"
bhakti08 7:f740e1a3890f 15 #include "DebouncedInterrupt.h"
bhakti08 7:f740e1a3890f 16 #include "DebouncedIn.h"
samux 0:c14069b9487e 17
bhakti08 8:1b64fa37f082 18 DigitalOut connect(LED1); //LED to indicate USB Mouse connected
bhakti08 7:f740e1a3890f 19 C12832_LCD lcd;
bhakti08 7:f740e1a3890f 20 Serial pc(USBTX,USBRX);
bhakti08 8:1b64fa37f082 21 BusOut button (LED2,LED3,LED4); //LEDs for the Button pressed on Device
bhakti08 7:f740e1a3890f 22
bhakti08 8:1b64fa37f082 23 /*Global Variables to process the X abd Y values*/
bhakti08 7:f740e1a3890f 24 int x_lcd=0,y_lcd=0;
bhakti08 7:f740e1a3890f 25 int Xpos=0,Ypos=0;
bhakti08 7:f740e1a3890f 26 uint8_t buttons;
bhakti08 7:f740e1a3890f 27 int8_t x;
bhakti08 7:f740e1a3890f 28 int8_t y;
bhakti08 7:f740e1a3890f 29 int8_t z;
samux 0:c14069b9487e 30
bhakti08 8:1b64fa37f082 31 /*****************************************************************************/
bhakti08 8:1b64fa37f082 32 /*Function: OnMouseEvent() */
bhakti08 8:1b64fa37f082 33 /* This function is called whenever there is change in mouse data */
bhakti08 8:1b64fa37f082 34 /* It copies the values of x,y and buttons from the device into */
bhakti08 8:1b64fa37f082 35 /* variables that can be accessed by the main logic to move the */
bhakti08 8:1b64fa37f082 36 /* bubble on the LCD */
bhakti08 8:1b64fa37f082 37 /*Inputs: The x,y and button values from the Mouse. */
bhakti08 8:1b64fa37f082 38 /*Outputs: None. */
bhakti08 8:1b64fa37f082 39 /*****************************************************************************/
samux 0:c14069b9487e 40 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
bhakti08 7:f740e1a3890f 41 pc.printf("Mouse values: x= %d, y= %d\r\n",x,y);
bhakti08 7:f740e1a3890f 42 Xpos = x;
bhakti08 7:f740e1a3890f 43 Ypos = y;
bhakti08 7:f740e1a3890f 44 button = buttons;
samux 0:c14069b9487e 45 }
bhakti08 8:1b64fa37f082 46 /*****************************************************************************/
samux 0:c14069b9487e 47
bhakti08 8:1b64fa37f082 48 /*****************************************************************************/
bhakti08 8:1b64fa37f082 49 /*This thread is the mouse_task thread which will check for the USB Device */
bhakti08 8:1b64fa37f082 50 /*connectivity. If the device is not connected it will issue an error message*/
bhakti08 8:1b64fa37f082 51 /*Once the device is connected, this thread will attach the mouse event */
bhakti08 8:1b64fa37f082 52 /*thread. */
bhakti08 8:1b64fa37f082 53 /*****************************************************************************/
samux 0:c14069b9487e 54 void mouse_task(void const *) {
samux 0:c14069b9487e 55
samux 0:c14069b9487e 56 USBHostMouse mouse;
samux 0:c14069b9487e 57
samux 0:c14069b9487e 58 while(1) {
bhakti08 7:f740e1a3890f 59
samux 0:c14069b9487e 60 // try to connect a USB mouse
bhakti08 8:1b64fa37f082 61 while(!mouse.connect()){
bhakti08 8:1b64fa37f082 62 lcd.cls();
bhakti08 8:1b64fa37f082 63 lcd.locate(0,0);
bhakti08 8:1b64fa37f082 64 lcd.printf("Connect the USB Mouse\n");
samux 0:c14069b9487e 65 Thread::wait(500);
bhakti08 8:1b64fa37f082 66 }
samux 0:c14069b9487e 67
samux 0:c14069b9487e 68 // when connected, attach handler called on mouse event
samux 2:be0aafb2edc2 69 mouse.attachEvent(onMouseEvent);
samux 0:c14069b9487e 70
samux 0:c14069b9487e 71 // wait until the mouse is disconnected
bhakti08 7:f740e1a3890f 72 while(mouse.connected()){
bhakti08 7:f740e1a3890f 73 lcd.cls();
bhakti08 8:1b64fa37f082 74 connect = 1;
bhakti08 7:f740e1a3890f 75 lcd.fillcircle(Xpos,Ypos, 2, 1);
bhakti08 7:f740e1a3890f 76 wait(.05);
bhakti08 7:f740e1a3890f 77 lcd.fillcircle(Xpos,Ypos, 2, 1);
bhakti08 7:f740e1a3890f 78 Thread::wait(100);
bhakti08 7:f740e1a3890f 79 }
bhakti08 8:1b64fa37f082 80 connect= 0;
samux 0:c14069b9487e 81 }
samux 0:c14069b9487e 82 }
bhakti08 8:1b64fa37f082 83 /*****************************************************************************/
bhakti08 8:1b64fa37f082 84
bhakti08 8:1b64fa37f082 85 /*****************************************************************************/
bhakti08 8:1b64fa37f082 86 /*This is the main thread. This will do nothing but initiate the mouse task */
bhakti08 8:1b64fa37f082 87 /*thread and then wait forever in the loop */
bhakti08 8:1b64fa37f082 88 /*****************************************************************************/
samux 0:c14069b9487e 89 int main() {
samux 0:c14069b9487e 90 Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
samux 0:c14069b9487e 91 while(1) {
bhakti08 7:f740e1a3890f 92 Thread::wait(osWaitForever);
samux 0:c14069b9487e 93 }
bhakti08 8:1b64fa37f082 94 }
bhakti08 8:1b64fa37f082 95 /*****************************************************************************/