Solo project mbed 2
Dependencies: PololuLedStrip mbed
Fork of SX10_BigBangFinal_A by
Revision 2:bc7345c92188, committed 2017-02-06
- Comitter:
- mptapton
- Date:
- Mon Feb 06 09:42:24 2017 +0000
- Parent:
- 1:e4d11295b3f4
- Commit message:
- Tapton School project guitar tuner mbed2 code
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r e4d11295b3f4 -r bc7345c92188 main.cpp --- a/main.cpp Mon Jan 30 13:10:56 2017 +0000 +++ b/main.cpp Mon Feb 06 09:42:24 2017 +0000 @@ -2,17 +2,22 @@ #include "PololuLedStrip.h" #include <iostream> -#define LED_COUNT 60 +#define LED_COUNT 36 //was 60 Serial pc(USBTX, USBRX); // tx, rx Serial device(p13, p14); // tx, rx to connect to mbed 1 +DigitalIn intune(p19); +DigitalIn toohigh(p18); +DigitalIn toolow(p17); +DigitalIn state(p16); DigitalOut ledd1(LED1); DigitalOut ledd2(LED2); DigitalOut ledd3(LED3); DigitalOut ledd4(LED4); +DigitalIn myInputPin (p21); //select tuner or chord learning mode -char rx_buffer[10]; //Only small as not an interupt buffer +char typed[20]; PololuLedStrip ledStrip(p11); @@ -20,7 +25,7 @@ Timer timer; -int aMajor[6] = {0,2,2,2,0,-1}; +int aMajor[6] = {0,2,2,2,0,-1}; //<<<< CHORDS supported in Chord Mode for LED Strip int bMajor[6] = {2,4,4,4,2,-1}; int cMajor[6] = {0,1,0,2,3,-1}; int dMajor[6] = {2,3,2,0,-1,-1}; @@ -28,23 +33,12 @@ int fMajor[6] = {1,-1,2,3,3,1}; int gMajor[6] = {3,0,0,0,2,3}; -int aMinor[6] = {0,1,2,2,0,-1}; -int bMinor[6] = {2,3,4,4,2,-1}; -int cMinor[6] = {3,4,5,5,3,-1}; -int dMinor[6] = {1,3,2,0,-1,-1}; -int eMinor[6] = {0,0,0,2,2,0}; -int fMinor[6] = {1,1,1,3,3,1}; -int gMinor[6] = {-1,-1,-1,-1,0,0}; -int gMinorActual[6] = {3,3,3,5,5,3}; - -int aSharpMajor[6] = {1,3,3,3,1,-1}; -int cSharpMajor[6] = {1,2,1,3,4,-1}; -int fSharpMajor[6] = {2,-1,3,4,4,2}; -int gSharpMajor[6] = {4,1,1,1,3,4}; - -int aSharpMinor[6] = {1,2,3,3,1,-1}; -int fSharpMinor[6] = {2,2,2,4,4,2}; - +int lowEstring[6] = {0,11,12,23,24,35}; //<<<< STRINGS supported in Tuner mode for LED strip +int Astring[6] = {1,10,13,22,25,34}; +int Dstring[6] = {2,9,14,21,26,33}; +int Gstring[6] = {3,8,15,20,27,32}; +int Bstring[6] = {4,7,16,19,28,31}; +int HiEstring[6] = {5,6,17,18,29,30}; /* Above are the various arrays which represent the location of the string played in a given fret to achieve a certain chord. The layout @@ -92,96 +86,42 @@ int decoder(string typed) { - int ascii = int(typed[0]); - // Convert the first character in the array 's' and find its - // hexadecimal code as a reference - int chordValue; - // Initialise a variable associated with the result so we can - // assess it later - if (65 <= ascii && ascii <= 71) - // If its hexadecimal number corresponds to a uppercase letter - // such that it is a major chord and... - { - // if (typed.size() == 1) - // ...if the input is only one letter/symbol... - // { - chordValue = ascii - 65; // Just set the chord as non sharp - - // ...then offset the hexadecimal code of that letter by 65 - // to map it back to the original list of non-sharp major chords and - // store that value. - // } - // else - // ...if the input is more than one letter/symbol then assume - // a hash (#) was entered to indicate a sharp value so... - /* { - chordValue = ascii - 51; - // ...offset the hexadecimal code by 51 to map it back to - // the original list of sharp major chords and store that value. - } - */ - } - else if (97 <= ascii && ascii <= 103) - // If its hexadecimal number corresponds to a lowercase letter - // such that it is a minor chord and... - { - // if (typed.size() == 1) - // ...if the input is only one letter/symbol... - // { - chordValue = ascii - 90; - // ...then offset the hexadecimal code by 90 to map it back to - // the original list of non-sharp minor chords and store that value. - // } - // else - // ...if the input is more than one letter/symbol then assume - // a hash (#) was entered to indicate a sharp value so... - // { - // chordValue = ascii - 76; - // ...offset the hexadecimal code by 51 to map it back to - // the original list of sharp minor chords and store that value. - // } - } - else - // If the typed character does not correspond to an existing chord then... - { - pc.printf("Incorrect input"); - // Output an error message that indicates an incorrect input by the - // user and... - return(-99); - // ...output -99 to indicate an error has occured to other parts of - // the code. - } - - /* if (typed.size() > 2) - // If the user input is greater than two characters then... - { - pc.printf("Incorrect input\n"); - // Output an error message that indicates an incorrect input by the - // user and... - return(-99); - // ...output -99 to indicate an error has occured to other parts of - // the code. - } - */ - if (chordValue != 15 && chordValue != 17 && chordValue != 18 && chordValue != 22 && chordValue != 23 && chordValue != 24 && chordValue != 25 && chordValue != 27) - // If the obtained chord value corresponds to a chord that can actually be - // played by Solo then... - { - return chordValue; - // ...output that chord value - } - else - // If the chord value corresponds to a chord that cannot be played by Solo or - // is not even in the original table of chords then... - { - pc.printf("This chord will not be played by the Solo\n"); - // Output an error message that indicates a chord that cannot be played by the - // Solo and... - return -1; - // ...output -1 to indicate an error with the input - } + int ascii = int(typed[0]);// get the hex value for input char from mbed1 + int chordValue; //set up integer 0-6 to represent A-G for later + if (65 <= ascii && ascii <= 71) //test if mbed1 input is a major chord A-G + { + chordValue = ascii - 65; // it is A-G so set int to appropriate 0-6 + // pc.printf(" %d ", chordValue); +/* + switch (chordValue) { //for diagnostics use putc not printf to send + case 0: //received char from mbed1 to mbed2's usb pc + pc.putc('A'); + break; + case 1: + pc.putc('B'); + break; + case 2: + pc.putc('C'); + break; + case 3: + pc.putc('D'); + break; + case 4: + pc.putc('E'); + break; + case 5: + pc.putc('F'); + break; + case 6: + pc.putc('G'); + break; + } + */ + } +return chordValue; } + void illuminator (int position, int colour) { //pc.printf ("Position: %d Colour: %d\n",position,colour); @@ -206,408 +146,272 @@ } } -void feedback(int colour) -{ - if (colour == 1) - { - pc.printf("Great Job!\n"); - } - else if (colour == 0) - { - pc.printf("Processing...\n"); - } - else if (colour == -1) - { - pc.printf("You suck!\n"); - } - else - { - pc.printf("Error: Colour input incorrect"); - } -} + + //Possible to change to a case/switch structure improve speed? +int shaper (int shape, int colour)// <<<<<< CHORD SHAPER >>>>>>> + { + if (shape == 0) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(aMajor[i],i+1), colour); + } + // feedback(colour); + } + else if (shape == 1) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(bMajor[i],i+1), colour); + } + // feedback(colour); + + } + else if (shape == 2) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(cMajor[i],i+1), colour); + } + // feedback(colour); + } + else if (shape == 3) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(dMajor[i],i+1), colour); + } + // feedback(colour); + } + else if (shape == 4) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(eMajor[i],i+1), colour); + } + // feedback(colour); + } + else if (shape == 5) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(fMajor[i],i+1), colour); + } + // feedback(colour); + } + else if (shape == 6) + { + for(int i = 0; i<6; i++) + { + illuminator (ledMapping(gMajor[i],i+1), colour); + } + // feedback(colour); + } -void identifier(int shape) -{ - int list; - // Initialise an integer variable 'result' that represents the list a chord belongs to. - int position; - // Initialise an integer variable 'position' that represents the chord's relative position - // in its allocated list. - const char* letter; - // Intialise a character variable 'letter' that will store the letter of the chord (A to G) - string type; - // Initialise a string (sentence) variable 'type' that will store the type of the chord - // e.g. "Sharp Minor" or "Major" etc. - - list = int(shape/7); - // Set the value of the 'list' variable to correspond to the list to which - // the chord (represented by 'shape') belongs to. Each list holds 7 chords - // relating to the first 7 letters of the alphabet (A to G). Therefore the list - // a chord originates from can be determined by interpreting the integer value - // the chord rounds down to, when its value is divided by seven. For example... - - if (list == 0) - // ...if the chord comes from list '0', then it must be... - { - type = "Major"; - // ... a Major chord. - } else if (list == 1) - // ...if the chord comes from list '1', then it must be... - { - type = "Minor"; - // ... a Minor chord. - } else if (list == 2) - // ...if the chord comes from list '2', then it must be... - { - type = "Sharp Major"; - // ... a Sharp Major chord. - } else if (list == 3) - // ...if the chord comes from list '3', then it must be... - { - type = "Sharp Minor"; - // ... a Sharp Minor chord. + // Send the colors to the LED strip. + // ledStrip.write(colors, LED_COUNT); + return (0); } - position = 7*(list + 1) - shape; - // Set the value of the 'position' as an indication of the position in the given list, - // that is to say that higher chord values in the same list will have a lower - // 'letter' due to the layout of the equation above. Irrespective of the list in - // which it occurs... + //Possible to change to a case/switch structure improve speed? +int Tunershaper (int shape, int colour) // <<<<<<<< TUNER SHAPER >>>>>>>>> + { + if (shape == 0) + { + for(int i = 0; i<6; i++) + { + illuminator (lowEstring[i], colour); + } + // feedback(colour); + } + else if (shape == 1) + { + for(int i = 0; i<6; i++) + { + illuminator (Astring[i], colour); + } + // feedback(colour); + + } + else if (shape == 2) + { + for(int i = 0; i<6; i++) + { + illuminator (Dstring[i], colour); + } + // feedback(colour); + } + else if (shape == 3) + { + for(int i = 0; i<6; i++) + { + illuminator (Gstring[i], colour); + } + // feedback(colour); + } + else if (shape == 4) + { + for(int i = 0; i<6; i++) + { + illuminator (Bstring[i], colour); + } + // feedback(colour); + } + else if (shape == 5) + { + for(int i = 0; i<6; i++) + { + illuminator (HiEstring[i], colour); + } + // feedback(colour); + } - if (position == 1) - // ...if the position value is 1, then it must be... - { - letter = "G"; - // ... a G chord. - } else if (position == 2) - // ...if the position value is 2, then it must be... - { - letter = "F"; - // ... an F chord. - } else if (position == 3) - // ...if the position value is 2, then it must be... - { - letter = "E"; - // ... an E chord. - } else if (position == 4) - // ...if the position value is 4, then it must be... - { - letter = "D"; - // ... a D chord. - } else if (position == 5) - // ...if the position value is 5, then it must be... - { - letter = "C"; - // ... a C chord. - } else if (position == 6) - // ...if the position value is 6, then it must be... - { - letter = "B"; - // ... a B chord. - } else if (position == 7) - // ...if the position value is 7, then it must be... - { - letter = "A"; - // ... an A chord. - } - pc.printf("%s %s\n",letter,type.c_str()); - // Output the letter of the chord, followed by its type separated by a - // space such that the full name of the chord is displayed. -} - -//TODO fix return values -//Change to a case/switch structure -int shaper (int shape, int colour) -{ - if (shape == 0) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(aMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 1) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(bMajor[i],i+1), colour); - } - feedback(colour); - - } - else if (shape == 2) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(cMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 3) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(dMajor[i],i+1), colour); - } - feedback(colour); + // Send the colors to the LED strip. + //ledStrip.write(colors, LED_COUNT); + return (0); } - else if (shape == 4) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(eMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 5) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(fMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 6) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(gMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 7) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(aMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 8) - { - for(int i = 0; i<6; i++) + +void clearLEDstrip() //writes to all the LEDs to clear them + { + wait(0.2); + for(int i=0; i < LED_COUNT ; i++) { - illuminator (ledMapping(bMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 9) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(cMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 10) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(dMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 11) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(eMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 12) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(fMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 13) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(gMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 14) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(aSharpMajor[i],i+1), colour); + colors[i] = (rgb_color){ 0, 0, 0 }; } - feedback(colour); - } - else if (shape == 16) - { - for(int i = 0; i<6; i++) - { - illuminator (ledMapping(cSharpMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 19) - { - for(int i = 0; i<6; i++) - { - illuminator(ledMapping(fSharpMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 20) - { - for(int i = 0; i<6; i++) - { - illuminator(ledMapping(gSharpMajor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 21) - { - for(int i = 0; i<6; i++) - { - illuminator(ledMapping(aSharpMinor[i],i+1), colour); - } - feedback(colour); - } - else if (shape == 26) - { - for(int i = 0; i<6; i++) - { - illuminator(ledMapping(fSharpMinor[i],i+1), colour); - } - feedback(colour); - } - else - { - //this point should never be reached; there are no other chords - pc.printf ("Error: shaper failed\n"); - } - // Send the colors to the LED strip. - ledStrip.write(colors, LED_COUNT); - if(colour == 1) - { - wait(0.5); - for(int i=0; i < LED_COUNT ; i++) - { - colors[i] = (rgb_color){ 0, 0, 0 }; - } - ledStrip.write(colors, LED_COUNT); - } + ledStrip.write(colors, LED_COUNT); + } - return (0); -} - -int tester (int testChord, int requestChord) -{ - /* if (testChord == 0 || requestChord == 0) - { - return (99); - } - */ - if (testChord == requestChord) - { - return (1); - } - else - { - return (-1); - } -} - - -/*int interpreter(int requestedCode)// this function was all commented out -{ - int r = 0; - int chord = 0; - - r = rand() % 10; - chord = rand() % 24; - if(chord == 0) chord=1; - - if(r>1) return(requestedCode); - else return(chord); - -} -*/ - +void callback() //interrupt from serial port from mbed1 + { + ledd1=1; // received character from mbed1 + typed[0]=device.getc(); //get char from mbed1 via serial port + } int main() { - pc.baud (19200); + pc.baud (115200); //for pc usb device.baud(19200); //for communications with mbed 1 + myInputPin.mode(PullUp); //set the mbed to use a pullup resistor string chord; - char typed[20]; - while(1) - { - // pc.printf("Type in the chord you would like to use \n"); - // pc.scanf("%s",typed); - ledd1=0; - ledd2=0; - ledd3=0; - ledd4=0; - if (device.readable()){ - //ledd1=1; - char som = device.getc(); - if (som == 'Q'){ //Is the received byte a SOM (letter Q)? - for (int j=0; j<=6; j++) { //if it is a SOM get the next 6 bytes from serial port - if (device.readable()){ - //rx_buffer[j] = device.getc(); - //pc.printf("%d%s", j,rx_buffer[j]); - som = device.getc(); - if (j==1) { //get byte 3 (key/note) from serial port message - typed[0] = som; // put the key /note into the typed array - pc.putc(typed[0]); //print it to a pc on the USB - } - //pc.putc(device.getc()); - //pc.printf("%d",j); - // ledd2=1; - wait(0.1); - } - } - } - // ledd3=1; - } - chordCode = decoder(typed); - if (chordCode != -99 && chordCode != -1) + int chordCode; + setbuf(stdin, NULL);//clear the serial input buffer + device.attach(&callback);// for serial interupt callback function + clearLEDstrip(); + + myInputPin.mode(PullUp); //set the mbed to use a pullup resistor + if (myInputPin) // <<<<SELECT GUITAR TUNER OR CHORD TRAINER FUNCTION>>> { - ledd1=1; - identifier(chordCode); - shaper(chordCode, 0); - interpretedChordCode = chordCode; // removed interpreter(chordCode); - testResult = tester(interpretedChordCode, chordCode); - - while(1) - { - if(testResult == -1) + while(1) + { + ledd1=0; //received character from mbed1 + ledd2=toolow; //inputs from mbed1 mapped onto mbed 2 LED's + ledd3=intune; // + ledd4=toohigh;// + // pc.putc(typed[0]); //debugging + /* chordCode = decoder(typed); //takes input char from mbed1 and gives back 0-6 to represent chord selected A-G + shaper(chordCode, 0); //light current chord in white (0) + wait(0.8); //hold it for a period so user can see it + if (intune) //read mbed1 intune pin and light the green LEDs + { + shaper(chordCode, 1); //green light (1) + wait(0.4); + } + if ((toohigh) || (toolow))//read mbed1 toohigh and toolow pins and light the red LEDs { - ledd2=1; - wait(2); - shaper(chordCode, testResult); - wait(0.5); - shaper(chordCode, 0); - wait(1); - interpretedChordCode = chordCode; // removed interpreter(chordCode); - testResult = tester(interpretedChordCode, chordCode); + shaper(chordCode, -1); //red light (-1) + wait(0.4); + } */ + + /* colors[0] = (rgb_color){0,40,0}; + colors[11] = (rgb_color){0,40,0}; + colors[12] = (rgb_color){0,40,0}; + colors[23] = (rgb_color){0,40,0}; + colors[24] = (rgb_color){0,40,0}; + colors[35] = (rgb_color){0,40,0}; + ledStrip.write(colors, LED_COUNT); + */ + + for(int j = 0; j<6; j++) + { + chordCode=j; + Tunershaper(chordCode, -1); //red + wait(0.5); + ledStrip.write(colors, LED_COUNT); + } + for(int j = 0; j<6; j++) + { + chordCode=j; + Tunershaper(chordCode, 1); //green + wait(0.5); + ledStrip.write(colors, LED_COUNT); + } + for(int j = 0; j<6; j++) + { + chordCode=j; + Tunershaper(chordCode, 0); //green + wait(0.5); + ledStrip.write(colors, LED_COUNT); } - else if(testResult == 1) - { - ledd3=1; - wait(2); - shaper(chordCode, testResult); - wait(0.5); - break; - } - else if(testResult == 99) + /* + chordCode=0; + Tunershaper(chordCode, 0); //white + wait(0.5); + ledStrip.write(colors, LED_COUNT); + + chordCode=1; + Tunershaper(chordCode, 0); //white + wait(0.5); + ledStrip.write(colors, LED_COUNT); + + chordCode=2; + Tunershaper(chordCode, 0); //white + wait(0.5); + ledStrip.write(colors, LED_COUNT); + + chordCode=3; + Tunershaper(chordCode, 0); //white + wait(0.5); + ledStrip.write(colors, LED_COUNT); + + chordCode=4; + Tunershaper(chordCode, 0); //white + wait(0.5); + ledStrip.write(colors, LED_COUNT); + + chordCode=5; + Tunershaper(chordCode, 0); //white + wait(0.5); + ledStrip.write(colors, LED_COUNT); + */ + + //clearLEDstrip(); + } + } + else //if myinputpin Chord or Tuner mode = Chord mode selected + { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + // ..............CHORD MODE......................... + // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + while(1) + { + ledd1=0; //received character from mbed1 + ledd2=intune; //intune input from mbed1 + ledd3=toohigh; // toohigh input from mbed1 + ledd4=state;// state input from mbed1 + // pc.putc(typed[0]); //debugging + chordCode = decoder(typed); //takes input char from mbed1 and gives back 0-6 to represent chord selected A-G + shaper(chordCode, 0); //light current chord in white (0) + wait(0.8); //hold it for a period so user can see it + if (intune) //read mbed1 intune pin and light the green LEDs { - ledd4=1; - pc.printf("Error"); - break; + shaper(chordCode, 1); //green light (1) + wait(0.4); } - } - wait_ms(10); - } - } + if ((toohigh) || (toolow))//read mbed1 toohigh and toolow pins and light the red LEDs + { + shaper(chordCode, -1); //red light (-1) + wait(0.4); + } + clearLEDstrip(); + } + } } \ No newline at end of file