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

Dependencies:   mbed

Committer:
JoKer
Date:
Thu Aug 21 05:39:33 2014 +0000
Revision:
6:75801b7a36a3
Parent:
5:aa0424f31fa1
Child:
7:a0f62fc80de0
Improved main routine for demonstrating generating an special effect.

Who changed what in which revision?

UserRevisionLine numberNew 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 4:d2f8ddb423e2 8 /* VARIOUS EXAMPLES OF HOW TO USE the shiftBriteDisplay class and the movie class follows below.
JoKer 4:d2f8ddb423e2 9 Please note, this is all 'beerware' code but I would appreciate a mention in your code headers
JoKer 4:d2f8ddb423e2 10 if you use some/all of my code/classes. My name is Johan Kritzinger and I go by 'JoKer' on MBED.
JoKer 4:d2f8ddb423e2 11 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 12 Any questions, please feel free to ask.
JoKer 0:f76850de7b57 13 */
JoKer 0:f76850de7b57 14
JoKer 4:d2f8ddb423e2 15
JoKer 2:3935d2ed40cd 16 //6 leds example. Format it suitably for easy reading
JoKer 2:3935d2ed40cd 17 unsigned short int aMovie[] = {
JoKer 2:3935d2ed40cd 18 /* LED1 LED2 LED3 LED4 LED5 LED6 */
JoKer 5:aa0424f31fa1 19 10,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 0*/
JoKer 5:aa0424f31fa1 20 0,0,0, 100,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 1*/
JoKer 5:aa0424f31fa1 21 0,0,0, 0,0,0, 500,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 2*/
JoKer 5:aa0424f31fa1 22 0,0,0, 0,0,0, 0,0,0, 750,0,0, 0,0,0, 0,0,0, /*Frame 3*/
JoKer 5:aa0424f31fa1 23 0,0,0, 0,0,0, 0,0,0, 0,0,0, 900,0,0, 0,0,0, /*Frame 4*/
JoKer 2:3935d2ed40cd 24 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 1023,0,0, /*Frame 5*/
JoKer 5:aa0424f31fa1 25 0,0,0, 0,0,0, 0,0,0, 0,0,0, 900,0,0, 0,0,0, /*Frame 6*/
JoKer 5:aa0424f31fa1 26 0,0,0, 0,0,0, 0,0,0, 750,0,0, 0,0,0, 0,0,0, /*Frame 7*/
JoKer 5:aa0424f31fa1 27 0,0,0, 0,0,0, 500,0,0, 0,0,0, 0,0,0, 0,0,0, /*Frame 8*/
JoKer 5:aa0424f31fa1 28 0,0,0, 100,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0 /*Frame 9*/
JoKer 2:3935d2ed40cd 29 /*A simple 'cylon' scanner 'movie'*/
JoKer 2:3935d2ed40cd 30 };
JoKer 4:d2f8ddb423e2 31
JoKer 5:aa0424f31fa1 32
JoKer 4:d2f8ddb423e2 33 Serial PC(PTA2, PTA1);//So I can use Serial as output to terminal and input
JoKer 0:f76850de7b57 34
JoKer 4:d2f8ddb423e2 35 //================== HARDWARE CONNECTIONS =========================================
JoKer 4:d2f8ddb423e2 36 //NB!!! I have only tested this on the FRDM KL25Z and h/w connection as detailed below
JoKer 0:f76850de7b57 37 //Instanced of DigitalOut for control SB signals
JoKer 0:f76850de7b57 38 DigitalOut latch(PTC16);//010=latch
JoKer 0:f76850de7b57 39 DigitalOut enable(PTA13);//0= enabled
JoKer 0:f76850de7b57 40 DigitalOut reset(PTC12);
JoKer 0:f76850de7b57 41 //Instance of the SPI contoller for SB data
JoKer 0:f76850de7b57 42 SPI spi(PTD2,NC,PTD1);//PDT2 = MOSI=DATA. PDT1=CLK
JoKer 4:d2f8ddb423e2 43 //=================== END OF HARDWARE CONNECTIONS =================================
JoKer 0:f76850de7b57 44
JoKer 0:f76850de7b57 45 int main() {
JoKer 5:aa0424f31fa1 46
JoKer 0:f76850de7b57 47
JoKer 0:f76850de7b57 48 //Instanciate a ticker object to handle framerate updates for the SB display
JoKer 0:f76850de7b57 49 Ticker t;
JoKer 0:f76850de7b57 50
JoKer 0:f76850de7b57 51 //Instanciate a string of 5 sb modules and tell the driver object where the control/data pins are
JoKer 4:d2f8ddb423e2 52 shiftBriteDisplay sbDisplay(latch, enable, reset, spi,6);
JoKer 0:f76850de7b57 53
JoKer 5:aa0424f31fa1 54
JoKer 6:75801b7a36a3 55 /* MAIN PROGRAM ONE to display various features including movie class
JoKer 5:aa0424f31fa1 56
JoKer 0:f76850de7b57 57 //Example calls to method f() of shiftBriteDisplay class
JoKer 0:f76850de7b57 58 //in this case, 6 of these statements wold be required to build one frame
JoKer 2:3935d2ed40cd 59 //HOW TO BUILD A FRAME IF YOU ARE NOT USING THE MOVIE CLASS
JoKer 2:3935d2ed40cd 60 sbDisplay.setLed(0,0XFF0000);//Red
JoKer 0:f76850de7b57 61 sbDisplay.setLed(1,0X00FF00);//Green
JoKer 0:f76850de7b57 62 sbDisplay.setLed(2,0X0000FF);//Blue
JoKer 0:f76850de7b57 63 sbDisplay.setLed(3,0XFFFF00);//Yellow
JoKer 0:f76850de7b57 64 sbDisplay.setLed(4,0X00FFFF); //Cyan?
JoKer 2:3935d2ed40cd 65 sbDisplay.setLed(5,0XFF00FF); // Purple
JoKer 0:f76850de7b57 66
JoKer 2:3935d2ed40cd 67 //HOW TO ADJUST THE CURRENT CONTROLLERS USED FOR DOT CORRECTION
JoKer 0:f76850de7b57 68 sbDisplay.setCurrentCorr(0,0,0);//suggested default value cor current control regs. Values g&b=100, r=120
JoKer 0:f76850de7b57 69 sbDisplay.displayFrame();//force an update
JoKer 0:f76850de7b57 70 wait(2);
JoKer 0:f76850de7b57 71 sbDisplay.setCurrentCorr(127,127,127);//This should be MAX
JoKer 0:f76850de7b57 72 wait(2);
JoKer 3:9376bf1f1bbd 73 //sbDisplay.setCurrentCorr(0x78,0x64,0x64);//sb suggested default
JoKer 3:9376bf1f1bbd 74 sbDisplay.setCurrentCorr(0,0,0);//Dim
JoKer 3:9376bf1f1bbd 75
JoKer 3:9376bf1f1bbd 76 wait(2);
JoKer 3:9376bf1f1bbd 77 PC.printf("INVERTED\r\n");
JoKer 3:9376bf1f1bbd 78 sbDisplay.invert();
JoKer 3:9376bf1f1bbd 79 sbDisplay.displayFrame();
JoKer 3:9376bf1f1bbd 80 wait(2);
JoKer 3:9376bf1f1bbd 81 PC.printf("FLIP\r\n");
JoKer 3:9376bf1f1bbd 82 sbDisplay.flip();
JoKer 3:9376bf1f1bbd 83 sbDisplay.displayFrame();
JoKer 0:f76850de7b57 84 wait(2);
JoKer 3:9376bf1f1bbd 85 sbDisplay.setLed(0,0XFF0000);//Red
JoKer 3:9376bf1f1bbd 86 sbDisplay.setLed(1,0X000500);//Green
JoKer 3:9376bf1f1bbd 87 sbDisplay.setLed(2,0X000005);//Blue
JoKer 3:9376bf1f1bbd 88 sbDisplay.setLed(3,0X050500);//Yellow
JoKer 3:9376bf1f1bbd 89 sbDisplay.setLed(4,0X000505); //Cyan?
JoKer 3:9376bf1f1bbd 90 sbDisplay.setLed(5,0X050005); // Purple
JoKer 3:9376bf1f1bbd 91 sbDisplay.displayFrame(); //get it on the LEDS
JoKer 3:9376bf1f1bbd 92
JoKer 3:9376bf1f1bbd 93 PC.printf("RotateL");
JoKer 3:9376bf1f1bbd 94 unsigned int loop;
JoKer 3:9376bf1f1bbd 95 for (loop=0; loop != 100; loop++){
JoKer 3:9376bf1f1bbd 96 sbDisplay.rotateLeft();
JoKer 3:9376bf1f1bbd 97 sbDisplay.displayFrame();
JoKer 3:9376bf1f1bbd 98 wait(0.1);
JoKer 3:9376bf1f1bbd 99 }
JoKer 3:9376bf1f1bbd 100 PC.printf("RotateR");
JoKer 3:9376bf1f1bbd 101 //unsigned int loop;
JoKer 3:9376bf1f1bbd 102 for (loop=0; loop != 100; loop++){
JoKer 3:9376bf1f1bbd 103 sbDisplay.rotateRight();
JoKer 3:9376bf1f1bbd 104 sbDisplay.displayFrame();
JoKer 3:9376bf1f1bbd 105 wait(0.1);
JoKer 3:9376bf1f1bbd 106 }
JoKer 3:9376bf1f1bbd 107 for(loop=0; loop !=6; loop++){
JoKer 3:9376bf1f1bbd 108 sbDisplay.shiftRight();
JoKer 3:9376bf1f1bbd 109 sbDisplay.displayFrame();
JoKer 3:9376bf1f1bbd 110 wait(0.2);
JoKer 3:9376bf1f1bbd 111 }
JoKer 2:3935d2ed40cd 112
JoKer 2:3935d2ed40cd 113
JoKer 2:3935d2ed40cd 114 sbDisplay.setLed(0,0X0F0000);//Red
JoKer 2:3935d2ed40cd 115 sbDisplay.setLed(1,0X000F00);//Green
JoKer 2:3935d2ed40cd 116 sbDisplay.setLed(2,0X00000F);//Blue
JoKer 2:3935d2ed40cd 117 sbDisplay.setLed(3,0X0F0F00);//Yellow
JoKer 2:3935d2ed40cd 118 sbDisplay.setLed(4,0X000F0F); //Cyan?
JoKer 2:3935d2ed40cd 119 sbDisplay.setLed(5,0X0F000F); // Purple
JoKer 2:3935d2ed40cd 120 sbDisplay.displayFrame(); //get it on the LEDS
JoKer 2:3935d2ed40cd 121 wait(0.5);
JoKer 2:3935d2ed40cd 122 sbDisplay.setCurrentCorr(0,0,0);//Dim
JoKer 2:3935d2ed40cd 123
JoKer 2:3935d2ed40cd 124 sbDisplay.displayFrame(); //get it on the LEDS
JoKer 4:d2f8ddb423e2 125
JoKer 4:d2f8ddb423e2 126 //Alternative to calling displayFrame() yourself, setup the framerate and update the display using Ticker e.g.
JoKer 2:3935d2ed40cd 127 //t.attach_us(&sbDisplay,&shiftBriteDisplay::displayFrame,41666);//call updateFrame 24 times per second (every 41666uS)
JoKer 4:d2f8ddb423e2 128 //or t.attach(&sbDisplay,&shiftBriteDisplay::displayFrame,0.5);// or only every 0.5s for testing
JoKer 2:3935d2ed40cd 129
JoKer 2:3935d2ed40cd 130
JoKer 5:aa0424f31fa1 131 //
JoKer 5:aa0424f31fa1 132 //EXPANDING THE FUNCTIONALITY OF shiftBriteDisplay class; USING THE MOVIE CLASS
JoKer 5:aa0424f31fa1 133 //I know, i know, 'MOVIE' is a HUGE stretch but it sounds good right?!?! :)
JoKer 5:aa0424f31fa1 134 //
JoKer 4:d2f8ddb423e2 135 //Note, it uses the previously declared sbDisplay object. It is passed as a reference.
JoKer 4:d2f8ddb423e2 136 movie myMovie(aMovie,sbDisplay,sizeof(aMovie));
JoKer 4:d2f8ddb423e2 137 myMovie.setRepeat(1);
JoKer 5:aa0424f31fa1 138
JoKer 4:d2f8ddb423e2 139 t.attach(&myMovie,&movie::play,0.05);//Beware, if you go too fast here the FRDM will crash
JoKer 6:75801b7a36a3 140 while(1){;} //nothing in main loop as Ticker calls the relevan member function
JoKer 5:aa0424f31fa1 141 */
JoKer 4:d2f8ddb423e2 142
JoKer 6:75801b7a36a3 143
JoKer 6:75801b7a36a3 144 //MAIN PROGRAM TWO - to show some special effects WITHOUT using the movie class
JoKer 6:75801b7a36a3 145 //Play around by adjusting phases, frequencies etc. Each will give different results
JoKer 6:75801b7a36a3 146 //Below is an example using 3 sin waves, one for each colour, but 120 deg out of phase
JoKer 6:75801b7a36a3 147 //and slowly scanning frequencies from 0.5 to 5 and back again. Each freq is active for 5 seconds.
JoKer 6:75801b7a36a3 148
JoKer 5:aa0424f31fa1 149 unsigned int j;
JoKer 5:aa0424f31fa1 150 for (j=0; j != sbDisplay.getModuleCount(); j++){
JoKer 5:aa0424f31fa1 151 sbDisplay.setLed(j,0,0,0);//set all led to black
JoKer 5:aa0424f31fa1 152 }
JoKer 5:aa0424f31fa1 153 sbDisplay.displayFrame();//get it on the leds
JoKer 5:aa0424f31fa1 154 sbDisplay.setCurrentCorr(0,0,0);//Dim down to 30%
JoKer 6:75801b7a36a3 155
JoKer 6:75801b7a36a3 156 double tim,max_t; // 'real' time and time limit for a simulation
JoKer 6:75801b7a36a3 157 double w1,w2,w3; // 2*pi*f
JoKer 6:75801b7a36a3 158 double phase1,phase2,phase3;//Phase offset
JoKer 6:75801b7a36a3 159 double f1,f2,f3;//Frequencies
JoKer 6:75801b7a36a3 160 double factor = 250;//250 steps or values over max_t time
JoKer 6:75801b7a36a3 161
JoKer 6:75801b7a36a3 162 unsigned char f_dir = 1; //used for up and down functionality
JoKer 6:75801b7a36a3 163
JoKer 6:75801b7a36a3 164 f1 = f2 = f3 = 1.0; //frequency in herz - set all to the same
JoKer 6:75801b7a36a3 165 //but make a 120 degree pahse shift between the 3 waves
JoKer 6:75801b7a36a3 166 phase1 = 0.0;
JoKer 6:75801b7a36a3 167 phase2 = 120.0*(M_PI/180.0);
JoKer 6:75801b7a36a3 168 phase3 = 240.0*(M_PI/180.0);
JoKer 6:75801b7a36a3 169
JoKer 6:75801b7a36a3 170 max_t=5.0; //i.e. time to iterate over - i.e. 5 second
JoKer 6:75801b7a36a3 171
JoKer 6:75801b7a36a3 172 while(1){ // generate special effects without using a movie and discretely calc values
JoKer 6:75801b7a36a3 173 for(tim=0; tim <= max_t; tim+=max_t/factor) {
JoKer 5:aa0424f31fa1 174 //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 175 sbDisplay.shiftRight((USI)511.0*(1.0+sin(w1*tim+phase1)),
JoKer 6:75801b7a36a3 176 (USI)511.0*(1.0+sin(w2*tim+phase2)),
JoKer 6:75801b7a36a3 177 (USI)511.0*(1.0+sin(w3*tim+phase3))
JoKer 6:75801b7a36a3 178 );
JoKer 5:aa0424f31fa1 179 sbDisplay.displayFrame();
JoKer 6:75801b7a36a3 180 wait( (max_t/factor));
JoKer 5:aa0424f31fa1 181 }
JoKer 6:75801b7a36a3 182 w1 = 2.0*M_PI*f1;
JoKer 6:75801b7a36a3 183 w2 = 2.0*M_PI*f2;
JoKer 6:75801b7a36a3 184 w3 = 2.0*M_PI*f3;
JoKer 6:75801b7a36a3 185 if(f_dir){
JoKer 6:75801b7a36a3 186 f1 = f2 = f3 = f3+0.5;
JoKer 6:75801b7a36a3 187 if (f1>=5.0)f_dir = 0;
JoKer 6:75801b7a36a3 188 } else {
JoKer 6:75801b7a36a3 189 f1 = f2 = f3 = f3-0.5;
JoKer 6:75801b7a36a3 190 if (f1<=0.5) f_dir = 1;
JoKer 6:75801b7a36a3 191 }
JoKer 6:75801b7a36a3 192
JoKer 0:f76850de7b57 193 }
JoKer 0:f76850de7b57 194 }
JoKer 4:d2f8ddb423e2 195
JoKer 4:d2f8ddb423e2 196
JoKer 4:d2f8ddb423e2 197 /*TODO -
JoKer 4:d2f8ddb423e2 198 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 199 from https://mbed.org/questions/1029/pcprintf-a-method/
JoKer 4:d2f8ddb423e2 200
JoKer 4:d2f8ddb423e2 201 */