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
main.cpp
- Committer:
- kjones99
- Date:
- 2017-12-13
- Revision:
- 2:5366ce17fe55
- Parent:
- 1:01305cc0e2a2
- Child:
- 3:d48372c1347e
File content as of revision 2:5366ce17fe55:
#include "mbed.h" #include "USBHostMIDI.h" #include "MIDISpeaker.h" #include "Watchdog.h" //#include "uLCD_4DGL.h" DigitalOut myled (LED1); MIDISpeaker myspeaker(p21); //ini Watchdog wdt; //create Watchdog Timer object //void notesound_t(unsigned char note, unsigned char velocity, volatile int time_us) { // myspeaker.PlayNote(note, velocity, time_us); //} void noteOn(unsigned char channel, unsigned char note, unsigned char velocity) { wdt.kick(3.0); myled = 1.0; //turn on LED1 on note press myspeaker.PlayNote(note, velocity); //uLCD.printf("\nkey: %d, velocity: %d\r\n", note, velocity); } void noteOff(unsigned char channel, unsigned char note, unsigned char velocity) { wdt.kick(); //reset watchdog timer when noteoff is called myled = 0.0; //turn off LED1 on note release myspeaker.StopNote(); } void midi_task(void const*) { // create usbhost midi object USBHostMIDI midi; // attach midi event callbacks midi.attachNoteOn(noteOn); midi.attachNoteOff(noteOff); while(1) { // try to connect a midi device while(!midi.connect()) Thread::wait(500); // if the device is disconnected, we try to connect it again while (1) { // if device disconnected, try to connect it again if (!midi.connected()) break; Thread::wait(1); } } } int main() { if ((LPC_WDT->WDMOD >> 2) & 1) myled4 = 1; else myled3 = 1; //wdt.kick(3.0); //use watchdog timer to reset mbed after 2 seconds without kick uLCD.background_color(BLUE); //set background for spectrum analyzer uLCD.cls(); uLCD.line(5,110,120,110,WHITE); // x-axis uLCD.line(5,5,5,110, WHITE); //y-axis //uLCD.fill(65,35,55,1,graphAxesColor); //uLCD.fill(65,35,1,85,graphAxesColor); Thread midiTask(midi_task, NULL, osPriorityNormal, 256 * 4); while(1) { Thread::wait(50); //wdt.kick(); myled4=!myled4; //uLCD.printf("This is a test\n"); //myspeaker.PlayNote(60, 127, 1.0); } }