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@10:983ed805d74f, 2016-10-06 (annotated)
- Committer:
- garphil
- Date:
- Thu Oct 06 11:27:20 2016 +0000
- Revision:
- 10:983ed805d74f
- Parent:
- 9:6b8118063819
- Child:
- 11:c0728c05f977
LED timings generic for different boards
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 | 10:983ed805d74f | 18 | // Motor motorBox(PA_9, PC_7, PB_6, PA_7, 30000); |
garphil | 10:983ed805d74f | 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 | 10:983ed805d74f | 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 | 10:983ed805d74f | 36 | } |
garphil | 1:ab4c9a0a5374 | 37 | |
garphil | 5:f62e799558c3 | 38 | void pressed() { |
garphil | 6:2f4867594333 | 39 | state++; |
garphil | 6:2f4867594333 | 40 | switch(state) { |
garphil | 6:2f4867594333 | 41 | case 1: |
garphil | 10:983ed805d74f | 42 | // motorDancer.Stop(); |
garphil | 10:983ed805d74f | 43 | // motorBox.RunDegrees(CLOCKWISE, (float)90.0); |
garphil | 6:2f4867594333 | 44 | ledBand.SetColor(WHITE); |
garphil | 6:2f4867594333 | 45 | break; |
garphil | 6:2f4867594333 | 46 | case 2: |
garphil | 10:983ed805d74f | 47 | // motorDancer.RunDegrees(CLOCKWISE, (float)180.0); |
garphil | 10:983ed805d74f | 48 | // motorBox.RunDegrees(COUNTERCLOCKWISE, (float)180.0); |
garphil | 7:592af6237038 | 49 | ledBand.SetColor(0x0000FF); |
garphil | 10:983ed805d74f | 50 | ledBand.SetIntensity(20.0); |
garphil | 6:2f4867594333 | 51 | break; |
garphil | 6:2f4867594333 | 52 | case 3: |
garphil | 7:592af6237038 | 53 | ledBand.InsertColor(BLUE); |
garphil | 10:983ed805d74f | 54 | ledBand.InsertColor(WHITE); |
garphil | 7:592af6237038 | 55 | ledBand.InsertColor(RED); |
garphil | 6:2f4867594333 | 56 | break; |
garphil | 7:592af6237038 | 57 | case 4: |
garphil | 7:592af6237038 | 58 | ledBand.StartRotation(1); |
garphil | 6:2f4867594333 | 59 | break; |
garphil | 6:2f4867594333 | 60 | case 5: |
garphil | 7:592af6237038 | 61 | ledBand.StopRotation(); |
garphil | 10:983ed805d74f | 62 | ledBand.ResetColor(); |
garphil | 10:983ed805d74f | 63 | ledBand.InsertColorNtimes(3,BLUE,20.0); |
garphil | 7:592af6237038 | 64 | ledBand.InsertColorNtimes(1,WHITE); |
garphil | 10:983ed805d74f | 65 | ledBand.InsertColorNtimes(4,RED,50.0); |
garphil | 9:6b8118063819 | 66 | ledBand.StartRotation(0.1); |
garphil | 10:983ed805d74f | 67 | // PLAY_MUSIC(song_happy_birthday, buzzer); |
garphil | 6:2f4867594333 | 68 | break; |
garphil | 6:2f4867594333 | 69 | default: |
garphil | 7:592af6237038 | 70 | ledBand.StopRotation(); |
garphil | 10:983ed805d74f | 71 | // motorDancer.Stop(); |
garphil | 10:983ed805d74f | 72 | // motorBox.Stop(); |
garphil | 6:2f4867594333 | 73 | state = 0; |
garphil | 7:592af6237038 | 74 | break; |
garphil | 5:f62e799558c3 | 75 | } |
garphil | 9:6b8118063819 | 76 | myled = !myled; // To see something is alive |
garphil | 7:592af6237038 | 77 | |
garphil | 5:f62e799558c3 | 78 | } |
garphil | 5:f62e799558c3 | 79 | |
garphil | 10:983ed805d74f | 80 | |
garphil | 10:983ed805d74f | 81 | |
garphil | 0:30cb0f6dad87 | 82 | int main() { |
garphil | 1:ab4c9a0a5374 | 83 | myled = 1; // To see something is alive |
garphil | 10:983ed805d74f | 84 | DEBUG("\n\n%d\r",SystemCoreClock); |
garphil | 1:ab4c9a0a5374 | 85 | DEBUG("------------------------------------------\n\r"); |
garphil | 8:fb28b9ec6d6e | 86 | DEBUG("----- LaLaBox (CreaLab) version 1.0 ----\n\r"); |
garphil | 1:ab4c9a0a5374 | 87 | DEBUG("------------------------------------------\n\r"); |
garphil | 1:ab4c9a0a5374 | 88 | help(); |
garphil | 1:ab4c9a0a5374 | 89 | DEBUG("------------------------------------------\n\r"); |
garphil | 10:983ed805d74f | 90 | ledBand.SetColor(BLACK); |
garphil | 10:983ed805d74f | 91 | DEBUG("SystemCoreClock = %d Hz = %f\n\r", SystemCoreClock); |
garphil | 10:983ed805d74f | 92 | |
garphil | 10:983ed805d74f | 93 | Timer opt; |
garphil | 10:983ed805d74f | 94 | int last = 0; |
garphil | 10:983ed805d74f | 95 | |
garphil | 10:983ed805d74f | 96 | int sum=0; |
garphil | 10:983ed805d74f | 97 | for(int kk=0; kk<20;kk++) { |
garphil | 10:983ed805d74f | 98 | opt.reset(); |
garphil | 10:983ed805d74f | 99 | opt.start(); |
garphil | 10:983ed805d74f | 100 | myled = 1; |
garphil | 10:983ed805d74f | 101 | for (int j=0; j < 10000; j++) { |
garphil | 10:983ed805d74f | 102 | __nop(); |
garphil | 10:983ed805d74f | 103 | } |
garphil | 10:983ed805d74f | 104 | opt.stop(); |
garphil | 10:983ed805d74f | 105 | sum=sum+opt.read_us(); |
garphil | 10:983ed805d74f | 106 | printf("read %d\n\r",opt.read_us()); |
garphil | 10:983ed805d74f | 107 | } |
garphil | 10:983ed805d74f | 108 | printf("sum=%d ==> average = %f\n\r",sum, sum/20.0); |
garphil | 8:fb28b9ec6d6e | 109 | CATCH_BUTTON(myButton,pressed); |
garphil | 6:2f4867594333 | 110 | |
garphil | 10:983ed805d74f | 111 | // PLAY_NOTE(la, buzzer); |
garphil | 9:6b8118063819 | 112 | |
garphil | 6:2f4867594333 | 113 | wait(2); // Some delay |
garphil | 1:ab4c9a0a5374 | 114 | myled = 0; // Real stuff starts here |
garphil | 10:983ed805d74f | 115 | |
garphil | 10:983ed805d74f | 116 | // motorBox.SetDirection(CLOCKWISE); |
garphil | 5:f62e799558c3 | 117 | while(1) { |
garphil | 1:ab4c9a0a5374 | 118 | char command; // Command to execute |
garphil | 5:f62e799558c3 | 119 | DEBUG(">> "); |
garphil | 1:ab4c9a0a5374 | 120 | command = pc_uart.getc(); |
garphil | 1:ab4c9a0a5374 | 121 | DEBUG("%c", command); |
garphil | 5:f62e799558c3 | 122 | |
garphil | 1:ab4c9a0a5374 | 123 | switch (command) { |
garphil | 1:ab4c9a0a5374 | 124 | case 'h': help(); break; |
garphil | 10:983ed805d74f | 125 | // case 'z': motorBox.SetZero(); break; |
garphil | 10:983ed805d74f | 126 | // case 'k': motorBox.TestMotor(); break; |
garphil | 4:d452a7e3461a | 127 | case 'c': |
garphil | 4:d452a7e3461a | 128 | DEBUG(" -- Direction CLOCKWISE --\n\r"); |
garphil | 10:983ed805d74f | 129 | // motorBox.SetDirection(CLOCKWISE); |
garphil | 1:ab4c9a0a5374 | 130 | break; |
garphil | 4:d452a7e3461a | 131 | case 'a': |
garphil | 4:d452a7e3461a | 132 | DEBUG(" -- Direction COUNTER CLOCKWISE --\n\r"); |
garphil | 10:983ed805d74f | 133 | // motorBox.SetDirection(COUNTERCLOCKWISE); |
garphil | 4:d452a7e3461a | 134 | break; |
garphil | 1:ab4c9a0a5374 | 135 | default : DEBUG("invalid command; use: 'h' for help()"); |
garphil | 1:ab4c9a0a5374 | 136 | } |
garphil | 0:30cb0f6dad87 | 137 | } |
garphil | 1:ab4c9a0a5374 | 138 | } |