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

Files at this revision

API Documentation at this revision

Comitter:
the_sz
Date:
Sat Nov 14 02:43:33 2015 +0000
Parent:
8:51e0f01d5c74
Child:
10:d4b452c6c972
Commit message:
slideshow working

Changed in this revision

FATFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
SD.cpp Show annotated file Show diff for this revision Revisions of this file
SD.h Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show diff for this revision Revisions of this file
TinyJpgDec.lib Show annotated file Show diff for this revision Revisions of this file
UI.cpp Show annotated file Show diff for this revision Revisions of this file
UI.h Show annotated file Show diff for this revision Revisions of this file
UI_Main.cpp Show annotated file Show diff for this revision Revisions of this file
WakeupLight.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FATFileSystem.lib	Sat Nov 14 02:43:33 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/mbed-official/code/FATFileSystem/#b3b3370574cf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SD.cpp	Sat Nov 14 02:43:33 2015 +0000
@@ -0,0 +1,206 @@
+#include "WakeupLight.h"
+
+class mySD:public FATFileSystem
+{
+    protected:
+        HAL_SD_CardInfoTypedef CardInfo;
+
+    public:
+        mySD(const char* name):FATFileSystem(name)
+        {
+            uint8_t result=BSP_SD_Init();
+            DPrintf("BSP_SD_Init: %u.\r\n",result);
+            BSP_SD_GetCardInfo(&CardInfo);
+            DPrintf("BSP_SD_GetCardInfo: 0x%llX bytes / %u sector size.\r\n",CardInfo.CardCapacity,CardInfo.CardBlockSize);
+        };
+        virtual int disk_initialize() { return 0; }
+        virtual int disk_status() { return 0; }
+        virtual int disk_read(uint8_t * buffer, uint64_t sector, uint8_t count)
+        {
+            DPrintf_("disk_read: %llu / %u.\r\n",sector,count);
+            return BSP_SD_ReadBlocks((uint32_t *)buffer,sector*CardInfo.CardBlockSize,CardInfo.CardBlockSize,count);
+        };
+        virtual int disk_write(const uint8_t * buffer, uint64_t sector, uint8_t count)
+        {
+            DPrintf_("disk_write: %llu / %u.\r\n",sector,count);
+            return BSP_SD_WriteBlocks((uint32_t *)buffer,sector*CardInfo.CardBlockSize,CardInfo.CardBlockSize,count);
+        };
+        virtual int disk_sync() { return 0; }
+        virtual uint64_t disk_sectors()
+        {
+            return CardInfo.CardCapacity/CardInfo.CardBlockSize;
+        };
+};
+
+mySD                        sd("sd");
+bool                        mustSrand=true;
+FileHandle                  *fileHandle;
+uint8_t                     buffer[6000];
+int16_t                     offsetX;
+int16_t                     offsetY;
+
+void SD_Init(void)
+{
+}
+
+JPG_UINT jpeg_input_func(JDEC *jd, BYTE *buff, JPG_UINT ndata)
+{
+    if(buff)
+    {
+        size_t n = fileHandle->read(buff,ndata);
+        return n == (size_t)-1 ? 0 : n;
+    }
+    else
+    {
+        off_t t = fileHandle->lseek( ndata, SEEK_CUR);
+        return t == (off_t)-1 ? 0 : ndata;
+    }
+}
+ 
+JPG_UINT jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect)
+{
+    int             x0=rect->left+offsetX;
+    int             x1=rect->right+offsetX;
+    int             y0=rect->top+offsetY;
+    int             y1=rect->bottom+offsetY;
+    int             w=x1-x0+1;
+
+    DPrintf_("jpeg_output_func: %ux%u / %ux%u\r\n",x0,y0,x1,y1);
+
+    if ((y0>=uiLcd.GetYSize()) || (x0>=uiLcd.GetXSize()))
+        return 1;
+ 
+    if (x1>uiLcd.GetXSize()-1)
+        x1=uiLcd.GetXSize()-1;
+    if (y1>uiLcd.GetYSize()-1)
+        y1=uiLcd.GetYSize()-1;
+ 
+    for (int y=y0;y<=y1;y++)
+    {
+        uint8_t *p=((uint8_t *)bitmap)+((w*(y-y0))*3);
+
+        for (int x=x0;x<=x1;x++)
+        {
+            if ((x>=0) && (y>=0))
+                uiLcd.DrawPixel(x,y,(0xFF000000 | p[0]<<16 | (p[1]<<8) | (p[2]) ));
+
+            p+=3;
+        }
+    }
+
+    return 1;
+}
+
+bool SD_ShowRandomPicture(void)
+{
+    uint32_t                count;
+    DIR                     *dir;
+    struct dirent           *dirEnt;
+    char                    *extension;
+    char                    file[100];
+    JRESULT                 jResult;
+    JDEC                    jdec;
+    bool                    result;
+    BYTE                    scale;
+    JPG_UINT                pictureWidth;
+    JPG_UINT                pictureHeight;
+    AnalogIn                analogIn(PA_0);
+
+    if (mustSrand==true)
+    {
+        srand(time(NULL)+analogIn.read_u16());
+        mustSrand=false;
+    }
+
+    //
+    // count all jpegs
+    //
+    count=0;
+    dir=opendir("/sd/");
+    while ((dirEnt=readdir(dir))!=NULL)
+    {
+        extension=strrchr(dirEnt->d_name,'.');
+        if ((extension==NULL) || (strcmp(extension,".jpg"))!=0)
+            continue;        
+
+        count++;
+
+        DPrintf_("SD_ShowRandomPicture: Count %s.\r\n",dirEnt->d_name);
+    }
+    closedir(dir);
+
+    DPrintf_("SD_ShowRandomPicture: Count %u.\r\n",count);
+
+    //
+    // get random number
+    //
+    count=(uint32_t)((uint64_t)rand()*(uint64_t)count/RAND_MAX);
+    DPrintf("SD_ShowRandomPicture: Take %u.\r\n",count);
+
+    //
+    // find random jpeg
+    //
+    file[0]='\0';
+    dir=opendir("/sd/");
+    while ((dirEnt=readdir(dir))!=NULL)
+    {
+DPrintf("check %s.\r\n",dirEnt->d_name);
+        extension=strrchr(dirEnt->d_name,'.');
+        if ((extension==NULL) || (strcmp(extension,".jpg"))!=0)
+            continue;        
+
+        if (count==0)
+        {
+            snprintf(file,sizeof(file),"%s",dirEnt->d_name);
+            break;
+        }
+
+        count--;
+    }
+    closedir(dir);
+DPrintf("got %s.\r\n",file);
+    
+    if (file[0]=='\0')
+        return false;
+
+    //
+    // load random jpeg
+    //
+    DPrintf("SD_ShowRandomPicture: Open %s.\r\n",file);
+    if ((fileHandle=sd.open(file,O_RDONLY))==NULL)
+        return false;
+
+    result=false;
+
+    jResult=jd_prepare(&jdec,jpeg_input_func,buffer,sizeof(buffer),NULL);
+    if (jResult==JDR_OK)
+    {
+        pictureWidth=jdec.width;
+        pictureHeight=jdec.height;
+        scale=0;
+        while ((pictureWidth>uiLcd.GetXSize()) || (pictureHeight>uiLcd.GetYSize()))
+        {
+            pictureWidth/=2;
+            pictureHeight/=2;
+            scale++;
+
+            if (scale>=3)
+                break;
+        }
+        offsetX=(uiLcd.GetXSize()-pictureWidth)/2;
+        offsetY=(uiLcd.GetYSize()-pictureHeight)/2;
+
+        jResult=jd_decomp(&jdec,jpeg_output_func,scale);
+        if (jResult==JDR_OK)
+            result=true;
+        else
+            DPrintf("SD_ShowRandomPicture: jd_decomp: %u\r\n",jResult);
+    }
+    else
+        DPrintf("SD_ShowRandomPicture: jd_prepare %u\r\n",jResult);
+
+    fileHandle->close();
+
+    return result;
+}
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SD.h	Sat Nov 14 02:43:33 2015 +0000
@@ -0,0 +1,7 @@
+#ifndef __SD_h
+#define __SD_h
+
+void SD_Init(void);
+bool SD_ShowRandomPicture(void);
+
+#endif
--- a/SDFileSystem.lib	Thu Nov 12 22:01:17 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TinyJpgDec.lib	Sat Nov 14 02:43:33 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/humlet/code/TinyJpgDec/#79e97662234d
--- a/UI.cpp	Thu Nov 12 22:01:17 2015 +0000
+++ b/UI.cpp	Sat Nov 14 02:43:33 2015 +0000
@@ -14,6 +14,9 @@
 #define CLOCK_COLOR_BG                  ((uint32_t)0xFF000000)
 #define CLOCK_COLOR_FG                  ((uint32_t)0xFF707070)
 
+#define SLIDESHOW_COLOR_BG              ((uint32_t)0xFF000000)
+#define SLIDESHOW_COLOR_FG              ((uint32_t)0xFF707070)
+
 #define BUTTON_WIDTH                    100
 #define BUTTON_HEIGHT                   60
 #define BUTTON_SMALL_WIDTH              50
@@ -39,15 +42,17 @@
 TS_DISCO_F746NG                         uiTs;
 uint16_t                                uiLastTouchX;
 uint16_t                                uiLastTouchY;
+int                                     lastSlideshowTick=0;
 UI_STRUCT                               *uiCurrent=NULL;
 UI_STRUCT                               uiClock;
 UI_STRUCT                               uiClockInWords;
 UI_STRUCT                               uiColorTest;
 UI_STRUCT                               uiWakeup;
+UI_STRUCT                               uiSlideshow;
 UI_STRUCT                               uiMain;
 static UI_BOX_LIST_ITEM_STRUCT          uiMainItems[]=
 {
-    { "Clock", ic_query_builder_white_48dp_1x }, { "Clock\nWith Words", ic_query_builder_white_48dp_1x }, { "Adjust\nTimers", ic_notifications_none_white_48dp_1x }, { "Lights On", NULL }, { "Lights Off", NULL }, { "Color Test", NULL }
+    { "Clock", ic_query_builder_white_48dp_1x }, { "Clock\nWith Words", ic_query_builder_white_48dp_1x }, { "Slideshow", NULL }, { "Adjust\nTimers", ic_notifications_none_white_48dp_1x }, { "Lights On", NULL }, { "Lights Off", NULL }, { "Color Test", NULL }
 };
 
 //
@@ -325,28 +330,6 @@
 }
 
 //
-// message box
-//
-void UI_ShowMessageBox(bool initial)
-{
-    if (initial==true)
-    {
-        // fill background
-        UI_ShowClearClientRect();
-    }
-}
-
-void UI_ClickMessageBox(uint16_t x,uint16_t y)
-{
-    uint32_t                    index;
-
-    // detect at which button was clicked
-    index=0;
-
-    uiCurrent->handler(UR_CLICK,index,uiCurrent);
-}
-
-//
 // clock
 //
 void UI_ShowClock(bool initial)
@@ -416,6 +399,44 @@
 }
 
 //
+// slideshow
+//
+void UI_ShowSlideshow(bool initial)
+{
+    char                        buffer[100];
+    struct tm                   *tmStruct;
+    time_t                      timeValue;
+
+    timeValue=time(NULL);
+    tmStruct=localtime(&timeValue);
+
+    if (((tmStruct->tm_sec / 15)!=lastSlideshowTick) || (initial==true))
+    {
+        lastSlideshowTick=(tmStruct->tm_sec / 15);
+
+        // fill background
+        uiLcd.SetTextColor(SLIDESHOW_COLOR_BG);
+        uiLcd.FillRect(0,0,uiLcd.GetXSize(),uiLcd.GetYSize());
+
+        // show picture
+        SD_ShowRandomPicture();
+
+        // show clock
+        uiLcd.SetFont(&display_font_12x22);
+        uiLcd.SetBackColor(0x00000000);
+        uiLcd.SetTextColor(SLIDESHOW_COLOR_FG);
+        snprintf(buffer,sizeof(buffer),"%u:%02u",tmStruct->tm_hour,tmStruct->tm_min);
+        uiLcd.DisplayStringAt(30,220,(uint8_t *)buffer,RIGHT_MODE);
+    }
+}
+
+void UI_ClickSlideshow(uint16_t x,uint16_t y)
+{
+    // exit view
+    UI_Show(&uiMain);
+}
+
+//
 // value adjust
 //
 void UI_ShowValueAdjust(bool initial)
@@ -631,6 +652,8 @@
     uiMain.data.boxList.items=uiMainItems;
     uiMain.data.boxList.count=COUNT_OF(uiMainItems);
 
+    uiSlideshow.flags=UI_FLAG_TYPE_SLIDESHOW;
+
     UI_Show(&uiMain);
 }
 
@@ -676,12 +699,12 @@
     typeFlag=uiCurrent->flags & ~(UI_FLAG_NEEDS_CHROME | UI_FLAG_HAS_BACK_BUTTON);
     if ((typeFlag & UI_FLAG_TYPE_BOX_LIST)!=0)
         UI_ShowBoxList(initial);
-    else if ((typeFlag & UI_FLAG_TYPE_MESSAGE_BOX)!=0)
-        UI_ShowMessageBox(initial);
     else if ((typeFlag & UI_FLAG_TYPE_CLOCK)!=0)
         UI_ShowClock(initial);
     else if ((typeFlag & UI_FLAG_TYPE_CLOCK_IN_WORDS)!=0)
         UI_ShowClockInWords(initial);
+    else if ((typeFlag & UI_FLAG_TYPE_SLIDESHOW)!=0)
+        UI_ShowSlideshow(initial);
     else if ((typeFlag & UI_FLAG_TYPE_VALUE_ADJUST)!=0)
         UI_ShowValueAdjust(initial);
 }
@@ -719,12 +742,12 @@
     typeFlag=uiCurrent->flags & ~(UI_FLAG_NEEDS_CHROME | UI_FLAG_HAS_BACK_BUTTON);
     if ((typeFlag & UI_FLAG_TYPE_BOX_LIST)!=0)
         UI_ClickBoxList(x,y);
-    else if ((typeFlag & UI_FLAG_TYPE_MESSAGE_BOX)!=0)
-        UI_ClickMessageBox(x,y);
     else if ((typeFlag & UI_FLAG_TYPE_CLOCK)!=0)
         UI_ClickClock(x,y);
     else if ((typeFlag & UI_FLAG_TYPE_CLOCK_IN_WORDS)!=0)
         UI_ClickClockInWords(x,y);
+    else if ((typeFlag & UI_FLAG_TYPE_SLIDESHOW)!=0)
+        UI_ClickSlideshow(x,y);
     else if ((typeFlag & UI_FLAG_TYPE_VALUE_ADJUST)!=0)
         UI_ClickValueAdjust(x,y);
 }
--- a/UI.h	Thu Nov 12 22:01:17 2015 +0000
+++ b/UI.h	Sat Nov 14 02:43:33 2015 +0000
@@ -2,12 +2,12 @@
 #define __UI_h
 
 #define UI_FLAG_TYPE_BOX_LIST           (0x01 | UI_FLAG_NEEDS_CHROME)
-#define UI_FLAG_TYPE_MESSAGE_BOX        (0x02 | UI_FLAG_NEEDS_CHROME)
-#define UI_FLAG_TYPE_CLOCK              0x04
-#define UI_FLAG_TYPE_CLOCK_IN_WORDS     (0x08 | UI_FLAG_NEEDS_CHROME)
-#define UI_FLAG_TYPE_VALUE_ADJUST       (0x10 | UI_FLAG_NEEDS_CHROME | UI_FLAG_HAS_BACK_BUTTON)
-#define UI_FLAG_NEEDS_CHROME            0x20
-#define UI_FLAG_HAS_BACK_BUTTON         0x40
+#define UI_FLAG_TYPE_CLOCK              0x02
+#define UI_FLAG_TYPE_CLOCK_IN_WORDS     (0x04 | UI_FLAG_NEEDS_CHROME)
+#define UI_FLAG_TYPE_VALUE_ADJUST       (0x08 | UI_FLAG_NEEDS_CHROME | UI_FLAG_HAS_BACK_BUTTON)
+#define UI_FLAG_NEEDS_CHROME            0x10
+#define UI_FLAG_HAS_BACK_BUTTON         0x20
+#define UI_FLAG_TYPE_SLIDESHOW          0x40
 
 typedef enum
 {
@@ -43,10 +43,6 @@
 
         struct
         {
-        } messageBox;
-
-        struct
-        {
             uint8_t                     count;
             int32_t                     values[10];
 
@@ -60,6 +56,7 @@
 extern UI_STRUCT                        uiClockInWords;
 extern UI_STRUCT                        uiColorTest;
 extern UI_STRUCT                        uiWakeup;
+extern UI_STRUCT                        uiSlideshow;
 extern UI_STRUCT                        uiMain;
 
 void UI_Init(void);
@@ -73,4 +70,6 @@
 
 void UI_ColorTestHandler(UI_REASON_ENUM reason,int32_t index,UI_STRUCT *ui);
 
+extern LCD_DISCO_F746NG                 uiLcd;
+
 #endif
--- a/UI_Main.cpp	Thu Nov 12 22:01:17 2015 +0000
+++ b/UI_Main.cpp	Sat Nov 14 02:43:33 2015 +0000
@@ -4,6 +4,7 @@
 {
     LUS_CLOCK,
     LUS_CLOCK_IN_WORDS,
+    LUS_SLIDESHOW,
 
 } LAST_USED_SCREENSAVER_ENUM;
     
@@ -26,12 +27,17 @@
                 UI_Show(&uiClockInWords);
             }
             else if (index==2)
+            {
+                lastUsedScreensaver=LUS_SLIDESHOW;
+                UI_Show(&uiSlideshow);
+            }
+            else if (index==3)
                 UI_WakeupShow();
-            else if (index==3)
+            else if (index==4)
                 LED_StartAnimation(LAE_WAKEUP);
-            else if (index==4)
+            else if (index==5)
                 LED_StartAnimation(LAE_OFF);
-            else if (index==5)
+            else if (index==6)
                 UI_Show(&uiColorTest);
             break;
 
@@ -42,6 +48,7 @@
                 {
                     case LUS_CLOCK:                 UI_Show(&uiClock);          break;
                     case LUS_CLOCK_IN_WORDS:        UI_Show(&uiClockInWords);   break;
+                    case LUS_SLIDESHOW:             UI_Show(&uiSlideshow);      break;
                 }
             }
             break;
--- a/WakeupLight.h	Thu Nov 12 22:01:17 2015 +0000
+++ b/WakeupLight.h	Sat Nov 14 02:43:33 2015 +0000
@@ -3,9 +3,14 @@
 
 #include "mbed.h"
 #include "stm32746g_discovery.h"
+#include "stm32746g_discovery_sd.h"
 #include "TS_DISCO_F746NG.h"
 #include "LCD_DISCO_F746NG.h"
 
+#include <FATFileSystem.h>
+
+#include <TinyJpgDec/TinyJpgDec.h>
+
 #define COUNT_OF(__ARRAY__)                                 (sizeof(__ARRAY__)/sizeof(__ARRAY__[0]))
 #define MIN(X,Y)                                            ((X) < (Y) ? (X) : (Y))
 #define MAX(X,Y)                                            ((X) > (Y) ? (X) : (Y))
@@ -15,6 +20,7 @@
 
 #include "OnBoardLED.h"
 #include "LED.h"
+#include "SD.h"
 #include "UI.h"
 #include "Config.h"
 #include "Fonts/display_fonts.h"
--- a/main.cpp	Thu Nov 12 22:01:17 2015 +0000
+++ b/main.cpp	Sat Nov 14 02:43:33 2015 +0000
@@ -65,22 +65,20 @@
 
 #include "stm32746g_discovery_audio.h"
 
-uint16_t buffer[2000];
-void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
+//uint16_t buffer1[2000];
+void BSP_AUDIO_OUT_TransferComplete_CallBack()
 {
     DPrintf("***1");
     BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
 }
-void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
+void BSP_AUDIO_OUT_HalfTransfer_CallBack()
 {
     DPrintf("***2");
     BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
 }
-void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
-{
-    DPrintf("***3");
-}
-    
+
+
+
 int main()
 {
     debug_Init();
@@ -91,31 +89,26 @@
 
     LED_Init();
 
+    SD_Init();
+
     UI_Init();
 
     Config_Init();
 
-
+/*
 // OUTPUT_DEVICE_SPEAKER OUTPUT_DEVICE_BOTH
-BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE,100,8000);
+uint8_t xxx=BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE,100,8000);
+DPrintf("resilt %u\r\n",xxx);
 uint32_t index;
 for (index=0;index<100;index++)
 {
     if ((index % 2)==0)
-        buffer[index]=0x0000;
+        buffer1[index]=0x0000;
     else
-        buffer[index]=0x7000;
+        buffer1[index]=0x7000;
 }
-    
-BSP_AUDIO_OUT_Play(buffer, sizeof(buffer)*sizeof(uint16_t));
-/*
-#include "SDFileSystem.h"
-SDFileSystem sd(PD_2, PC_8, PC_12, PC_11, "sd"); // MOSI, MISO, SCLK, SSEL
-DigitalOut sdsd(PC_9);
-mkdir("/sd/foo",0777);
-FILE *fp = fopen("/sd/mbed.txt", "w");
-fprintf(fp, "Hello World!\n");
-fclose(fp);
+   
+BSP_AUDIO_OUT_Play(buffer1, sizeof(buffer1)*sizeof(uint16_t));
 */
 
     for (;;)