retargettft.c

Go to the documentation of this file.
00001 /***************************************************************************/
00017 #include <stdio.h>
00018 #include <stdint.h>
00019 #include "em_device.h"
00020 #include "em_cmu.h"
00021 #include "em_ebi.h"
00022 #include "em_gpio.h"
00023 #include "dmd/ssd2119/dmd_ssd2119.h"
00024 #include "bsp.h"
00025 #include "retargettft.h"
00026 
00027 #include "displayfont8x8.h"
00028 #define fontBits        chars_8x8_bits      
00030 #define CHARS    40                         
00031 #define LINES    30                         
00033 static uint8_t charBuffer[LINES][CHARS];    
00034 static uint8_t rgbColor[3];                 
00037 static int  xpos, ypos;                     
00039 static bool fullUpdate  = true;             
00040 static bool bufferReset = true;             
00041 static bool tftReset    = true;             
00042 static bool LFtoCRLF    = 0;                
00043 static bool initialized = false;            
00045 /* Static functions. */
00046 static void tftTextReset(void);
00047 static void tftTextScrollUp(void);
00048 
00049 
00050 /**************************************************************************/
00053 void RETARGET_TftInit(void)
00054 {
00055 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00056   setvbuf(stdout, NULL, _IONBF, 0);   /*Set unbuffered mode for stdout (newlib)*/
00057 #endif
00058 
00059   /* Reset the TFT text display state. */
00060   tftTextReset();
00061 }
00062 
00063 
00064 /**************************************************************************/
00068 void RETARGET_TftCrLf(int on)
00069 {
00070   if (on)
00071     LFtoCRLF = true;
00072   else
00073     LFtoCRLF = false;
00074 }
00075 
00076 
00077 /**************************************************************************/
00080 static void tftTextReset(void)
00081 {
00082   int          x, y;
00083   volatile int i;
00084   EMSTATUS     status;
00085 
00086   /* Initialize color for font */
00087   /* Use \b for red text (bell/warning) */
00088   rgbColor[0] = 0xff;
00089   rgbColor[1] = 0xff;
00090   rgbColor[2] = 0xff;
00091 
00092   /* Character buffer */
00093   if (bufferReset)
00094   {
00095     /* Clear character buffer */
00096     for (y = 0; y < LINES; y++)
00097     {
00098       for (x = 0; x < CHARS; x++)
00099       {
00100         charBuffer[y][x] = 0;
00101       }
00102     }
00103     /* Set cursor position to upper left */
00104     xpos = 0;
00105     ypos = 0;
00106   }
00107 
00108   /* Display controller */
00109   if (tftReset)
00110   {
00111     /* Configure for EBI mode and reset display */
00112     BSP_DisplayControl(BSP_Display_EBI);
00113     BSP_DisplayControl(BSP_Display_ResetAssert);
00114     BSP_DisplayControl(BSP_Display_PowerDisable);
00115     /* Short delay */
00116     for (i = 0; i < 10000; i++) ;
00117     /* Configure display for Direct Drive + SPI mode */
00118     BSP_DisplayControl(BSP_Display_Mode8080);
00119     BSP_DisplayControl(BSP_Display_PowerEnable);
00120     BSP_DisplayControl(BSP_Display_ResetRelease);
00121 
00122     /* Initialize graphics - abort on failure */
00123     status = DMDIF_init(BC_SSD2119_BASE, BC_SSD2119_BASE + 2);
00124     if (status == DMD_OK) status = DMD_init(0);
00125     if ((status != DMD_OK) && (status != DMD_ERROR_DRIVER_ALREADY_INITIALIZED)) while (1) ;
00126     /* Make sure display is configured with correct rotation */
00127     if ((status == DMD_OK)) status = DMD_flipDisplay(1, 1);
00128   }
00129   initialized = true;
00130 }
00131 
00132 /**************************************************************************/
00135 static void tftTextScrollUp(void)
00136 {
00137   int y;
00138   int x;
00139 
00140   /* copy all lines one line up */
00141   for (y = 0; y < (LINES - 1); y++)
00142   {
00143     for (x = 0; x < CHARS; x++)
00144     {
00145       charBuffer[y][x] = charBuffer[y + 1][x];
00146     }
00147   }
00148   /* clear last line */
00149   for (x = 0; x < CHARS; x++)
00150   {
00151     charBuffer[LINES - 1][x] = 0;
00152   }
00153   xpos       = 0;
00154   ypos       = LINES - 1;
00155   fullUpdate = true;
00156 }
00157 
00158 /***************************************************************************/
00163 /**************************************************************************/
00168 void RETARGET_TFTTX(int c)
00169 {
00170   /* check for CR */
00171   if (c == '\r')
00172   {
00173     xpos = 0;
00174     return;
00175   }
00176   /* check for LF */
00177   if (c == '\n')
00178   {
00179     ypos = ypos + 1;
00180     xpos = 0;
00181     if (ypos >= LINES)
00182     {
00183       /* scroll characters one line up */
00184       tftTextScrollUp();
00185       ypos = (LINES - 1);
00186     }
00187     return;
00188   }
00189   /* check for bell character, changes color to red */
00190   if (c == '\b')
00191   {
00192     if (rgbColor[1] == 0xff)
00193     {
00194       rgbColor[1] = 0x00;
00195       rgbColor[2] = 0x00;
00196     }
00197     else
00198     {
00199       rgbColor[1] = 0xff;
00200       rgbColor[2] = 0xff;
00201     }
00202     return;
00203   }
00204 
00205   /* check for non-printable characters */
00206   if (c < ' ' || c > '~')
00207   {
00208     c = ' ';
00209   }
00210   xpos = xpos + 1;
00211   if (xpos >= CHARS)
00212   {
00213     xpos = 0;
00214     ypos = ypos + 1;
00215   }
00216   if (ypos >= LINES)
00217   {
00218     tftTextScrollUp();
00219     ypos = 29;
00220   }
00221   charBuffer[ypos][xpos] = c - ' ';
00222 }
00223 
00224 
00225 /**************************************************************************/
00230 void RETARGET_TFTUpdate(bool fullFrame)
00231 {
00232   int      x, y;
00233   uint32_t pixelX, pixelY;
00234   uint8_t  c, bitField;
00235   int      i;
00236 
00237   /* Draw a full screen */
00238   if (fullFrame)
00239   {
00240     for (y = 0; y < LINES; y++)
00241     {
00242       for (x = 0; x < CHARS; x++)
00243       {
00244         pixelX = x * 8;
00245         pixelY = y * 8;
00246 
00247         c = charBuffer[y][x];
00248         for (i = 0; i < 8; i++)
00249         {
00250           bitField = fontBits[c + 100 * i];
00251           if (bitField == 0)
00252           {
00253             DMD_writeData(pixelX, pixelY + i, (uint8_t *) "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 8);
00254             continue;
00255           }
00256 
00257           if (bitField & 0x01)
00258           {
00259             DMD_writeColor(pixelX + 0, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00260           }
00261           else
00262           {
00263             DMD_writeColor(pixelX + 0, pixelY + i, 0x00, 0x00, 0x00, 1);
00264           }
00265           if (bitField & 0x02)
00266           {
00267             DMD_writeColor(pixelX + 1, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00268           }
00269           else
00270           {
00271             DMD_writeColor(pixelX + 1, pixelY + i, 0x00, 0x00, 0x00, 1);
00272           }
00273           if (bitField & 0x04)
00274           {
00275             DMD_writeColor(pixelX + 2, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00276           }
00277           else
00278           {
00279             DMD_writeColor(pixelX + 2, pixelY + i, 0x00, 0x00, 0x00, 1);
00280           }
00281           if (bitField & 0x08)
00282           {
00283             DMD_writeColor(pixelX + 3, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00284           }
00285           else
00286           {
00287             DMD_writeColor(pixelX + 3, pixelY + i, 0x00, 0x00, 0x00, 1);
00288           }
00289           if (bitField & 0x10)
00290           {
00291             DMD_writeColor(pixelX + 4, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00292           }
00293           else
00294           {
00295             DMD_writeColor(pixelX + 4, pixelY + i, 0x00, 0x00, 0x00, 1);
00296           }
00297           if (bitField & 0x20)
00298           {
00299             DMD_writeColor(pixelX + 5, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00300           }
00301           else
00302           {
00303             DMD_writeColor(pixelX + 5, pixelY + i, 0x00, 0x00, 0x00, 1);
00304           }
00305           if (bitField & 0x40)
00306           {
00307             DMD_writeColor(pixelX + 6, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00308           }
00309           else
00310           {
00311             DMD_writeColor(pixelX + 6, pixelY + i, 0x00, 0x00, 0x00, 1);
00312           }
00313           if (bitField & 0x80)
00314           {
00315             DMD_writeColor(pixelX + 7, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00316           }
00317           else
00318           {
00319             DMD_writeColor(pixelX + 7, pixelY + i, 0x00, 0x00, 0x00, 1);
00320           }
00321         }
00322       }
00323     }
00324   }
00325   else
00326   {
00327     /* Draw xpos, ypos only */
00328     c      = charBuffer[ypos][xpos];
00329     pixelX = xpos * 8;
00330     pixelY = ypos * 8;
00331     for (i = 0; i < 8; i++)
00332     {
00333       bitField = fontBits[c + 100 * i];
00334       if (bitField == 0)
00335       {
00336         DMD_writeData(pixelX, pixelY + i, (uint8_t *) "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 8);
00337         continue;
00338       }
00339 
00340       if (bitField & 0x01)
00341 
00342       {
00343         DMD_writeColor(pixelX + 0, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00344       }
00345       else
00346       {
00347         DMD_writeColor(pixelX + 0, pixelY + i, 0x00, 0x00, 0x00, 1);
00348       }
00349       if (bitField & 0x02)
00350       {
00351         DMD_writeColor(pixelX + 1, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00352       }
00353       else
00354       {
00355         DMD_writeColor(pixelX + 1, pixelY + i, 0x00, 0x00, 0x00, 1);
00356       }
00357       if (bitField & 0x04)
00358       {
00359         DMD_writeColor(pixelX + 2, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00360       }
00361       else
00362       {
00363         DMD_writeColor(pixelX + 2, pixelY + i, 0x00, 0x00, 0x00, 1);
00364       }
00365       if (bitField & 0x08)
00366       {
00367         DMD_writeColor(pixelX + 3, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00368       }
00369       else
00370       {
00371         DMD_writeColor(pixelX + 3, pixelY + i, 0x00, 0x00, 0x00, 1);
00372       }
00373       if (bitField & 0x10)
00374       {
00375         DMD_writeColor(pixelX + 4, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00376       }
00377       else
00378       {
00379         DMD_writeColor(pixelX + 4, pixelY + i, 0x00, 0x00, 0x00, 1);
00380       }
00381       if (bitField & 0x20)
00382       {
00383         DMD_writeColor(pixelX + 5, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00384       }
00385       else
00386       {
00387         DMD_writeColor(pixelX + 5, pixelY + i, 0x00, 0x00, 0x00, 1);
00388       }
00389       if (bitField & 0x40)
00390       {
00391         DMD_writeColor(pixelX + 6, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00392       }
00393       else
00394       {
00395         DMD_writeColor(pixelX + 6, pixelY + i, 0x00, 0x00, 0x00, 1);
00396       }
00397       if (bitField & 0x80)
00398       {
00399         DMD_writeColor(pixelX + 7, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00400       }
00401       else
00402       {
00403         DMD_writeColor(pixelX + 7, pixelY + i, 0x00, 0x00, 0x00, 1);
00404       }
00405     }
00406   }
00407 }
00408 
00411 /**************************************************************************/
00416 int RETARGET_ReadChar(void)
00417 {
00418   return -1;
00419 }
00420 
00421 /**************************************************************************/
00426 int RETARGET_WriteChar(char c)
00427 {
00428   if ((BSP_RegisterRead(&BC_REGISTER->UIF_AEM) == BC_UIF_AEM_EFM))
00429   {
00430     if ((BSP_RegisterRead(&BC_REGISTER->ARB_CTRL) != BC_ARB_CTRL_EBI) || (initialized == false))
00431     {
00432       if (initialized)
00433       {
00434         bufferReset = false;
00435         tftReset    = true;
00436         tftTextReset();
00437       }
00438       else
00439       {
00440         bufferReset = true;
00441         tftReset    = true;
00442         tftTextReset();
00443       }
00444       fullUpdate = true;
00445     }
00446   }
00447 
00448   /* Check for form feed - clear screen */
00449   if (c == '\f')
00450   {
00451     bufferReset = true;
00452     tftReset    = false;
00453     tftTextReset();
00454     fullUpdate = true;
00455     return c;
00456   }
00457 
00458   /* Add CR or LF to CRLF if enabled */
00459   if (LFtoCRLF && (c == '\n'))
00460   {
00461     RETARGET_TFTTX('\r');
00462   }
00463   RETARGET_TFTTX(c);
00464 
00465   if (LFtoCRLF && (c == '\r'))
00466   {
00467     RETARGET_TFTTX('\n');
00468   }
00469 
00470   /* Update display */
00471   RETARGET_TFTUpdate(fullUpdate);
00472   fullUpdate = false;
00473   return c;
00474 }