a demo of GUI on DISCOF7 consisting of a few buttons and feeders

Dependencies:   ADXL345 BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG

Files at this revision

API Documentation at this revision

Comitter:
habiburrahman
Date:
Sat Aug 06 02:48:56 2016 +0000
Parent:
2:76522cfa03cd
Child:
4:8c94c3ee8a3a
Commit message:
fixed bugs,created objects for buttons and feeders for better control, button presses are controlled

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Aug 05 23:49:42 2016 +0000
+++ b/main.cpp	Sat Aug 06 02:48:56 2016 +0000
@@ -6,67 +6,157 @@
 #include <string>
 using namespace std;
  
+ 
+#define BUTTON_DEPRESS_TIME_MIN 0.065000f
 
  
 LCD_DISCO_F746NG lcd;
 TS_DISCO_F746NG ts;
 TS_StateTypeDef TS_State;
+ 
+typedef struct{
+     uint16_t x;
+     uint16_t y;
+     uint16_t width;
+     uint16_t height;
+}rectData_t;
 
+typedef struct{
+    rectData_t rectObj;
+    uint16_t value;
+    bool update_flag;
+}hFeeder_t;
+
+typedef struct{
+    rectData_t rectObj;
+    bool isPressed;
+    bool update_flag;
+    Timer depressTime;
+}button_t;
+ 
 
 
-bool touch_test_rectangle(uint16_t tx, uint16_t ty, uint16_t x, uint16_t y, uint16_t width, uint16_t height){
+rectData_t but1pos = {48, 68, 96, 60};
+rectData_t but2pos = {192, 68, 96, 60};
+rectData_t but3pos = {338, 68, 96, 60};
+rectData_t hFeed1pos = {48, 152, 384, 30};
+rectData_t hFeed2pos = {48, 212, 384, 30};
 
+button_t   button1 = {but1pos,0,0};
+button_t   button2 = {but2pos,0,0};
+button_t   button3 = {but3pos,0,0};
+hFeeder_t  hFeed1 = {hFeed1pos,0, 0};
+hFeeder_t  hFeed2 = {hFeed2pos,0, 0};
+
+void button(button_t *buttonObj, TS_StateTypeDef *TS_State){
     bool x_is_in =0;
-    bool y_is_in = 0;
+    bool y_is_in =0; 
+    
+    uint16_t dyn_x = TS_State->touchX[0];
+    uint16_t dyn_y = TS_State->touchY[0];
+
+    if( (dyn_x>buttonObj->rectObj.x) && (dyn_x<(buttonObj->rectObj.x+buttonObj->rectObj.width)))  x_is_in = 1;
+    if( (dyn_y>buttonObj->rectObj.y) && (dyn_y<(buttonObj->rectObj.y+buttonObj->rectObj.height)))  y_is_in = 1;  
     
-    if( (tx>x) && (tx<(x+width)))  x_is_in = 1;
-    if( (ty>y) && (ty<(y+height)))  y_is_in = 1;
+    if(x_is_in && y_is_in){
+        if(!(buttonObj->isPressed)){
+            buttonObj->depressTime.start();
+            buttonObj->isPressed = 1;
+            lcd.SetTextColor(LCD_COLOR_GREEN);
+            lcd.FillRect(buttonObj->rectObj.x, buttonObj->rectObj.y, buttonObj->rectObj.width, buttonObj->rectObj.height);
+        }
+    }
+    
     
-    bool buttest;
-    buttest = x_is_in & y_is_in;
-    
-    return (bool) buttest;
+    else{
+        if(buttonObj->isPressed){
+            buttonObj->depressTime.stop();
+            buttonObj->isPressed = 0;
+            if(buttonObj->depressTime.read()>BUTTON_DEPRESS_TIME_MIN){
+                buttonObj->update_flag=1;
+
+            }
+            buttonObj->depressTime.reset();
+            lcd.SetTextColor(LCD_COLOR_RED);
+            lcd.FillRect(buttonObj->rectObj.x, buttonObj->rectObj.y, buttonObj->rectObj.width, buttonObj->rectObj.height);
+        }
+    }   
 }
 
 
-uint16_t h_feeder(uint16_t tx, uint16_t ty,uint16_t x, uint16_t y, uint16_t width, uint16_t height){
-    
+void h_feeder(hFeeder_t *feederObj, TS_StateTypeDef *TS_State){
     bool x_is_in =0;
     bool y_is_in =0;
     
-    if( (tx>x) && (tx<(x+width)))  x_is_in = 1;
-    if( (ty>y) && (ty<(y+height)))  y_is_in = 1;
+    uint16_t dyn_x = TS_State->touchX[0];
+    uint16_t dyn_y = TS_State->touchY[0];
     
-    bool buttest;
-    buttest = x_is_in & y_is_in;
+    if( (dyn_x>feederObj->rectObj.x) && (dyn_x<(feederObj->rectObj.x+feederObj->rectObj.width)))  x_is_in = 1;
+    if( (dyn_y>feederObj->rectObj.y) && (dyn_y<(feederObj->rectObj.y+feederObj->rectObj.height)))  y_is_in = 1;
     
-    if(buttest){
-        return tx - x;
+
+    if(x_is_in && y_is_in){
+           uint16_t newFeedValue;
+           newFeedValue = dyn_x - feederObj->rectObj.x;
+           if(newFeedValue != feederObj->value){
+                feederObj->update_flag = 1;
+                feederObj->value = newFeedValue; 
+                            
+                lcd.SetTextColor(LCD_COLOR_RED);
+                lcd.FillRect(feederObj->rectObj.x, feederObj->rectObj.y, feederObj->rectObj.width, feederObj->rectObj.height);
+                lcd.SetTextColor(LCD_COLOR_GREEN);
+                lcd.FillRect(feederObj->rectObj.x, feederObj->rectObj.y, feederObj->value, feederObj->rectObj.height);  
+            }
     }
-    
-    
+}   
+
+int b1cnt=0, b2cnt=0,b3cnt=0;
+
+void button1_callback(){
+    b1cnt++;
+    button1.update_flag = 0;
 }
 
-bool but1=0,but2=0,but3=0;
-uint16_t feed1_dist = 0;
-uint16_t feed2_dist = 0;
+void button2_callback(){
+    b2cnt++;
+    button2.update_flag = 0;
+}
+
+void button3_callback(){
+    b3cnt++;
+    button3.update_flag = 0;
+}
 
-int main2()
-{
-   
-    uint16_t x, y;
-    uint8_t text[30];
-    uint8_t status;
-    uint8_t idx;
-    uint8_t cleared = 0;
-    uint8_t prev_nb_touches = 0;
+void hFeeder1_callback(){
+    //lcd.SetTextColor(LCD_COLOR_RED);
+    //lcd.FillRect(hFeed1pos.x, hFeed1pos.y, hFeed1pos.width, hFeed1pos.height);
+    //lcd.SetTextColor(LCD_COLOR_GREEN);
+    //lcd.FillRect(hFeed1pos.x, hFeed1pos.y, hFeed1.value, hFeed1pos.height);
+    hFeed1.update_flag = 0;
+}
 
-    lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
-    wait(1);
+void hFeeder2_callback(){
+    //lcd.SetTextColor(LCD_COLOR_RED);
+    //lcd.FillRect(hFeed2pos.x, hFeed2pos.y, hFeed2pos.width, hFeed2pos.height);
+    //lcd.SetTextColor(LCD_COLOR_GREEN);
+    //lcd.FillRect(hFeed2pos.x, hFeed2pos.y, hFeed2.value, hFeed2pos.height);
+    hFeed2.update_flag = 0;     
+}
 
 
 
 
+int main(){
+
+
+    
+    
+    
+    uint8_t status;
+    
+    lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
+    wait(1);
+
     status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
     if (status != TS_OK) {
         lcd.Clear(LCD_COLOR_RED);
@@ -87,76 +177,44 @@
     
     
     lcd.SetTextColor(LCD_COLOR_RED);
-    lcd.FillRect(48, 68, 96, 60);
-    lcd.FillRect(192, 68, 96, 60);
-    lcd.FillRect(338, 68, 96, 60);
+    lcd.FillRect(but1pos.x, but1pos.y, but1pos.width, but1pos.height);
+    lcd.FillRect(but2pos.x, but2pos.y, but2pos.width, but2pos.height);
+    lcd.FillRect(but3pos.x, but3pos.y, but3pos.width, but3pos.height);
     
-    lcd.FillRect(48, 152, 384, 30);
-    lcd.FillRect(48, 212, 384, 30);
+    lcd.FillRect(hFeed1pos.x, hFeed1pos.y, hFeed1pos.width, hFeed1pos.height);
+    lcd.FillRect(hFeed2pos.x, hFeed2pos.y, hFeed2pos.width, hFeed2pos.height);
     
     wait(1);
 
-    
+
     while(1){
         
-        but1=0;
-        but2=0;
-        but3=0;
-        
+        ts.ResetTouchData(&TS_State);
         ts.GetState(&TS_State);
-        if (TS_State.touchDetected) {
-            
-            feed1_dist = h_feeder(TS_State.touchX[0],TS_State.touchY[0],48, 152, 384, 30);
-            feed2_dist = h_feeder(TS_State.touchX[0],TS_State.touchY[0],48, 212, 384, 30);
-            but1 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 48, 68, 96, 60);
-            but2 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 192, 68, 96, 60);
-            but3 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 338, 68, 96, 60);
+        //if (TS_State.touchDetected) 
+        {
+            h_feeder(&hFeed1, &TS_State);
+            h_feeder(&hFeed2, &TS_State);
+            button(&button1, &TS_State);
+            button(&button2, &TS_State);
+            button(&button3, &TS_State);
             
-            if(feed1_dist>0){
-            lcd.SetTextColor(LCD_COLOR_RED);
-            lcd.FillRect(48, 152, 384, 30);
-            lcd.SetTextColor(LCD_COLOR_GREEN);
-            lcd.FillRect(48, 152, feed1_dist, 30);
-            }
-            
-            if(feed2_dist>0){
-            lcd.SetTextColor(LCD_COLOR_RED);
-            lcd.FillRect(48, 212, 384, 30);
-            lcd.SetTextColor(LCD_COLOR_GREEN);
-            lcd.FillRect(48, 212, feed2_dist, 30);
-            }   
         }
         
-        if(but1){
-                lcd.SetTextColor(LCD_COLOR_GREEN);
-                lcd.FillRect(48, 68, 96, 60);        
-        }
-        else{
-                lcd.SetTextColor(LCD_COLOR_RED);
-                lcd.FillRect(48, 68, 96, 60);   
-        }
+        if(hFeed1.update_flag)hFeeder1_callback();
+        if(hFeed2.update_flag)hFeeder2_callback();
+        if(button1.update_flag) button1_callback();
+        if(button2.update_flag) button2_callback();
+        if(button3.update_flag) button3_callback();
         
-        if(but2){
-                lcd.SetTextColor(LCD_COLOR_GREEN);
-                lcd.FillRect(192, 68, 96, 60);
-        }
-        else{
-                lcd.SetTextColor(LCD_COLOR_RED);
-                lcd.FillRect(192, 68, 96, 60); 
-        }
-        if(but3){
-                lcd.SetTextColor(LCD_COLOR_GREEN);
-                lcd.FillRect(338, 68, 96, 60);
-        }
-        else{
-                lcd.SetTextColor(LCD_COLOR_RED);
-                lcd.FillRect(338, 68, 96, 60);   
-        }        
+        printf("feeder values are %d,%d,%d  %d,%d\r\n", b1cnt, b2cnt,b3cnt,hFeed1.value, hFeed2.value);
+        //printf("feeder values are (%d,%d) %f,%f,%f  %d,%d\r\n", TS_State.touchX[0],TS_State.touchY[0],button1.depressTime.read(), button2.depressTime.read(),button3.depressTime.read(),hFeed1.value, hFeed2.value);
         
     }
 }
 
 
+
 /////////////////////////////////////////////////////////////////////
 //ACCELEROMETER TEST
 
@@ -165,7 +223,7 @@
 ADXL345 accelerometer(D11,D12,D13,D10);
 Serial pc(USBTX, USBRX);
 
-int main() {
+int main2() {
  
     int readings[3] = {0, 0, 0};