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