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

Committer:
the_sz
Date:
Sun Feb 21 04:26:17 2016 +0000
Revision:
14:2044ad5cd3fe
Parent:
12:a89096944f20
all fonts are working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
the_sz 9:fe2c9b3a312b 1 #include "WakeupLight.h"
the_sz 9:fe2c9b3a312b 2
the_sz 9:fe2c9b3a312b 3 class mySD:public FATFileSystem
the_sz 9:fe2c9b3a312b 4 {
the_sz 9:fe2c9b3a312b 5 protected:
the_sz 9:fe2c9b3a312b 6 HAL_SD_CardInfoTypedef CardInfo;
the_sz 9:fe2c9b3a312b 7
the_sz 9:fe2c9b3a312b 8 public:
the_sz 9:fe2c9b3a312b 9 mySD(const char* name):FATFileSystem(name)
the_sz 9:fe2c9b3a312b 10 {
the_sz 9:fe2c9b3a312b 11 uint8_t result=BSP_SD_Init();
the_sz 12:a89096944f20 12 DPrintf_("BSP_SD_Init: %u.\r\n",result);
the_sz 9:fe2c9b3a312b 13 BSP_SD_GetCardInfo(&CardInfo);
the_sz 9:fe2c9b3a312b 14 DPrintf("BSP_SD_GetCardInfo: 0x%llX bytes / %u sector size.\r\n",CardInfo.CardCapacity,CardInfo.CardBlockSize);
the_sz 9:fe2c9b3a312b 15 };
the_sz 9:fe2c9b3a312b 16 virtual int disk_initialize() { return 0; }
the_sz 9:fe2c9b3a312b 17 virtual int disk_status() { return 0; }
the_sz 9:fe2c9b3a312b 18 virtual int disk_read(uint8_t * buffer, uint64_t sector, uint8_t count)
the_sz 9:fe2c9b3a312b 19 {
the_sz 9:fe2c9b3a312b 20 DPrintf_("disk_read: %llu / %u.\r\n",sector,count);
the_sz 9:fe2c9b3a312b 21 return BSP_SD_ReadBlocks((uint32_t *)buffer,sector*CardInfo.CardBlockSize,CardInfo.CardBlockSize,count);
the_sz 9:fe2c9b3a312b 22 };
the_sz 9:fe2c9b3a312b 23 virtual int disk_write(const uint8_t * buffer, uint64_t sector, uint8_t count)
the_sz 9:fe2c9b3a312b 24 {
the_sz 9:fe2c9b3a312b 25 DPrintf_("disk_write: %llu / %u.\r\n",sector,count);
the_sz 9:fe2c9b3a312b 26 return BSP_SD_WriteBlocks((uint32_t *)buffer,sector*CardInfo.CardBlockSize,CardInfo.CardBlockSize,count);
the_sz 9:fe2c9b3a312b 27 };
the_sz 9:fe2c9b3a312b 28 virtual int disk_sync() { return 0; }
the_sz 9:fe2c9b3a312b 29 virtual uint64_t disk_sectors()
the_sz 9:fe2c9b3a312b 30 {
the_sz 9:fe2c9b3a312b 31 return CardInfo.CardCapacity/CardInfo.CardBlockSize;
the_sz 9:fe2c9b3a312b 32 };
the_sz 9:fe2c9b3a312b 33 };
the_sz 9:fe2c9b3a312b 34
the_sz 9:fe2c9b3a312b 35 mySD sd("sd");
the_sz 9:fe2c9b3a312b 36 FileHandle *fileHandle;
the_sz 9:fe2c9b3a312b 37 uint8_t buffer[6000];
the_sz 9:fe2c9b3a312b 38 int16_t offsetX;
the_sz 9:fe2c9b3a312b 39 int16_t offsetY;
the_sz 9:fe2c9b3a312b 40
the_sz 9:fe2c9b3a312b 41 void SD_Init(void)
the_sz 9:fe2c9b3a312b 42 {
the_sz 9:fe2c9b3a312b 43 }
the_sz 9:fe2c9b3a312b 44
the_sz 9:fe2c9b3a312b 45 JPG_UINT jpeg_input_func(JDEC *jd, BYTE *buff, JPG_UINT ndata)
the_sz 9:fe2c9b3a312b 46 {
the_sz 9:fe2c9b3a312b 47 if(buff)
the_sz 9:fe2c9b3a312b 48 {
the_sz 9:fe2c9b3a312b 49 size_t n = fileHandle->read(buff,ndata);
the_sz 9:fe2c9b3a312b 50 return n == (size_t)-1 ? 0 : n;
the_sz 9:fe2c9b3a312b 51 }
the_sz 9:fe2c9b3a312b 52 else
the_sz 9:fe2c9b3a312b 53 {
the_sz 9:fe2c9b3a312b 54 off_t t = fileHandle->lseek( ndata, SEEK_CUR);
the_sz 9:fe2c9b3a312b 55 return t == (off_t)-1 ? 0 : ndata;
the_sz 9:fe2c9b3a312b 56 }
the_sz 9:fe2c9b3a312b 57 }
the_sz 9:fe2c9b3a312b 58
the_sz 9:fe2c9b3a312b 59 JPG_UINT jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect)
the_sz 9:fe2c9b3a312b 60 {
the_sz 9:fe2c9b3a312b 61 int x0=rect->left+offsetX;
the_sz 9:fe2c9b3a312b 62 int x1=rect->right+offsetX;
the_sz 9:fe2c9b3a312b 63 int y0=rect->top+offsetY;
the_sz 9:fe2c9b3a312b 64 int y1=rect->bottom+offsetY;
the_sz 9:fe2c9b3a312b 65 int w=x1-x0+1;
the_sz 9:fe2c9b3a312b 66
the_sz 9:fe2c9b3a312b 67 DPrintf_("jpeg_output_func: %ux%u / %ux%u\r\n",x0,y0,x1,y1);
the_sz 9:fe2c9b3a312b 68
the_sz 9:fe2c9b3a312b 69 if ((y0>=uiLcd.GetYSize()) || (x0>=uiLcd.GetXSize()))
the_sz 9:fe2c9b3a312b 70 return 1;
the_sz 9:fe2c9b3a312b 71
the_sz 9:fe2c9b3a312b 72 if (x1>uiLcd.GetXSize()-1)
the_sz 9:fe2c9b3a312b 73 x1=uiLcd.GetXSize()-1;
the_sz 9:fe2c9b3a312b 74 if (y1>uiLcd.GetYSize()-1)
the_sz 9:fe2c9b3a312b 75 y1=uiLcd.GetYSize()-1;
the_sz 9:fe2c9b3a312b 76
the_sz 9:fe2c9b3a312b 77 for (int y=y0;y<=y1;y++)
the_sz 9:fe2c9b3a312b 78 {
the_sz 9:fe2c9b3a312b 79 uint8_t *p=((uint8_t *)bitmap)+((w*(y-y0))*3);
the_sz 9:fe2c9b3a312b 80
the_sz 9:fe2c9b3a312b 81 for (int x=x0;x<=x1;x++)
the_sz 9:fe2c9b3a312b 82 {
the_sz 9:fe2c9b3a312b 83 if ((x>=0) && (y>=0))
the_sz 9:fe2c9b3a312b 84 uiLcd.DrawPixel(x,y,(0xFF000000 | p[0]<<16 | (p[1]<<8) | (p[2]) ));
the_sz 9:fe2c9b3a312b 85
the_sz 9:fe2c9b3a312b 86 p+=3;
the_sz 9:fe2c9b3a312b 87 }
the_sz 9:fe2c9b3a312b 88 }
the_sz 9:fe2c9b3a312b 89
the_sz 9:fe2c9b3a312b 90 return 1;
the_sz 9:fe2c9b3a312b 91 }
the_sz 9:fe2c9b3a312b 92
the_sz 9:fe2c9b3a312b 93 bool SD_ShowRandomPicture(void)
the_sz 9:fe2c9b3a312b 94 {
the_sz 9:fe2c9b3a312b 95 uint32_t count;
the_sz 9:fe2c9b3a312b 96 DIR *dir;
the_sz 9:fe2c9b3a312b 97 struct dirent *dirEnt;
the_sz 9:fe2c9b3a312b 98 char *extension;
the_sz 9:fe2c9b3a312b 99 char file[100];
the_sz 9:fe2c9b3a312b 100 JRESULT jResult;
the_sz 9:fe2c9b3a312b 101 JDEC jdec;
the_sz 9:fe2c9b3a312b 102 bool result;
the_sz 9:fe2c9b3a312b 103 BYTE scale;
the_sz 9:fe2c9b3a312b 104 JPG_UINT pictureWidth;
the_sz 9:fe2c9b3a312b 105 JPG_UINT pictureHeight;
the_sz 9:fe2c9b3a312b 106
the_sz 9:fe2c9b3a312b 107 //
the_sz 9:fe2c9b3a312b 108 // count all jpegs
the_sz 9:fe2c9b3a312b 109 //
the_sz 9:fe2c9b3a312b 110 count=0;
the_sz 9:fe2c9b3a312b 111 dir=opendir("/sd/");
the_sz 9:fe2c9b3a312b 112 while ((dirEnt=readdir(dir))!=NULL)
the_sz 9:fe2c9b3a312b 113 {
the_sz 9:fe2c9b3a312b 114 extension=strrchr(dirEnt->d_name,'.');
the_sz 9:fe2c9b3a312b 115 if ((extension==NULL) || (strcmp(extension,".jpg"))!=0)
the_sz 9:fe2c9b3a312b 116 continue;
the_sz 9:fe2c9b3a312b 117
the_sz 9:fe2c9b3a312b 118 count++;
the_sz 9:fe2c9b3a312b 119
the_sz 9:fe2c9b3a312b 120 DPrintf_("SD_ShowRandomPicture: Count %s.\r\n",dirEnt->d_name);
the_sz 9:fe2c9b3a312b 121 }
the_sz 9:fe2c9b3a312b 122 closedir(dir);
the_sz 9:fe2c9b3a312b 123
the_sz 9:fe2c9b3a312b 124 DPrintf_("SD_ShowRandomPicture: Count %u.\r\n",count);
the_sz 9:fe2c9b3a312b 125
the_sz 9:fe2c9b3a312b 126 //
the_sz 9:fe2c9b3a312b 127 // get random number
the_sz 9:fe2c9b3a312b 128 //
the_sz 12:a89096944f20 129 count=(uint32_t)((uint64_t)TM_RNG_Get()*(uint64_t)count/0xFFFFFFFF);
the_sz 12:a89096944f20 130 DPrintf_("SD_ShowRandomPicture: Take %u.\r\n",count);
the_sz 9:fe2c9b3a312b 131
the_sz 9:fe2c9b3a312b 132 //
the_sz 9:fe2c9b3a312b 133 // find random jpeg
the_sz 9:fe2c9b3a312b 134 //
the_sz 9:fe2c9b3a312b 135 file[0]='\0';
the_sz 9:fe2c9b3a312b 136 dir=opendir("/sd/");
the_sz 9:fe2c9b3a312b 137 while ((dirEnt=readdir(dir))!=NULL)
the_sz 9:fe2c9b3a312b 138 {
the_sz 9:fe2c9b3a312b 139 extension=strrchr(dirEnt->d_name,'.');
the_sz 9:fe2c9b3a312b 140 if ((extension==NULL) || (strcmp(extension,".jpg"))!=0)
the_sz 9:fe2c9b3a312b 141 continue;
the_sz 9:fe2c9b3a312b 142
the_sz 9:fe2c9b3a312b 143 if (count==0)
the_sz 9:fe2c9b3a312b 144 {
the_sz 9:fe2c9b3a312b 145 snprintf(file,sizeof(file),"%s",dirEnt->d_name);
the_sz 9:fe2c9b3a312b 146 break;
the_sz 9:fe2c9b3a312b 147 }
the_sz 9:fe2c9b3a312b 148
the_sz 9:fe2c9b3a312b 149 count--;
the_sz 9:fe2c9b3a312b 150 }
the_sz 9:fe2c9b3a312b 151 closedir(dir);
the_sz 9:fe2c9b3a312b 152
the_sz 9:fe2c9b3a312b 153 if (file[0]=='\0')
the_sz 9:fe2c9b3a312b 154 return false;
the_sz 9:fe2c9b3a312b 155
the_sz 9:fe2c9b3a312b 156 //
the_sz 9:fe2c9b3a312b 157 // load random jpeg
the_sz 9:fe2c9b3a312b 158 //
the_sz 12:a89096944f20 159 DPrintf_("SD_ShowRandomPicture: Open %s.\r\n",file);
the_sz 9:fe2c9b3a312b 160 if ((fileHandle=sd.open(file,O_RDONLY))==NULL)
the_sz 9:fe2c9b3a312b 161 return false;
the_sz 9:fe2c9b3a312b 162
the_sz 9:fe2c9b3a312b 163 result=false;
the_sz 9:fe2c9b3a312b 164
the_sz 9:fe2c9b3a312b 165 jResult=jd_prepare(&jdec,jpeg_input_func,buffer,sizeof(buffer),NULL);
the_sz 9:fe2c9b3a312b 166 if (jResult==JDR_OK)
the_sz 9:fe2c9b3a312b 167 {
the_sz 9:fe2c9b3a312b 168 pictureWidth=jdec.width;
the_sz 9:fe2c9b3a312b 169 pictureHeight=jdec.height;
the_sz 9:fe2c9b3a312b 170 scale=0;
the_sz 12:a89096944f20 171
the_sz 12:a89096944f20 172 DPrintf("SD_ShowRandomPicture: Picture %ux%u, LCD: %ux%u.\r\n",pictureWidth,pictureHeight,uiLcd.GetXSize(),uiLcd.GetYSize());
the_sz 12:a89096944f20 173
the_sz 9:fe2c9b3a312b 174 while ((pictureWidth>uiLcd.GetXSize()) || (pictureHeight>uiLcd.GetYSize()))
the_sz 9:fe2c9b3a312b 175 {
the_sz 9:fe2c9b3a312b 176 pictureWidth/=2;
the_sz 9:fe2c9b3a312b 177 pictureHeight/=2;
the_sz 9:fe2c9b3a312b 178 scale++;
the_sz 9:fe2c9b3a312b 179
the_sz 9:fe2c9b3a312b 180 if (scale>=3)
the_sz 9:fe2c9b3a312b 181 break;
the_sz 9:fe2c9b3a312b 182 }
the_sz 9:fe2c9b3a312b 183 offsetX=(uiLcd.GetXSize()-pictureWidth)/2;
the_sz 9:fe2c9b3a312b 184 offsetY=(uiLcd.GetYSize()-pictureHeight)/2;
the_sz 9:fe2c9b3a312b 185
the_sz 12:a89096944f20 186 DPrintf("SD_ShowRandomPicture: Scale %u.\r\n",scale);
the_sz 12:a89096944f20 187
the_sz 9:fe2c9b3a312b 188 jResult=jd_decomp(&jdec,jpeg_output_func,scale);
the_sz 9:fe2c9b3a312b 189 if (jResult==JDR_OK)
the_sz 9:fe2c9b3a312b 190 result=true;
the_sz 9:fe2c9b3a312b 191 else
the_sz 12:a89096944f20 192 DPrintf("SD_ShowRandomPicture: jd_decomp: %u on %s\r\n",jResult,file);
the_sz 9:fe2c9b3a312b 193 }
the_sz 9:fe2c9b3a312b 194 else
the_sz 12:a89096944f20 195 DPrintf("SD_ShowRandomPicture: jd_prepare %u on %s\r\n",jResult,file);
the_sz 9:fe2c9b3a312b 196
the_sz 9:fe2c9b3a312b 197 fileHandle->close();
the_sz 9:fe2c9b3a312b 198
the_sz 9:fe2c9b3a312b 199 return result;
the_sz 9:fe2c9b3a312b 200 }