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 03:31:09 2017 +0000
Revision:
2:5366ce17fe55
Parent:
1:01305cc0e2a2
Child:
3:d48372c1347e
Final version as of right now.

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
kjones99 2:5366ce17fe55 14
kjones99 2:5366ce17fe55 15 //void notesound_t(unsigned char note, unsigned char velocity, volatile int time_us) {
kjones99 2:5366ce17fe55 16 // myspeaker.PlayNote(note, velocity, time_us);
kjones99 2:5366ce17fe55 17 //}
kjones99 2:5366ce17fe55 18
kshoji 1:01305cc0e2a2 19
kshoji 0:78ea62ee7eab 20 void noteOn(unsigned char channel, unsigned char note, unsigned char velocity) {
kjones99 2:5366ce17fe55 21 wdt.kick(3.0);
kjones99 2:5366ce17fe55 22 myled = 1.0; //turn on LED1 on note press
kjones99 2:5366ce17fe55 23 myspeaker.PlayNote(note, velocity);
kjones99 2:5366ce17fe55 24 //uLCD.printf("\nkey: %d, velocity: %d\r\n", note, velocity);
kjones99 2:5366ce17fe55 25
kshoji 0:78ea62ee7eab 26 }
kshoji 0:78ea62ee7eab 27
kjones99 2:5366ce17fe55 28
kshoji 0:78ea62ee7eab 29 void noteOff(unsigned char channel, unsigned char note, unsigned char velocity) {
kjones99 2:5366ce17fe55 30 wdt.kick(); //reset watchdog timer when noteoff is called
kjones99 2:5366ce17fe55 31 myled = 0.0; //turn off LED1 on note release
kjones99 2:5366ce17fe55 32 myspeaker.StopNote();
kshoji 0:78ea62ee7eab 33
kshoji 0:78ea62ee7eab 34 }
kshoji 0:78ea62ee7eab 35
kshoji 0:78ea62ee7eab 36
kshoji 0:78ea62ee7eab 37 void midi_task(void const*) {
kjones99 2:5366ce17fe55 38 // create usbhost midi object
kshoji 0:78ea62ee7eab 39 USBHostMIDI midi;
kshoji 1:01305cc0e2a2 40
kshoji 0:78ea62ee7eab 41 // attach midi event callbacks
kshoji 0:78ea62ee7eab 42 midi.attachNoteOn(noteOn);
kshoji 0:78ea62ee7eab 43 midi.attachNoteOff(noteOff);
kjones99 2:5366ce17fe55 44
kshoji 0:78ea62ee7eab 45
kshoji 0:78ea62ee7eab 46 while(1) {
kshoji 0:78ea62ee7eab 47 // try to connect a midi device
kshoji 0:78ea62ee7eab 48 while(!midi.connect())
kshoji 0:78ea62ee7eab 49 Thread::wait(500);
kshoji 0:78ea62ee7eab 50
kshoji 0:78ea62ee7eab 51 // if the device is disconnected, we try to connect it again
kshoji 0:78ea62ee7eab 52 while (1) {
kshoji 0:78ea62ee7eab 53 // if device disconnected, try to connect it again
kshoji 0:78ea62ee7eab 54 if (!midi.connected())
kshoji 0:78ea62ee7eab 55 break;
kjones99 2:5366ce17fe55 56
kjones99 2:5366ce17fe55 57 Thread::wait(1);
kshoji 0:78ea62ee7eab 58 }
kshoji 0:78ea62ee7eab 59 }
kshoji 0:78ea62ee7eab 60 }
kjones99 2:5366ce17fe55 61
kshoji 0:78ea62ee7eab 62 int main() {
kjones99 2:5366ce17fe55 63 if ((LPC_WDT->WDMOD >> 2) & 1)
kjones99 2:5366ce17fe55 64 myled4 = 1; else myled3 = 1;
kjones99 2:5366ce17fe55 65
kjones99 2:5366ce17fe55 66 //wdt.kick(3.0); //use watchdog timer to reset mbed after 2 seconds without kick
kjones99 2:5366ce17fe55 67 uLCD.background_color(BLUE); //set background for spectrum analyzer
kjones99 2:5366ce17fe55 68 uLCD.cls();
kjones99 2:5366ce17fe55 69 uLCD.line(5,110,120,110,WHITE); // x-axis
kjones99 2:5366ce17fe55 70 uLCD.line(5,5,5,110, WHITE); //y-axis
kjones99 2:5366ce17fe55 71 //uLCD.fill(65,35,55,1,graphAxesColor);
kjones99 2:5366ce17fe55 72 //uLCD.fill(65,35,1,85,graphAxesColor);
kjones99 2:5366ce17fe55 73
kshoji 0:78ea62ee7eab 74 Thread midiTask(midi_task, NULL, osPriorityNormal, 256 * 4);
kshoji 0:78ea62ee7eab 75 while(1) {
kjones99 2:5366ce17fe55 76 Thread::wait(50);
kjones99 2:5366ce17fe55 77 //wdt.kick();
kjones99 2:5366ce17fe55 78 myled4=!myled4;
kjones99 2:5366ce17fe55 79 //uLCD.printf("This is a test\n");
kjones99 2:5366ce17fe55 80 //myspeaker.PlayNote(60, 127, 1.0);
kjones99 2:5366ce17fe55 81
kshoji 0:78ea62ee7eab 82 }
kshoji 0:78ea62ee7eab 83 }