A synthesizer that is controlled by a USB MIDI Keyboard. Also displays corresponding frequencies via a simple spectrum analyzer on a LCD. Uses a watchdog timer to reset Mbed in case it freezes.

Dependencies:   4DGL-uLCD-SE USBHostMIDI mbed

Fork of USBHostMIDI_example by Kaoru Shoji

Committer:
kjones99
Date:
Wed Dec 13 17:17:07 2017 +0000
Revision:
3:d48372c1347e
Parent:
2:5366ce17fe55
Final version with comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kshoji 0:78ea62ee7eab 1 #include "mbed.h"
kshoji 0:78ea62ee7eab 2 #include "USBHostMIDI.h"
kjones99 2:5366ce17fe55 3 #include "MIDISpeaker.h"
kjones99 2:5366ce17fe55 4 #include "Watchdog.h"
kshoji 1:01305cc0e2a2 5
kjones99 2:5366ce17fe55 6 //#include "uLCD_4DGL.h"
kjones99 2:5366ce17fe55 7
kjones99 2:5366ce17fe55 8 DigitalOut myled (LED1);
kjones99 2:5366ce17fe55 9 MIDISpeaker myspeaker(p21); //ini
kjones99 2:5366ce17fe55 10 Watchdog wdt; //create Watchdog Timer object
kjones99 2:5366ce17fe55 11
kjones99 2:5366ce17fe55 12
kjones99 2:5366ce17fe55 13
kshoji 0:78ea62ee7eab 14 void noteOn(unsigned char channel, unsigned char note, unsigned char velocity) {
kjones99 3:d48372c1347e 15 wdt.kick(3.0); //start watchdog timer
kjones99 2:5366ce17fe55 16 myled = 1.0; //turn on LED1 on note press
kjones99 2:5366ce17fe55 17 myspeaker.PlayNote(note, velocity);
kjones99 2:5366ce17fe55 18 //uLCD.printf("\nkey: %d, velocity: %d\r\n", note, velocity);
kjones99 2:5366ce17fe55 19
kshoji 0:78ea62ee7eab 20 }
kshoji 0:78ea62ee7eab 21
kjones99 2:5366ce17fe55 22
kshoji 0:78ea62ee7eab 23 void noteOff(unsigned char channel, unsigned char note, unsigned char velocity) {
kjones99 2:5366ce17fe55 24 wdt.kick(); //reset watchdog timer when noteoff is called
kjones99 2:5366ce17fe55 25 myled = 0.0; //turn off LED1 on note release
kjones99 2:5366ce17fe55 26 myspeaker.StopNote();
kshoji 0:78ea62ee7eab 27
kshoji 0:78ea62ee7eab 28 }
kshoji 0:78ea62ee7eab 29
kshoji 0:78ea62ee7eab 30
kshoji 0:78ea62ee7eab 31 void midi_task(void const*) {
kjones99 2:5366ce17fe55 32 // create usbhost midi object
kshoji 0:78ea62ee7eab 33 USBHostMIDI midi;
kshoji 1:01305cc0e2a2 34
kshoji 0:78ea62ee7eab 35 // attach midi event callbacks
kshoji 0:78ea62ee7eab 36 midi.attachNoteOn(noteOn);
kshoji 0:78ea62ee7eab 37 midi.attachNoteOff(noteOff);
kjones99 2:5366ce17fe55 38
kshoji 0:78ea62ee7eab 39
kshoji 0:78ea62ee7eab 40 while(1) {
kshoji 0:78ea62ee7eab 41 // try to connect a midi device
kshoji 0:78ea62ee7eab 42 while(!midi.connect())
kshoji 0:78ea62ee7eab 43 Thread::wait(500);
kshoji 0:78ea62ee7eab 44
kshoji 0:78ea62ee7eab 45 // if the device is disconnected, we try to connect it again
kshoji 0:78ea62ee7eab 46 while (1) {
kshoji 0:78ea62ee7eab 47 // if device disconnected, try to connect it again
kshoji 0:78ea62ee7eab 48 if (!midi.connected())
kshoji 0:78ea62ee7eab 49 break;
kjones99 2:5366ce17fe55 50
kjones99 2:5366ce17fe55 51 Thread::wait(1);
kshoji 0:78ea62ee7eab 52 }
kshoji 0:78ea62ee7eab 53 }
kshoji 0:78ea62ee7eab 54 }
kjones99 2:5366ce17fe55 55
kshoji 0:78ea62ee7eab 56 int main() {
kjones99 3:d48372c1347e 57 if ((LPC_WDT->WDMOD >> 2) & 1) //indicate if watchdog timer has not been kicked, and resets mbed
kjones99 2:5366ce17fe55 58 myled4 = 1; else myled3 = 1;
kjones99 2:5366ce17fe55 59
kjones99 2:5366ce17fe55 60 uLCD.background_color(BLUE); //set background for spectrum analyzer
kjones99 2:5366ce17fe55 61 uLCD.cls();
kjones99 2:5366ce17fe55 62 uLCD.line(5,110,120,110,WHITE); // x-axis
kjones99 2:5366ce17fe55 63 uLCD.line(5,5,5,110, WHITE); //y-axis
kjones99 2:5366ce17fe55 64
kshoji 0:78ea62ee7eab 65 Thread midiTask(midi_task, NULL, osPriorityNormal, 256 * 4);
kshoji 0:78ea62ee7eab 66 while(1) {
kjones99 2:5366ce17fe55 67 Thread::wait(50);
kjones99 2:5366ce17fe55 68 //wdt.kick();
kjones99 3:d48372c1347e 69 myled4=!myled4; //blink LED4 to indicate that mbed is ready to receive input from keyboard
kshoji 0:78ea62ee7eab 70 }
kshoji 0:78ea62ee7eab 71 }