A few classes to interface one or more ShiftBrite module to the FRDM KL25Z.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
JoKer
Date:
Fri Aug 22 01:18:49 2014 +0000
Parent:
6:75801b7a36a3
Commit message:
Cleaned up main. Divided into 3 sections to demonstrate various aspects of the libraries and their usage.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Aug 21 05:39:33 2014 +0000
+++ b/main.cpp	Fri Aug 22 01:18:49 2014 +0000
@@ -5,7 +5,17 @@
 #define FACTOR 1.0
 #define USI unsigned short int
 
-/* VARIOUS EXAMPLES OF HOW TO USE the shiftBriteDisplay class and the movie class follows below.
+// !!!!!!!!!!!!!!!!!! R E A D     T H I S     S E C T I O N    F I R S T   !!!!!!!!!!!!!
+// !!!!!  INSTUCTIONS   INSTRUCTIONS   INSTRUCTIONS   INSTRUCTIONS    INSTRUCTIONS !!!!!
+//A number of main loop examples follows. Select one by ...
+//uncommenting ONLY ONE of the following. Then you can pick appart the relevant section.
+#define BALLBOUNCE
+//#define SINEWAVE
+//#define VARIOUS
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+
+/** VARIOUS EXAMPLES OF HOW TO USE the shiftBriteDisplay class and the movie class follows below.
 Please note, this is all 'beerware' code but I would appreciate a mention in your code headers
 if you use some/all of my code/classes. My name is Johan Kritzinger and I go by 'JoKer' on MBED.
 My C++ is not that hot (I'm an old PLM-51 and C programmer) but the only way to learn is to do.
@@ -13,6 +23,7 @@
 */
 
 
+#ifdef VARIOUS
 //6 leds example. Format it suitably for easy reading
 unsigned short int aMovie[] = {
     /* LED1      LED2      LED3      LED4      LED5      LED6 */
@@ -28,7 +39,7 @@
        0,0,0, 100,0,0,    0,0,0,    0,0,0,    0,0,0,    0,0,0  /*Frame 9*/
    /*A simple 'cylon' scanner 'movie'*/    
 };
-
+#endif
 
 Serial PC(PTA2, PTA1);//So I can use Serial as output to terminal and input
 
@@ -51,8 +62,9 @@
 //Instanciate a string of 5 sb modules and tell the driver object where the control/data pins are
 shiftBriteDisplay sbDisplay(latch, enable, reset, spi,6);
 
-
-/* MAIN PROGRAM ONE to display various features including movie class
+//=================== EXAMPLE ONE MAIN LOOP ==========================================
+#ifdef VARIOUS
+// MAIN PROGRAM ONE to display various features including movie class
 
 //Example calls to method f() of shiftBriteDisplay class
 //in this case, 6 of these statements wold be required to build one frame
@@ -138,9 +150,11 @@
 
     t.attach(&myMovie,&movie::play,0.05);//Beware, if you go too fast here the FRDM will crash
     while(1){;} //nothing in main loop as Ticker calls the relevan member function
-*/
+#endif //VARIOUS
 
+//=================== EXAMPLE TWO MAIN LOOP ==========================================
 
+#ifdef SINEWAVE
 //MAIN PROGRAM TWO - to show some special effects WITHOUT using the movie class
 //Play around by adjusting phases, frequencies etc. Each will give different results
 //Below is an example  using 3 sin waves, one for each colour, but 120 deg out of phase
@@ -151,7 +165,7 @@
         sbDisplay.setLed(j,0,0,0);//set all led to black
     }
     sbDisplay.displayFrame();//get it on the leds
-    sbDisplay.setCurrentCorr(0,0,0);//Dim down to 30%
+    sbDisplay.setCurrentCorr(0,0,0);//30% power
     
 double tim,max_t; // 'real' time and time limit for a simulation
 double w1,w2,w3; // 2*pi*f
@@ -190,9 +204,70 @@
                 if (f1<=0.5) f_dir = 1;                
             }
 
+    } //end of while 1
+#endif //SINWAVE 
+
+
+//=================== EXAMPLE THREE MAIN LOOP ==========================================
+
+#ifdef BALLBOUNCE
+ //HOW ABOUT A BOUNCING BALL?
+      //fisrt, clear the display
+      unsigned int i;
+    for(i=0 ; i != sbDisplay.getModuleCount(); i++){
+        sbDisplay.setLed(i,0,0,0);
     }
+    sbDisplay.displayFrame();
+    
+    double tim;//time
+    double tf;//time it takes to fall 
+    double h=0;//current height - calculated
+    double h0=sbDisplay.getModuleCount();//starting height. can be anything but best to set to the number of leds in the chain
+    double vi=0;//initial velocity
+    double vf; //final velocity
+    double g=9.81;//gravity
+    double step;//size of time step in h calculation iteration    
+    double cor = 0.85; //Cooeficient of restitution - a measure of how much velocity is lost when a ball bounces
+    //cor is dependent on the material off the ball and the surface it bounces on.
+
+  while(h0 >= 0.5){
+    //First, find out how long before it falls h=0
+    tf=sqrt((h0*2)/g);//how long to fall from h0 to 0
+    step = tf/20;//dynamically calculated step size
+    //---FALL ----
+    for (tim = 0; tim <= tf; tim+=step) {
+        h=h0-(vi*tim + 0.5*g*(tim * tim));
+        //PC.printf("D%f\r\n",h);
+        sbDisplay.setLed((unsigned int)h,500,0,0);//set
+        sbDisplay.displayFrame();
+        //wait(step);//made it too slow - enough time lost doing all the calcs
+        sbDisplay.setLed((unsigned int)h,0,0,0);//clear
+        sbDisplay.displayFrame();
+    }
+    vf=vi+g*tf;//Find final velocity (i.e., as it hits the ground
+    h0=0;
+    vi=vf*cor;//use vf to calc new vi using cooef of restitution
+    //higest it will go
+    tf=vi/g;
+    step = tf/20;//alter the step size since were spending less time in this iteration
+    
+    //--- BOUNCE ------
+    for (tim=0 ; tim <=tf; tim+=step) {
+        h=(vi*tim) - 0.5*g*(tim * tim) + h0;
+        //PC.printf("U%f\r\n",h);
+        sbDisplay.setLed((unsigned int)h,500,0,0);//set
+        sbDisplay.displayFrame();
+        //wait(step);
+        sbDisplay.setLed((unsigned int)h,0,0,0);//clear
+        sbDisplay.displayFrame();
+    }
+    h0=h;
+      vi=0;
+   }
+    while(1){}//nowhere else to go so get stuck here. User will need to reset to see another drop
+#endif //BALLBOUNCE
 }
-
+ 
 
 /*TODO - 
 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.