Bluetooth Enabled Keyboard/Synthesizer for mbed

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem mbed-rtos

Committer:
jmpin
Date:
Wed Apr 27 22:40:03 2016 +0000
Revision:
3:3aba1d783730
Parent:
2:f06ba516b1ad
Child:
4:406f59c6a1a6
Wrote function that will write notes to the record on the SD card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmpin 0:48311ffdfa96 1 #include "mbed.h"
jmpin 3:3aba1d783730 2 #include "SDFileSystem.h"
jmpin 3:3aba1d783730 3 #include "synthesizer.h"
jmpin 0:48311ffdfa96 4 Serial Blue(p28,p27);
jmpin 0:48311ffdfa96 5 Serial PC(USBTX,USBRX);
jmpin 0:48311ffdfa96 6 DigitalOut myled(LED1);
jmpin 0:48311ffdfa96 7 DigitalOut myled4(LED4);
jmpin 3:3aba1d783730 8
jmpin 3:3aba1d783730 9 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
jmpin 3:3aba1d783730 10
jmpin 0:48311ffdfa96 11 //global variables for main and interrupt routine
jmpin 0:48311ffdfa96 12 volatile bool readyFlag = true;
jmpin 0:48311ffdfa96 13 volatile char keyPress;
jmpin 3:3aba1d783730 14 WaveType myWave = sine; // default to sine wave
jmpin 3:3aba1d783730 15 volatile int currentOctave = 4; // default to 4 because thats where middle C is
jmpin 3:3aba1d783730 16 volatile int currentAttackVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 17 volatile int currentDelayVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 18 volatile int currentSustainVal = 3; // values will range from 1-5, default to 3
jmpin 3:3aba1d783730 19 volatile int currentReleaseVal = 3; // values will range from 1-5, default to 3
jmpin 0:48311ffdfa96 20 //Interrupt routine to parse message with one new character per serial RX interrupt
jmpin 0:48311ffdfa96 21 void parse_message()
jmpin 0:48311ffdfa96 22 {
jmpin 0:48311ffdfa96 23 keyPress = Blue.getc();
jmpin 0:48311ffdfa96 24 PC.putc(keyPress);
jmpin 0:48311ffdfa96 25 readyFlag = true;
jmpin 0:48311ffdfa96 26 PC.printf("\n\r Value of readyFlag is: %i",readyFlag);
jmpin 0:48311ffdfa96 27
jmpin 0:48311ffdfa96 28 //PC.printf("Value of keyPress is: %c\n\r",keyPress);
jmpin 0:48311ffdfa96 29 }
jmpin 3:3aba1d783730 30
jmpin 3:3aba1d783730 31
jmpin 3:3aba1d783730 32 /*
jmpin 3:3aba1d783730 33 This function writes which note was just played to a text file on the SDCard.
jmpin 3:3aba1d783730 34 The note played will be encoded in hexadecimal, as well as the octave, Attack Value,
jmpin 3:3aba1d783730 35 Delay Value, Sustain Value, and Release Value. The format of the bits will be as follows:
jmpin 3:3aba1d783730 36 | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits |
jmpin 3:3aba1d783730 37 | Attack | Delay | Susttain | Release | Octave | Note |
jmpin 3:3aba1d783730 38 For the 3 bits representing note, A will correspond to 1, B to 2, and so on.
jmpin 3:3aba1d783730 39 For example, if the lower 3 bits corresponding to note are 001, then the note is an A.
jmpin 3:3aba1d783730 40
jmpin 3:3aba1d783730 41 @param: The note that is being played/recorded into the text file
jmpin 3:3aba1d783730 42 */
jmpin 3:3aba1d783730 43
jmpin 3:3aba1d783730 44 void write_to_SDCard(char note)
jmpin 3:3aba1d783730 45 {
jmpin 3:3aba1d783730 46 int AttackBits, SustainBits, DelayBits, ReleaseBits, OctaveBits, NoteBits;
jmpin 2:f06ba516b1ad 47
jmpin 3:3aba1d783730 48 AttackBits = currentAttackVal;
jmpin 3:3aba1d783730 49 DelayBits = currentDelayVal;
jmpin 3:3aba1d783730 50 SustainBits = currentSustainVal;
jmpin 3:3aba1d783730 51 ReleaseBits = currentReleaseVal;
jmpin 3:3aba1d783730 52 OctaveBits = currentOctave;
jmpin 3:3aba1d783730 53 switch(note){
jmpin 3:3aba1d783730 54 case 'C':
jmpin 3:3aba1d783730 55 NoteBits = 3;
jmpin 3:3aba1d783730 56 break;
jmpin 3:3aba1d783730 57 case 'D':
jmpin 3:3aba1d783730 58 NoteBits = 4;
jmpin 3:3aba1d783730 59 break;
jmpin 3:3aba1d783730 60 case 'E':
jmpin 3:3aba1d783730 61 NoteBits = 5;
jmpin 3:3aba1d783730 62 break;
jmpin 3:3aba1d783730 63 case 'F':
jmpin 3:3aba1d783730 64 NoteBits = 6;
jmpin 3:3aba1d783730 65 break;
jmpin 3:3aba1d783730 66 case 'G':
jmpin 3:3aba1d783730 67 NoteBits = 7;
jmpin 3:3aba1d783730 68 break;
jmpin 3:3aba1d783730 69 case 'A':
jmpin 3:3aba1d783730 70 NoteBits = 1;
jmpin 3:3aba1d783730 71 break;
jmpin 3:3aba1d783730 72 case 'B':
jmpin 3:3aba1d783730 73 NoteBits = 2;
jmpin 3:3aba1d783730 74 break;
jmpin 3:3aba1d783730 75 default:
jmpin 3:3aba1d783730 76 NoteBits = 0;
jmpin 3:3aba1d783730 77 break;
jmpin 3:3aba1d783730 78 }
jmpin 3:3aba1d783730 79 int writeVal;
jmpin 3:3aba1d783730 80 writeVal = (AttackBits << 15) | (DelayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
jmpin 3:3aba1d783730 81
jmpin 3:3aba1d783730 82 FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w");
jmpin 3:3aba1d783730 83 if(fp == NULL) {
jmpin 3:3aba1d783730 84 error("Could not open file for write\n");
jmpin 3:3aba1d783730 85 }
jmpin 3:3aba1d783730 86 fprintf(fp,"%X\r\n",writeVal); // writes value to the text file in hexadecimal
jmpin 3:3aba1d783730 87 fclose(fp);
jmpin 3:3aba1d783730 88 }
jmpin 3:3aba1d783730 89
jmpin 0:48311ffdfa96 90 int main()
jmpin 0:48311ffdfa96 91 {
jmpin 3:3aba1d783730 92 // make directory to hold the record of notes played
jmpin 3:3aba1d783730 93 mkdir("/sd/noteRecords", 0777);
jmpin 3:3aba1d783730 94
jmpin 3:3aba1d783730 95
jmpin 0:48311ffdfa96 96 //attach interrupt function for each new Bluetooth serial character
jmpin 0:48311ffdfa96 97 Blue.attach(&parse_message,Serial::RxIrq);
jmpin 0:48311ffdfa96 98 while(1) {
jmpin 0:48311ffdfa96 99 //check for a new button message ready
jmpin 3:3aba1d783730 100 if((keyPress == C_NOTE_KEY) && (readyFlag)){ // button Z pressed
jmpin 2:f06ba516b1ad 101 PC.printf("Got n Z");
jmpin 3:3aba1d783730 102 write_to_SDCard('C');
jmpin 0:48311ffdfa96 103 readyFlag = false;
jmpin 0:48311ffdfa96 104 // Play note that corresponds to Z
jmpin 0:48311ffdfa96 105 }
jmpin 3:3aba1d783730 106 else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // button X pressed
jmpin 0:48311ffdfa96 107 PC.printf("Got an X");
jmpin 3:3aba1d783730 108 write_to_SDCard('D');
jmpin 0:48311ffdfa96 109 // Play note that corresponds to X
jmpin 0:48311ffdfa96 110 }
jmpin 3:3aba1d783730 111 else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed
jmpin 0:48311ffdfa96 112 // Play note that corresponds to C
jmpin 3:3aba1d783730 113 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 114 write_to_SDCard('E');
jmpin 3:3aba1d783730 115 }
jmpin 3:3aba1d783730 116 else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed
jmpin 0:48311ffdfa96 117 // Play note that corresponds to V
jmpin 3:3aba1d783730 118 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 119 write_to_SDCard('F');
jmpin 3:3aba1d783730 120 }
jmpin 3:3aba1d783730 121 else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed
jmpin 0:48311ffdfa96 122 // Play note that corresponds to B
jmpin 3:3aba1d783730 123 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 124 write_to_SDCard('G');
jmpin 3:3aba1d783730 125 }
jmpin 3:3aba1d783730 126 else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed
jmpin 0:48311ffdfa96 127 // Play note that corresponds to N
jmpin 3:3aba1d783730 128 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 129 write_to_SDCard('A');
jmpin 3:3aba1d783730 130 }
jmpin 3:3aba1d783730 131 else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed
jmpin 0:48311ffdfa96 132 // Play note that corresponds to M
jmpin 3:3aba1d783730 133 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 134 write_to_SDCard('B');
jmpin 3:3aba1d783730 135 }
jmpin 2:f06ba516b1ad 136 else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
jmpin 0:48311ffdfa96 137 // Raise an octave
jmpin 2:f06ba516b1ad 138 currentOctave++;
jmpin 2:f06ba516b1ad 139 }
jmpin 2:f06ba516b1ad 140 else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed
jmpin 2:f06ba516b1ad 141 // Lower an octave
jmpin 2:f06ba516b1ad 142 currentOctave--;
jmpin 2:f06ba516b1ad 143 }
jmpin 2:f06ba516b1ad 144 else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed
jmpin 0:48311ffdfa96 145 // Raise Attack Value
jmpin 2:f06ba516b1ad 146 currentAttackVal++;
jmpin 2:f06ba516b1ad 147 }
jmpin 2:f06ba516b1ad 148 else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed
jmpin 0:48311ffdfa96 149 // Lower Attack Value
jmpin 2:f06ba516b1ad 150 currentAttackVal--;
jmpin 2:f06ba516b1ad 151 }
jmpin 2:f06ba516b1ad 152 else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed
jmpin 0:48311ffdfa96 153 // Raise Delay Value
jmpin 2:f06ba516b1ad 154 currentDelayVal++;
jmpin 2:f06ba516b1ad 155 }
jmpin 2:f06ba516b1ad 156 else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed
jmpin 0:48311ffdfa96 157 // Lower Delay Value
jmpin 2:f06ba516b1ad 158 currentDelayVal--;
jmpin 2:f06ba516b1ad 159 }
jmpin 2:f06ba516b1ad 160 else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed
jmpin 0:48311ffdfa96 161 // Raise Sustain Value
jmpin 2:f06ba516b1ad 162 currentSustainVal++;
jmpin 2:f06ba516b1ad 163 }
jmpin 2:f06ba516b1ad 164 else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed
jmpin 0:48311ffdfa96 165 // Lower Sustain Value
jmpin 2:f06ba516b1ad 166 currentSustainVal--;
jmpin 2:f06ba516b1ad 167 }
jmpin 2:f06ba516b1ad 168 else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed
jmpin 0:48311ffdfa96 169 // Raise Release Value
jmpin 2:f06ba516b1ad 170 currentReleaseVal++;
jmpin 2:f06ba516b1ad 171 }
jmpin 2:f06ba516b1ad 172 else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed
jmpin 0:48311ffdfa96 173 // Lower Release Value
jmpin 2:f06ba516b1ad 174 currentReleaseVal--;
jmpin 2:f06ba516b1ad 175 }
jmpin 2:f06ba516b1ad 176 else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed
jmpin 2:f06ba516b1ad 177 // Change waveform shape to next waveform type
jmpin 2:f06ba516b1ad 178 switch(myWave){
jmpin 2:f06ba516b1ad 179 case sine:
jmpin 2:f06ba516b1ad 180 myWave = square;
jmpin 2:f06ba516b1ad 181 break;
jmpin 2:f06ba516b1ad 182 case square:
jmpin 2:f06ba516b1ad 183 myWave = sawtooth;
jmpin 2:f06ba516b1ad 184 break;
jmpin 2:f06ba516b1ad 185 case sawtooth:
jmpin 2:f06ba516b1ad 186 myWave = sine;
jmpin 2:f06ba516b1ad 187 break;
jmpin 2:f06ba516b1ad 188 default:
jmpin 2:f06ba516b1ad 189 break;
jmpin 2:f06ba516b1ad 190 }
jmpin 2:f06ba516b1ad 191 }
jmpin 2:f06ba516b1ad 192 else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed
jmpin 2:f06ba516b1ad 193 // Change waveform shape to previous waveform type
jmpin 2:f06ba516b1ad 194 switch(myWave){
jmpin 2:f06ba516b1ad 195 case sine:
jmpin 2:f06ba516b1ad 196 myWave = sawtooth;
jmpin 2:f06ba516b1ad 197 break;
jmpin 2:f06ba516b1ad 198 case square:
jmpin 2:f06ba516b1ad 199 myWave = sine;
jmpin 2:f06ba516b1ad 200 break;
jmpin 2:f06ba516b1ad 201 case sawtooth:
jmpin 2:f06ba516b1ad 202 myWave = square;
jmpin 2:f06ba516b1ad 203 break;
jmpin 2:f06ba516b1ad 204 default:
jmpin 2:f06ba516b1ad 205 break;
jmpin 2:f06ba516b1ad 206 }
jmpin 2:f06ba516b1ad 207 }
jmpin 2:f06ba516b1ad 208
jmpin 0:48311ffdfa96 209 }
jmpin 0:48311ffdfa96 210 //do other tasks in main - interrupts will process button message characters
jmpin 0:48311ffdfa96 211 myled = 1;
jmpin 0:48311ffdfa96 212 wait(0.1);
jmpin 0:48311ffdfa96 213 myled = 0;
jmpin 0:48311ffdfa96 214 wait(0.1);
jmpin 0:48311ffdfa96 215
jmpin 0:48311ffdfa96 216 }