Program with 5510 functions for mbedd, this are not organized in a object model, just functions

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "figures.h"
00003 //#include <pcd8544_drv.hpp>
00004 
00005 /*
00006 This Code has extra features
00007 including a XY positioning function on Display
00008 and a Line Draw function on Nokia 3310 LCD
00009 It is modded from the original
00010 http://www.arduino.cc/playground/Code/PCD8544
00011 */
00012 // Mods by Jim Park
00013 // jim(^dOt^)buzz(^aT^)gmail(^dOt^)com
00014 // hope it works for you
00015 DigitalOut PIN_SCE(p26);//p21);  // 7  // LCD CS  .... Pin 3
00016 DigitalOut PIN_RESET(p27);//p22);// 6  // LCD RST .... Pin 1
00017 DigitalOut PIN_DC(p28);//p23);   //  5  // LCD Dat/Com. Pin 5
00018 DigitalOut PIN_SDIN(p29);//p24); //  4  // LCD SPIDat . Pin 6
00019 DigitalOut PIN_SCLK(p30);//p25); //  3  // LCD SPIClk . Pin 4
00020 // LCD Gnd .... Pin 2
00021 // LCD Vcc .... Pin 8
00022 // LCD Vlcd ... Pin 7
00023 DigitalOut PIN_BCKL(p25);//Nokia 5110 backligth
00024 
00025 
00026 #define LCD_C     0
00027 #define LCD_D     1
00028 
00029 #define LCD_X     84
00030 #define LCD_Y     48
00031 #define LCD_CMD   0
00032 
00033 #define YMAX 48
00034 #define XMAX 84
00035 int bufferLcd[XMAX][YMAX]={0};
00036 
00037 #define byte char
00038 // Function prototype
00039 void LcdWrite(char dc, char data);
00040 void shiftOut(DigitalOut dataPin,DigitalOut clockPin,char bitOrder, char value );
00041 #define MSBFIRST 1
00042 #define LSBFIRST 0
00043 
00044 int a = 0;
00045 
00046 static const char ASCII[][5] = {
00047     {0x00, 0x00, 0x00, 0x00, 0x00} // 20
00048     ,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
00049     ,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
00050     ,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
00051     ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
00052     ,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
00053     ,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
00054     ,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
00055     ,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
00056     ,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
00057     ,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
00058     ,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
00059     ,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
00060     ,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
00061     ,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
00062     ,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
00063     ,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
00064     ,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
00065     ,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
00066     ,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
00067     ,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
00068     ,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
00069     ,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
00070     ,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
00071     ,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
00072     ,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
00073     ,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
00074     ,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
00075     ,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
00076     ,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
00077     ,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
00078     ,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
00079     ,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
00080     ,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
00081     ,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
00082     ,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
00083     ,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
00084     ,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
00085     ,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
00086     ,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
00087     ,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
00088     ,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
00089     ,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
00090     ,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
00091     ,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
00092     ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
00093     ,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
00094     ,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
00095     ,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
00096     ,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
00097     ,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
00098     ,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
00099     ,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
00100     ,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
00101     ,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
00102     ,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
00103     ,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
00104     ,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
00105     ,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
00106     ,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
00107     ,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c &#65533;
00108     ,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
00109     ,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
00110     ,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
00111     ,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
00112     ,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
00113     ,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
00114     ,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
00115     ,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
00116     ,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
00117     ,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
00118     ,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
00119     ,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
00120     ,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
00121     ,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
00122     ,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
00123     ,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
00124     ,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
00125     ,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
00126     ,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
00127     ,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
00128     ,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
00129     ,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
00130     ,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
00131     ,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
00132     ,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
00133     ,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
00134     ,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
00135     ,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
00136     ,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
00137     ,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
00138     ,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
00139     ,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
00140     ,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
00141     ,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e &#8592;
00142     ,{0x00, 0x06, 0x09, 0x09, 0x06} // 7f &#8594;
00143 };
00144 
00145 
00146 /*************************************************************************/
00147 const char *byte_to_binary
00148 (
00149     int x
00150 )
00151 {
00152     static char b[9];
00153     b[0] = '\0';
00154 
00155     int z;
00156     for (z = 256; z > 0; z >>= 1)
00157     {
00158         strcat(b, ((x & z) == z) ? "|" : "0");
00159     }
00160 
00161     return b;
00162 }
00163 /**********************************************************************************************/
00164 
00165 void LcdCharacter(char character) {
00166     LcdWrite(LCD_D, 0x00);
00167     for (int index = 0; index < 5; index++) {
00168         LcdWrite(LCD_D, ASCII[character - 0x20][index]);
00169     }
00170     LcdWrite(LCD_D, 0x00);
00171 }
00172 
00173 void LcdClear(void) {
00174     for (int index = 0; index < LCD_X * LCD_Y / 8; index++) {
00175         LcdWrite(LCD_D, 0x00);
00176     }
00177 }
00178 
00179 void LcdInitialise(void) {
00180     //pinMode(PIN_SCE,OUTPUT);
00181     //pinMode(PIN_RESET, OUTPUT);
00182     //pinMode(PIN_DC,    OUTPUT);
00183     //pinMode(PIN_SDIN,  OUTPUT);
00184     //pinMode(PIN_SCLK,  OUTPUT);
00185 
00186     PIN_RESET=0;
00187     //digitalWrite(PIN_RESET, LOW);
00188 
00189 // delay(1);
00190 // wait(0.001);
00191     PIN_RESET=1;
00192     //digitalWrite(PIN_RESET, HIGH);
00193 
00194     LcdWrite( LCD_CMD, 0x21 );  // LCD Extended Commands.
00195     LcdWrite( LCD_CMD, 0xB0 );  // Set LCD Vop (Contrast). //B1 for normal clear background, b2 to bf for dark background
00196     LcdWrite( LCD_CMD, 0x04 );  // Set Temp coefficent. //0x04
00197     LcdWrite( LCD_CMD, 0x14 );  // LCD bias mode 1:48. //0x13
00198     LcdWrite( LCD_CMD, 0x0C );  // LCD in normal mode. 0x0d for inverse
00199     LcdWrite(LCD_C, 0x20);
00200     LcdWrite(LCD_C, 0x0C);
00201 }
00202 
00203 void LcdString(char *characters) {
00204     while (*characters) {
00205         LcdCharacter(*characters++);
00206     }
00207 }
00208 
00209 void LcdWrite(byte dc, byte data) {
00210     PIN_DC=dc;
00211     PIN_SCE=0;
00212     shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
00213     //->wait(0.001);
00214     PIN_SCE=1;
00215     /*
00216     digitalWrite(PIN_DC, dc);
00217     digitalWrite(PIN_SCE, LOW);
00218     shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
00219     digitalWrite(PIN_SCE, HIGH);
00220     */
00221 }
00222 
00223 // gotoXY routine to position cursor
00224 // x - range: 0 to 84
00225 // y - range: 0 to 5
00226 
00227 void gotoXY(int x, int y) {
00228     LcdWrite( 0, 0x80 | x);  // Column.
00229     LcdWrite( 0, 0x40 | y);  // Row.
00230 
00231 }
00232 
00233 
00234 
00235 void drawLine(int x0,int y0,//Initial Point
00236               int x1,int y1)//Final point
00237 {
00238         int dy = y1 - y0;
00239         int dx = x1 - x0;
00240         int stepx, stepy;
00241         if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
00242         if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
00243         dy <<= 1;                 // dy is now 2*dy
00244         dx <<= 1;                 // dx is now 2*dx
00245         bufferLcd[x0][y0]=1;
00246      //   printf("Ini X0,y0=  %2u ,%2u\n", x0,y0);
00247         if (dx > dy) {
00248             int fraction = dy - (dx >> 1);// same as 2*dy - dx
00249             while (x0 != x1) {
00250                 if (fraction >= 0) {
00251                     y0 += stepy;
00252                     fraction -= dx;      // same as fraction -= 2*dx
00253                 }
00254                 x0 += stepx;
00255                 fraction += dy;         // same as fraction -= 2*dy
00256                 bufferLcd[x0][y0]=1;
00257              //   printf("   X0,y0=  %2u ,%2u\n", x0,y0);
00258             }
00259         } else {
00260             int fraction = dx - (dy >> 1);
00261             while (y0 != y1) {
00262                 if (fraction >= 0) {
00263                     x0 += stepx;
00264                     fraction -= dy;
00265                 }
00266                 y0 += stepy;
00267                 fraction += dx;
00268                bufferLcd[x0][y0]=1;
00269               // printf("   X0,y0=  %2u ,%2u\n", x0,y0);
00270             }
00271         }
00272         
00273         // printf("<-------       ------->\n");
00274         
00275 }
00276 
00277 void updateLcd(void) {//Update with data from Buffer(Use drawPoint)
00278 int blockVal;
00279     for(int x=0;x<XMAX;x++) { //For every value of x
00280         for(int block=0;block<6;block++) {//For everyone of the 6 block in y.
00281             blockVal=0;//Initialise to zero value of hte block
00282             for(int i=0;i<8;i++) {//For every block�s bit
00283                 blockVal|=(bufferLcd[x][(block*8)+i])<<i;//Current block�s bit value is pushed
00284             }
00285             //The point is drawed
00286             gotoXY (x,block);
00287             LcdWrite (1,blockVal);
00288             //wait(0.1);
00289         }
00290     }
00291 }
00292 
00293 void clearLcdBuff() { //Clear the entire buffer (Use drawPoint)
00294 
00295     for(int i=0;i<XMAX;i++) {
00296         for(int j=0;j<YMAX;j++) {
00297              bufferLcd[i][j]=0;   
00298     
00299         }
00300     }
00301 /*int blockVal;
00302     for(int x=0;x<XMAX;x++) {
00303         for(int block=0;block<5;block++) {
00304             blockVal=0;
00305 
00306             //The point is drawed
00307             gotoXY (x,block);
00308             LcdWrite (1,blockVal);
00309         }
00310     }
00311 */    
00312 }
00313 
00314 void drawPoint(int X, int Y) { //drawing point using values  0->83 in X and 0->47 in Y [READ BLOCK THEN add/Modify NEW POINT]
00315 /*int blck,blckPos,buffY;
00316     //Selection of y block(0->5)
00317     blck=(int)(floor((y_dbl/8))); //Determining the block
00318     blckPos=(int)(y%8);//Calculating position in block
00319     buffY|=1<<blckPos;
00320 */    
00321     //The point is stored
00322      bufferLcd[X][Y]=1;
00323     
00324 }
00325 void drawPoint_inst(int x, int y) { //drawing point using values  0->83 in X and 0->47 in Y [READ BLOCK THEN add/Modify NEW POINT]
00326 int buf_y=0,blck,blckPos;
00327 double y_dbl;
00328     y_dbl=(double)(y);
00329     //Selection of y block(0->5)
00330     blck=(int)(floor((y_dbl/8))); //Determining the block
00331     blckPos=(int)(y%8);//Calculating position in block
00332     buf_y|=1<<blckPos;
00333     
00334     //The point is stored
00335     gotoXY (x,blck);
00336     LcdWrite (1,buf_y);
00337     
00338 }
00339 /*
00340 int roundVal(float value ) {//Round point in line
00341 int y;
00342     if(abs(value-ceil(value))>=abs(value-floor(value))) {
00343         y=(int)(floor(value));
00344     }
00345     else {
00346         y=(int)(ceil(value));  
00347     }
00348     
00349     return y;                
00350 }
00351 */
00352 void setup(void) {
00353 
00354     LcdInitialise();
00355     LcdClear();
00356 
00357 }
00358 
00359 int main(void) {
00360     // Display some simple character animation
00361     //
00362     int a,b;
00363     char Str[15];
00364     int count=0;
00365 
00366 
00367     setup();
00368     //wait(0.1);
00369     PIN_BCKL=0;
00370     wait(0.5);
00371     PIN_BCKL=1;
00372 while(1) {
00373         printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
00374         for(int a=0;a<504;a++) {
00375          if((a%84)==0)
00376             printf("Last frame\n");
00377          printf("%3u:: %s\n",a,byte_to_binary(MCertified2[a]));
00378         }
00379         printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
00380         clearLcdBuff();
00381         printf("Start---------\n");
00382         for (int index3 = 0; index3 < 6; index3++) {
00383             for( int index1= 0; index1 <8; index1++) {
00384                 for( int index2= 0; index2 < 84; index2++) {
00385                     if((MCertified[(index3*84)+index2]&(1<<index1))!=0) {
00386                         #ifdef FastLcd
00387                         bufferLcd[index2][(index3*8)+index1]=1;//(7-index1)]= 1;
00388                         #endif
00389                         #ifdef  Bitmap2Lcd
00390                         bufferLcd[index2][(index3*8)+(7-index1)]= 1;
00391                         #endif
00392                         printf("|");
00393                     }
00394                     else
00395                         printf(" ");
00396                 }
00397                 printf("\n");
00398             }
00399         }
00400         printf("\End---------\n");
00401         updateLcd();
00402         wait(0.5);
00403 }
00404     while (1) {
00405         //gotoXY(0,0);
00406         count++;
00407         //LcdString("Hello there esta es una prueba de lcd ");
00408 
00409         //drawLine(1,1,count,47);
00410         clearLcdBuff();
00411       /* drawLine(0,0,83,0);
00412         drawLine(83,0,83,47);
00413         drawLine(83,47,0,47);
00414         drawLine(0,47,0,0);
00415        */ 
00416        drawLine(0,0,count,47);
00417         updateLcd();
00418         wait(0.15);
00419         LcdClear();
00420         if (count==83)
00421             count=0;
00422     }
00423     // Draw a Box
00424 // for(b=1000; b>0; b--){
00425 // drawLine();
00426 // for(a=0; a<=5 ; a++){
00427 // gotoXY(4,1);
00428     // Put text in Box
00429     LcdString("TestDisplay");
00430     gotoXY(24,3);
00431     LcdCharacter('H');
00432     LcdCharacter('E');
00433     LcdCharacter('L');
00434     LcdCharacter('L');
00435     LcdCharacter('O');
00436     LcdCharacter(' ');
00437     LcdCharacter('=');
00438     // Draw + at this position
00439     gotoXY(10,3);
00440     LcdCharacter('=');
00441     wait(0.5);
00442     gotoXY(24,3);
00443     LcdCharacter('h');
00444     LcdCharacter('e');
00445     LcdCharacter('l');
00446     LcdCharacter('l');
00447     LcdCharacter('o');
00448     LcdCharacter(' ');
00449     LcdCharacter('-');
00450     // Draw - at this position
00451     gotoXY(10,3);
00452     LcdCharacter('-');
00453     wait(0.5);
00454 // }
00455 // }
00456 
00457     while (1);
00458     return 0;
00459 }
00460 
00461 /*
00462 LYBRARIES
00463 */
00464 //shiftOut(dataPin, clockPin, bitOrder, value)
00465 void shiftOut(DigitalOut dataPin,DigitalOut clockPin,char bitOrder, char value ) {
00466     int i;
00467     clockPin=0;
00468     if (bitOrder==LSBFIRST) {
00469         for (i=0;i<=7;i++) {
00470             //dataPin=value&(1>>i);
00471             dataPin=value&(1);
00472             value=value>>1;
00473             clockPin=1;
00474             //->wait(0.0001);
00475             clockPin=0;
00476         }
00477     } else {
00478         for (i=7;i>=0;i--) {
00479             //dataPin=value&(1<<i);
00480             dataPin=value&(1<<7);
00481             value=value<<1;
00482             clockPin=1;
00483             //->wait(0.0001);
00484             clockPin=0;
00485         }
00486     }
00487 
00488 }
00489