/ lightcontrollegacy

Description: Hackers Delight Weekender light display

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "string.h"
00004 //#include "RPCFunction.h"
00005 #include "font.h"
00006 
00007 //#include "EthernetNetIf.h"
00008 //#include "HTTPServer.h"
00009 
00010 
00011 
00012 #define cell_height 5
00013 #define cell_width 4
00014 #define numberofcolumns 16
00015 
00016 #define numberofchars 10
00017 
00018 #define string1length 28
00019 #define string2length 36
00020 
00021 #define maxtextlength 160
00022 
00023 #define numberofbuffercolumns maxtextlength * (cell_width + 1)
00024 #define bufferendspace 16
00025 
00026 #define maxdisplayx 10
00027 #define maxdisplayy 4
00028 
00029 
00030 DigitalOut myLed1(LED1);
00031 DigitalOut myLed2(LED2);
00032 DigitalOut signal1(p8);
00033 DigitalOut signal2(p10);
00034 Serial pc(USBTX, USBRX); // tx, rx
00035 
00036 
00037 typedef int cellarray[numberofcolumns][cell_height];
00038 typedef int bufferarray[numberofbuffercolumns + bufferendspace][cell_height];
00039 
00040 
00041 bufferarray bitmap;
00042 cellarray displaybitmap;
00043     
00044 int fgcolour = 1;
00045 int bgcolour = 0;    
00046 
00047 const int bitsPerBulb = 26;
00048 const int numberOfBulbs = 56;
00049 int ten = 10; // Ten microseconds
00050 int twenty = 20; // Twenty microseconds
00051 int coordTable[55] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33 , 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54};
00052 int bulbBitStream[bitsPerBulb * numberOfBulbs];
00053 
00054 
00055 void create_bitmap(bufferarray bitmap, char* displaystring, int colour);
00056 void text_update(char * input, char * output);
00057 void fgcol_update(char * input, char * output);
00058 void bgcol_update(char * input, char * output);
00059 
00060 void scroll(void);
00061 
00062 void create_string(cellarray bitmap, int startbulb, int length, int stringarray[]);
00063 
00064 void setBulbValue(int bulbNum, int red, int green, int blue);
00065 void updateString(void);
00066 void setPixel(int xCoord, int yCoord, int red, int green, int blue);
00067 
00068 /*
00069 RPCFunction rpc_text_update(&text_update, "text");
00070 RPCFunction rpc_fgcol_update(&fgcol_update, "fgcolour");
00071 RPCFunction rpc_bgcol_update(&bgcol_update, "bgcolour");
00072 */
00073 char* displaytext;
00074 //EthernetNetIf eth;  
00075 //HTTPServer svr;
00076 
00077 int offset = 0;
00078 bool updatetextrequired = false;
00079 
00080 #define maxoffset numberofbuffercolumns * cell_height
00081 
00082 Ticker scroll_ticker;
00083 
00084 int main(void)
00085 {
00086      
00087     displaytext = (char*)malloc(maxtextlength * sizeof(char));
00088 /*
00089     printf("Setting up Ethernet...\n");
00090     EthernetErr ethErr = eth.setup();
00091     if(ethErr)
00092     {
00093         printf("Error %d in setup.\n", ethErr);
00094         return -1;
00095     }
00096     printf("Ethernet setup OK\n");
00097 
00098     FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
00099     svr.addHandler<FSHandler>("/"); //Default handler
00100 
00101     svr.addHandler<RPCHandler>("/rpc");
00102     svr.bind(80);
00103 */
00104     //enumerate String 1 of bulb bitstream array.
00105     for (int bulb = 0 ; bulb < numberOfBulbs ; bulb++) {
00106         setBulbValue(bulb, 0, 0, 0); // all WHITE
00107     }
00108     updateString();
00109 
00110 /*    while(true)
00111     {
00112         Net::poll();
00113     }
00114 
00115     while (true) {
00116         for (int y = 0 ; y < 5 ; y++) {
00117             for  (int x = 0 ; x < 11 ; x++) {
00118             
00119                 Net::poll();
00120                 setPixel (x, y, 13, 0, 0);
00121                 updateString();
00122                 
00123                 Net::poll();
00124                 setPixel (x, y, 0, 0, 13);
00125                 updateString();
00126             }
00127         }
00128     }*/
00129 
00130     strcpy(displaytext, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
00131    
00132     create_bitmap(bitmap, displaytext, 1); 
00133     updatetextrequired = false;
00134    
00135     scroll_ticker.attach(&scroll, 0.07);
00136     
00137     while(1)
00138     {
00139         //Net::poll();
00140         if(offset == 0 && updatetextrequired == true)
00141         {
00142             create_bitmap(bitmap, displaytext, 1); 
00143             updatetextrequired = false;
00144         }
00145     }    
00146 
00147     return(0);
00148 }
00149 
00150 void scroll(void)
00151 {
00152     
00153     int red;
00154     int green;
00155     int blue;
00156  
00157 //    printf("Scroll function offset %d (maxoffset = %d)\r\n", offset, maxoffset);
00158 
00159 
00160     for(int y = 0; y<maxdisplayy+1; y++)
00161     {
00162         for(int x =0; x<maxdisplayx+1; x++)
00163         {   
00164             int pixelcolour;
00165             pixelcolour = bitmap[x+offset][y];
00166 //            printf("%d",pixelcolour);
00167             if (pixelcolour == 1) {pixelcolour = 13;} else {pixelcolour = 0;}
00168             setPixel(x,y,pixelcolour,pixelcolour,pixelcolour);
00169             
00170         }   
00171 //        printf("\r\n");
00172     }
00173 
00174 
00175 
00176 
00177     updateString();
00178 
00179     offset++;
00180     if(offset >= 40)
00181     {
00182         offset = 0;
00183     }
00184 }
00185 
00186 
00187 void text_update(char * input, char * output)
00188 {
00189     int inputtextlength;
00190     int displaytextlength;
00191     
00192     displaytextlength = maxtextlength;
00193     inputtextlength = sizeof(input);
00194     if (inputtextlength < maxtextlength)
00195     {
00196         displaytextlength = inputtextlength;
00197     }
00198     
00199     strncpy(displaytext, input, displaytextlength);
00200     
00201     updatetextrequired = true;
00202     
00203 }
00204 
00205 
00206 void fgcol_update(char * input, char * output)
00207 {
00208     fgcolour = atoi(input);
00209     updatetextrequired = true;
00210 }
00211 
00212 void bgcol_update(char * input, char * output)
00213 {
00214     bgcolour = atoi(input);
00215     updatetextrequired = true;
00216 }
00217 
00218 void create_string(bufferarray bitmap, int startbulb, int length, int stringarray[])
00219 {
00220     int *buff;
00221     
00222     buff = (int*)bitmap;
00223     
00224     for (int string_iter = 0; string_iter < length; string_iter++)
00225     {
00226      //   stringarray[string_iter - startbulb] = bitmap[string_iter];
00227     }
00228 }
00229 
00230 void create_bitmap(bufferarray bitmap, char* displaystring, int colour)
00231 {
00232     int displaystringlen;
00233     
00234     printf("create_bitmap %s\r\n", displaystring);
00235     
00236     displaystringlen = strlen(displaystring);
00237     
00238     
00239     if(displaystringlen > numberofchars) {displaystringlen = numberofchars;}
00240     
00241     for(int displaychar = 0; displaychar < numberofchars; displaychar++ )
00242     {
00243         int fontref = 26; //default to space
00244         unsigned char asciichar;
00245         
00246         
00247         
00248         asciichar = (unsigned char)(displaystring[displaychar]);
00249         
00250         
00251         if (asciichar == 32) {fontref = 26;} //convert ascii space to font
00252         if (asciichar > 64 && asciichar < 91){fontref = asciichar - 65;}// convert uppercase to font
00253         if (asciichar > 96 && asciichar < 123){fontref = asciichar - 97;}// convert lowercase to font (hacked to upper)
00254     
00255         printf("char %c, fontref %d\r\n", asciichar, fontref);
00256         
00257             unsigned char mask = 0x10; //MAGIC NUMBER, x=0 == top
00258         for (int columnoffset = 0; columnoffset < cell_width-1; columnoffset++)
00259         {
00260             
00261             
00262             for (int row = 0; row < cell_height; row++)
00263             {
00264                 int bulbcontents=0;
00265                 
00266                 printf("Fontref %x, masked %x (mask %x)\r\n",FONT[fontref][row],(FONT[fontref][row] & mask), mask);
00267                 
00268                 if ((FONT[fontref][row] & mask) !=0) {bulbcontents = fgcolour;} else {bulbcontents = bgcolour;}
00269             
00270                 bitmap[displaychar * (cell_width + 1) + columnoffset][row] = bulbcontents; //cell_width + 1 for 1 col space between chars             
00271                 
00272             }
00273                 mask = mask >> 1;   
00274         }
00275         
00276     }
00277         for(int y=0; y<cell_height; y++)
00278         {
00279             for(int x=displaychar * 5; x<100; x++)
00280             {
00281                 printf("%d ", bitmap[x][y]);
00282             }
00283             printf("\r\n");
00284         }
00285 }
00286 
00287 void setPixel(int xCoord, int yCoord, int red, int green, int blue) {
00288     setBulbValue(coordTable[(yCoord * 11) + xCoord], red, green, blue);
00289 }
00290 
00291 
00292 void setBulbValue(int bulbNum, int red, int green, int blue) {
00293 
00294     int intensity = 204;
00295 
00296     int bulbBitstreamStart = (bulbNum * bitsPerBulb); // So this seems right.
00297 
00298     // Bulb number
00299     //pc.printf("Bulb %d ", bulbNum);
00300     for (int loop = 5 ; loop >= 0 ; loop--) {
00301 
00302         if ((bulbNum % 2) > 0) {
00303             bulbBitStream[bulbBitstreamStart + loop] = 1;
00304             //pc.printf("1");
00305         } else {
00306             bulbBitStream[bulbBitstreamStart + loop] = 0;
00307             // pc.printf("0");
00308         }
00309         bulbNum = bulbNum / 2;
00310     }
00311     // Intensity
00312     //pc.printf(" Intensity %d ", intensity);
00313     for (int loop = 13 ; loop >= 6 ; loop--) {
00314 
00315         if ((intensity % 2) > 0) {
00316             bulbBitStream[bulbBitstreamStart + loop] = 1;
00317             //pc.printf("1");
00318         } else {
00319             bulbBitStream[bulbBitstreamStart + loop] = 0;
00320             //pc.printf("0");
00321         }
00322         intensity = intensity / 2;
00323     }
00324     // Blue
00325     //pc.printf(" Blue %d ", blue);
00326     for (int loop = 17 ; loop >= 14 ; loop--) {
00327 
00328         if ((blue % 2) > 0) {
00329             bulbBitStream[bulbBitstreamStart + loop] = 1;
00330             //pc.printf("1");
00331         } else {
00332             bulbBitStream[bulbBitstreamStart + loop] = 0;
00333             //pc.printf("0");
00334         }
00335         blue = blue / 2;
00336 
00337     }
00338     // Green
00339     //pc.printf(" Green %d ", green);
00340     for (int loop = 21 ; loop >= 18 ; loop--) {
00341 
00342         if ((green % 2) > 0) {
00343             bulbBitStream[bulbBitstreamStart + loop] = 1;
00344             //pc.printf("1");
00345         } else {
00346             bulbBitStream[bulbBitstreamStart + loop] = 0;
00347             //pc.printf("0");
00348         }
00349         green = green / 2;
00350     }
00351     // Red
00352     //pc.printf(" Red %d ", red);
00353     for (int loop = 25 ; loop >= 22 ; loop--) {
00354 
00355         if ((red % 2) > 0) {
00356             bulbBitStream[bulbBitstreamStart + loop] = 1;
00357             //pc.printf("1");
00358         } else {
00359             bulbBitStream[bulbBitstreamStart + loop] = 0;
00360             //pc.printf("0");
00361         }
00362         red = red / 2;
00363     }
00364 }
00365 
00366 void updateString(void) {
00367     // set bus state low for 60 microseconds to make sure we're reset.
00368     signal1 = 0;
00369     wait_us(twenty * 3);
00370 
00371     for (int x = 0; x < numberOfBulbs ; x++) //Iterate for as many bulbs are in the string
00372     {// x
00373         //Start Bit: High for 10microSeconds
00374         signal1 = 1;
00375         wait_us(ten);
00376         signal1 = 0;
00377 
00378         for (int i = 0; i < bitsPerBulb; i++) { // i
00379             if (bulbBitStream[i + (x * bitsPerBulb)] == 0) {
00380                 signal1 = 0;
00381                 wait_us(ten);
00382                 signal1 = 1;
00383                 wait_us(twenty);
00384                 signal1 = 0;
00385             } else {
00386                 signal1 = 0;
00387                 wait_us(twenty);
00388                 signal1 = 1;
00389                 wait_us(ten);
00390                 signal1 = 0;
00391             }
00392         } // i
00393         wait_us(twenty*2); // Wait AT LEAST 30 microseconds to end frame, so 40 in this case.
00394     }// x
00395 
00396 
00397 } // end updateString ()