/*############################################################# Program Name : ex3_TS_Faces2 Author : Grant Phillips Date Modified : 05/05/2016 Compiler : ARMmbed Tested On : STM32F429ZI-Discovery Description : A modified version of ex2_TS_Faces.c. In the previous example you would have noticed flickering as you hold your finger on a button. This is because the x and y coordinates are within the specified range for a specific button and this causes the program to continuously call the specific function to draw a face. In this example we modify the code to only call a function once until the the screen touch is released again to prevent this function from being called continuously and thus creating a flicker. Requirements : * STM32F429ZI-Discovery Board Circuit : * NONE ##############################################################*/ #include "mbed.h" #include "TS_DISCO_F429ZI.h" #include "LCD_DISCO_F429ZI.h" void DrawSmile(void); void DrawSad(void); void DrawFrown(void); void DrawSmileButton(void); void DrawFrownButton(void); void DrawSadButton(void); LCD_DISCO_F429ZI lcd; //Create a LCD_DISCO_F429ZI object TS_DISCO_F429ZI ts; //Create a TS_DISCO_F429ZI object int main() { TS_StateTypeDef TS_State; //create a variable to store the state of the touchscreen uint16_t x, y; uint8_t status; uint8_t was_called_once = 0; //variable to indicate if a function was called already lcd.Clear(LCD_COLOR_BLACK); status = ts.Init(240, 320); //initialize the touchscreen for a 240x320 display if (status != TS_OK) { //test if the initialization was ERROR lcd.Clear(LCD_COLOR_RED); lcd.SetBackColor(LCD_COLOR_RED); lcd.SetTextColor(LCD_COLOR_WHITE); lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN", CENTER_MODE); lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"INIT FAIL", CENTER_MODE); while(1); //do not continue if there is an error } DrawSmile(); DrawSmileButton(); DrawFrownButton(); DrawSadButton(); while(1) { ts.GetState(&TS_State); //Read the status of the touchscreen if (TS_State.TouchDetected) { //if the screen was touched x = TS_State.X; //read the x value y = TS_State.Y; //read the y value if((x >= 60) && (x <= 180) && (y >= 140) && (y <= 180) && (was_called_once == 0)) { //if the x and y touch position is within the bounds of the Smile button DrawSmile(); //AND if the function was not called before was_called_once=1; //set the flag to indicate that the function was called once } else if((x >= 60) && (x <= 180) && (y >= 200) && (y <= 240) && (was_called_once == 0)) { //if the x and y touch position is within the bounds of the Frown button DrawFrown(); //AND if the function was not called before was_called_once=1; //set the flag to indicate that the function was called once } else if((x >= 60) && (x <= 180) && (y >= 260) && (y <= 300) && (was_called_once == 0)) { //if the x and y touch position is within the bounds of the Sad button DrawSad(); //AND if the function was not called before was_called_once=1; //set the flag to indicate that the function was called once } else { //do nothing when not incorrect touch position } wait(0.01); //delay to prevent contact bounce on the screen } else was_called_once=0; //if the screen touch was released, reset the flag to indicate that the function can now be called again } } void DrawSmile(void) { uint16_t x, y; x = 120; y = 60; /* Draw face */ lcd.SetTextColor(LCD_COLOR_MAGENTA); lcd.FillCircle(x,y,50); /* Draw eyes */ lcd.SetTextColor(LCD_COLOR_YELLOW); lcd.FillCircle(x-20,y-20,8); lcd.FillCircle(x+20,y-20,8); /* Draw mouth */ lcd.SetTextColor(LCD_COLOR_YELLOW); lcd.DrawLine(x-10,y+20,x+10,y+20); lcd.DrawLine(x-10,y+20,x-20,y+10); lcd.DrawLine(x+10,y+20,x+20,y+10); } void DrawSad(void) { uint16_t x, y; x = 120; y = 60; /* Draw face */ lcd.SetTextColor(LCD_COLOR_MAGENTA); lcd.FillCircle(x,y,50); /* Draw eyes */ lcd.SetTextColor(LCD_COLOR_YELLOW); lcd.FillCircle(x-20,y-20,8); lcd.FillCircle(x+20,y-20,8); /* Draw mouth */ lcd.SetTextColor(LCD_COLOR_YELLOW); lcd.DrawLine(x-10,y+10,x+10,y+10); lcd.DrawLine(x-10,y+10,x-20,y+20); lcd.DrawLine(x+10,y+10,x+20,y+20); } void DrawFrown(void) { uint16_t x, y; x = 120; y = 60; /* Draw face */ lcd.SetTextColor(LCD_COLOR_MAGENTA); lcd.FillCircle(x,y,50); /* Draw eyes */ lcd.SetTextColor(LCD_COLOR_YELLOW); lcd.FillCircle(x-20,y-20,8); lcd.FillCircle(x+20,y-20,8); /* Draw mouth */ lcd.SetTextColor(LCD_COLOR_YELLOW); lcd.DrawLine(x-20,y+15,x+20,y+15); } void DrawSmileButton(void) { uint16_t x, y; x = 60; y = 140; /* Draw rectangle for button */ lcd.SetTextColor(LCD_COLOR_WHITE); lcd.FillRect(x, y, 120, 40); /* Draw text */ lcd.SetFont(&Font24); lcd.SetTextColor(LCD_COLOR_BLUE); lcd.DisplayStringAt(0,y+10,(uint8_t *)"SMILE", CENTER_MODE); } void DrawFrownButton(void) { uint16_t x, y; x = 60; y = 200; /* Draw rectangle for button */ lcd.SetTextColor(LCD_COLOR_WHITE); lcd.FillRect(x, y, 120, 40); /* Draw text */ lcd.SetFont(&Font24); lcd.SetTextColor(LCD_COLOR_BLUE); lcd.DisplayStringAt(0,y+10,(uint8_t *)"FROWN", CENTER_MODE); } void DrawSadButton(void) { uint16_t x, y; x = 60; y = 260; /* Draw rectangle for button */ lcd.SetTextColor(LCD_COLOR_WHITE); lcd.FillRect(x, y, 120, 40); /* Draw text */ lcd.SetFont(&Font24); lcd.SetTextColor(LCD_COLOR_BLUE); lcd.DisplayStringAt(0,y+10,(uint8_t *)"SAD", CENTER_MODE); }