
Bluetooth Enabled Keyboard/Synthesizer for mbed
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem mbed-rtos
main.cpp
- Committer:
- jmpin
- Date:
- 2016-04-28
- Revision:
- 4:406f59c6a1a6
- Parent:
- 3:3aba1d783730
- Child:
- 5:afd67e985df0
File content as of revision 4:406f59c6a1a6:
#include "mbed.h" #include "SDFileSystem.h" #include "synthesizer.h" Serial Blue(p28,p27); Serial PC(USBTX,USBRX); DigitalOut myled(LED1); DigitalOut myled4(LED4); SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup //global variables for main and interrupt routine volatile bool readyFlag = true; volatile char keyPress; WaveType myWave = sine; // default to sine wave volatile int currentOctave = 4; // default to 4 because thats where middle C is volatile int currentAttackVal = 3; // values will range from 1-5, default to 3 volatile int currentDelayVal = 3; // values will range from 1-5, default to 3 volatile int currentSustainVal = 3; // values will range from 1-5, default to 3 volatile int currentReleaseVal = 3; // values will range from 1-5, default to 3 //Interrupt routine to parse message with one new character per serial RX interrupt void parse_message() { keyPress = Blue.getc(); PC.putc(keyPress); readyFlag = true; PC.printf("\n\r Value of readyFlag is: %i",readyFlag); //PC.printf("Value of keyPress is: %c\n\r",keyPress); } /* This function writes which note was just played to a text file on the SDCard. The note played will be encoded in hexadecimal, as well as the octave, Attack Value, Delay Value, Sustain Value, and Release Value. The format of the bits will be as follows: | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | | Attack | Delay | Susttain | Release | Octave | Note | For the 3 bits representing note, A will correspond to 1, B to 2, and so on. For example, if the lower 3 bits corresponding to note are 001, then the note is an A. @param: The note that is being played/recorded into the text file */ void write_to_SDCard(char note) { int AttackBits, SustainBits, DelayBits, ReleaseBits, OctaveBits, NoteBits; AttackBits = currentAttackVal; DelayBits = currentDelayVal; SustainBits = currentSustainVal; ReleaseBits = currentReleaseVal; OctaveBits = currentOctave; switch(note){ case 'C': NoteBits = 3; break; case 'D': NoteBits = 4; break; case 'E': NoteBits = 5; break; case 'F': NoteBits = 6; break; case 'G': NoteBits = 7; break; case 'A': NoteBits = 1; break; case 'B': NoteBits = 2; break; default: NoteBits = 0; break; } int writeVal; writeVal = (AttackBits << 15) | (DelayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits); FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w"); if(fp == NULL) { error("Could not open file for write\n"); } fprintf(fp,"%X\r\n",writeVal); // writes value to the text file in hexadecimal fclose(fp); } int main() { // make directory to hold the record of notes played mkdir("/sd/noteRecords", 0777); //attach interrupt function for each new Bluetooth serial character Blue.attach(&parse_message,Serial::RxIrq); while(1) { //check for a new button message ready if((keyPress == C_NOTE_KEY) && (readyFlag)){ // button Z pressed PC.printf("Got a Z"); write_to_SDCard('C'); readyFlag = false; // Play note that corresponds to Z } else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // button X pressed PC.printf("Got an X"); write_to_SDCard('D'); readyFlag = false; // Play note that corresponds to X } else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed // Play note that corresponds to C // Make note of which note was played in file on SD Card write_to_SDCard('E'); readyFlag = false; } else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed // Play note that corresponds to V // Make note of which note was played in file on SD Card write_to_SDCard('F'); readyFlag = false; } else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed // Play note that corresponds to B // Make note of which note was played in file on SD Card write_to_SDCard('G'); readyFlag = false; } else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed // Play note that corresponds to N // Make note of which note was played in file on SD Card write_to_SDCard('A'); readyFlag = false; } else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed // Play note that corresponds to M // Make note of which note was played in file on SD Card write_to_SDCard('B'); readyFlag = false; } else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed // Raise an octave if(currentOcatve < 7) currentOctave++; } else printf("Cannot raise octave above 7.\r\n"); else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed // Lower an octave if(currentOctave > 1) currentOctave--; else printf("Cannot lower octave below 1.\r\n"); } else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed // Raise Attack Value if(currentAttackVal < 5) currentAttackVal++; else printf("Cannot raise value above 5.\r\n"); } else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed // Lower Attack Value if(currentAttackVal > 1) currentAttackVal--; else printf("Cannot lower value below 1.\r\n"; } else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed // Raise Delay Value if(currentDelayVal < 5) currentDelayVal++; else printf("Cannot raise value above 5.\r\n"); } else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed // Lower Delay Value if(currentDelayVal > 1) currentDelayVal--; else printf("Cannot lower value below 1.\r\n"; } else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed // Raise Sustain Value if(currentSustainVal < 5) currentSustainVal++; else printf("Cannot raise value above 5.\r\n"); } else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed // Lower Sustain Value if(currentSustainVal > 1) currentSustainVal--; else printf("Cannot lower value below 1.\r\n"; } else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed // Raise Release Value if(currentReleaseVal < 5) currentReleaseVal++; else printf("Cannot raise value above 5.\r\n"); } else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed // Lower Release Value if(currentReleaseVal > 1) currentReleaseVal--; else printf("Cannot lower value below 1.\r\n"; } else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed // Change waveform shape to next waveform type switch(myWave){ case sine: myWave = square; break; case square: myWave = sawtooth; break; case sawtooth: myWave = sine; break; default: break; } } else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed // Change waveform shape to previous waveform type switch(myWave){ case sine: myWave = sawtooth; break; case square: myWave = sine; break; case sawtooth: myWave = square; break; default: break; } } } //do other tasks in main - interrupts will process button message characters myled = 1; wait(0.1); myled = 0; wait(0.1); }