Johan Kritzinger
/
FRDMKL25Z-ShiftBrite
A few classes to interface one or more ShiftBrite module to the FRDM KL25Z.
main.cpp@7:a0f62fc80de0, 2014-08-22 (annotated)
- Committer:
- JoKer
- Date:
- Fri Aug 22 01:18:49 2014 +0000
- Revision:
- 7:a0f62fc80de0
- Parent:
- 6:75801b7a36a3
Cleaned up main. Divided into 3 sections to demonstrate various aspects of the libraries and their usage.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoKer | 0:f76850de7b57 | 1 | #include "mbed.h" |
JoKer | 0:f76850de7b57 | 2 | #include "sbDriver.h" |
JoKer | 2:3935d2ed40cd | 3 | #include "movie.h" |
JoKer | 5:aa0424f31fa1 | 4 | # define M_PI 3.14159265358979323846 |
JoKer | 6:75801b7a36a3 | 5 | #define FACTOR 1.0 |
JoKer | 6:75801b7a36a3 | 6 | #define USI unsigned short int |
JoKer | 0:f76850de7b57 | 7 | |
JoKer | 7:a0f62fc80de0 | 8 | // !!!!!!!!!!!!!!!!!! R E A D T H I S S E C T I O N F I R S T !!!!!!!!!!!!! |
JoKer | 7:a0f62fc80de0 | 9 | // !!!!! INSTUCTIONS INSTRUCTIONS INSTRUCTIONS INSTRUCTIONS INSTRUCTIONS !!!!! |
JoKer | 7:a0f62fc80de0 | 10 | //A number of main loop examples follows. Select one by ... |
JoKer | 7:a0f62fc80de0 | 11 | //uncommenting ONLY ONE of the following. Then you can pick appart the relevant section. |
JoKer | 7:a0f62fc80de0 | 12 | #define BALLBOUNCE |
JoKer | 7:a0f62fc80de0 | 13 | //#define SINEWAVE |
JoKer | 7:a0f62fc80de0 | 14 | //#define VARIOUS |
JoKer | 7:a0f62fc80de0 | 15 | // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
JoKer | 7:a0f62fc80de0 | 16 | |
JoKer | 7:a0f62fc80de0 | 17 | |
JoKer | 7:a0f62fc80de0 | 18 | /** VARIOUS EXAMPLES OF HOW TO USE the shiftBriteDisplay class and the movie class follows below. |
JoKer | 4:d2f8ddb423e2 | 19 | Please note, this is all 'beerware' code but I would appreciate a mention in your code headers |
JoKer | 4:d2f8ddb423e2 | 20 | if you use some/all of my code/classes. My name is Johan Kritzinger and I go by 'JoKer' on MBED. |
JoKer | 4:d2f8ddb423e2 | 21 | My C++ is not that hot (I'm an old PLM-51 and C programmer) but the only way to learn is to do. |
JoKer | 4:d2f8ddb423e2 | 22 | Any questions, please feel free to ask. |
JoKer | 0:f76850de7b57 | 23 | */ |
JoKer | 0:f76850de7b57 | 24 | |
JoKer | 4:d2f8ddb423e2 | 25 | |
JoKer | 7:a0f62fc80de0 | 26 | #ifdef VARIOUS |
JoKer | 2:3935d2ed40cd | 27 | //6 leds example. Format it suitably for easy reading |
JoKer | 2:3935d2ed40cd | 28 | unsigned short int aMovie[] = { |
JoKer | 2:3935d2ed40cd | 29 | /* LED1 LED2 LED3 LED4 LED5 LED6 */ |
JoKer | 5:aa0424f31fa1 | 30 | 10,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 0*/ |
JoKer | 5:aa0424f31fa1 | 31 | 0,0,0, 100,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 1*/ |
JoKer | 5:aa0424f31fa1 | 32 | 0,0,0, 0,0,0, 500,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 2*/ |
JoKer | 5:aa0424f31fa1 | 33 | 0,0,0, 0,0,0, 0,0,0, 750,0,0, 0,0,0, 0,0,0, /*Frame 3*/ |
JoKer | 5:aa0424f31fa1 | 34 | 0,0,0, 0,0,0, 0,0,0, 0,0,0, 900,0,0, 0,0,0, /*Frame 4*/ |
JoKer | 2:3935d2ed40cd | 35 | 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 1023,0,0, /*Frame 5*/ |
JoKer | 5:aa0424f31fa1 | 36 | 0,0,0, 0,0,0, 0,0,0, 0,0,0, 900,0,0, 0,0,0, /*Frame 6*/ |
JoKer | 5:aa0424f31fa1 | 37 | 0,0,0, 0,0,0, 0,0,0, 750,0,0, 0,0,0, 0,0,0, /*Frame 7*/ |
JoKer | 5:aa0424f31fa1 | 38 | 0,0,0, 0,0,0, 500,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 8*/ |
JoKer | 5:aa0424f31fa1 | 39 | 0,0,0, 100,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0 /*Frame 9*/ |
JoKer | 2:3935d2ed40cd | 40 | /*A simple 'cylon' scanner 'movie'*/ |
JoKer | 2:3935d2ed40cd | 41 | }; |
JoKer | 7:a0f62fc80de0 | 42 | #endif |
JoKer | 5:aa0424f31fa1 | 43 | |
JoKer | 4:d2f8ddb423e2 | 44 | Serial PC(PTA2, PTA1);//So I can use Serial as output to terminal and input |
JoKer | 0:f76850de7b57 | 45 | |
JoKer | 4:d2f8ddb423e2 | 46 | //================== HARDWARE CONNECTIONS ========================================= |
JoKer | 4:d2f8ddb423e2 | 47 | //NB!!! I have only tested this on the FRDM KL25Z and h/w connection as detailed below |
JoKer | 0:f76850de7b57 | 48 | //Instanced of DigitalOut for control SB signals |
JoKer | 0:f76850de7b57 | 49 | DigitalOut latch(PTC16);//010=latch |
JoKer | 0:f76850de7b57 | 50 | DigitalOut enable(PTA13);//0= enabled |
JoKer | 0:f76850de7b57 | 51 | DigitalOut reset(PTC12); |
JoKer | 0:f76850de7b57 | 52 | //Instance of the SPI contoller for SB data |
JoKer | 0:f76850de7b57 | 53 | SPI spi(PTD2,NC,PTD1);//PDT2 = MOSI=DATA. PDT1=CLK |
JoKer | 4:d2f8ddb423e2 | 54 | //=================== END OF HARDWARE CONNECTIONS ================================= |
JoKer | 0:f76850de7b57 | 55 | |
JoKer | 0:f76850de7b57 | 56 | int main() { |
JoKer | 5:aa0424f31fa1 | 57 | |
JoKer | 0:f76850de7b57 | 58 | |
JoKer | 0:f76850de7b57 | 59 | //Instanciate a ticker object to handle framerate updates for the SB display |
JoKer | 0:f76850de7b57 | 60 | Ticker t; |
JoKer | 0:f76850de7b57 | 61 | |
JoKer | 0:f76850de7b57 | 62 | //Instanciate a string of 5 sb modules and tell the driver object where the control/data pins are |
JoKer | 4:d2f8ddb423e2 | 63 | shiftBriteDisplay sbDisplay(latch, enable, reset, spi,6); |
JoKer | 0:f76850de7b57 | 64 | |
JoKer | 7:a0f62fc80de0 | 65 | //=================== EXAMPLE ONE MAIN LOOP ========================================== |
JoKer | 7:a0f62fc80de0 | 66 | #ifdef VARIOUS |
JoKer | 7:a0f62fc80de0 | 67 | // MAIN PROGRAM ONE to display various features including movie class |
JoKer | 5:aa0424f31fa1 | 68 | |
JoKer | 0:f76850de7b57 | 69 | //Example calls to method f() of shiftBriteDisplay class |
JoKer | 0:f76850de7b57 | 70 | //in this case, 6 of these statements wold be required to build one frame |
JoKer | 2:3935d2ed40cd | 71 | //HOW TO BUILD A FRAME IF YOU ARE NOT USING THE MOVIE CLASS |
JoKer | 2:3935d2ed40cd | 72 | sbDisplay.setLed(0,0XFF0000);//Red |
JoKer | 0:f76850de7b57 | 73 | sbDisplay.setLed(1,0X00FF00);//Green |
JoKer | 0:f76850de7b57 | 74 | sbDisplay.setLed(2,0X0000FF);//Blue |
JoKer | 0:f76850de7b57 | 75 | sbDisplay.setLed(3,0XFFFF00);//Yellow |
JoKer | 0:f76850de7b57 | 76 | sbDisplay.setLed(4,0X00FFFF); //Cyan? |
JoKer | 2:3935d2ed40cd | 77 | sbDisplay.setLed(5,0XFF00FF); // Purple |
JoKer | 0:f76850de7b57 | 78 | |
JoKer | 2:3935d2ed40cd | 79 | //HOW TO ADJUST THE CURRENT CONTROLLERS USED FOR DOT CORRECTION |
JoKer | 0:f76850de7b57 | 80 | sbDisplay.setCurrentCorr(0,0,0);//suggested default value cor current control regs. Values g&b=100, r=120 |
JoKer | 0:f76850de7b57 | 81 | sbDisplay.displayFrame();//force an update |
JoKer | 0:f76850de7b57 | 82 | wait(2); |
JoKer | 0:f76850de7b57 | 83 | sbDisplay.setCurrentCorr(127,127,127);//This should be MAX |
JoKer | 0:f76850de7b57 | 84 | wait(2); |
JoKer | 3:9376bf1f1bbd | 85 | //sbDisplay.setCurrentCorr(0x78,0x64,0x64);//sb suggested default |
JoKer | 3:9376bf1f1bbd | 86 | sbDisplay.setCurrentCorr(0,0,0);//Dim |
JoKer | 3:9376bf1f1bbd | 87 | |
JoKer | 3:9376bf1f1bbd | 88 | wait(2); |
JoKer | 3:9376bf1f1bbd | 89 | PC.printf("INVERTED\r\n"); |
JoKer | 3:9376bf1f1bbd | 90 | sbDisplay.invert(); |
JoKer | 3:9376bf1f1bbd | 91 | sbDisplay.displayFrame(); |
JoKer | 3:9376bf1f1bbd | 92 | wait(2); |
JoKer | 3:9376bf1f1bbd | 93 | PC.printf("FLIP\r\n"); |
JoKer | 3:9376bf1f1bbd | 94 | sbDisplay.flip(); |
JoKer | 3:9376bf1f1bbd | 95 | sbDisplay.displayFrame(); |
JoKer | 0:f76850de7b57 | 96 | wait(2); |
JoKer | 3:9376bf1f1bbd | 97 | sbDisplay.setLed(0,0XFF0000);//Red |
JoKer | 3:9376bf1f1bbd | 98 | sbDisplay.setLed(1,0X000500);//Green |
JoKer | 3:9376bf1f1bbd | 99 | sbDisplay.setLed(2,0X000005);//Blue |
JoKer | 3:9376bf1f1bbd | 100 | sbDisplay.setLed(3,0X050500);//Yellow |
JoKer | 3:9376bf1f1bbd | 101 | sbDisplay.setLed(4,0X000505); //Cyan? |
JoKer | 3:9376bf1f1bbd | 102 | sbDisplay.setLed(5,0X050005); // Purple |
JoKer | 3:9376bf1f1bbd | 103 | sbDisplay.displayFrame(); //get it on the LEDS |
JoKer | 3:9376bf1f1bbd | 104 | |
JoKer | 3:9376bf1f1bbd | 105 | PC.printf("RotateL"); |
JoKer | 3:9376bf1f1bbd | 106 | unsigned int loop; |
JoKer | 3:9376bf1f1bbd | 107 | for (loop=0; loop != 100; loop++){ |
JoKer | 3:9376bf1f1bbd | 108 | sbDisplay.rotateLeft(); |
JoKer | 3:9376bf1f1bbd | 109 | sbDisplay.displayFrame(); |
JoKer | 3:9376bf1f1bbd | 110 | wait(0.1); |
JoKer | 3:9376bf1f1bbd | 111 | } |
JoKer | 3:9376bf1f1bbd | 112 | PC.printf("RotateR"); |
JoKer | 3:9376bf1f1bbd | 113 | //unsigned int loop; |
JoKer | 3:9376bf1f1bbd | 114 | for (loop=0; loop != 100; loop++){ |
JoKer | 3:9376bf1f1bbd | 115 | sbDisplay.rotateRight(); |
JoKer | 3:9376bf1f1bbd | 116 | sbDisplay.displayFrame(); |
JoKer | 3:9376bf1f1bbd | 117 | wait(0.1); |
JoKer | 3:9376bf1f1bbd | 118 | } |
JoKer | 3:9376bf1f1bbd | 119 | for(loop=0; loop !=6; loop++){ |
JoKer | 3:9376bf1f1bbd | 120 | sbDisplay.shiftRight(); |
JoKer | 3:9376bf1f1bbd | 121 | sbDisplay.displayFrame(); |
JoKer | 3:9376bf1f1bbd | 122 | wait(0.2); |
JoKer | 3:9376bf1f1bbd | 123 | } |
JoKer | 2:3935d2ed40cd | 124 | |
JoKer | 2:3935d2ed40cd | 125 | |
JoKer | 2:3935d2ed40cd | 126 | sbDisplay.setLed(0,0X0F0000);//Red |
JoKer | 2:3935d2ed40cd | 127 | sbDisplay.setLed(1,0X000F00);//Green |
JoKer | 2:3935d2ed40cd | 128 | sbDisplay.setLed(2,0X00000F);//Blue |
JoKer | 2:3935d2ed40cd | 129 | sbDisplay.setLed(3,0X0F0F00);//Yellow |
JoKer | 2:3935d2ed40cd | 130 | sbDisplay.setLed(4,0X000F0F); //Cyan? |
JoKer | 2:3935d2ed40cd | 131 | sbDisplay.setLed(5,0X0F000F); // Purple |
JoKer | 2:3935d2ed40cd | 132 | sbDisplay.displayFrame(); //get it on the LEDS |
JoKer | 2:3935d2ed40cd | 133 | wait(0.5); |
JoKer | 2:3935d2ed40cd | 134 | sbDisplay.setCurrentCorr(0,0,0);//Dim |
JoKer | 2:3935d2ed40cd | 135 | |
JoKer | 2:3935d2ed40cd | 136 | sbDisplay.displayFrame(); //get it on the LEDS |
JoKer | 4:d2f8ddb423e2 | 137 | |
JoKer | 4:d2f8ddb423e2 | 138 | //Alternative to calling displayFrame() yourself, setup the framerate and update the display using Ticker e.g. |
JoKer | 2:3935d2ed40cd | 139 | //t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS) |
JoKer | 4:d2f8ddb423e2 | 140 | //or t.attach(&sbDisplay,&shiftBriteDisplay::displayFrame,0.5);// or only every 0.5s for testing |
JoKer | 2:3935d2ed40cd | 141 | |
JoKer | 2:3935d2ed40cd | 142 | |
JoKer | 5:aa0424f31fa1 | 143 | // |
JoKer | 5:aa0424f31fa1 | 144 | //EXPANDING THE FUNCTIONALITY OF shiftBriteDisplay class; USING THE MOVIE CLASS |
JoKer | 5:aa0424f31fa1 | 145 | //I know, i know, 'MOVIE' is a HUGE stretch but it sounds good right?!?! :) |
JoKer | 5:aa0424f31fa1 | 146 | // |
JoKer | 4:d2f8ddb423e2 | 147 | //Note, it uses the previously declared sbDisplay object. It is passed as a reference. |
JoKer | 4:d2f8ddb423e2 | 148 | movie myMovie(aMovie,sbDisplay,sizeof(aMovie)); |
JoKer | 4:d2f8ddb423e2 | 149 | myMovie.setRepeat(1); |
JoKer | 5:aa0424f31fa1 | 150 | |
JoKer | 4:d2f8ddb423e2 | 151 | t.attach(&myMovie,&movie::play,0.05);//Beware, if you go too fast here the FRDM will crash |
JoKer | 6:75801b7a36a3 | 152 | while(1){;} //nothing in main loop as Ticker calls the relevan member function |
JoKer | 7:a0f62fc80de0 | 153 | #endif //VARIOUS |
JoKer | 4:d2f8ddb423e2 | 154 | |
JoKer | 7:a0f62fc80de0 | 155 | //=================== EXAMPLE TWO MAIN LOOP ========================================== |
JoKer | 6:75801b7a36a3 | 156 | |
JoKer | 7:a0f62fc80de0 | 157 | #ifdef SINEWAVE |
JoKer | 6:75801b7a36a3 | 158 | //MAIN PROGRAM TWO - to show some special effects WITHOUT using the movie class |
JoKer | 6:75801b7a36a3 | 159 | //Play around by adjusting phases, frequencies etc. Each will give different results |
JoKer | 6:75801b7a36a3 | 160 | //Below is an example using 3 sin waves, one for each colour, but 120 deg out of phase |
JoKer | 6:75801b7a36a3 | 161 | //and slowly scanning frequencies from 0.5 to 5 and back again. Each freq is active for 5 seconds. |
JoKer | 6:75801b7a36a3 | 162 | |
JoKer | 5:aa0424f31fa1 | 163 | unsigned int j; |
JoKer | 5:aa0424f31fa1 | 164 | for (j=0; j != sbDisplay.getModuleCount(); j++){ |
JoKer | 5:aa0424f31fa1 | 165 | sbDisplay.setLed(j,0,0,0);//set all led to black |
JoKer | 5:aa0424f31fa1 | 166 | } |
JoKer | 5:aa0424f31fa1 | 167 | sbDisplay.displayFrame();//get it on the leds |
JoKer | 7:a0f62fc80de0 | 168 | sbDisplay.setCurrentCorr(0,0,0);//30% power |
JoKer | 6:75801b7a36a3 | 169 | |
JoKer | 6:75801b7a36a3 | 170 | double tim,max_t; // 'real' time and time limit for a simulation |
JoKer | 6:75801b7a36a3 | 171 | double w1,w2,w3; // 2*pi*f |
JoKer | 6:75801b7a36a3 | 172 | double phase1,phase2,phase3;//Phase offset |
JoKer | 6:75801b7a36a3 | 173 | double f1,f2,f3;//Frequencies |
JoKer | 6:75801b7a36a3 | 174 | double factor = 250;//250 steps or values over max_t time |
JoKer | 6:75801b7a36a3 | 175 | |
JoKer | 6:75801b7a36a3 | 176 | unsigned char f_dir = 1; //used for up and down functionality |
JoKer | 6:75801b7a36a3 | 177 | |
JoKer | 6:75801b7a36a3 | 178 | f1 = f2 = f3 = 1.0; //frequency in herz - set all to the same |
JoKer | 6:75801b7a36a3 | 179 | //but make a 120 degree pahse shift between the 3 waves |
JoKer | 6:75801b7a36a3 | 180 | phase1 = 0.0; |
JoKer | 6:75801b7a36a3 | 181 | phase2 = 120.0*(M_PI/180.0); |
JoKer | 6:75801b7a36a3 | 182 | phase3 = 240.0*(M_PI/180.0); |
JoKer | 6:75801b7a36a3 | 183 | |
JoKer | 6:75801b7a36a3 | 184 | max_t=5.0; //i.e. time to iterate over - i.e. 5 second |
JoKer | 6:75801b7a36a3 | 185 | |
JoKer | 6:75801b7a36a3 | 186 | while(1){ // generate special effects without using a movie and discretely calc values |
JoKer | 6:75801b7a36a3 | 187 | for(tim=0; tim <= max_t; tim+=max_t/factor) { |
JoKer | 5:aa0424f31fa1 | 188 | //sbDisplay.shiftRight((unsigned short int)(5000.0*(1+sin(p+0))),(unsigned short int)(5000.0*(1+sin(p+(2.0*M_PI)/3.0))),(unsigned short int)(5000.0*(1+sin(p+2.0*((2.0*M_PI)/3.0))))); |
JoKer | 6:75801b7a36a3 | 189 | sbDisplay.shiftRight((USI)511.0*(1.0+sin(w1*tim+phase1)), |
JoKer | 6:75801b7a36a3 | 190 | (USI)511.0*(1.0+sin(w2*tim+phase2)), |
JoKer | 6:75801b7a36a3 | 191 | (USI)511.0*(1.0+sin(w3*tim+phase3)) |
JoKer | 6:75801b7a36a3 | 192 | ); |
JoKer | 5:aa0424f31fa1 | 193 | sbDisplay.displayFrame(); |
JoKer | 6:75801b7a36a3 | 194 | wait( (max_t/factor)); |
JoKer | 5:aa0424f31fa1 | 195 | } |
JoKer | 6:75801b7a36a3 | 196 | w1 = 2.0*M_PI*f1; |
JoKer | 6:75801b7a36a3 | 197 | w2 = 2.0*M_PI*f2; |
JoKer | 6:75801b7a36a3 | 198 | w3 = 2.0*M_PI*f3; |
JoKer | 6:75801b7a36a3 | 199 | if(f_dir){ |
JoKer | 6:75801b7a36a3 | 200 | f1 = f2 = f3 = f3+0.5; |
JoKer | 6:75801b7a36a3 | 201 | if (f1>=5.0)f_dir = 0; |
JoKer | 6:75801b7a36a3 | 202 | } else { |
JoKer | 6:75801b7a36a3 | 203 | f1 = f2 = f3 = f3-0.5; |
JoKer | 6:75801b7a36a3 | 204 | if (f1<=0.5) f_dir = 1; |
JoKer | 6:75801b7a36a3 | 205 | } |
JoKer | 6:75801b7a36a3 | 206 | |
JoKer | 7:a0f62fc80de0 | 207 | } //end of while 1 |
JoKer | 7:a0f62fc80de0 | 208 | #endif //SINWAVE |
JoKer | 7:a0f62fc80de0 | 209 | |
JoKer | 7:a0f62fc80de0 | 210 | |
JoKer | 7:a0f62fc80de0 | 211 | //=================== EXAMPLE THREE MAIN LOOP ========================================== |
JoKer | 7:a0f62fc80de0 | 212 | |
JoKer | 7:a0f62fc80de0 | 213 | #ifdef BALLBOUNCE |
JoKer | 7:a0f62fc80de0 | 214 | //HOW ABOUT A BOUNCING BALL? |
JoKer | 7:a0f62fc80de0 | 215 | //fisrt, clear the display |
JoKer | 7:a0f62fc80de0 | 216 | unsigned int i; |
JoKer | 7:a0f62fc80de0 | 217 | for(i=0 ; i != sbDisplay.getModuleCount(); i++){ |
JoKer | 7:a0f62fc80de0 | 218 | sbDisplay.setLed(i,0,0,0); |
JoKer | 0:f76850de7b57 | 219 | } |
JoKer | 7:a0f62fc80de0 | 220 | sbDisplay.displayFrame(); |
JoKer | 7:a0f62fc80de0 | 221 | |
JoKer | 7:a0f62fc80de0 | 222 | double tim;//time |
JoKer | 7:a0f62fc80de0 | 223 | double tf;//time it takes to fall |
JoKer | 7:a0f62fc80de0 | 224 | double h=0;//current height - calculated |
JoKer | 7:a0f62fc80de0 | 225 | double h0=sbDisplay.getModuleCount();//starting height. can be anything but best to set to the number of leds in the chain |
JoKer | 7:a0f62fc80de0 | 226 | double vi=0;//initial velocity |
JoKer | 7:a0f62fc80de0 | 227 | double vf; //final velocity |
JoKer | 7:a0f62fc80de0 | 228 | double g=9.81;//gravity |
JoKer | 7:a0f62fc80de0 | 229 | double step;//size of time step in h calculation iteration |
JoKer | 7:a0f62fc80de0 | 230 | double cor = 0.85; //Cooeficient of restitution - a measure of how much velocity is lost when a ball bounces |
JoKer | 7:a0f62fc80de0 | 231 | //cor is dependent on the material off the ball and the surface it bounces on. |
JoKer | 7:a0f62fc80de0 | 232 | |
JoKer | 7:a0f62fc80de0 | 233 | while(h0 >= 0.5){ |
JoKer | 7:a0f62fc80de0 | 234 | //First, find out how long before it falls h=0 |
JoKer | 7:a0f62fc80de0 | 235 | tf=sqrt((h0*2)/g);//how long to fall from h0 to 0 |
JoKer | 7:a0f62fc80de0 | 236 | step = tf/20;//dynamically calculated step size |
JoKer | 7:a0f62fc80de0 | 237 | //---FALL ---- |
JoKer | 7:a0f62fc80de0 | 238 | for (tim = 0; tim <= tf; tim+=step) { |
JoKer | 7:a0f62fc80de0 | 239 | h=h0-(vi*tim + 0.5*g*(tim * tim)); |
JoKer | 7:a0f62fc80de0 | 240 | //PC.printf("D%f\r\n",h); |
JoKer | 7:a0f62fc80de0 | 241 | sbDisplay.setLed((unsigned int)h,500,0,0);//set |
JoKer | 7:a0f62fc80de0 | 242 | sbDisplay.displayFrame(); |
JoKer | 7:a0f62fc80de0 | 243 | //wait(step);//made it too slow - enough time lost doing all the calcs |
JoKer | 7:a0f62fc80de0 | 244 | sbDisplay.setLed((unsigned int)h,0,0,0);//clear |
JoKer | 7:a0f62fc80de0 | 245 | sbDisplay.displayFrame(); |
JoKer | 7:a0f62fc80de0 | 246 | } |
JoKer | 7:a0f62fc80de0 | 247 | vf=vi+g*tf;//Find final velocity (i.e., as it hits the ground |
JoKer | 7:a0f62fc80de0 | 248 | h0=0; |
JoKer | 7:a0f62fc80de0 | 249 | vi=vf*cor;//use vf to calc new vi using cooef of restitution |
JoKer | 7:a0f62fc80de0 | 250 | //higest it will go |
JoKer | 7:a0f62fc80de0 | 251 | tf=vi/g; |
JoKer | 7:a0f62fc80de0 | 252 | step = tf/20;//alter the step size since were spending less time in this iteration |
JoKer | 7:a0f62fc80de0 | 253 | |
JoKer | 7:a0f62fc80de0 | 254 | //--- BOUNCE ------ |
JoKer | 7:a0f62fc80de0 | 255 | for (tim=0 ; tim <=tf; tim+=step) { |
JoKer | 7:a0f62fc80de0 | 256 | h=(vi*tim) - 0.5*g*(tim * tim) + h0; |
JoKer | 7:a0f62fc80de0 | 257 | //PC.printf("U%f\r\n",h); |
JoKer | 7:a0f62fc80de0 | 258 | sbDisplay.setLed((unsigned int)h,500,0,0);//set |
JoKer | 7:a0f62fc80de0 | 259 | sbDisplay.displayFrame(); |
JoKer | 7:a0f62fc80de0 | 260 | //wait(step); |
JoKer | 7:a0f62fc80de0 | 261 | sbDisplay.setLed((unsigned int)h,0,0,0);//clear |
JoKer | 7:a0f62fc80de0 | 262 | sbDisplay.displayFrame(); |
JoKer | 7:a0f62fc80de0 | 263 | } |
JoKer | 7:a0f62fc80de0 | 264 | h0=h; |
JoKer | 7:a0f62fc80de0 | 265 | vi=0; |
JoKer | 7:a0f62fc80de0 | 266 | } |
JoKer | 7:a0f62fc80de0 | 267 | while(1){}//nowhere else to go so get stuck here. User will need to reset to see another drop |
JoKer | 7:a0f62fc80de0 | 268 | #endif //BALLBOUNCE |
JoKer | 0:f76850de7b57 | 269 | } |
JoKer | 7:a0f62fc80de0 | 270 | |
JoKer | 4:d2f8ddb423e2 | 271 | |
JoKer | 4:d2f8ddb423e2 | 272 | /*TODO - |
JoKer | 4:d2f8ddb423e2 | 273 | Inheritance is one of the great features of object oriented languages like C++. The stream methods are not included in serial to allow you to use them in other classes as well. They are for example also included in the TextLCD class and in my enhanced TextLCD class. All you need to do to make use of the powerful printf features is implement a putc() method in your own new class. |
JoKer | 4:d2f8ddb423e2 | 274 | from https://mbed.org/questions/1029/pcprintf-a-method/ |
JoKer | 4:d2f8ddb423e2 | 275 | |
JoKer | 4:d2f8ddb423e2 | 276 | */ |