Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Committer:
jmpin
Date:
Thu Apr 28 01:30:43 2016 +0000
Revision:
4:406f59c6a1a6
Parent:
3:3aba1d783730
Child:
5:afd67e985df0
Added input checking so that you cannot lower the ADSR or octave below 1, and you cannot raise these factors above their maximum values.

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 4:406f59c6a1a6 101 PC.printf("Got a 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 4:406f59c6a1a6 109 readyFlag = false;
jmpin 0:48311ffdfa96 110 // Play note that corresponds to X
jmpin 0:48311ffdfa96 111 }
jmpin 3:3aba1d783730 112 else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed
jmpin 0:48311ffdfa96 113 // Play note that corresponds to C
jmpin 3:3aba1d783730 114 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 115 write_to_SDCard('E');
jmpin 4:406f59c6a1a6 116 readyFlag = false;
jmpin 3:3aba1d783730 117 }
jmpin 3:3aba1d783730 118 else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed
jmpin 0:48311ffdfa96 119 // Play note that corresponds to V
jmpin 3:3aba1d783730 120 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 121 write_to_SDCard('F');
jmpin 4:406f59c6a1a6 122 readyFlag = false;
jmpin 3:3aba1d783730 123 }
jmpin 3:3aba1d783730 124 else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed
jmpin 0:48311ffdfa96 125 // Play note that corresponds to B
jmpin 3:3aba1d783730 126 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 127 write_to_SDCard('G');
jmpin 4:406f59c6a1a6 128 readyFlag = false;
jmpin 3:3aba1d783730 129 }
jmpin 3:3aba1d783730 130 else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed
jmpin 0:48311ffdfa96 131 // Play note that corresponds to N
jmpin 3:3aba1d783730 132 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 133 write_to_SDCard('A');
jmpin 4:406f59c6a1a6 134 readyFlag = false;
jmpin 3:3aba1d783730 135 }
jmpin 3:3aba1d783730 136 else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed
jmpin 0:48311ffdfa96 137 // Play note that corresponds to M
jmpin 3:3aba1d783730 138 // Make note of which note was played in file on SD Card
jmpin 3:3aba1d783730 139 write_to_SDCard('B');
jmpin 4:406f59c6a1a6 140 readyFlag = false;
jmpin 3:3aba1d783730 141 }
jmpin 2:f06ba516b1ad 142 else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
jmpin 0:48311ffdfa96 143 // Raise an octave
jmpin 4:406f59c6a1a6 144 if(currentOcatve < 7)
jmpin 2:f06ba516b1ad 145 currentOctave++;
jmpin 2:f06ba516b1ad 146 }
jmpin 4:406f59c6a1a6 147 else
jmpin 4:406f59c6a1a6 148 printf("Cannot raise octave above 7.\r\n");
jmpin 2:f06ba516b1ad 149 else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed
jmpin 2:f06ba516b1ad 150 // Lower an octave
jmpin 4:406f59c6a1a6 151 if(currentOctave > 1)
jmpin 2:f06ba516b1ad 152 currentOctave--;
jmpin 4:406f59c6a1a6 153 else
jmpin 4:406f59c6a1a6 154 printf("Cannot lower octave below 1.\r\n");
jmpin 2:f06ba516b1ad 155 }
jmpin 2:f06ba516b1ad 156 else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed
jmpin 0:48311ffdfa96 157 // Raise Attack Value
jmpin 4:406f59c6a1a6 158 if(currentAttackVal < 5)
jmpin 2:f06ba516b1ad 159 currentAttackVal++;
jmpin 4:406f59c6a1a6 160 else
jmpin 4:406f59c6a1a6 161 printf("Cannot raise value above 5.\r\n");
jmpin 2:f06ba516b1ad 162 }
jmpin 2:f06ba516b1ad 163 else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed
jmpin 0:48311ffdfa96 164 // Lower Attack Value
jmpin 4:406f59c6a1a6 165 if(currentAttackVal > 1)
jmpin 2:f06ba516b1ad 166 currentAttackVal--;
jmpin 4:406f59c6a1a6 167 else
jmpin 4:406f59c6a1a6 168 printf("Cannot lower value below 1.\r\n";
jmpin 2:f06ba516b1ad 169 }
jmpin 2:f06ba516b1ad 170 else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed
jmpin 0:48311ffdfa96 171 // Raise Delay Value
jmpin 4:406f59c6a1a6 172 if(currentDelayVal < 5)
jmpin 2:f06ba516b1ad 173 currentDelayVal++;
jmpin 4:406f59c6a1a6 174 else
jmpin 4:406f59c6a1a6 175 printf("Cannot raise value above 5.\r\n");
jmpin 2:f06ba516b1ad 176 }
jmpin 2:f06ba516b1ad 177 else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed
jmpin 0:48311ffdfa96 178 // Lower Delay Value
jmpin 4:406f59c6a1a6 179 if(currentDelayVal > 1)
jmpin 2:f06ba516b1ad 180 currentDelayVal--;
jmpin 4:406f59c6a1a6 181 else
jmpin 4:406f59c6a1a6 182 printf("Cannot lower value below 1.\r\n";
jmpin 2:f06ba516b1ad 183 }
jmpin 2:f06ba516b1ad 184 else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed
jmpin 0:48311ffdfa96 185 // Raise Sustain Value
jmpin 4:406f59c6a1a6 186 if(currentSustainVal < 5)
jmpin 2:f06ba516b1ad 187 currentSustainVal++;
jmpin 4:406f59c6a1a6 188 else
jmpin 4:406f59c6a1a6 189 printf("Cannot raise value above 5.\r\n");
jmpin 2:f06ba516b1ad 190 }
jmpin 2:f06ba516b1ad 191 else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed
jmpin 0:48311ffdfa96 192 // Lower Sustain Value
jmpin 4:406f59c6a1a6 193 if(currentSustainVal > 1)
jmpin 2:f06ba516b1ad 194 currentSustainVal--;
jmpin 4:406f59c6a1a6 195 else
jmpin 4:406f59c6a1a6 196 printf("Cannot lower value below 1.\r\n";
jmpin 2:f06ba516b1ad 197 }
jmpin 2:f06ba516b1ad 198 else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed
jmpin 0:48311ffdfa96 199 // Raise Release Value
jmpin 4:406f59c6a1a6 200 if(currentReleaseVal < 5)
jmpin 2:f06ba516b1ad 201 currentReleaseVal++;
jmpin 4:406f59c6a1a6 202 else
jmpin 4:406f59c6a1a6 203 printf("Cannot raise value above 5.\r\n");
jmpin 2:f06ba516b1ad 204 }
jmpin 2:f06ba516b1ad 205 else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed
jmpin 0:48311ffdfa96 206 // Lower Release Value
jmpin 4:406f59c6a1a6 207 if(currentReleaseVal > 1)
jmpin 2:f06ba516b1ad 208 currentReleaseVal--;
jmpin 4:406f59c6a1a6 209 else
jmpin 4:406f59c6a1a6 210 printf("Cannot lower value below 1.\r\n";
jmpin 2:f06ba516b1ad 211 }
jmpin 2:f06ba516b1ad 212 else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed
jmpin 2:f06ba516b1ad 213 // Change waveform shape to next waveform type
jmpin 2:f06ba516b1ad 214 switch(myWave){
jmpin 2:f06ba516b1ad 215 case sine:
jmpin 2:f06ba516b1ad 216 myWave = square;
jmpin 2:f06ba516b1ad 217 break;
jmpin 2:f06ba516b1ad 218 case square:
jmpin 2:f06ba516b1ad 219 myWave = sawtooth;
jmpin 2:f06ba516b1ad 220 break;
jmpin 2:f06ba516b1ad 221 case sawtooth:
jmpin 2:f06ba516b1ad 222 myWave = sine;
jmpin 2:f06ba516b1ad 223 break;
jmpin 2:f06ba516b1ad 224 default:
jmpin 2:f06ba516b1ad 225 break;
jmpin 2:f06ba516b1ad 226 }
jmpin 2:f06ba516b1ad 227 }
jmpin 2:f06ba516b1ad 228 else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed
jmpin 2:f06ba516b1ad 229 // Change waveform shape to previous waveform type
jmpin 2:f06ba516b1ad 230 switch(myWave){
jmpin 2:f06ba516b1ad 231 case sine:
jmpin 2:f06ba516b1ad 232 myWave = sawtooth;
jmpin 2:f06ba516b1ad 233 break;
jmpin 2:f06ba516b1ad 234 case square:
jmpin 2:f06ba516b1ad 235 myWave = sine;
jmpin 2:f06ba516b1ad 236 break;
jmpin 2:f06ba516b1ad 237 case sawtooth:
jmpin 2:f06ba516b1ad 238 myWave = square;
jmpin 2:f06ba516b1ad 239 break;
jmpin 2:f06ba516b1ad 240 default:
jmpin 2:f06ba516b1ad 241 break;
jmpin 2:f06ba516b1ad 242 }
jmpin 2:f06ba516b1ad 243 }
jmpin 2:f06ba516b1ad 244
jmpin 0:48311ffdfa96 245 }
jmpin 0:48311ffdfa96 246 //do other tasks in main - interrupts will process button message characters
jmpin 0:48311ffdfa96 247 myled = 1;
jmpin 0:48311ffdfa96 248 wait(0.1);
jmpin 0:48311ffdfa96 249 myled = 0;
jmpin 0:48311ffdfa96 250 wait(0.1);
jmpin 0:48311ffdfa96 251
jmpin 0:48311ffdfa96 252 }