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/WANDongleSerialPort.cpp
- Committer:
- mbed_official
- Date:
- 2013-10-16
- Revision:
- 17:c7b1b8451598
- Child:
- 18:37c948cf0dbf
File content as of revision 17:c7b1b8451598:
/* Copyright (c) 2010-2012 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "USBHostConf.h"
#ifdef USBHOST_3GMODULE
#define __DEBUG__ 0
#ifndef __MODULE__
#define __MODULE__ "WANDongleSerialPort.cpp"
#endif
#include "core/dbg.h"
#include <cstdint>
#include "rtos.h"
#include "WANDongleSerialPort.h"
WANDongleSerialPort::WANDongleSerialPort() : cb_tx_en(false), cb_rx_en(false), listener(NULL)
{
reset();
}
void WANDongleSerialPort::init(USBHost* pHost)
{
host = pHost;
}
void WANDongleSerialPort::reset()
{
tx_mtx.lock();
rx_mtx.lock();
bulk_in = NULL;
bulk_out = NULL;
buf_out_len = 0;
max_out_size = 0;
lock_tx = false;
cb_tx_pending = false;
buf_in_len = 0;
buf_in_read_pos = 0;
lock_rx = false;
cb_rx_pending = false;
tx_mtx.unlock();
rx_mtx.unlock();
}
int WANDongleSerialPort::readPacket()
{
DBG("Read packet on %p", this);
rx_mtx.lock();
if(lock_rx)
{
ERR("Fail");
rx_mtx.unlock();
return -1;
}
if( bulk_in == NULL )
{
WARN("Port is disconnected");
rx_mtx.unlock();
return -1;
}
lock_rx = true; //Receiving
rx_mtx.unlock();
// DBG("readPacket");
//lock_rx.lock();
USB_TYPE res = host->bulkRead(dev, (USBEndpoint *)bulk_in, buf_in, ((USBEndpoint *)bulk_in)->getSize(), false); //Queue transfer
if(res != USB_TYPE_PROCESSING)
{
//lock_rx.unlock();
ERR("host->bulkRead() returned %d", res);
Thread::wait(100);
return -1;
}
return 0;
}
int WANDongleSerialPort::writePacket()
{
tx_mtx.lock();
if(lock_tx)
{
ERR("Fail");
tx_mtx.unlock();
return -1;
}
if( bulk_out == NULL )
{
WARN("Port is disconnected");
tx_mtx.unlock();
return -1;
}
lock_tx = true; //Transmitting
tx_mtx.unlock();
// DBG("writePacket");
//lock_tx.lock();
USB_TYPE res = host->bulkWrite(dev, (USBEndpoint *)bulk_out, buf_out, buf_out_len, false); //Queue transfer
if(res != USB_TYPE_PROCESSING)
{
//lock_tx.unlock();
ERR("host->bulkWrite() returned %d", res);
Thread::wait(100);
return -1;
}
return 0;
}
int WANDongleSerialPort::putc(int c)
{
tx_mtx.lock();
if(!lock_tx)
{
if(buf_out_len < max_out_size)
{
buf_out[buf_out_len] = (uint8_t)c;
buf_out_len++;
}
}
else
{
ERR("CAN'T WRITE!");
}
tx_mtx.unlock();
return c;
}
int WANDongleSerialPort::getc()
{
rx_mtx.lock();
int c = 0;
if(!lock_rx)
{
if(buf_in_read_pos < buf_in_len)
{
c = (int)buf_in[buf_in_read_pos];
buf_in_read_pos++;
}
}
else
{
ERR("CAN'T READ!");
}
rx_mtx.unlock();
return c;
}
int WANDongleSerialPort::readable()
{
rx_mtx.lock();
if (lock_rx)
{
rx_mtx.unlock();
return 0;
}
/* if( !lock_rx.trylock() )
{
return 0;
}*/
int res = buf_in_len - buf_in_read_pos;
//lock_rx.unlock();
rx_mtx.unlock();
return res;
}
int WANDongleSerialPort::writeable()
{
tx_mtx.lock();
if (lock_tx)
{
tx_mtx.unlock();
return 0;
}
/*if( !lock_tx.trylock() )
{
return 0;
}*/
int res = max_out_size - buf_out_len;
tx_mtx.unlock();
//lock_tx.unlock();
return res;
}
void WANDongleSerialPort::attach(IUSBHostSerialListener* pListener)
{
if(pListener == NULL)
{
setupIrq(false, RxIrq);
setupIrq(false, TxIrq);
}
listener = pListener;
if(pListener != NULL)
{
setupIrq(true, RxIrq);
setupIrq(true, TxIrq);
}
}
void WANDongleSerialPort::setupIrq(bool en, IrqType irq /*= RxIrq*/)
{
switch(irq)
{
case RxIrq:
rx_mtx.lock();
cb_rx_en = en;
if(en && cb_rx_pending)
{
cb_rx_pending = false;
rx_mtx.unlock();
listener->readable(); //Process the interrupt that was raised
}
else
{
rx_mtx.unlock();
}
break;
case TxIrq:
tx_mtx.lock();
cb_tx_en = en;
if(en && cb_tx_pending)
{
cb_tx_pending = false;
tx_mtx.unlock();
listener->writeable(); //Process the interrupt that was raised
}
else
{
tx_mtx.unlock();
}
break;
}
}
void WANDongleSerialPort::connect( USBDeviceConnected* pDev, USBEndpoint* pInEp, USBEndpoint* pOutEp )
{
dev = pDev;
bulk_in = pInEp;
bulk_out = pOutEp;
max_out_size = bulk_out->getSize();
if( max_out_size > WANDONGLE_MAX_OUTEP_SIZE )
{
max_out_size = WANDONGLE_MAX_OUTEP_SIZE;
}
bulk_in->attach(this, &WANDongleSerialPort::rxHandler);
bulk_out->attach(this, &WANDongleSerialPort::txHandler);
readPacket(); //Start receiving data
}
void WANDongleSerialPort::disconnect( )
{
reset();
}
//Private methods
void WANDongleSerialPort::rxHandler()
{
if (((USBEndpoint *) bulk_in)->getState() == USB_TYPE_IDLE) //Success
{
buf_in_read_pos = 0;
buf_in_len = ((USBEndpoint *) bulk_in)->getLengthTransferred(); //Update length
//lock_rx.unlock();
rx_mtx.lock();
lock_rx = false; //Transmission complete
if(cb_rx_en)
{
rx_mtx.unlock();
listener->readable(); //Call handler from the IRQ context
//readPacket() should be called by the handler subsequently once the buffer has been emptied
}
else
{
cb_rx_pending = true; //Queue the callback
rx_mtx.unlock();
}
}
else //Error, try reading again
{
//lock_rx.unlock();
DBG("Trying again");
readPacket();
}
}
void WANDongleSerialPort::txHandler()
{
if (((USBEndpoint *) bulk_out)->getState() == USB_TYPE_IDLE) //Success
{
tx_mtx.lock();
buf_out_len = 0; //Reset length
lock_tx = false; //Transmission complete
//lock_tx.unlock();
if(cb_tx_en)
{
tx_mtx.unlock();
listener->writeable(); //Call handler from the IRQ context
//writePacket() should be called by the handler subsequently once the buffer has been filled
}
else
{
cb_tx_pending = true; //Queue the callback
tx_mtx.unlock();
}
}
else //Error, try reading again
{
//lock_tx.unlock();
writePacket();
}
}
#endif /* USBHOST_3GMODULE */
