James Kemp / Mbed 2 deprecated DISCO-F429ZI_LCD_demo

Dependencies:   BSP_DISCO_F429ZI LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //
00002 //  Powerhouse Electronics - Fluid Dispense System
00003 //      By: Jim Kemp June 6th, 2017
00004 //
00005 //  Notes: Screen Size: 240x320
00006 
00007 #include "TS_DISCO_F429ZI.h"
00008 #include "LCD_DISCO_F429ZI.h"
00009 #include "mbed.h"
00010 
00011 #define FLASH_USER_START_ADDR   FLASH_SECTOR_10     /* Start @ of user Flash area */
00012 #define FLASH_USER_END_ADDR     FLASH_SECTOR_11     /* End @ of user Flash area */
00013 
00014 #define TS_IDLE             15  // Reset for touch screen idle. On idle, save values.
00015 //#define BUFFER_SIZE         ((uint32_t)0x0100)
00016 //#define WRITE_READ_ADDR     ((uint32_t)0x0800)
00017 #define WRITE_READ_ADDR     ((uint32_t)0x0000)
00018 
00019 LCD_DISCO_F429ZI lcd;
00020 TS_DISCO_F429ZI ts;
00021 Timer tsTimer;
00022 
00023 DigitalOut led1(LED1);
00024 bool tsPress( uint16_t *x, uint16_t *y );
00025 
00026 // Used to define a bounding box by x,y and width, height.
00027 typedef struct {
00028     uint16_t x, y;
00029     uint16_t width, height;
00030 } box;
00031 
00032 // Used to define a point.
00033 typedef struct {
00034     uint16_t x, y;
00035 } pt;
00036 
00037 typedef enum { P0, P1, P2, P3, INC, DEC, NONE } winLocs_t;
00038 
00039 const box butLeft =  { 0,   270,  110, 50 };
00040 const box butRight = { 130, 270,  110, 50 };
00041 const box parm0Win = { 0,0,   239, 55 };
00042 const box parm1Win = { 0,60,  239, 55 };
00043 const box parm2Win = { 0,120, 239, 55 };
00044 const box parm3Win = { 0,180, 239, 55 };
00045 
00046 static void SystemClock_Config();
00047 bool testTouch( uint16_t x, uint16_t y, box b );
00048 void myFillRect( LCD_DISCO_F429ZI lcd, box b );
00049 void myDrawRect( LCD_DISCO_F429ZI lcd, box b, uint32_t color );
00050 void dispParm( LCD_DISCO_F429ZI lcd, uint8_t parmNum, char *s, char *units, float val );
00051 winLocs_t locateTouch( uint16_t x, uint16_t y );
00052 
00053 uint32_t FirstSector = 0, NbOfSectors = 0, Address = 0;
00054 uint32_t SectorError = 0;
00055 __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
00056 
00057 int main() {      
00058     led1 = 1;
00059     //char s[20];
00060     float cnt=20.0f, cnt2=12.0f, chg=0.1f;
00061     uint8_t status;
00062     uint16_t x=0, y=0;  // Touch Screen X/Y coord.
00063     uint8_t tsFast=0;   // Switch to fast change after a long touch.
00064     uint8_t tsIdle=0;   // After changes are made, on idle save values.
00065     winLocs_t activeWin = NONE;
00066     uint32_t vals[10];
00067 
00068     FLASH_EraseInitTypeDef EraseInitStruct;
00069     
00070     HAL_Init();
00071     SystemClock_Config();
00072     //wait( 1 );
00073 
00074     //vals[0] = (uint32_t)0;
00075     //vals[1] = (uint32_t)0;
00076     vals[0] = (uint32_t)222;
00077     vals[1] = (uint32_t)111;
00078     wait_ms( 50 );
00079 
00080     FirstSector = FLASH_USER_START_ADDR;
00081     /* Get the number of sector to erase from 1st sector*/
00082     NbOfSectors = FLASH_USER_END_ADDR - FirstSector + 1;
00083     
00084     /* Fill EraseInit structure*/
00085     EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
00086     EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3;
00087     EraseInitStruct.Sector = FirstSector;
00088     EraseInitStruct.NbSectors = NbOfSectors;
00089 
00090     HAL_FLASH_Unlock();
00091     __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR 
00092         | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR );
00093     //FLASH_Erase_Sector( FLASH_USER_START_ADDR, VOLTAGE_RANGE_3 );
00094     HAL_FLASHEx_Erase( &EraseInitStruct, &SectorError );
00095 
00096     Address = FLASH_USER_START_ADDR;
00097     HAL_FLASH_Program( TYPEPROGRAM_WORD, Address, vals[0]);
00098     HAL_FLASH_Program( TYPEPROGRAM_WORD, Address+4, vals[1]);
00099     HAL_FLASH_Lock();
00100 
00101     lcd.Clear( LCD_COLOR_BLACK );
00102     lcd.SetBackColor( LCD_COLOR_BLACK );
00103     lcd.SetTextColor( LCD_COLOR_WHITE );
00104     BSP_LCD_SetFont( &Font20 );
00105     lcd.DisplayStringAt( 0, LINE(4), (uint8_t *)"Powerhouse-", LEFT_MODE );
00106     lcd.DisplayStringAt( 0, LINE(5), (uint8_t *)"Electronics", CENTER_MODE );
00107     BSP_LCD_SetFont( &Font24 );
00108     lcd.DisplayStringAt( 0, LINE(8), (uint8_t *)"Ph-Elec.com", CENTER_MODE );
00109     printf( "\nPowerhouse Electronics\n" );
00110     printf( "Dispencer System\n" );
00111 
00112     Address = FLASH_USER_START_ADDR;
00113     vals[0] = *(__IO uint32_t*)Address;
00114     vals[1] = *(__IO uint32_t*)(Address+4);
00115     
00116     printf( "Read Data Done: %d %d\n", (int16_t)vals[0], (int16_t)vals[1] );
00117 
00118     wait( 1 );
00119 
00120     lcd.Clear(LCD_COLOR_BLACK);
00121     status = ts.Init( lcd.GetXSize(), lcd.GetYSize() );
00122     if ( status != TS_OK ) {
00123         lcd.Clear(LCD_COLOR_RED);
00124         lcd.SetBackColor(LCD_COLOR_RED);
00125         lcd.SetTextColor(LCD_COLOR_WHITE);
00126         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN", CENTER_MODE);
00127         lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"INIT FAIL", CENTER_MODE);
00128         while ( 1 );
00129     }
00130 
00131     printf( "Screen Size: %d / %d\n", lcd.GetXSize(), lcd.GetYSize() );
00132 
00133     lcd.SetTextColor(LCD_COLOR_BLUE);
00134     myFillRect( lcd, butLeft );
00135     myFillRect( lcd, butRight );
00136     lcd.SetBackColor(LCD_COLOR_BLUE);
00137     lcd.SetTextColor(LCD_COLOR_WHITE);
00138     BSP_LCD_SetFont(&Font24);
00139     lcd.DisplayStringAt( 20, LINE(12), (uint8_t *)"Inc.", LEFT_MODE);
00140     lcd.DisplayStringAt( 160, LINE(12), (uint8_t *)"Dec.", LEFT_MODE);
00141     lcd.SetBackColor(LCD_COLOR_BLACK);
00142     
00143     while ( 1 ) {
00144         
00145         if ( tsPress( &x, &y ) ) {
00146             if ( activeWin == P0 ) {
00147                 if ( testTouch( x,y, butLeft ) ) {
00148                     cnt += chg;
00149                     if ( cnt >= 1000.0f ) cnt = 999.9f;
00150                 }
00151                 if ( testTouch( x,y, butRight ) ) {
00152                     cnt -= chg;
00153                     if ( cnt < 0.0f ) cnt = 0.0f;
00154                 }
00155             }
00156             
00157             if ( activeWin == P1 ) {
00158                 if ( testTouch( x,y, butLeft ) ) {
00159                     cnt2 += chg;
00160                     if ( cnt2 >= 1000.0f ) cnt2 = 999.9f;
00161                 }
00162                 if ( testTouch( x,y, butRight ) ) {
00163                     cnt2 -= chg;
00164                     if ( cnt2 < 0.0f ) cnt2 = 0.0f;
00165                 }
00166             }
00167 
00168             tsFast += 1;
00169             if ( tsFast >  5 ) chg = 1.0;
00170             if ( tsFast > 25 ) chg = 10.0;
00171             if ( tsFast > 50 ) chg = 100.0;
00172             tsIdle = TS_IDLE;
00173         }
00174         else {
00175             tsFast = 0;
00176             chg = 0.1f;
00177             if ( tsIdle == 1 ) printf( "Save Values...\n" );
00178             if ( tsIdle ) --tsIdle;
00179         }
00180 
00181         // Update faster and faster when the ts is held down long.
00182         if ( tsFast < 5 ) wait( 0.3 );
00183         else if ( tsFast < 20 ) wait( 0.2 );
00184         else wait( 0.1 );
00185 
00186         dispParm( lcd, 0, "Dispense Speed:", "mm/min", cnt );
00187         dispParm( lcd, 1, "PreFill Dist:", "mm", cnt2 );
00188         dispParm( lcd, 2, "PostRetact Dist:", "mm", 12.0f );
00189         dispParm( lcd, 3, "Pre/Post Speed:", "mm/min", 100.0f );
00190         
00191         if( tsIdle == TS_IDLE ) {
00192             if( (locateTouch( x,y ) == P0) && (activeWin != P0) ) {
00193                 printf( "P0 Selected.\n" );
00194                 activeWin = P0;
00195                 myDrawRect( lcd, parm0Win, LCD_COLOR_RED );
00196                 myDrawRect( lcd, parm1Win, LCD_COLOR_BLACK );
00197                 myDrawRect( lcd, parm2Win, LCD_COLOR_BLACK );
00198                 myDrawRect( lcd, parm3Win, LCD_COLOR_BLACK );
00199             }
00200 
00201             if( (locateTouch( x,y ) == P1) && (activeWin != P1) ) {
00202                 printf( "P1 Selected.\n" );
00203                 activeWin = P1;
00204                 myDrawRect( lcd, parm0Win, LCD_COLOR_BLACK );
00205                 myDrawRect( lcd, parm1Win, LCD_COLOR_RED );
00206                 myDrawRect( lcd, parm2Win, LCD_COLOR_BLACK );
00207                 myDrawRect( lcd, parm3Win, LCD_COLOR_BLACK );
00208             }
00209 
00210             if( (locateTouch( x,y ) == P2) && (activeWin != P2) ) {
00211                 printf( "P2 Selected.\n" );
00212                 activeWin = P2;
00213                 myDrawRect( lcd, parm0Win, LCD_COLOR_BLACK );
00214                 myDrawRect( lcd, parm1Win, LCD_COLOR_BLACK );
00215                 myDrawRect( lcd, parm2Win, LCD_COLOR_RED );
00216                 myDrawRect( lcd, parm3Win, LCD_COLOR_BLACK );
00217             }
00218 
00219             if( (locateTouch( x,y ) == P3) && (activeWin != P3) ) {
00220                 printf( "P3 Selected.\n" );
00221                 activeWin = P3;
00222                 myDrawRect( lcd, parm0Win, LCD_COLOR_BLACK );
00223                 myDrawRect( lcd, parm1Win, LCD_COLOR_BLACK );
00224                 myDrawRect( lcd, parm2Win, LCD_COLOR_BLACK );
00225                 myDrawRect( lcd, parm3Win, LCD_COLOR_RED );
00226             }
00227         }
00228         if ( tsIdle == 1 ) {
00229             if( activeWin != NONE ) {
00230                 myDrawRect( lcd, parm0Win, LCD_COLOR_BLACK );
00231                 myDrawRect( lcd, parm1Win, LCD_COLOR_BLACK );
00232                 myDrawRect( lcd, parm2Win, LCD_COLOR_BLACK );
00233                 myDrawRect( lcd, parm3Win, LCD_COLOR_BLACK );
00234                 activeWin = NONE;
00235             }       
00236         }
00237 
00238         led1 = !led1;
00239     }
00240 }
00241 
00242 // ============================================================================
00243 winLocs_t locateTouch( uint16_t x, uint16_t y ) {
00244     if( testTouch( x,y, parm0Win ) ) return P0; 
00245     if( testTouch( x,y, parm1Win ) ) return P1; 
00246     if( testTouch( x,y, parm2Win ) ) return P2; 
00247     if( testTouch( x,y, parm3Win ) ) return P3; 
00248     return NONE;
00249 }
00250 
00251 // Paint a parameter on the screen. 
00252 // ============================================================================
00253 void dispParm( LCD_DISCO_F429ZI lcd, uint8_t parmNum, char *s, char *units, float val ) {
00254     char str[20];
00255     uint16_t y = parmNum * 60;
00256     //box p;
00257     
00258     BSP_LCD_SetFont( &Font20 );
00259     lcd.DisplayStringAt(4, y+6, (uint8_t *)s, LEFT_MODE );
00260     BSP_LCD_SetFont( &Font24 );
00261     sprintf( str, "%5.1f%s ", val, units );
00262     lcd.DisplayStringAt( 0, y+30, (uint8_t *)str, RIGHT_MODE );
00263     
00264     //p.x=0; p.y=y;
00265     //p.width=239; p.height=55;
00266     //myDrawRect( lcd, p, LCD_COLOR_WHITE );
00267 }
00268 
00269 // Helper fuction to fill a rect given a lcd handle and a box struct.
00270 // ============================================================================
00271 void myFillRect( LCD_DISCO_F429ZI lcd, box b ) {
00272     lcd.FillRect( b.x, b.y,  b.width, b.height );
00273 }
00274 
00275 // ============================================================================
00276 void myDrawRect( LCD_DISCO_F429ZI lcd, box b, uint32_t color ) {
00277     uint32_t tmpColor = lcd.GetTextColor();     // Save current color.
00278     
00279     lcd.SetTextColor( color );
00280     lcd.DrawRect( b.x, b.y,  b.width, b.height );
00281     // Draw an inner box to make the line 2 pixels wide.
00282     lcd.DrawRect( b.x+1, b.y+1,  b.width-2, b.height-2 );
00283     
00284     lcd.SetTextColor( tmpColor );               // Restore original color.
00285 }
00286 
00287 
00288 // Return true if x,y is within bounding box struct.
00289 // ============================================================================
00290 bool testTouch( uint16_t x, uint16_t y, box b ) {
00291 
00292     if ( (x <= b.x) || (x >= (b.x+b.width)) )       return false;
00293     if ( (y <= b.y) || (y >= (b.y+b.height)) )  return false;
00294     return true;
00295 }
00296 
00297 // Update the param X/Y values and return true if new touch values.
00298 // ============================================================================
00299 bool tsPress( uint16_t* xx, uint16_t* yy  ) {
00300     TS_StateTypeDef TS_State;
00301     static bool flgTouch=true;
00302     static uint16_t x=0, y=0;
00303 
00304     tsTimer.start();
00305 
00306     ts.GetState(&TS_State);      
00307     if ( TS_State.TouchDetected ) {
00308         x = TS_State.X;
00309         y = TS_State.Y;
00310         printf("* x=%d y=%d\n", x, y);
00311         tsTimer.reset();    // Reset on new touch.
00312         flgTouch = true;    // Set flag on new touch.
00313     }
00314     if ( flgTouch && (tsTimer.read_ms() > 250) ) {
00315         printf("  x=%d y=%d\n", x, y);
00316         flgTouch = false;
00317     }
00318 
00319     *xx = x; *yy = y;
00320     return flgTouch;    // Return true on touch.
00321 }
00322 
00323 static void SystemClock_Config(void)
00324 {
00325   RCC_ClkInitTypeDef RCC_ClkInitStruct;
00326   RCC_OscInitTypeDef RCC_OscInitStruct;
00327 
00328   /* Enable Power Control clock */
00329   __PWR_CLK_ENABLE();
00330   
00331   /* The voltage scaling allows optimizing the power consumption when the device is 
00332      clocked below the maximum system frequency, to update the voltage scaling value 
00333      regarding system frequency refer to product datasheet.  */
00334   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
00335   
00336   /* Enable HSE Oscillator and activate PLL with HSE as source */
00337   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
00338   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
00339   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
00340   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
00341   RCC_OscInitStruct.PLL.PLLM = 8;
00342   RCC_OscInitStruct.PLL.PLLN = 336;
00343   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
00344   RCC_OscInitStruct.PLL.PLLQ = 7;
00345   HAL_RCC_OscConfig(&RCC_OscInitStruct);
00346  
00347   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
00348      clocks dividers */
00349   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
00350   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
00351   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
00352   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
00353   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
00354   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
00355 }