SDG+USBHost(Mouse) Sample

Dependencies:   Sound_Generator USBHost_custom

Fork of SDG_Mouse_Sample by GR-PEACH_producer_meeting

Information

Japanese version is available in lower part of this page.
このページの後半に日本語版が用意されています.

What is this?

This program is a demonstration that sounds the sound by mouse operation by using USBHost(Mouse) and Sound Generator.

Settings

Close JP3 of GR-PEACH.
/media/uploads/RyoheiHagimoto/sdg-mouse.jpg

Operation

operationeffect
Right clickSounds
Left clickReset to base tone (C)
Moves the mouse to the rightLower the sound
Moves the mouse to the leftHigher the sound
Center cursorAdjust the sensitivity.
Reset the reference value in the click.

Others

The default setting of serial communication (baud rate etc.) in mbed is shown the following link.
Please refer to the link and change the settings of your PC terminal software.
The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication


概要

このプログラムは、USBHost(Mouse) + Sound Generatorで、マウス操作による擬似笛デモです。

設定

GR-PEACHのJP3をショートする必要があります。
/media/uploads/RyoheiHagimoto/sdg-mouse.jpg

操作方法

操作内容
右クリック音出力開始
左クリック基準音(ド)にリセット
マウス右移動高音になります
マウス左移動低音になります
センターカーソル音高低の変化量調整(クリックで基準値にリセット)

Others

mbedのシリアル通信(ボーレート等)のデフォルト設定は以下のリンクに示しています。
リンクを参考に、お使いのPCターミナルソフトの設定を変更して下さい。
mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Wed Mar 13 10:23:01 2013 +0000
Parent:
4:b320d68e98e7
Child:
6:1571e517a91b
Commit message:
add button, x, y, z event callbacks

Changed in this revision

USBHostHID/USBHostKeyboard.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostHID/USBHostMouse.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostHID/USBHostMouse.h Show annotated file Show diff for this revision Revisions of this file
USBHostMSD/USBHostMSD.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostSerial/USBHostSerial.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USBHostHID/USBHostKeyboard.cpp	Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostHID/USBHostKeyboard.cpp	Wed Mar 13 10:23:01 2013 +0000
@@ -103,13 +103,11 @@
 
 bool USBHostKeyboard::connect() {
     
-    U8 i;
-    
     if (dev_connected) {
         return true;
     }
     
-    for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
         if ((dev = host->getDevice(i)) != NULL) {
 
             if (host->enumerate(dev, this))
@@ -149,11 +147,11 @@
         if (key && onKey) {
             (*onKey)(key);
         }
-        if (onKeyCode) {
+        if ((report[index + 2] || modifier) && onKeyCode) {
             (*onKeyCode)(report[index + 2], modifier);
         }
     }
-    if (dev != NULL)
+    if (dev && int_in)
         host->interruptRead(dev, int_in, report, len_listen, false);
 }
 
--- a/USBHostHID/USBHostMouse.cpp	Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostHID/USBHostMouse.cpp	Wed Mar 13 10:23:01 2013 +0000
@@ -29,10 +29,19 @@
     dev = NULL;
     int_in = NULL;
     onUpdate = NULL;
+    onButtonUpdate = NULL;
+    onXUpdate = NULL;
+    onYUpdate = NULL;
+    onZUpdate = NULL;
     report_id = 0;
     dev_connected = false;
     mouse_device_found = false;
     mouse_intf = -1;
+    
+    buttons = 0;
+    x = 0;
+    y = 0;
+    z = 0;
 }
 
 bool USBHostMouse::connected() {
@@ -40,14 +49,12 @@
 }
 
 bool USBHostMouse::connect() {
-    
-    U8 i;
-    
+
     if (dev_connected) {
         return true;
     }
     
-    for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
         if ((dev = host->getDevice(i)) != NULL) {
 
             if(host->enumerate(dev, this))
@@ -78,10 +85,34 @@
 void USBHostMouse::rxHandler() {
     int len_listen = int_in->getSize();
     int len = int_in->getLengthTransferred();
+    
     if (onUpdate) {
         (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
     }
-    if (dev != NULL)
+    
+    if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
+        (*onButtonUpdate)(report[0] & 0x07);
+    }
+    
+    if (onXUpdate && (x != report[1])) {
+        (*onXUpdate)(report[1]);
+    }
+    
+    if (onYUpdate && (y != report[2])) {
+        (*onYUpdate)(report[2]);
+    }
+    
+    if (onZUpdate && (z != report[3])) {
+        (*onZUpdate)(report[3]);
+    }
+        
+    // update mouse state
+    buttons = report[0] & 0x07;
+    x = report[1];
+    y = report[2];
+    z = report[3];
+    
+    if (dev)
         host->interruptRead(dev, int_in, report, len_listen, false);
 }
 
--- a/USBHostHID/USBHostMouse.h	Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostHID/USBHostMouse.h	Wed Mar 13 10:23:01 2013 +0000
@@ -55,11 +55,55 @@
      *
      * @param ptr function pointer
      */
-    inline void attach(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) {
+    inline void attachEvent(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) {
         if (ptr != NULL) {
             onUpdate = ptr;
         }
     }
+    
+    /**
+     * Attach a callback called when the button state changes
+     *
+     * @param ptr function pointer
+     */
+    inline void attachButtonEvent(void (*ptr)(uint8_t buttons)) {
+        if (ptr != NULL) {
+            onButtonUpdate = ptr;
+        }
+    }
+    
+    /**
+     * Attach a callback called when the X axis value changes
+     *
+     * @param ptr function pointer
+     */
+    inline void attachXEvent(void (*ptr)(int8_t x)) {
+        if (ptr != NULL) {
+            onXUpdate = ptr;
+        }
+    }
+    
+    /**
+     * Attach a callback called when the Y axis value changes
+     *
+     * @param ptr function pointer
+     */
+    inline void attachYEvent(void (*ptr)(int8_t y)) {
+        if (ptr != NULL) {
+            onYUpdate = ptr;
+        }
+    }
+    
+    /**
+     * Attach a callback called when the Z axis value changes (scrolling)
+     *
+     * @param ptr function pointer
+     */
+    inline void attachZEvent(void (*ptr)(int8_t z)) {
+        if (ptr != NULL) {
+            onZUpdate = ptr;
+        }
+    }
 
 protected:
     //From IUSBEnumerator
@@ -77,8 +121,17 @@
     bool mouse_device_found;
     int mouse_intf;
 
+    uint8_t buttons;
+    int8_t x;
+    int8_t y;
+    int8_t z;
+
     void rxHandler();
     void (*onUpdate)(uint8_t buttons, int8_t x, int8_t y, int8_t z);
+    void (*onButtonUpdate)(uint8_t buttons);
+    void (*onXUpdate)(int8_t x);
+    void (*onYUpdate)(int8_t y);
+    void (*onZUpdate)(int8_t z);
     int report_id;
     void init();
 };
--- a/USBHostMSD/USBHostMSD.cpp	Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostMSD/USBHostMSD.cpp	Wed Mar 13 10:23:01 2013 +0000
@@ -60,13 +60,12 @@
 
 bool USBHostMSD::connect()
 {
-    U8 i;
 
     if (dev_connected) {
         return true;
     }
 
-    for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
         if ((dev = host->getDevice(i)) != NULL) {
             
             USB_DBG("Trying to connect MSD device\r\n");
--- a/USBHostSerial/USBHostSerial.cpp	Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostSerial/USBHostSerial.cpp	Wed Mar 13 10:23:01 2013 +0000
@@ -52,7 +52,7 @@
     if (dev_connected) {
         return true;
     }
-    for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
         if ((dev = host->getDevice(i)) != NULL) {
             
             USB_DBG("Trying to connect serial device\r\n");