Wakeup Light with touch user interface, anti-aliased Font, SD card access and RTC usage on STM32F746NG-DISCO board
Dependencies: BSP_DISCO_F746NG_patch_fixed LCD_DISCO_F746NG TS_DISCO_F746NG FATFileSystem TinyJpgDec_interwork mbed-src
SD.cpp
00001 #include "WakeupLight.h" 00002 00003 class mySD:public FATFileSystem 00004 { 00005 protected: 00006 HAL_SD_CardInfoTypedef CardInfo; 00007 00008 public: 00009 mySD(const char* name):FATFileSystem(name) 00010 { 00011 uint8_t result=BSP_SD_Init(); 00012 DPrintf_("BSP_SD_Init: %u.\r\n",result); 00013 BSP_SD_GetCardInfo(&CardInfo); 00014 DPrintf("BSP_SD_GetCardInfo: 0x%llX bytes / %u sector size.\r\n",CardInfo.CardCapacity,CardInfo.CardBlockSize); 00015 }; 00016 virtual int disk_initialize() { return 0; } 00017 virtual int disk_status() { return 0; } 00018 virtual int disk_read(uint8_t * buffer, uint64_t sector, uint8_t count) 00019 { 00020 DPrintf_("disk_read: %llu / %u.\r\n",sector,count); 00021 return BSP_SD_ReadBlocks((uint32_t *)buffer,sector*CardInfo.CardBlockSize,CardInfo.CardBlockSize,count); 00022 }; 00023 virtual int disk_write(const uint8_t * buffer, uint64_t sector, uint8_t count) 00024 { 00025 DPrintf_("disk_write: %llu / %u.\r\n",sector,count); 00026 return BSP_SD_WriteBlocks((uint32_t *)buffer,sector*CardInfo.CardBlockSize,CardInfo.CardBlockSize,count); 00027 }; 00028 virtual int disk_sync() { return 0; } 00029 virtual uint64_t disk_sectors() 00030 { 00031 return CardInfo.CardCapacity/CardInfo.CardBlockSize; 00032 }; 00033 }; 00034 00035 mySD sd("sd"); 00036 FileHandle *fileHandle; 00037 uint8_t buffer[6000]; 00038 int16_t offsetX; 00039 int16_t offsetY; 00040 00041 void SD_Init(void) 00042 { 00043 } 00044 00045 JPG_UINT jpeg_input_func(JDEC *jd, BYTE *buff, JPG_UINT ndata) 00046 { 00047 if(buff) 00048 { 00049 size_t n = fileHandle->read(buff,ndata); 00050 return n == (size_t)-1 ? 0 : n; 00051 } 00052 else 00053 { 00054 off_t t = fileHandle->lseek( ndata, SEEK_CUR); 00055 return t == (off_t)-1 ? 0 : ndata; 00056 } 00057 } 00058 00059 JPG_UINT jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect) 00060 { 00061 int x0=rect->left+offsetX; 00062 int x1=rect->right+offsetX; 00063 int y0=rect->top+offsetY; 00064 int y1=rect->bottom+offsetY; 00065 int w=x1-x0+1; 00066 00067 DPrintf_("jpeg_output_func: %ux%u / %ux%u\r\n",x0,y0,x1,y1); 00068 00069 if ((y0>=uiLcd.GetYSize()) || (x0>=uiLcd.GetXSize())) 00070 return 1; 00071 00072 if (x1>uiLcd.GetXSize()-1) 00073 x1=uiLcd.GetXSize()-1; 00074 if (y1>uiLcd.GetYSize()-1) 00075 y1=uiLcd.GetYSize()-1; 00076 00077 for (int y=y0;y<=y1;y++) 00078 { 00079 uint8_t *p=((uint8_t *)bitmap)+((w*(y-y0))*3); 00080 00081 for (int x=x0;x<=x1;x++) 00082 { 00083 if ((x>=0) && (y>=0)) 00084 uiLcd.DrawPixel(x,y,(0xFF000000 | p[0]<<16 | (p[1]<<8) | (p[2]) )); 00085 00086 p+=3; 00087 } 00088 } 00089 00090 return 1; 00091 } 00092 00093 bool SD_ShowRandomPicture(void) 00094 { 00095 uint32_t count; 00096 DIR *dir; 00097 struct dirent *dirEnt; 00098 char *extension; 00099 char file[100]; 00100 JRESULT jResult; 00101 JDEC jdec; 00102 bool result; 00103 BYTE scale; 00104 JPG_UINT pictureWidth; 00105 JPG_UINT pictureHeight; 00106 00107 // 00108 // count all jpegs 00109 // 00110 count=0; 00111 dir=opendir("/sd/"); 00112 while ((dirEnt=readdir(dir))!=NULL) 00113 { 00114 extension=strrchr(dirEnt->d_name,'.'); 00115 if ((extension==NULL) || (strcmp(extension,".jpg"))!=0) 00116 continue; 00117 00118 count++; 00119 00120 DPrintf_("SD_ShowRandomPicture: Count %s.\r\n",dirEnt->d_name); 00121 } 00122 closedir(dir); 00123 00124 DPrintf_("SD_ShowRandomPicture: Count %u.\r\n",count); 00125 00126 // 00127 // get random number 00128 // 00129 count=(uint32_t)((uint64_t)TM_RNG_Get()*(uint64_t)count/0xFFFFFFFF); 00130 DPrintf_("SD_ShowRandomPicture: Take %u.\r\n",count); 00131 00132 // 00133 // find random jpeg 00134 // 00135 file[0]='\0'; 00136 dir=opendir("/sd/"); 00137 while ((dirEnt=readdir(dir))!=NULL) 00138 { 00139 extension=strrchr(dirEnt->d_name,'.'); 00140 if ((extension==NULL) || (strcmp(extension,".jpg"))!=0) 00141 continue; 00142 00143 if (count==0) 00144 { 00145 snprintf(file,sizeof(file),"%s",dirEnt->d_name); 00146 break; 00147 } 00148 00149 count--; 00150 } 00151 closedir(dir); 00152 00153 if (file[0]=='\0') 00154 return false; 00155 00156 // 00157 // load random jpeg 00158 // 00159 DPrintf_("SD_ShowRandomPicture: Open %s.\r\n",file); 00160 if ((fileHandle=sd.open(file,O_RDONLY))==NULL) 00161 return false; 00162 00163 result=false; 00164 00165 jResult=jd_prepare(&jdec,jpeg_input_func,buffer,sizeof(buffer),NULL); 00166 if (jResult==JDR_OK) 00167 { 00168 pictureWidth=jdec.width; 00169 pictureHeight=jdec.height; 00170 scale=0; 00171 00172 DPrintf("SD_ShowRandomPicture: Picture %ux%u, LCD: %ux%u.\r\n",pictureWidth,pictureHeight,uiLcd.GetXSize(),uiLcd.GetYSize()); 00173 00174 while ((pictureWidth>uiLcd.GetXSize()) || (pictureHeight>uiLcd.GetYSize())) 00175 { 00176 pictureWidth/=2; 00177 pictureHeight/=2; 00178 scale++; 00179 00180 if (scale>=3) 00181 break; 00182 } 00183 offsetX=(uiLcd.GetXSize()-pictureWidth)/2; 00184 offsetY=(uiLcd.GetYSize()-pictureHeight)/2; 00185 00186 DPrintf("SD_ShowRandomPicture: Scale %u.\r\n",scale); 00187 00188 jResult=jd_decomp(&jdec,jpeg_output_func,scale); 00189 if (jResult==JDR_OK) 00190 result=true; 00191 else 00192 DPrintf("SD_ShowRandomPicture: jd_decomp: %u on %s\r\n",jResult,file); 00193 } 00194 else 00195 DPrintf("SD_ShowRandomPicture: jd_prepare %u on %s\r\n",jResult,file); 00196 00197 fileHandle->close(); 00198 00199 return result; 00200 }
Generated on Tue Jul 12 2022 21:40:04 by 1.7.2