EA EPAPER

Dependencies:   BurstSPI EaEpaper SDFileSystem frdm_tsi_slider mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EaEpaper.h"
00003 #include "SDFileSystem.h"
00004 #include "tsi_sensor.h"
00005 #include "img0.h"
00006 
00007 #include <string>
00008 #include <vector>
00009 
00010 /* This defines will be replaced by PinNames soon */
00011 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
00012   #define ELEC0 9
00013   #define ELEC1 10
00014 #elif defined (TARGET_KL05Z)
00015   #define ELEC0 9
00016   #define ELEC1 8
00017 #else
00018   #error TARGET NOT DEFINED
00019 #endif
00020 
00021 EaEpaper epaper(PTD7,            // PWR_CTRL
00022                 PTD6,            // BORDER
00023                 PTE31,           // DISCHARGE
00024                 PTA17,           // RESET_DISP
00025                 PTA16,           // BUSY
00026                 PTC17,           // SSEL
00027                 PTD4,            // PWM
00028                 PTD2,PTD3,PTD1,  // MOSI,MISO,SCLK
00029 //                PTE0,PTE1);      // SDA,SDL 
00030                 PTC11,PTC10);      // SDA,SDL 
00031 
00032 //SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
00033 SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd" );
00034  
00035 //Serial pc(USBTX,USBRX);
00036 
00037 vector<string> filenames; //filenames are stored in a vector string
00038 
00039 extern uint8_t _newImage[EA_IMG_BUF_SZ];
00040 
00041 Bitmap bitmARM0 = {
00042   264, // XSize
00043   176, // YSize 
00044   33,  // Bytes in Line
00045   (char*)img0_bits // Pointer to picture data 
00046 };
00047 
00048 char img_bits[5808];
00049 Bitmap bitmIMG0 = {
00050   264, // XSize
00051   176, // YSize 
00052   33,  // Bytes in Line
00053   img_bits // Pointer to picture data 
00054 };
00055 
00056 void read_file_names(char *dir)
00057 {
00058     DIR *dp;
00059     struct dirent *dirp;
00060     dp = opendir(dir);
00061   //read all directory and file names in current directory into filename vector
00062     while((dirp = readdir(dp)) != NULL) {
00063         filenames.push_back(string(dirp->d_name));
00064     }
00065     closedir(dp);
00066 }
00067 
00068 int read_filedata(char* fname)
00069 {
00070     char buf[128];
00071     char sbuf[10];
00072     int line_idx, idx, len, i,j;
00073     uint8_t val;
00074     
00075     int ret = 0;
00076     
00077 //    strcpy(path, "/sd");
00078 //    strcat(path, fname);
00079 
00080 //    pc.printf("PATH = %s\n", path);
00081 
00082     FILE *fp = fopen(fname, "r");
00083     if(fp == NULL)
00084     {
00085 //        pc.printf("Could not open file for write\n");
00086         ret = 1;
00087         return ret;
00088     }
00089     
00090     line_idx = 0; 
00091     idx = 0;
00092 
00093 //    while( fgets(buf, sizeof(buf), fp) != NULL )
00094     for(j=0;j<484;j++)
00095     {
00096         fgets(buf, sizeof(buf), fp);
00097         line_idx++;
00098         
00099  //       pc.printf("line=%d:%s", line_idx, buf);
00100         
00101         if(line_idx == 1)
00102         {
00103             if(strstr(buf,"264") == NULL )
00104             {
00105                 ret = 1;
00106                 break;
00107             }
00108         }
00109         if(line_idx == 2)
00110         {
00111             if(strstr(buf,"176") == NULL )
00112             {
00113                 ret = 1;
00114                 break;
00115             }
00116         }
00117         if(line_idx == 3)
00118         {
00119             continue;
00120         }
00121         
00122         len = strlen(buf);
00123         
00124         for(i=0;i<len;i++)
00125         {
00126             if(buf[i] == ',')
00127             {
00128                 strncpy(sbuf, &buf[i-4], 4);
00129                 /* 型変換 */
00130                 sscanf(sbuf, "%x", &val);
00131                 img_bits[idx] = val;
00132                 idx++;
00133 //                i=i+4;
00134             }
00135             else if(buf[i] == '}')
00136             {
00137                 strncpy(sbuf, &buf[i-5], 4);
00138                 /* 型変換 */
00139                 sscanf(sbuf, "%x", &val);
00140                 img_bits[idx] = val;
00141                 idx++;
00142             }
00143             else if(buf[i] == 0x0D)
00144             {
00145                 break;
00146             }
00147         }
00148     }
00149 
00150     fclose(fp); 
00151 
00152     return ret;
00153 }
00154  
00155 int main() 
00156 {
00157     char str1[17];
00158     int i;
00159 
00160 //    pc.printf("\nHello World!\n");
00161 
00162     TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
00163 
00164     epaper.cls();
00165  
00166     // read file names into vector of strings
00167     //read_file_names("/sd");
00168   
00169     while (true) 
00170     {
00171         float w;
00172         int cnt;
00173         int stop_flag = 0;
00174 
00175         for(i=0;i<1000;i++)
00176         {
00177             if(i==0)
00178             {
00179                 epaper.print_bm(bitmARM0, 1, 1);
00180                 epaper.write_disp(); // update screen
00181                 wait(0.1);
00182             }
00183             else
00184             {
00185                 sprintf(str1,"%d",i);   
00186                 char idir[14] = "/sd/";
00187                 char dir[100] = "";
00188                 strcat(dir,idir);
00189                 strcat(dir,"/");
00190                 strcat(dir,str1);
00191                 strcat(dir,".xbm");
00192 //                dir[4] = 0x30 + i;
00193 //                if(read_filedata("/sd/1.xbm")== 0)
00194                 if(read_filedata(dir)== 0)
00195                 {
00196                     epaper.print_bm(bitmIMG0, 1, 1);
00197                     epaper.write_disp(); // update screen
00198                 }
00199                 else
00200                 {
00201                     continue;
00202                 }       
00203                 cnt = 0;
00204                 while (true) 
00205                 {
00206                     w = tsi.readPercentage();
00207                     if(w > 0.8)
00208                     {
00209                         stop_flag = 1;
00210                         break;
00211                     }
00212                     else if(w > 0.3)
00213                     {
00214                         break;
00215                     }
00216                     cnt++;
00217                     if(cnt > 100)
00218                     {
00219                         break;
00220                     }
00221                     wait(0.1);
00222                 }
00223 //                epaper.cls();
00224                 if(stop_flag == 1)
00225                 {
00226                     break;
00227                 }
00228             }
00229         }            
00230     }
00231 }