MIDI interpreter using mbed

Dependencies:   MIDI TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 #include "define.h"
00005 #include "MIDIChannel.h"
00006 #include "Instrument.h"
00007 #include "RingBuffer.h"
00008 #include "events.h"
00009 #include "parser.h"
00010 
00011 Serial midiIn(/*Tx*/ NC, /*Rx*/ p10);
00012 TextLCD lcd(/*RS*/ p26, /*E*/ p25, /*D4 through D7*/ p24, p23, p22, p21, TextLCD::LCD16x2);
00013 BusOut obLeds(LED1, LED2, LED3, LED4);
00014 I2C i2c(/*SDA*/ p28, /*SCL*/ p27);
00015 Timer t;
00016 #ifdef USE_PC_SERIAL
00017 Serial pc(/*Tx*/ USBTX, /*Rx*/ USBRX);
00018 #endif
00019 
00020 uint8_t const performerAddress[NUM_PERFORMER] = { 0x34 };
00021 
00022 Instrument inst[NUM_INSTRUMENT];
00023 uint32_t receivedBytes;
00024 RingBuffer<char> buffer(BUFFER_LENGTH);
00025 
00026 extern "C"
00027 void HardFault_Handler() {
00028     volatile uint32_t i;
00029     
00030     while (1) {
00031         i = 0;
00032         obLeds = obLeds ^ 0x8;
00033         while (i < 1000000) i++;
00034     }
00035 }
00036 
00037 void readMidiIn() {
00038     char c;
00039     
00040     // Put a MIDI input byte into buffer if available
00041     if (midiIn.readable()) {
00042         c = midiIn.getc();
00043         receivedBytes++;
00044 
00045         // Discard if input byte is an active sensing message
00046         if (c != 0xfe) {
00047             buffer.write(c);
00048             /*
00049             #ifdef USE_PC_SERIAL
00050             pc.printf("%02X\r\n", c);
00051             #endif
00052             */
00053         }
00054     }
00055 }
00056 
00057 void setup() {
00058     // Setup MIDI port
00059     midiIn.baud(31250);
00060     midiIn.format(8, Serial::None, 1);
00061     midiIn.attach(readMidiIn, Serial::RxIrq);
00062     receivedBytes = 0;
00063     
00064     #ifdef USE_PC_SERIAL
00065     // Setup PC Serial port
00066     pc.baud(115200);
00067     pc.format(8, Serial::None, 1);
00068     #endif
00069 
00070     // Initialize Text LCD
00071     lcd.cls();
00072     wait_ms(10);
00073     
00074     // Initialize onboard LEDs
00075     obLeds = 0x0;
00076 
00077     // Start Timer
00078     t.start();
00079 }
00080 
00081 void loop() {
00082     /*
00083     lcd.locate(0, 0);
00084     lcd.printf("Recv'd: %8u", receivedBytes);
00085     lcd.locate(0, 1);
00086     lcd.printf("Buffer: %8u", buffer.items());
00087     */
00088     
00089     // Parse message buffer
00090     parseMessage(buffer);
00091 }
00092 
00093 int main() {
00094     setup();
00095     
00096     while (1) {
00097         loop();
00098     }
00099 }