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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FontX.cpp Source File

FontX.cpp

00001 //
00002 // use http://www.eran.io/the-dot-factory-an-lcd-font-and-image-generator/ to create the font definitions
00003 // plus modify output:
00004 //   - char height: in bits
00005 //   - font height: don't display
00006 //   - generate space bitmap
00007 //
00008 //   - remove \ comment from const FONT_CHAR_INFO xxxDescriptors[]
00009 //   - rename fontxxx.c to fontxxx.cpp file
00010 //
00011 
00012 #include "FontX.h"
00013 
00014 #include "stm32746g_discovery_lcd.h"
00015 #include "debug.h"
00016 
00017 #define CHAR_SPACE      2
00018 
00019 static bool             useAlias=true;
00020 
00021 uint32_t FontX_GetLength(char *Text,const FONT_INFO *pFont)
00022 {
00023     uint32_t            length;
00024     uint32_t            index;
00025 
00026     length=0;
00027     
00028     while ((*Text)!='\0')
00029     {
00030         if (((*Text)>=pFont->StartCharacter) && (((*Text)<=pFont->EndCharacter) || ((pFont->EndCharacter>0x80))))       // mbed used a different character table than TheDotFactory.exe
00031         {
00032             index=(*Text)-pFont->StartCharacter;
00033             length+=pFont->Descriptors[index].Charwidth;
00034         }
00035 
00036         length+=CHAR_SPACE;
00037 
00038         if (useAlias==true)
00039             length+=CHAR_SPACE;
00040 
00041         Text++;
00042     }
00043 
00044     if (useAlias==true)
00045         length/=2;
00046 
00047     return length;
00048 }
00049 
00050 uint32_t FontX_GetHeight(const FONT_INFO *pFont)
00051 {
00052     uint32_t                height;
00053 
00054     height=pFont->Descriptors[0].Charheight;
00055 
00056     if (useAlias==true)
00057         height/=2;
00058 
00059     return height;
00060 }
00061 
00062 void FontX_DisplayStringAt(uint16_t Xpos,uint16_t Ypos,uint16_t width,char *Text,FONTX_ALIGNMENT_ENUM Alignment,const FONT_INFO *pFont,uint32_t colorText,uint32_t colorBack)
00063 {
00064     uint32_t xsize = 0; 
00065 
00066     /* get pixel length */
00067     xsize = FontX_GetLength(Text,pFont);
00068 
00069     switch (Alignment)
00070     {
00071         case ALIGN_CENTER:
00072             Xpos+=(width-xsize)/2;
00073             break;
00074 
00075         case ALIGN_RIGHT:
00076             Xpos=width-xsize-Xpos;
00077             break;
00078 
00079         case ALIGN_LEFT:
00080         default:
00081             break;
00082     }
00083     
00084     /* send the string character by character on LCD */
00085     while ((*Text)!='\0')
00086     {
00087         /* display one character on LCD */
00088         Xpos+=FontX_DisplayChar(Xpos,Ypos,*Text,pFont,colorText,colorBack);
00089 
00090         /* point on the next character */
00091         Text++;
00092     }  
00093 }
00094 
00095 uint32_t FontX_DisplayChar(uint16_t Xpos,uint16_t Ypos,char Ascii,const FONT_INFO *pFont,uint32_t colorText,uint32_t colorBack)
00096 {
00097     uint32_t        line;
00098     uint32_t        line2;
00099     uint32_t        index;
00100     uint32_t        offset;
00101     uint32_t        lineOffset;
00102     uint32_t        i;
00103     uint32_t        j;
00104     uint32_t        bits;
00105     uint32_t        color;
00106     uint32_t        red;
00107     uint32_t        green;
00108     uint32_t        blue;
00109     uint32_t        transparency;
00110 
00111     if (Ascii<pFont->StartCharacter)
00112         return 0;
00113     if ((Ascii>pFont->EndCharacter) && (pFont->EndCharacter<0x80))      // mbed used a different character table than TheDotFactory.exe
00114         return 0;
00115 
00116     index=Ascii-pFont->StartCharacter;
00117 
00118     lineOffset=8 * ((pFont->Descriptors[index].Charwidth + 7) / 8) -  pFont->Descriptors[index].Charwidth;
00119 
00120     offset=pFont->Descriptors[index].Offset;
00121 
00122     for (i=0;i<pFont->Descriptors[index].Charheight;i++)
00123     {
00124         if (useAlias==true)
00125         {
00126             // get average of 2x2 pixel array
00127             for (j=0;j<pFont->Descriptors[index].Charwidth;j+=2)
00128             {
00129                 line=pFont->Bitmaps[offset+(j /8)];
00130                 line2=pFont->Bitmaps[offset+(j /8) + ((pFont->Descriptors[index].Charwidth + 7)/8)];
00131 
00132                 bits=0;
00133                 if (line & (1 << ((pFont->Descriptors[index].Charwidth - j + lineOffset - 1) % 8)))
00134                     bits++;
00135                 if (line & (1 << ((pFont->Descriptors[index].Charwidth - (j+1) + lineOffset - 1) % 8)))
00136                     bits++;
00137                 if (line2 & (1 << ((pFont->Descriptors[index].Charwidth - j + lineOffset - 1) % 8)))
00138                     bits++;
00139                 if (line2 & (1 << ((pFont->Descriptors[index].Charwidth - (j+1) + lineOffset - 1) % 8)))
00140                     bits++;
00141 
00142                 // alpha blend it to the current screen
00143                 if ((colorBack==0x00000000) || (colorBack==0x00000001))
00144                     color=BSP_LCD_ReadPixel((Xpos+(j/2)),Ypos);
00145                 else
00146                     color=colorBack;
00147 
00148                 if (colorBack==0x00000000)
00149                     transparency=255;
00150                 else if (colorBack==0x00000001)
00151                     transparency=255*bits/4;
00152                 else
00153                     transparency=255;
00154 
00155                 red=((color >> 16) & 0xFF)*(4-bits)/4;
00156                 green=((color >> 8) & 0xFF)*(4-bits)/4;
00157                 blue=(color & 0xFF)*(4-bits)/4;
00158 
00159                 red+=(((colorText >> 16) & 0xFF)*bits/4);
00160                 if (red>255)
00161                     red=255;
00162                 green+=(((colorText >> 8) & 0xFF)*bits/4);
00163                 if (green>255)
00164                     green=255;
00165                 blue+=((colorText & 0xFF)*bits/4);
00166                 if (blue>255)
00167                     blue=255;
00168 
00169                 // write pixel
00170                 BSP_LCD_DrawPixel((Xpos+(j/2)),Ypos,(transparency << 24) | (red << 16) | (green << 8) | blue);
00171             }
00172         }
00173         else
00174         {
00175             for (j=0;j<pFont->Descriptors[index].Charwidth;j++)
00176             {
00177                 if (line & (1 << (pFont->Descriptors[index].Charwidth - j + lineOffset - 1)))
00178                 {
00179                     BSP_LCD_DrawPixel((Xpos+j),Ypos,colorText);
00180                 }
00181                 else
00182                 {
00183                     if ((colorBack!=0x00000000) && (colorBack!=0x00000001))
00184                         BSP_LCD_DrawPixel((Xpos+j),Ypos,colorBack);
00185                 }
00186             }
00187         }
00188 
00189         // fill char space area
00190         if ((colorBack!=0x00000000) && (colorBack!=0x00000001))
00191         {
00192             for (j=0;j<CHAR_SPACE;j++)
00193                 BSP_LCD_DrawPixel((Xpos+(pFont->Descriptors[index].Charwidth/2)+j),Ypos,colorBack);
00194         }
00195 
00196         Ypos++;
00197 
00198         offset+=((pFont->Descriptors[index].Charwidth + 7)/8);
00199         if (useAlias==true)
00200             offset+=((pFont->Descriptors[index].Charwidth + 7)/8);
00201 
00202         if (useAlias==true)
00203             i++;
00204     }
00205 
00206     i=pFont->Descriptors[index].Charwidth;
00207 
00208     if (useAlias==true)
00209         i/=2;
00210 
00211     return i+CHAR_SPACE;
00212 }