Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@9:6b8118063819, 2016-10-05 (annotated)
- Committer:
- garphil
- Date:
- Wed Oct 05 18:55:27 2016 +0000
- Revision:
- 9:6b8118063819
- Parent:
- 8:fb28b9ec6d6e
- Child:
- 10:983ed805d74f
Buzzer made asynchronous
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
garphil | 1:ab4c9a0a5374 | 1 | #include "LaLaBox.h" |
garphil | 9:6b8118063819 | 2 | Serial pc_uart(PA_2, PA_3); |
garphil | 1:ab4c9a0a5374 | 3 | |
garphil | 1:ab4c9a0a5374 | 4 | // ---------------- Local global variables -------------- |
garphil | 1:ab4c9a0a5374 | 5 | |
garphil | 1:ab4c9a0a5374 | 6 | // --- Sound --- |
garphil | 6:2f4867594333 | 7 | Music song_happy_birthday("Happy Birthday Song:d=4,o=5,b=125:16c,32p,32c,32p,8d,32p,8c,32p,8f,32p,e,16p,16c,32p,32c,32p,8d,32p,8c,32p,8g,32p,f,8p,16c,32p,32c,32p,8c6,32p,8a,32p,8f,32p,8e,32p,8d,32p,16a#,32p,32a#,32p,8a,32p,8f,32p,8g,32p,f"); |
garphil | 6:2f4867594333 | 8 | Music song_greensleaves("Greensleaves:d=4,o=5,b=140:g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,2a#,g,g.,8f,g,2a,f,2d,g,2a#,c6,d.6,8e6,d6,2c6,a,f.,8g,a,a#.,8a,g,f#.,8e,f#,2g"); |
garphil | 6:2f4867594333 | 9 | Music song_lightmyfire("LightMyFire:d=4,o=5,b=140:8b,16g,16a,8b,8d6,8c6,8b,8a,8g,8a,16f,16a,8c6,8f6,16d6,16c6,16a#,16g,8g#,8g,8g#,16g,16a,8b,8c#6,16b,16a,16g,16f,8e,8f,1a,a"); |
garphil | 6:2f4867594333 | 10 | Music song_xfile("Xfiles:d=4,o=5,b=140:e,b,a,b,d6,2b."); |
garphil | 6:2f4867594333 | 11 | Music song_christmas("Christmas:d=4,o=5,b=100:f#,g#,2a#,2a#,d#.,8f,f,f,2f#,2d#,2f#.6,f#,#g,8g#,g#,8a#,b,8c#,c#,2c#,8d#,8f.,8f#.,8f.,d#,f,2f#.,a#,8b.,8b.,8b.,d#,f,2c#.,a#,8b.,8b.,8b.,d#,f,f#,p,g#,g#,g#,8g#,8a#,8g#,f#.,g#,a#,p,c,c,c,8c,8c#,8c,a#.,c,c#,a#,b,a#,a,a#,b,c,"); |
garphil | 1:ab4c9a0a5374 | 12 | |
garphil | 2:050f12806bc5 | 13 | // ---------------- PIN DEFINITIONS --------------------- |
garphil | 2:050f12806bc5 | 14 | DigitalOut myled(LED1); // Blinking LED |
garphil | 5:f62e799558c3 | 15 | InterruptIn myButton(USER_BUTTON); |
garphil | 2:050f12806bc5 | 16 | |
garphil | 6:2f4867594333 | 17 | // --- Define the Four PINs & Time of movement used for Motor drive ----- |
garphil | 6:2f4867594333 | 18 | Motor motorBox(PA_9, PC_7, PB_6, PA_7, 30000); |
garphil | 6:2f4867594333 | 19 | Motor motorDancer(PA_8, PB_10, PB_4, PB_5, 10000); |
garphil | 6:2f4867594333 | 20 | |
garphil | 6:2f4867594333 | 21 | // PIN & number of LEDS. Available color ==> BLUE, LIGHTBLUE, RED, GREEN, BLACK, WHITE, PURPLE, PINK, YELLOW |
garphil | 7:592af6237038 | 22 | LED_WS2812 ledBand(D3,8); |
garphil | 2:050f12806bc5 | 23 | |
garphil | 1:ab4c9a0a5374 | 24 | // --- Define PC_8 as the output of PWM use for Tones ----- |
garphil | 1:ab4c9a0a5374 | 25 | Buzzer buzzer(PC_8); |
garphil | 0:30cb0f6dad87 | 26 | |
garphil | 1:ab4c9a0a5374 | 27 | Note la("A#4",50); //the sound |
garphil | 1:ab4c9a0a5374 | 28 | |
garphil | 5:f62e799558c3 | 29 | // init |
garphil | 6:2f4867594333 | 30 | int state = 0; |
garphil | 2:050f12806bc5 | 31 | |
garphil | 1:ab4c9a0a5374 | 32 | void help() // Display list of Commands |
garphil | 1:ab4c9a0a5374 | 33 | { |
garphil | 1:ab4c9a0a5374 | 34 | DEBUG("List of commands:\n\r"); |
garphil | 1:ab4c9a0a5374 | 35 | DEBUG(" h --> Help, display list of cammands\n\r"); |
garphil | 1:ab4c9a0a5374 | 36 | DEBUG(" z --> Go to motor zero\n\r"); |
garphil | 1:ab4c9a0a5374 | 37 | DEBUG(" k --> Calibrate motor\n\r"); |
garphil | 1:ab4c9a0a5374 | 38 | DEBUG(" n dddd --> define Number of coils (default=100)\n\r"); |
garphil | 1:ab4c9a0a5374 | 39 | DEBUG(" c --> define Clockwise (default)\n\r"); |
garphil | 1:ab4c9a0a5374 | 40 | DEBUG(" a --> define Anti-clockwise\n\r"); |
garphil | 1:ab4c9a0a5374 | 41 | DEBUG(" m --> Memorise configuration\n\r"); |
garphil | 1:ab4c9a0a5374 | 42 | DEBUG(" l --> List current configuration\n\r"); |
garphil | 1:ab4c9a0a5374 | 43 | DEBUG(" w --> start Wiring the coils\n\r"); |
garphil | 1:ab4c9a0a5374 | 44 | DEBUG(" p --> Pause wiring\n\r"); |
garphil | 1:ab4c9a0a5374 | 45 | DEBUG(" r --> Resume wiring\n\r"); |
garphil | 1:ab4c9a0a5374 | 46 | DEBUG(" s --> Stop (abort) wiring\n\r"); |
garphil | 1:ab4c9a0a5374 | 47 | DEBUG(" [space] --> print remaining Motor steps\n\r"); |
garphil | 1:ab4c9a0a5374 | 48 | } |
garphil | 1:ab4c9a0a5374 | 49 | |
garphil | 5:f62e799558c3 | 50 | void pressed() { |
garphil | 6:2f4867594333 | 51 | state++; |
garphil | 6:2f4867594333 | 52 | switch(state) { |
garphil | 6:2f4867594333 | 53 | case 1: |
garphil | 6:2f4867594333 | 54 | motorDancer.Stop(); |
garphil | 6:2f4867594333 | 55 | motorBox.RunDegrees(CLOCKWISE, (float)90.0); |
garphil | 6:2f4867594333 | 56 | ledBand.SetColor(WHITE); |
garphil | 6:2f4867594333 | 57 | break; |
garphil | 6:2f4867594333 | 58 | case 2: |
garphil | 6:2f4867594333 | 59 | motorDancer.RunDegrees(CLOCKWISE, (float)180.0); |
garphil | 6:2f4867594333 | 60 | motorBox.RunDegrees(COUNTERCLOCKWISE, (float)180.0); |
garphil | 7:592af6237038 | 61 | ledBand.SetColor(0x0000FF); |
garphil | 6:2f4867594333 | 62 | ledBand.SetIntensity(50.0); |
garphil | 6:2f4867594333 | 63 | break; |
garphil | 6:2f4867594333 | 64 | case 3: |
garphil | 7:592af6237038 | 65 | ledBand.InsertColor(BLUE); |
garphil | 7:592af6237038 | 66 | ledBand.InsertColor(WHITE); |
garphil | 7:592af6237038 | 67 | ledBand.InsertColor(RED); |
garphil | 6:2f4867594333 | 68 | break; |
garphil | 7:592af6237038 | 69 | case 4: |
garphil | 7:592af6237038 | 70 | ledBand.StartRotation(1); |
garphil | 6:2f4867594333 | 71 | break; |
garphil | 6:2f4867594333 | 72 | case 5: |
garphil | 7:592af6237038 | 73 | ledBand.StopRotation(); |
garphil | 8:fb28b9ec6d6e | 74 | // ledBand.ResetColor(); |
garphil | 7:592af6237038 | 75 | ledBand.InsertColorNtimes(3,BLUE); |
garphil | 7:592af6237038 | 76 | ledBand.InsertColorNtimes(1,WHITE); |
garphil | 7:592af6237038 | 77 | ledBand.InsertColorNtimes(4,RED,50.0); |
garphil | 9:6b8118063819 | 78 | ledBand.StartRotation(0.1); |
garphil | 6:2f4867594333 | 79 | PLAY_MUSIC(song_happy_birthday, buzzer); |
garphil | 6:2f4867594333 | 80 | break; |
garphil | 6:2f4867594333 | 81 | default: |
garphil | 7:592af6237038 | 82 | ledBand.StopRotation(); |
garphil | 6:2f4867594333 | 83 | motorDancer.Stop(); |
garphil | 6:2f4867594333 | 84 | motorBox.Stop(); |
garphil | 6:2f4867594333 | 85 | state = 0; |
garphil | 7:592af6237038 | 86 | break; |
garphil | 5:f62e799558c3 | 87 | } |
garphil | 9:6b8118063819 | 88 | myled = !myled; // To see something is alive |
garphil | 7:592af6237038 | 89 | |
garphil | 5:f62e799558c3 | 90 | } |
garphil | 5:f62e799558c3 | 91 | |
garphil | 0:30cb0f6dad87 | 92 | int main() { |
garphil | 1:ab4c9a0a5374 | 93 | myled = 1; // To see something is alive |
garphil | 1:ab4c9a0a5374 | 94 | DEBUG("\n\n\r"); |
garphil | 1:ab4c9a0a5374 | 95 | DEBUG("------------------------------------------\n\r"); |
garphil | 8:fb28b9ec6d6e | 96 | DEBUG("----- LaLaBox (CreaLab) version 1.0 ----\n\r"); |
garphil | 1:ab4c9a0a5374 | 97 | DEBUG("------------------------------------------\n\r"); |
garphil | 1:ab4c9a0a5374 | 98 | help(); |
garphil | 1:ab4c9a0a5374 | 99 | DEBUG("------------------------------------------\n\r"); |
garphil | 9:6b8118063819 | 100 | ledBand.SetColor(BLACK); |
garphil | 5:f62e799558c3 | 101 | |
garphil | 8:fb28b9ec6d6e | 102 | CATCH_BUTTON(myButton,pressed); |
garphil | 6:2f4867594333 | 103 | |
garphil | 9:6b8118063819 | 104 | PLAY_NOTE(la, buzzer); |
garphil | 9:6b8118063819 | 105 | |
garphil | 6:2f4867594333 | 106 | wait(2); // Some delay |
garphil | 1:ab4c9a0a5374 | 107 | myled = 0; // Real stuff starts here |
garphil | 9:6b8118063819 | 108 | ledBand.SetColor(BLACK); |
garphil | 9:6b8118063819 | 109 | |
garphil | 9:6b8118063819 | 110 | motorBox.SetDirection(CLOCKWISE); |
garphil | 5:f62e799558c3 | 111 | while(1) { |
garphil | 1:ab4c9a0a5374 | 112 | char command; // Command to execute |
garphil | 5:f62e799558c3 | 113 | DEBUG(">> "); |
garphil | 1:ab4c9a0a5374 | 114 | command = pc_uart.getc(); |
garphil | 1:ab4c9a0a5374 | 115 | DEBUG("%c", command); |
garphil | 5:f62e799558c3 | 116 | |
garphil | 1:ab4c9a0a5374 | 117 | switch (command) { |
garphil | 1:ab4c9a0a5374 | 118 | case 'h': help(); break; |
garphil | 6:2f4867594333 | 119 | case 'z': motorBox.SetZero(); break; |
garphil | 4:d452a7e3461a | 120 | case 'k': motorBox.TestMotor(); break; |
garphil | 4:d452a7e3461a | 121 | case 'c': |
garphil | 4:d452a7e3461a | 122 | DEBUG(" -- Direction CLOCKWISE --\n\r"); |
garphil | 6:2f4867594333 | 123 | motorBox.SetDirection(CLOCKWISE); |
garphil | 1:ab4c9a0a5374 | 124 | break; |
garphil | 4:d452a7e3461a | 125 | case 'a': |
garphil | 4:d452a7e3461a | 126 | DEBUG(" -- Direction COUNTER CLOCKWISE --\n\r"); |
garphil | 6:2f4867594333 | 127 | motorBox.SetDirection(COUNTERCLOCKWISE); |
garphil | 4:d452a7e3461a | 128 | break; |
garphil | 6:2f4867594333 | 129 | case 'w': DEBUG(" -- Starting --\n\r"); motorBox.Start(); break; |
garphil | 6:2f4867594333 | 130 | case 'p': DEBUG(" -- Pause --\n\r"); motorBox.Stop(); break; |
garphil | 6:2f4867594333 | 131 | case 'r': DEBUG(" -- Re-Start --\n\r"); motorBox.Restart(); break; |
garphil | 6:2f4867594333 | 132 | case 's': DEBUG(" -- Stop --\n\r"); motorBox.Stop(); break; |
garphil | 1:ab4c9a0a5374 | 133 | default : DEBUG("invalid command; use: 'h' for help()"); |
garphil | 1:ab4c9a0a5374 | 134 | } |
garphil | 0:30cb0f6dad87 | 135 | } |
garphil | 1:ab4c9a0a5374 | 136 | } |