Buttons changing color when tapped.

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI BSP_DISCO_F429ZI

Files at this revision

API Documentation at this revision

Comitter:
fzajdel
Date:
Sun May 19 11:59:32 2019 +0000
Parent:
1:dec3203ec015
Commit message:
Touch screen with buttons

Changed in this revision

button.cpp Show annotated file Show diff for this revision Revisions of this file
button.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
rectangle.cpp Show annotated file Show diff for this revision Revisions of this file
widget.cpp Show diff for this revision Revisions of this file
diff -r dec3203ec015 -r bd49dd9c7bd6 button.cpp
--- a/button.cpp	Thu Apr 04 15:27:33 2019 +0000
+++ b/button.cpp	Sun May 19 11:59:32 2019 +0000
@@ -32,6 +32,8 @@
 
 void Button::Check(uint16_t X, uint16_t Y){
     
+    State = (CheckRectRange(X,Y) ? PRESSED : RELEASED);
+    
     switch(State){
         case PRESSED:
             OnPress();
@@ -40,13 +42,16 @@
             OnRelease();
             break;
     }
-    
-    State = (CheckRectRange(X,Y) ? PRESSED : RELEASED);
-};          
+};         
+
+bool Button::isPressed(uint16_t X, uint16_t Y){
+    return (CheckRectRange(X,Y) ? PRESSED : RELEASED);
+} 
 
 void Button::OnPress(){
     
     MyRectangle->SetFillingColor(BUTTON_PUSH_COLOR);
+    
     if(NULL != CbOnPress){
         CbOnPress->Execute();
     }
@@ -55,6 +60,7 @@
 void Button::OnRelease(){
     
     MyRectangle->SetFillingColor(BUTTON_RELEASE_COLOR);
+    
     if(NULL != CbOnRelease) {
         CbOnRelease->Execute();
     }
diff -r dec3203ec015 -r bd49dd9c7bd6 button.h
--- a/button.h	Thu Apr 04 15:27:33 2019 +0000
+++ b/button.h	Sun May 19 11:59:32 2019 +0000
@@ -16,6 +16,7 @@
     Button(uint8_t x, uint8_t y, uint8_t width, uint8_t height, WidgetCallback* CbOnPress, WidgetCallback* CbOnRelease, string Text = RECTANGLE_DEFAULT_TEXT);
     virtual ~Button(){};
     virtual void Check(uint16_t X, uint16_t Y);
+    virtual bool isPressed(uint16_t X, uint16_t Y);
     virtual Figure *GetFigure();   // Temporary Solution
     
 private:
diff -r dec3203ec015 -r bd49dd9c7bd6 main.cpp
--- a/main.cpp	Thu Apr 04 15:27:33 2019 +0000
+++ b/main.cpp	Sun May 19 11:59:32 2019 +0000
@@ -2,58 +2,51 @@
 #include "TS_DISCO_F429ZI.h"
 #include "LCD_DISCO_F429ZI.h"
 #include "rectangle.h"
-#include "widget.h"
 #include "config.h"
 #include "button.h"
 #include <vector>
-#include <sstream>
 
 using std::vector;
 
-LCD_DISCO_F429ZI lcd;
-TS_DISCO_F429ZI ts;
-
 int main()
 {
-    vector <Figure*> figures(RECTANGLE_NR);
+    LCD_DISCO_F429ZI LcdScreen;
+    TS_DISCO_F429ZI TouchScreen;
+    vector <Figure*> Figures(RECTANGLE_NR);
     vector <Widget*> Buttons(BUTTONS_NR);
+    uint8_t XCoord = 0, YCoord = 0, ButtonCounter = 0;
     
     for(vector<Widget*>::iterator itWidget=Buttons.begin(); itWidget != Buttons.end(); itWidget++){
-        static uint8_t ButtonCounter = 0;
-        static uint8_t x = 0;
-        static uint8_t y = 0;
+        
         char ButtonCounterStr[4];
-        
-        sprintf(ButtonCounterStr, "%u", ButtonCounter++);
+        sprintf(ButtonCounterStr, "%u", ButtonCounter);
 
-        *itWidget = new Button(x, ((0 != y) ? y-1:y) ,RECTANGE_WIDTH, RECTANGE_HEIGHT, ButtonCounterStr);
-        y += RECTANGE_HEIGHT;   
+        *itWidget = new Button(XCoord, ((0 != YCoord) ? YCoord-1:YCoord) ,RECTANGE_WIDTH, RECTANGE_HEIGHT, ButtonCounterStr);
+        YCoord += RECTANGE_HEIGHT;   
+        ButtonCounter++;
     }
   
-    BSP_LCD_SetFont(&Font24);
+    BSP_LCD_SetFont(&Font24);    
+    LcdScreen.Clear(LCD_COLOR_BLACK);
+    LcdScreen.SetBackColor(LCD_COLOR_BLACK);
+    uint8_t TsStatus = TouchScreen.Init(LcdScreen.GetXSize(), LcdScreen.GetYSize());
     
-    wait(1);
-  
-    uint8_t status = ts.Init(lcd.GetXSize(), lcd.GetYSize());    
-    
-    lcd.Clear(LCD_COLOR_BLACK);
-    lcd.SetBackColor(LCD_COLOR_BLACK);
-    
-    while(1)
+    while(TS_OK == TsStatus)
     {
-        TS_StateTypeDef TS_State;
-        ts.GetState(&TS_State);  
-        uint16_t x=SCREEN_WIDTH+1, y=SCREEN_HEIGHT+1;
+        TS_StateTypeDef TsState;
+        uint16_t XCoord = SCREEN_WIDTH+1, YCoord = SCREEN_HEIGHT+1;
+        
+        TouchScreen.GetState(&TsState);  
         
-        if (TS_State.TouchDetected){
-            x = TS_State.X;
-            y = TS_State.Y;
-        }
-        
-        for(vector<Widget*>::iterator itWidget=Buttons.begin(); itWidget != Buttons.end(); itWidget++){
-            Figure *Rect = (*itWidget)->GetFigure();
-            lcd<<Rect;  
-            (*itWidget)->Check(x, y); 
+        for(vector<Widget*>::iterator ItWidget=Buttons.begin(); ItWidget != Buttons.end(); ItWidget++){
+            
+            if(TsState.TouchDetected){
+                XCoord = TsState.X;
+                YCoord = TsState.Y;
+            } 
+            
+            (*ItWidget)->Check(XCoord, YCoord);
+            LcdScreen << (*ItWidget)->GetFigure();
         }
         
         wait(0.1);
diff -r dec3203ec015 -r bd49dd9c7bd6 rectangle.cpp
--- a/rectangle.cpp	Thu Apr 04 15:27:33 2019 +0000
+++ b/rectangle.cpp	Sun May 19 11:59:32 2019 +0000
@@ -33,7 +33,7 @@
 void Rectangle::DisplayString(LCD_DISCO_F429ZI &lcd){
     
     uint8_t *uText = new uint8_t [Text.length()+1];
-    sprintf((char*)uText, "%s", Text);
+    sprintf((char*)uText, "%s", Text.c_str());
     lcd.SetBackColor(TextBackColor);
     lcd.SetTextColor(TextColor);
     lcd.DisplayStringAt(x, y, uText, LEFT_MODE);   
diff -r dec3203ec015 -r bd49dd9c7bd6 widget.cpp