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

main.cpp

Committer:
Osamu Nakamura
Date:
2017-04-24
Revision:
33:5fa9ed01abae
Parent:
30:ff42cc7a0d55

File content as of revision 33:5fa9ed01abae:

#include "mbed.h"
#include "USBHostMouse.h"
extern "C" {
#include "devdrv_sdg.h"
}
/* defines */
#define SDG_OUTPUT_CH         (1)
#define DEFAULT_LOUDNESS      (4)
#define DIV_MAX               (100)
#define DIV_DEFAULT           (75)
#define DIV_MIN               (50)
#define MODE_TONE             (2)

#define MOUSE_BUTTON_LEFGT    (0x01)
#define MOUSE_BUTTON_RIGHT    (0x02)
#define MOUSE_BUTTON_CENTER   (0x04)

#define OCTAVE_MAX            (4)
#define OCTAVE_LEV_MAX        (OCTAVE_MAX - 1)
#define OCTAVE_LEV_MIN        (0)
static const struct {
    uint8_t     tone_max;
    uint8_t     tone_min;
    uint8_t     sfs;
} octave_table[OCTAVE_MAX] = {
    { 31, 16, 150},  /* octave 3(*) */
    { 50, 24, 190},  /* octave 2(+) */
    { 86, 43, 220},  /* octave 1() */
    {124, 73, 255},  /* octave 0(-) */
};

/*  RAMS   */
DigitalOut led(LED1);
static NOTE output_note;
static int32_t work_tone;
static int32_t work_octave_lev;
static int32_t work_div;


void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
    
    //update div
    work_div += z;
    if (work_div < DIV_MIN) {
        work_div = DIV_MIN;
    } else if (work_div > DIV_MAX) {
        work_div = DIV_MAX;
    }
    if ((buttons & MOUSE_BUTTON_CENTER) == MOUSE_BUTTON_CENTER) {
        work_div = DIV_DEFAULT;
    }
    
    //update tone
    x = x * (-1);
    work_tone += x;
    if (work_tone < (octave_table[work_octave_lev].tone_min * work_div * MODE_TONE)) {
        if (work_octave_lev != OCTAVE_LEV_MIN) {
            work_octave_lev--;
            work_tone = octave_table[work_octave_lev].tone_max * work_div * MODE_TONE;
        } else {
            work_tone = octave_table[work_octave_lev].tone_min * work_div * MODE_TONE;
        }
    } else if (work_tone > (octave_table[work_octave_lev].tone_max * work_div * MODE_TONE)) {
        if (work_octave_lev != OCTAVE_LEV_MAX) {
            work_octave_lev++;
            work_tone = octave_table[work_octave_lev].tone_min * work_div * MODE_TONE;
        } else {
            work_tone = octave_table[work_octave_lev].tone_max * work_div * MODE_TONE;
        }
    }

    
    if ((buttons & MOUSE_BUTTON_RIGHT) == MOUSE_BUTTON_RIGHT) {
        work_tone = 72 * work_div * MODE_TONE;
        work_octave_lev = 2;
    }
    output_note.tone = work_tone / (work_div * MODE_TONE);
    output_note.sfs  = octave_table[work_octave_lev].sfs;
    
    printf("sfs: %d, tone: %d, div: %d\r\n", output_note.sfs, output_note.tone, work_div);
    if ((buttons & MOUSE_BUTTON_LEFGT) == MOUSE_BUTTON_LEFGT) {
        // mouse left button clicked
        output_note.loud = DEFAULT_LOUDNESS;
        R_SDG_Tone(SDG_OUTPUT_CH, &output_note);
    } else {
        // mouse left button released
        output_note.loud = 0;
        R_SDG_Tone(SDG_OUTPUT_CH, &output_note);
    }
}

void mouse_task(void const *) {
    int32_t        ret;        /* function result */
    USBHostMouse mouse;
    
    while(1) {
        // try to connect a USB mouse
        while(!mouse.connect())
            Thread::wait(500);
        
        // when connected, attach handler called on mouse event
        mouse.attachEvent(onMouseEvent);
        
        ret = R_SDG_Open(SDG_OUTPUT_CH, R_SDG_CLOCK_4);
        if (ret < 0) {
            printf("SDG Open Error\r\n");
        }
        
        
        // wait until the mouse is disconnected
        while(mouse.connected())
            Thread::wait(500);
            
        ret = R_SDG_Close(SDG_OUTPUT_CH);
        if (ret < 0) {
            printf("SDG Close Error\r\n");
        }
    }
}

int main() {
    // init SDG Note
    work_tone = 72;       /* C */
    work_octave_lev = 2;
    work_div  = DIV_DEFAULT;
    output_note.tone              = work_tone;
    output_note.sfs               = octave_table[work_octave_lev].sfs;
    output_note.loud              = 0;
    output_note.attenuation       = 0;

    Thread mouseTask(mouse_task, NULL, osPriorityNormal, 256 * 4);
    while(1) {
        led=!led;
        Thread::wait(500);
    }
}