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
00001 #include "mbed.h" 00002 #include "sbDriver.h" 00003 #include "movie.h" 00004 # define M_PI 3.14159265358979323846 00005 #define FACTOR 1.0 00006 #define USI unsigned short int 00007 00008 // !!!!!!!!!!!!!!!!!! R E A D T H I S S E C T I O N F I R S T !!!!!!!!!!!!! 00009 // !!!!! INSTUCTIONS INSTRUCTIONS INSTRUCTIONS INSTRUCTIONS INSTRUCTIONS !!!!! 00010 //A number of main loop examples follows. Select one by ... 00011 //uncommenting ONLY ONE of the following. Then you can pick appart the relevant section. 00012 #define BALLBOUNCE 00013 //#define SINEWAVE 00014 //#define VARIOUS 00015 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 00016 00017 00018 /** VARIOUS EXAMPLES OF HOW TO USE the shiftBriteDisplay class and the movie class follows below. 00019 Please note, this is all 'beerware' code but I would appreciate a mention in your code headers 00020 if you use some/all of my code/classes. My name is Johan Kritzinger and I go by 'JoKer' on MBED. 00021 My C++ is not that hot (I'm an old PLM-51 and C programmer) but the only way to learn is to do. 00022 Any questions, please feel free to ask. 00023 */ 00024 00025 00026 #ifdef VARIOUS 00027 //6 leds example. Format it suitably for easy reading 00028 unsigned short int aMovie[] = { 00029 /* LED1 LED2 LED3 LED4 LED5 LED6 */ 00030 10,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 0*/ 00031 0,0,0, 100,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 1*/ 00032 0,0,0, 0,0,0, 500,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 2*/ 00033 0,0,0, 0,0,0, 0,0,0, 750,0,0, 0,0,0, 0,0,0, /*Frame 3*/ 00034 0,0,0, 0,0,0, 0,0,0, 0,0,0, 900,0,0, 0,0,0, /*Frame 4*/ 00035 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 1023,0,0, /*Frame 5*/ 00036 0,0,0, 0,0,0, 0,0,0, 0,0,0, 900,0,0, 0,0,0, /*Frame 6*/ 00037 0,0,0, 0,0,0, 0,0,0, 750,0,0, 0,0,0, 0,0,0, /*Frame 7*/ 00038 0,0,0, 0,0,0, 500,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 8*/ 00039 0,0,0, 100,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0 /*Frame 9*/ 00040 /*A simple 'cylon' scanner 'movie'*/ 00041 }; 00042 #endif 00043 00044 Serial PC(PTA2, PTA1);//So I can use Serial as output to terminal and input 00045 00046 //================== HARDWARE CONNECTIONS ========================================= 00047 //NB!!! I have only tested this on the FRDM KL25Z and h/w connection as detailed below 00048 //Instanced of DigitalOut for control SB signals 00049 DigitalOut latch(PTC16);//010=latch 00050 DigitalOut enable(PTA13);//0= enabled 00051 DigitalOut reset(PTC12); 00052 //Instance of the SPI contoller for SB data 00053 SPI spi(PTD2,NC,PTD1);//PDT2 = MOSI=DATA. PDT1=CLK 00054 //=================== END OF HARDWARE CONNECTIONS ================================= 00055 00056 int main() { 00057 00058 00059 //Instanciate a ticker object to handle framerate updates for the SB display 00060 Ticker t; 00061 00062 //Instanciate a string of 5 sb modules and tell the driver object where the control/data pins are 00063 shiftBriteDisplay sbDisplay(latch, enable, reset, spi,6); 00064 00065 //=================== EXAMPLE ONE MAIN LOOP ========================================== 00066 #ifdef VARIOUS 00067 // MAIN PROGRAM ONE to display various features including movie class 00068 00069 //Example calls to method f() of shiftBriteDisplay class 00070 //in this case, 6 of these statements wold be required to build one frame 00071 //HOW TO BUILD A FRAME IF YOU ARE NOT USING THE MOVIE CLASS 00072 sbDisplay.setLed(0,0XFF0000);//Red 00073 sbDisplay.setLed(1,0X00FF00);//Green 00074 sbDisplay.setLed(2,0X0000FF);//Blue 00075 sbDisplay.setLed(3,0XFFFF00);//Yellow 00076 sbDisplay.setLed(4,0X00FFFF); //Cyan? 00077 sbDisplay.setLed(5,0XFF00FF); // Purple 00078 00079 //HOW TO ADJUST THE CURRENT CONTROLLERS USED FOR DOT CORRECTION 00080 sbDisplay.setCurrentCorr(0,0,0);//suggested default value cor current control regs. Values g&b=100, r=120 00081 sbDisplay.displayFrame();//force an update 00082 wait(2); 00083 sbDisplay.setCurrentCorr(127,127,127);//This should be MAX 00084 wait(2); 00085 //sbDisplay.setCurrentCorr(0x78,0x64,0x64);//sb suggested default 00086 sbDisplay.setCurrentCorr(0,0,0);//Dim 00087 00088 wait(2); 00089 PC.printf("INVERTED\r\n"); 00090 sbDisplay.invert(); 00091 sbDisplay.displayFrame(); 00092 wait(2); 00093 PC.printf("FLIP\r\n"); 00094 sbDisplay.flip(); 00095 sbDisplay.displayFrame(); 00096 wait(2); 00097 sbDisplay.setLed(0,0XFF0000);//Red 00098 sbDisplay.setLed(1,0X000500);//Green 00099 sbDisplay.setLed(2,0X000005);//Blue 00100 sbDisplay.setLed(3,0X050500);//Yellow 00101 sbDisplay.setLed(4,0X000505); //Cyan? 00102 sbDisplay.setLed(5,0X050005); // Purple 00103 sbDisplay.displayFrame(); //get it on the LEDS 00104 00105 PC.printf("RotateL"); 00106 unsigned int loop; 00107 for (loop=0; loop != 100; loop++){ 00108 sbDisplay.rotateLeft(); 00109 sbDisplay.displayFrame(); 00110 wait(0.1); 00111 } 00112 PC.printf("RotateR"); 00113 //unsigned int loop; 00114 for (loop=0; loop != 100; loop++){ 00115 sbDisplay.rotateRight(); 00116 sbDisplay.displayFrame(); 00117 wait(0.1); 00118 } 00119 for(loop=0; loop !=6; loop++){ 00120 sbDisplay.shiftRight(); 00121 sbDisplay.displayFrame(); 00122 wait(0.2); 00123 } 00124 00125 00126 sbDisplay.setLed(0,0X0F0000);//Red 00127 sbDisplay.setLed(1,0X000F00);//Green 00128 sbDisplay.setLed(2,0X00000F);//Blue 00129 sbDisplay.setLed(3,0X0F0F00);//Yellow 00130 sbDisplay.setLed(4,0X000F0F); //Cyan? 00131 sbDisplay.setLed(5,0X0F000F); // Purple 00132 sbDisplay.displayFrame(); //get it on the LEDS 00133 wait(0.5); 00134 sbDisplay.setCurrentCorr(0,0,0);//Dim 00135 00136 sbDisplay.displayFrame(); //get it on the LEDS 00137 00138 //Alternative to calling displayFrame() yourself, setup the framerate and update the display using Ticker e.g. 00139 //t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS) 00140 //or t.attach(&sbDisplay,&shiftBriteDisplay::displayFrame,0.5);// or only every 0.5s for testing 00141 00142 00143 // 00144 //EXPANDING THE FUNCTIONALITY OF shiftBriteDisplay class; USING THE MOVIE CLASS 00145 //I know, i know, 'MOVIE' is a HUGE stretch but it sounds good right?!?! :) 00146 // 00147 //Note, it uses the previously declared sbDisplay object. It is passed as a reference. 00148 movie myMovie(aMovie,sbDisplay,sizeof(aMovie)); 00149 myMovie.setRepeat(1); 00150 00151 t.attach(&myMovie,&movie::play,0.05);//Beware, if you go too fast here the FRDM will crash 00152 while(1){;} //nothing in main loop as Ticker calls the relevan member function 00153 #endif //VARIOUS 00154 00155 //=================== EXAMPLE TWO MAIN LOOP ========================================== 00156 00157 #ifdef SINEWAVE 00158 //MAIN PROGRAM TWO - to show some special effects WITHOUT using the movie class 00159 //Play around by adjusting phases, frequencies etc. Each will give different results 00160 //Below is an example using 3 sin waves, one for each colour, but 120 deg out of phase 00161 //and slowly scanning frequencies from 0.5 to 5 and back again. Each freq is active for 5 seconds. 00162 00163 unsigned int j; 00164 for (j=0; j != sbDisplay.getModuleCount(); j++){ 00165 sbDisplay.setLed(j,0,0,0);//set all led to black 00166 } 00167 sbDisplay.displayFrame();//get it on the leds 00168 sbDisplay.setCurrentCorr(0,0,0);//30% power 00169 00170 double tim,max_t; // 'real' time and time limit for a simulation 00171 double w1,w2,w3; // 2*pi*f 00172 double phase1,phase2,phase3;//Phase offset 00173 double f1,f2,f3;//Frequencies 00174 double factor = 250;//250 steps or values over max_t time 00175 00176 unsigned char f_dir = 1; //used for up and down functionality 00177 00178 f1 = f2 = f3 = 1.0; //frequency in herz - set all to the same 00179 //but make a 120 degree pahse shift between the 3 waves 00180 phase1 = 0.0; 00181 phase2 = 120.0*(M_PI/180.0); 00182 phase3 = 240.0*(M_PI/180.0); 00183 00184 max_t=5.0; //i.e. time to iterate over - i.e. 5 second 00185 00186 while(1){ // generate special effects without using a movie and discretely calc values 00187 for(tim=0; tim <= max_t; tim+=max_t/factor) { 00188 //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))))); 00189 sbDisplay.shiftRight((USI)511.0*(1.0+sin(w1*tim+phase1)), 00190 (USI)511.0*(1.0+sin(w2*tim+phase2)), 00191 (USI)511.0*(1.0+sin(w3*tim+phase3)) 00192 ); 00193 sbDisplay.displayFrame(); 00194 wait( (max_t/factor)); 00195 } 00196 w1 = 2.0*M_PI*f1; 00197 w2 = 2.0*M_PI*f2; 00198 w3 = 2.0*M_PI*f3; 00199 if(f_dir){ 00200 f1 = f2 = f3 = f3+0.5; 00201 if (f1>=5.0)f_dir = 0; 00202 } else { 00203 f1 = f2 = f3 = f3-0.5; 00204 if (f1<=0.5) f_dir = 1; 00205 } 00206 00207 } //end of while 1 00208 #endif //SINWAVE 00209 00210 00211 //=================== EXAMPLE THREE MAIN LOOP ========================================== 00212 00213 #ifdef BALLBOUNCE 00214 //HOW ABOUT A BOUNCING BALL? 00215 //fisrt, clear the display 00216 unsigned int i; 00217 for(i=0 ; i != sbDisplay.getModuleCount(); i++){ 00218 sbDisplay.setLed(i,0,0,0); 00219 } 00220 sbDisplay.displayFrame(); 00221 00222 double tim;//time 00223 double tf;//time it takes to fall 00224 double h=0;//current height - calculated 00225 double h0=sbDisplay.getModuleCount();//starting height. can be anything but best to set to the number of leds in the chain 00226 double vi=0;//initial velocity 00227 double vf; //final velocity 00228 double g=9.81;//gravity 00229 double step;//size of time step in h calculation iteration 00230 double cor = 0.85; //Cooeficient of restitution - a measure of how much velocity is lost when a ball bounces 00231 //cor is dependent on the material off the ball and the surface it bounces on. 00232 00233 while(h0 >= 0.5){ 00234 //First, find out how long before it falls h=0 00235 tf=sqrt((h0*2)/g);//how long to fall from h0 to 0 00236 step = tf/20;//dynamically calculated step size 00237 //---FALL ---- 00238 for (tim = 0; tim <= tf; tim+=step) { 00239 h=h0-(vi*tim + 0.5*g*(tim * tim)); 00240 //PC.printf("D%f\r\n",h); 00241 sbDisplay.setLed((unsigned int)h,500,0,0);//set 00242 sbDisplay.displayFrame(); 00243 //wait(step);//made it too slow - enough time lost doing all the calcs 00244 sbDisplay.setLed((unsigned int)h,0,0,0);//clear 00245 sbDisplay.displayFrame(); 00246 } 00247 vf=vi+g*tf;//Find final velocity (i.e., as it hits the ground 00248 h0=0; 00249 vi=vf*cor;//use vf to calc new vi using cooef of restitution 00250 //higest it will go 00251 tf=vi/g; 00252 step = tf/20;//alter the step size since were spending less time in this iteration 00253 00254 //--- BOUNCE ------ 00255 for (tim=0 ; tim <=tf; tim+=step) { 00256 h=(vi*tim) - 0.5*g*(tim * tim) + h0; 00257 //PC.printf("U%f\r\n",h); 00258 sbDisplay.setLed((unsigned int)h,500,0,0);//set 00259 sbDisplay.displayFrame(); 00260 //wait(step); 00261 sbDisplay.setLed((unsigned int)h,0,0,0);//clear 00262 sbDisplay.displayFrame(); 00263 } 00264 h0=h; 00265 vi=0; 00266 } 00267 while(1){}//nowhere else to go so get stuck here. User will need to reset to see another drop 00268 #endif //BALLBOUNCE 00269 } 00270 00271 00272 /*TODO - 00273 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. 00274 from https://mbed.org/questions/1029/pcprintf-a-method/ 00275 00276 */
Generated on Thu Jul 14 2022 13:46:59 by
1.7.2