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:
Sat Nov 14 02:43:33 2015 +0000
Revision:
9:fe2c9b3a312b
Child:
12:a89096944f20
slideshow 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 9:fe2c9b3a312b 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 bool mustSrand=true;
the_sz 9:fe2c9b3a312b 37 FileHandle *fileHandle;
the_sz 9:fe2c9b3a312b 38 uint8_t buffer[6000];
the_sz 9:fe2c9b3a312b 39 int16_t offsetX;
the_sz 9:fe2c9b3a312b 40 int16_t offsetY;
the_sz 9:fe2c9b3a312b 41
the_sz 9:fe2c9b3a312b 42 void SD_Init(void)
the_sz 9:fe2c9b3a312b 43 {
the_sz 9:fe2c9b3a312b 44 }
the_sz 9:fe2c9b3a312b 45
the_sz 9:fe2c9b3a312b 46 JPG_UINT jpeg_input_func(JDEC *jd, BYTE *buff, JPG_UINT ndata)
the_sz 9:fe2c9b3a312b 47 {
the_sz 9:fe2c9b3a312b 48 if(buff)
the_sz 9:fe2c9b3a312b 49 {
the_sz 9:fe2c9b3a312b 50 size_t n = fileHandle->read(buff,ndata);
the_sz 9:fe2c9b3a312b 51 return n == (size_t)-1 ? 0 : n;
the_sz 9:fe2c9b3a312b 52 }
the_sz 9:fe2c9b3a312b 53 else
the_sz 9:fe2c9b3a312b 54 {
the_sz 9:fe2c9b3a312b 55 off_t t = fileHandle->lseek( ndata, SEEK_CUR);
the_sz 9:fe2c9b3a312b 56 return t == (off_t)-1 ? 0 : ndata;
the_sz 9:fe2c9b3a312b 57 }
the_sz 9:fe2c9b3a312b 58 }
the_sz 9:fe2c9b3a312b 59
the_sz 9:fe2c9b3a312b 60 JPG_UINT jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect)
the_sz 9:fe2c9b3a312b 61 {
the_sz 9:fe2c9b3a312b 62 int x0=rect->left+offsetX;
the_sz 9:fe2c9b3a312b 63 int x1=rect->right+offsetX;
the_sz 9:fe2c9b3a312b 64 int y0=rect->top+offsetY;
the_sz 9:fe2c9b3a312b 65 int y1=rect->bottom+offsetY;
the_sz 9:fe2c9b3a312b 66 int w=x1-x0+1;
the_sz 9:fe2c9b3a312b 67
the_sz 9:fe2c9b3a312b 68 DPrintf_("jpeg_output_func: %ux%u / %ux%u\r\n",x0,y0,x1,y1);
the_sz 9:fe2c9b3a312b 69
the_sz 9:fe2c9b3a312b 70 if ((y0>=uiLcd.GetYSize()) || (x0>=uiLcd.GetXSize()))
the_sz 9:fe2c9b3a312b 71 return 1;
the_sz 9:fe2c9b3a312b 72
the_sz 9:fe2c9b3a312b 73 if (x1>uiLcd.GetXSize()-1)
the_sz 9:fe2c9b3a312b 74 x1=uiLcd.GetXSize()-1;
the_sz 9:fe2c9b3a312b 75 if (y1>uiLcd.GetYSize()-1)
the_sz 9:fe2c9b3a312b 76 y1=uiLcd.GetYSize()-1;
the_sz 9:fe2c9b3a312b 77
the_sz 9:fe2c9b3a312b 78 for (int y=y0;y<=y1;y++)
the_sz 9:fe2c9b3a312b 79 {
the_sz 9:fe2c9b3a312b 80 uint8_t *p=((uint8_t *)bitmap)+((w*(y-y0))*3);
the_sz 9:fe2c9b3a312b 81
the_sz 9:fe2c9b3a312b 82 for (int x=x0;x<=x1;x++)
the_sz 9:fe2c9b3a312b 83 {
the_sz 9:fe2c9b3a312b 84 if ((x>=0) && (y>=0))
the_sz 9:fe2c9b3a312b 85 uiLcd.DrawPixel(x,y,(0xFF000000 | p[0]<<16 | (p[1]<<8) | (p[2]) ));
the_sz 9:fe2c9b3a312b 86
the_sz 9:fe2c9b3a312b 87 p+=3;
the_sz 9:fe2c9b3a312b 88 }
the_sz 9:fe2c9b3a312b 89 }
the_sz 9:fe2c9b3a312b 90
the_sz 9:fe2c9b3a312b 91 return 1;
the_sz 9:fe2c9b3a312b 92 }
the_sz 9:fe2c9b3a312b 93
the_sz 9:fe2c9b3a312b 94 bool SD_ShowRandomPicture(void)
the_sz 9:fe2c9b3a312b 95 {
the_sz 9:fe2c9b3a312b 96 uint32_t count;
the_sz 9:fe2c9b3a312b 97 DIR *dir;
the_sz 9:fe2c9b3a312b 98 struct dirent *dirEnt;
the_sz 9:fe2c9b3a312b 99 char *extension;
the_sz 9:fe2c9b3a312b 100 char file[100];
the_sz 9:fe2c9b3a312b 101 JRESULT jResult;
the_sz 9:fe2c9b3a312b 102 JDEC jdec;
the_sz 9:fe2c9b3a312b 103 bool result;
the_sz 9:fe2c9b3a312b 104 BYTE scale;
the_sz 9:fe2c9b3a312b 105 JPG_UINT pictureWidth;
the_sz 9:fe2c9b3a312b 106 JPG_UINT pictureHeight;
the_sz 9:fe2c9b3a312b 107 AnalogIn analogIn(PA_0);
the_sz 9:fe2c9b3a312b 108
the_sz 9:fe2c9b3a312b 109 if (mustSrand==true)
the_sz 9:fe2c9b3a312b 110 {
the_sz 9:fe2c9b3a312b 111 srand(time(NULL)+analogIn.read_u16());
the_sz 9:fe2c9b3a312b 112 mustSrand=false;
the_sz 9:fe2c9b3a312b 113 }
the_sz 9:fe2c9b3a312b 114
the_sz 9:fe2c9b3a312b 115 //
the_sz 9:fe2c9b3a312b 116 // count all jpegs
the_sz 9:fe2c9b3a312b 117 //
the_sz 9:fe2c9b3a312b 118 count=0;
the_sz 9:fe2c9b3a312b 119 dir=opendir("/sd/");
the_sz 9:fe2c9b3a312b 120 while ((dirEnt=readdir(dir))!=NULL)
the_sz 9:fe2c9b3a312b 121 {
the_sz 9:fe2c9b3a312b 122 extension=strrchr(dirEnt->d_name,'.');
the_sz 9:fe2c9b3a312b 123 if ((extension==NULL) || (strcmp(extension,".jpg"))!=0)
the_sz 9:fe2c9b3a312b 124 continue;
the_sz 9:fe2c9b3a312b 125
the_sz 9:fe2c9b3a312b 126 count++;
the_sz 9:fe2c9b3a312b 127
the_sz 9:fe2c9b3a312b 128 DPrintf_("SD_ShowRandomPicture: Count %s.\r\n",dirEnt->d_name);
the_sz 9:fe2c9b3a312b 129 }
the_sz 9:fe2c9b3a312b 130 closedir(dir);
the_sz 9:fe2c9b3a312b 131
the_sz 9:fe2c9b3a312b 132 DPrintf_("SD_ShowRandomPicture: Count %u.\r\n",count);
the_sz 9:fe2c9b3a312b 133
the_sz 9:fe2c9b3a312b 134 //
the_sz 9:fe2c9b3a312b 135 // get random number
the_sz 9:fe2c9b3a312b 136 //
the_sz 9:fe2c9b3a312b 137 count=(uint32_t)((uint64_t)rand()*(uint64_t)count/RAND_MAX);
the_sz 9:fe2c9b3a312b 138 DPrintf("SD_ShowRandomPicture: Take %u.\r\n",count);
the_sz 9:fe2c9b3a312b 139
the_sz 9:fe2c9b3a312b 140 //
the_sz 9:fe2c9b3a312b 141 // find random jpeg
the_sz 9:fe2c9b3a312b 142 //
the_sz 9:fe2c9b3a312b 143 file[0]='\0';
the_sz 9:fe2c9b3a312b 144 dir=opendir("/sd/");
the_sz 9:fe2c9b3a312b 145 while ((dirEnt=readdir(dir))!=NULL)
the_sz 9:fe2c9b3a312b 146 {
the_sz 9:fe2c9b3a312b 147 DPrintf("check %s.\r\n",dirEnt->d_name);
the_sz 9:fe2c9b3a312b 148 extension=strrchr(dirEnt->d_name,'.');
the_sz 9:fe2c9b3a312b 149 if ((extension==NULL) || (strcmp(extension,".jpg"))!=0)
the_sz 9:fe2c9b3a312b 150 continue;
the_sz 9:fe2c9b3a312b 151
the_sz 9:fe2c9b3a312b 152 if (count==0)
the_sz 9:fe2c9b3a312b 153 {
the_sz 9:fe2c9b3a312b 154 snprintf(file,sizeof(file),"%s",dirEnt->d_name);
the_sz 9:fe2c9b3a312b 155 break;
the_sz 9:fe2c9b3a312b 156 }
the_sz 9:fe2c9b3a312b 157
the_sz 9:fe2c9b3a312b 158 count--;
the_sz 9:fe2c9b3a312b 159 }
the_sz 9:fe2c9b3a312b 160 closedir(dir);
the_sz 9:fe2c9b3a312b 161 DPrintf("got %s.\r\n",file);
the_sz 9:fe2c9b3a312b 162
the_sz 9:fe2c9b3a312b 163 if (file[0]=='\0')
the_sz 9:fe2c9b3a312b 164 return false;
the_sz 9:fe2c9b3a312b 165
the_sz 9:fe2c9b3a312b 166 //
the_sz 9:fe2c9b3a312b 167 // load random jpeg
the_sz 9:fe2c9b3a312b 168 //
the_sz 9:fe2c9b3a312b 169 DPrintf("SD_ShowRandomPicture: Open %s.\r\n",file);
the_sz 9:fe2c9b3a312b 170 if ((fileHandle=sd.open(file,O_RDONLY))==NULL)
the_sz 9:fe2c9b3a312b 171 return false;
the_sz 9:fe2c9b3a312b 172
the_sz 9:fe2c9b3a312b 173 result=false;
the_sz 9:fe2c9b3a312b 174
the_sz 9:fe2c9b3a312b 175 jResult=jd_prepare(&jdec,jpeg_input_func,buffer,sizeof(buffer),NULL);
the_sz 9:fe2c9b3a312b 176 if (jResult==JDR_OK)
the_sz 9:fe2c9b3a312b 177 {
the_sz 9:fe2c9b3a312b 178 pictureWidth=jdec.width;
the_sz 9:fe2c9b3a312b 179 pictureHeight=jdec.height;
the_sz 9:fe2c9b3a312b 180 scale=0;
the_sz 9:fe2c9b3a312b 181 while ((pictureWidth>uiLcd.GetXSize()) || (pictureHeight>uiLcd.GetYSize()))
the_sz 9:fe2c9b3a312b 182 {
the_sz 9:fe2c9b3a312b 183 pictureWidth/=2;
the_sz 9:fe2c9b3a312b 184 pictureHeight/=2;
the_sz 9:fe2c9b3a312b 185 scale++;
the_sz 9:fe2c9b3a312b 186
the_sz 9:fe2c9b3a312b 187 if (scale>=3)
the_sz 9:fe2c9b3a312b 188 break;
the_sz 9:fe2c9b3a312b 189 }
the_sz 9:fe2c9b3a312b 190 offsetX=(uiLcd.GetXSize()-pictureWidth)/2;
the_sz 9:fe2c9b3a312b 191 offsetY=(uiLcd.GetYSize()-pictureHeight)/2;
the_sz 9:fe2c9b3a312b 192
the_sz 9:fe2c9b3a312b 193 jResult=jd_decomp(&jdec,jpeg_output_func,scale);
the_sz 9:fe2c9b3a312b 194 if (jResult==JDR_OK)
the_sz 9:fe2c9b3a312b 195 result=true;
the_sz 9:fe2c9b3a312b 196 else
the_sz 9:fe2c9b3a312b 197 DPrintf("SD_ShowRandomPicture: jd_decomp: %u\r\n",jResult);
the_sz 9:fe2c9b3a312b 198 }
the_sz 9:fe2c9b3a312b 199 else
the_sz 9:fe2c9b3a312b 200 DPrintf("SD_ShowRandomPicture: jd_prepare %u\r\n",jResult);
the_sz 9:fe2c9b3a312b 201
the_sz 9:fe2c9b3a312b 202 fileHandle->close();
the_sz 9:fe2c9b3a312b 203
the_sz 9:fe2c9b3a312b 204 return result;
the_sz 9:fe2c9b3a312b 205 }
the_sz 9:fe2c9b3a312b 206