SDG+USBHost(Mouse) Sample
Dependencies: Sound_Generator USBHost_custom
Fork of SDG_Mouse_Sample by
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.
Operation
| operation | effect |
|---|---|
| Right click | Sounds |
| Left click | Reset to base tone (C) |
| Moves the mouse to the right | Lower the sound |
| Moves the mouse to the left | Higher the sound |
| Center cursor | Adjust 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をショートする必要があります。
操作方法
| 操作 | 内容 |
|---|---|
| 右クリック | 音出力開始 |
| 左クリック | 基準音(ド)にリセット |
| マウス右移動 | 高音になります |
| マウス左移動 | 低音になります |
| センターカーソル | 音高低の変化量調整(クリックで基準値にリセット) |
Others
mbedのシリアル通信(ボーレート等)のデフォルト設定は以下のリンクに示しています。
リンクを参考に、お使いのPCターミナルソフトの設定を変更して下さい。
mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication
USBHost3GModule/USBSerialStream.cpp@17:c7b1b8451598, 2013-10-16 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Oct 16 14:15:18 2013 +0100
- Revision:
- 17:c7b1b8451598
Synchronized with git revision d8c3822c4c4e995cd7eaff0ce99f2d3284dde9cd
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 17:c7b1b8451598 | 1 | /* USBSerialStream.cpp */ |
| mbed_official | 17:c7b1b8451598 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
| mbed_official | 17:c7b1b8451598 | 3 | * |
| mbed_official | 17:c7b1b8451598 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| mbed_official | 17:c7b1b8451598 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| mbed_official | 17:c7b1b8451598 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| mbed_official | 17:c7b1b8451598 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| mbed_official | 17:c7b1b8451598 | 8 | * furnished to do so, subject to the following conditions: |
| mbed_official | 17:c7b1b8451598 | 9 | * |
| mbed_official | 17:c7b1b8451598 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
| mbed_official | 17:c7b1b8451598 | 11 | * substantial portions of the Software. |
| mbed_official | 17:c7b1b8451598 | 12 | * |
| mbed_official | 17:c7b1b8451598 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| mbed_official | 17:c7b1b8451598 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| mbed_official | 17:c7b1b8451598 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| mbed_official | 17:c7b1b8451598 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| mbed_official | 17:c7b1b8451598 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| mbed_official | 17:c7b1b8451598 | 18 | */ |
| mbed_official | 17:c7b1b8451598 | 19 | |
| mbed_official | 17:c7b1b8451598 | 20 | #define __DEBUG__ 0 |
| mbed_official | 17:c7b1b8451598 | 21 | #ifndef __MODULE__ |
| mbed_official | 17:c7b1b8451598 | 22 | #define __MODULE__ "USBSerialStream.cpp" |
| mbed_official | 17:c7b1b8451598 | 23 | #endif |
| mbed_official | 17:c7b1b8451598 | 24 | |
| mbed_official | 17:c7b1b8451598 | 25 | #include "core/fwk.h" |
| mbed_official | 17:c7b1b8451598 | 26 | |
| mbed_official | 17:c7b1b8451598 | 27 | #include <cstring> |
| mbed_official | 17:c7b1b8451598 | 28 | |
| mbed_official | 17:c7b1b8451598 | 29 | #include "USBSerialStream.h" |
| mbed_official | 17:c7b1b8451598 | 30 | |
| mbed_official | 17:c7b1b8451598 | 31 | |
| mbed_official | 17:c7b1b8451598 | 32 | USBSerialStream::USBSerialStream(IUSBHostSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true), |
| mbed_official | 17:c7b1b8451598 | 33 | m_availableSphre(1), m_spaceSphre(1), m_inBuf() |
| mbed_official | 17:c7b1b8451598 | 34 | { |
| mbed_official | 17:c7b1b8451598 | 35 | m_availableSphre.wait(); |
| mbed_official | 17:c7b1b8451598 | 36 | m_spaceSphre.wait(); |
| mbed_official | 17:c7b1b8451598 | 37 | //Attach interrupts |
| mbed_official | 17:c7b1b8451598 | 38 | m_serial.attach(this); |
| mbed_official | 17:c7b1b8451598 | 39 | } |
| mbed_official | 17:c7b1b8451598 | 40 | |
| mbed_official | 17:c7b1b8451598 | 41 | /*virtual*/ USBSerialStream::~USBSerialStream() |
| mbed_official | 17:c7b1b8451598 | 42 | { |
| mbed_official | 17:c7b1b8451598 | 43 | m_serial.attach(NULL); |
| mbed_official | 17:c7b1b8451598 | 44 | } |
| mbed_official | 17:c7b1b8451598 | 45 | |
| mbed_official | 17:c7b1b8451598 | 46 | //0 for non-blocking (returns immediately), -1 for infinite blocking |
| mbed_official | 17:c7b1b8451598 | 47 | /*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/) |
| mbed_official | 17:c7b1b8451598 | 48 | { |
| mbed_official | 17:c7b1b8451598 | 49 | DBG("Trying to read at most %d chars", maxLength); |
| mbed_official | 17:c7b1b8451598 | 50 | int ret = waitAvailable(timeout); |
| mbed_official | 17:c7b1b8451598 | 51 | if(ret) |
| mbed_official | 17:c7b1b8451598 | 52 | { |
| mbed_official | 17:c7b1b8451598 | 53 | WARN("Error %d while waiting for incoming data", ret); |
| mbed_official | 17:c7b1b8451598 | 54 | return ret; |
| mbed_official | 17:c7b1b8451598 | 55 | } |
| mbed_official | 17:c7b1b8451598 | 56 | int a = available(); //Prevent macro issues |
| mbed_official | 17:c7b1b8451598 | 57 | int readLen = MIN( a, maxLength ); |
| mbed_official | 17:c7b1b8451598 | 58 | *pLength = readLen; |
| mbed_official | 17:c7b1b8451598 | 59 | |
| mbed_official | 17:c7b1b8451598 | 60 | setupReadableISR(false); |
| mbed_official | 17:c7b1b8451598 | 61 | while(readLen--) |
| mbed_official | 17:c7b1b8451598 | 62 | { |
| mbed_official | 17:c7b1b8451598 | 63 | m_inBuf.dequeue(buf); |
| mbed_official | 17:c7b1b8451598 | 64 | buf++; |
| mbed_official | 17:c7b1b8451598 | 65 | } |
| mbed_official | 17:c7b1b8451598 | 66 | setupReadableISR(true); |
| mbed_official | 17:c7b1b8451598 | 67 | DBG("Read %d chars successfully", *pLength); |
| mbed_official | 17:c7b1b8451598 | 68 | return OK; |
| mbed_official | 17:c7b1b8451598 | 69 | } |
| mbed_official | 17:c7b1b8451598 | 70 | |
| mbed_official | 17:c7b1b8451598 | 71 | /*virtual*/ size_t USBSerialStream::available() |
| mbed_official | 17:c7b1b8451598 | 72 | { |
| mbed_official | 17:c7b1b8451598 | 73 | setupReadableISR(false); //m_inBuf.available() is not reentrant |
| mbed_official | 17:c7b1b8451598 | 74 | size_t len = m_inBuf.available(); |
| mbed_official | 17:c7b1b8451598 | 75 | setupReadableISR(true); |
| mbed_official | 17:c7b1b8451598 | 76 | return len; |
| mbed_official | 17:c7b1b8451598 | 77 | } |
| mbed_official | 17:c7b1b8451598 | 78 | |
| mbed_official | 17:c7b1b8451598 | 79 | /*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available |
| mbed_official | 17:c7b1b8451598 | 80 | { |
| mbed_official | 17:c7b1b8451598 | 81 | int ret; |
| mbed_official | 17:c7b1b8451598 | 82 | if(available()) //Is data already available? |
| mbed_official | 17:c7b1b8451598 | 83 | { |
| mbed_official | 17:c7b1b8451598 | 84 | while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available |
| mbed_official | 17:c7b1b8451598 | 85 | return OK; |
| mbed_official | 17:c7b1b8451598 | 86 | } |
| mbed_official | 17:c7b1b8451598 | 87 | |
| mbed_official | 17:c7b1b8451598 | 88 | DBG("Waiting for data availability %d ms (-1 is infinite)", timeout); |
| mbed_official | 17:c7b1b8451598 | 89 | ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort |
| mbed_official | 17:c7b1b8451598 | 90 | if(ret <= 0) |
| mbed_official | 17:c7b1b8451598 | 91 | { |
| mbed_official | 17:c7b1b8451598 | 92 | DBG("Timeout"); |
| mbed_official | 17:c7b1b8451598 | 93 | return NET_TIMEOUT; |
| mbed_official | 17:c7b1b8451598 | 94 | } |
| mbed_official | 17:c7b1b8451598 | 95 | if(!m_inBuf.available()) //Even if abort has been called, return that data is available |
| mbed_official | 17:c7b1b8451598 | 96 | { |
| mbed_official | 17:c7b1b8451598 | 97 | DBG("Aborted"); |
| mbed_official | 17:c7b1b8451598 | 98 | return NET_INTERRUPTED; |
| mbed_official | 17:c7b1b8451598 | 99 | } |
| mbed_official | 17:c7b1b8451598 | 100 | DBG("Finished waiting"); |
| mbed_official | 17:c7b1b8451598 | 101 | while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available |
| mbed_official | 17:c7b1b8451598 | 102 | return OK; |
| mbed_official | 17:c7b1b8451598 | 103 | } |
| mbed_official | 17:c7b1b8451598 | 104 | |
| mbed_official | 17:c7b1b8451598 | 105 | /*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation |
| mbed_official | 17:c7b1b8451598 | 106 | { |
| mbed_official | 17:c7b1b8451598 | 107 | if( /*!available()*/true ) //If there is data pending, no need to abort |
| mbed_official | 17:c7b1b8451598 | 108 | { |
| mbed_official | 17:c7b1b8451598 | 109 | m_availableSphre.release(); //Force exiting the waiting state |
| mbed_official | 17:c7b1b8451598 | 110 | } |
| mbed_official | 17:c7b1b8451598 | 111 | else |
| mbed_official | 17:c7b1b8451598 | 112 | { |
| mbed_official | 17:c7b1b8451598 | 113 | DBG("Serial is readable"); ; |
| mbed_official | 17:c7b1b8451598 | 114 | } |
| mbed_official | 17:c7b1b8451598 | 115 | return OK; |
| mbed_official | 17:c7b1b8451598 | 116 | } |
| mbed_official | 17:c7b1b8451598 | 117 | |
| mbed_official | 17:c7b1b8451598 | 118 | void USBSerialStream::setupReadableISR(bool en) |
| mbed_official | 17:c7b1b8451598 | 119 | { |
| mbed_official | 17:c7b1b8451598 | 120 | m_serial.setupIrq(en, IUSBHostSerial::RxIrq); |
| mbed_official | 17:c7b1b8451598 | 121 | } |
| mbed_official | 17:c7b1b8451598 | 122 | |
| mbed_official | 17:c7b1b8451598 | 123 | void USBSerialStream::readable() //Callback from m_serial when new data is available |
| mbed_official | 17:c7b1b8451598 | 124 | { |
| mbed_official | 17:c7b1b8451598 | 125 | while(m_serial.readable()) |
| mbed_official | 17:c7b1b8451598 | 126 | { |
| mbed_official | 17:c7b1b8451598 | 127 | m_inBuf.queue(m_serial.getc()); |
| mbed_official | 17:c7b1b8451598 | 128 | } |
| mbed_official | 17:c7b1b8451598 | 129 | m_serial.readPacket(); //Start read of next packet |
| mbed_official | 17:c7b1b8451598 | 130 | m_availableSphre.release(); //Force exiting the waiting state |
| mbed_official | 17:c7b1b8451598 | 131 | } |
| mbed_official | 17:c7b1b8451598 | 132 | |
| mbed_official | 17:c7b1b8451598 | 133 | //0 for non-blocking (returns immediately), -1 for infinite blocking |
| mbed_official | 17:c7b1b8451598 | 134 | /*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/) |
| mbed_official | 17:c7b1b8451598 | 135 | { |
| mbed_official | 17:c7b1b8451598 | 136 | DBG("Trying to write %d chars", length); |
| mbed_official | 17:c7b1b8451598 | 137 | do |
| mbed_official | 17:c7b1b8451598 | 138 | { |
| mbed_official | 17:c7b1b8451598 | 139 | int ret = waitSpace(timeout); |
| mbed_official | 17:c7b1b8451598 | 140 | if(ret) |
| mbed_official | 17:c7b1b8451598 | 141 | { |
| mbed_official | 17:c7b1b8451598 | 142 | WARN("Error %d while waiting for space", ret); |
| mbed_official | 17:c7b1b8451598 | 143 | return ret; |
| mbed_official | 17:c7b1b8451598 | 144 | } |
| mbed_official | 17:c7b1b8451598 | 145 | int s = space(); //Prevent macro issues |
| mbed_official | 17:c7b1b8451598 | 146 | int writeLen = MIN( s, length ); |
| mbed_official | 17:c7b1b8451598 | 147 | DBG("Writing %d chars", writeLen); |
| mbed_official | 17:c7b1b8451598 | 148 | setupWriteableISR(false); |
| mbed_official | 17:c7b1b8451598 | 149 | while(writeLen) |
| mbed_official | 17:c7b1b8451598 | 150 | { |
| mbed_official | 17:c7b1b8451598 | 151 | m_outBuf.queue(*buf); |
| mbed_official | 17:c7b1b8451598 | 152 | buf++; |
| mbed_official | 17:c7b1b8451598 | 153 | length--; |
| mbed_official | 17:c7b1b8451598 | 154 | writeLen--; |
| mbed_official | 17:c7b1b8451598 | 155 | } |
| mbed_official | 17:c7b1b8451598 | 156 | //If m_serial tx fifo is empty we need to start the packet write |
| mbed_official | 17:c7b1b8451598 | 157 | if( m_outBuf.available() && m_serialTxFifoEmpty ) |
| mbed_official | 17:c7b1b8451598 | 158 | { |
| mbed_official | 17:c7b1b8451598 | 159 | writeable(); |
| mbed_official | 17:c7b1b8451598 | 160 | } |
| mbed_official | 17:c7b1b8451598 | 161 | setupWriteableISR(true); |
| mbed_official | 17:c7b1b8451598 | 162 | } while(length); |
| mbed_official | 17:c7b1b8451598 | 163 | |
| mbed_official | 17:c7b1b8451598 | 164 | DBG("Write successful"); |
| mbed_official | 17:c7b1b8451598 | 165 | return OK; |
| mbed_official | 17:c7b1b8451598 | 166 | } |
| mbed_official | 17:c7b1b8451598 | 167 | |
| mbed_official | 17:c7b1b8451598 | 168 | /*virtual*/ size_t USBSerialStream::space() |
| mbed_official | 17:c7b1b8451598 | 169 | { |
| mbed_official | 17:c7b1b8451598 | 170 | setupWriteableISR(false); //m_outBuf.available() is not reentrant |
| mbed_official | 17:c7b1b8451598 | 171 | size_t len = CIRCBUF_SIZE - m_outBuf.available(); |
| mbed_official | 17:c7b1b8451598 | 172 | setupWriteableISR(true); |
| mbed_official | 17:c7b1b8451598 | 173 | return len; |
| mbed_official | 17:c7b1b8451598 | 174 | } |
| mbed_official | 17:c7b1b8451598 | 175 | |
| mbed_official | 17:c7b1b8451598 | 176 | /*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout/*=-1*/) //Wait for space to be available |
| mbed_official | 17:c7b1b8451598 | 177 | { |
| mbed_official | 17:c7b1b8451598 | 178 | int ret; |
| mbed_official | 17:c7b1b8451598 | 179 | if(space()) //Is still space already left? |
| mbed_official | 17:c7b1b8451598 | 180 | { |
| mbed_official | 17:c7b1b8451598 | 181 | while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available |
| mbed_official | 17:c7b1b8451598 | 182 | return OK; |
| mbed_official | 17:c7b1b8451598 | 183 | } |
| mbed_official | 17:c7b1b8451598 | 184 | |
| mbed_official | 17:c7b1b8451598 | 185 | DBG("Waiting for data space %d ms (-1 is infinite)", timeout); |
| mbed_official | 17:c7b1b8451598 | 186 | ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort |
| mbed_official | 17:c7b1b8451598 | 187 | if(ret <= 0) |
| mbed_official | 17:c7b1b8451598 | 188 | { |
| mbed_official | 17:c7b1b8451598 | 189 | DBG("Timeout"); |
| mbed_official | 17:c7b1b8451598 | 190 | return NET_TIMEOUT; |
| mbed_official | 17:c7b1b8451598 | 191 | } |
| mbed_official | 17:c7b1b8451598 | 192 | if(!space()) //Even if abort has been called, return that space is available |
| mbed_official | 17:c7b1b8451598 | 193 | { |
| mbed_official | 17:c7b1b8451598 | 194 | DBG("Aborted"); |
| mbed_official | 17:c7b1b8451598 | 195 | return NET_INTERRUPTED; |
| mbed_official | 17:c7b1b8451598 | 196 | } |
| mbed_official | 17:c7b1b8451598 | 197 | while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available |
| mbed_official | 17:c7b1b8451598 | 198 | return OK; |
| mbed_official | 17:c7b1b8451598 | 199 | } |
| mbed_official | 17:c7b1b8451598 | 200 | |
| mbed_official | 17:c7b1b8451598 | 201 | /*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation |
| mbed_official | 17:c7b1b8451598 | 202 | { |
| mbed_official | 17:c7b1b8451598 | 203 | if( !space() ) //If there is space left, no need to abort |
| mbed_official | 17:c7b1b8451598 | 204 | { |
| mbed_official | 17:c7b1b8451598 | 205 | m_spaceSphre.release(); //Force exiting the waiting state |
| mbed_official | 17:c7b1b8451598 | 206 | } |
| mbed_official | 17:c7b1b8451598 | 207 | return OK; |
| mbed_official | 17:c7b1b8451598 | 208 | } |
| mbed_official | 17:c7b1b8451598 | 209 | |
| mbed_official | 17:c7b1b8451598 | 210 | void USBSerialStream::setupWriteableISR(bool en) |
| mbed_official | 17:c7b1b8451598 | 211 | { |
| mbed_official | 17:c7b1b8451598 | 212 | m_serial.setupIrq(en, IUSBHostSerial::TxIrq); |
| mbed_official | 17:c7b1b8451598 | 213 | } |
| mbed_official | 17:c7b1b8451598 | 214 | |
| mbed_official | 17:c7b1b8451598 | 215 | void USBSerialStream::writeable() //Callback from m_serial when new space is available |
| mbed_official | 17:c7b1b8451598 | 216 | { |
| mbed_official | 17:c7b1b8451598 | 217 | if(m_outBuf.isEmpty()) |
| mbed_official | 17:c7b1b8451598 | 218 | { |
| mbed_official | 17:c7b1b8451598 | 219 | m_serialTxFifoEmpty = true; |
| mbed_official | 17:c7b1b8451598 | 220 | } |
| mbed_official | 17:c7b1b8451598 | 221 | else |
| mbed_official | 17:c7b1b8451598 | 222 | { |
| mbed_official | 17:c7b1b8451598 | 223 | m_serialTxFifoEmpty = false; |
| mbed_official | 17:c7b1b8451598 | 224 | while(m_serial.writeable() && !m_outBuf.isEmpty()) |
| mbed_official | 17:c7b1b8451598 | 225 | { |
| mbed_official | 17:c7b1b8451598 | 226 | uint8_t c; |
| mbed_official | 17:c7b1b8451598 | 227 | m_outBuf.dequeue(&c); |
| mbed_official | 17:c7b1b8451598 | 228 | m_serial.putc((char)c); |
| mbed_official | 17:c7b1b8451598 | 229 | } |
| mbed_official | 17:c7b1b8451598 | 230 | m_serial.writePacket(); //Start packet write |
| mbed_official | 17:c7b1b8451598 | 231 | } |
| mbed_official | 17:c7b1b8451598 | 232 | if(!m_outBuf.isFull()) |
| mbed_official | 17:c7b1b8451598 | 233 | { |
| mbed_official | 17:c7b1b8451598 | 234 | m_spaceSphre.release(); //Force exiting the waiting state |
| mbed_official | 17:c7b1b8451598 | 235 | } |
| mbed_official | 17:c7b1b8451598 | 236 | } |
