Simple radar simulator. Example of 2D graphics on DISCO-F746NG display.

Dependencies:   BSP_DISCO_F746NG Graphics mbed TS_DISCO_F746NG

Files at this revision

API Documentation at this revision

Comitter:
karpent
Date:
Sat Nov 05 20:24:59 2016 +0000
Parent:
3:732f7144ec81
Commit message:
Code refactoring, 400ms timeout added when button is pressed.

Changed in this revision

PointingDevice/TouchScreen.cpp Show annotated file Show diff for this revision Revisions of this file
PointingDevice/TouchScreen.h Show annotated file Show diff for this revision Revisions of this file
RadarDemo/RadarDemo.cpp Show annotated file Show diff for this revision Revisions of this file
RadarDemo/RadarDemo.h Show annotated file Show diff for this revision Revisions of this file
RadarDemo/RadarTimer.cpp Show annotated file Show diff for this revision Revisions of this file
RadarDemo/RadarTimer.h Show annotated file Show diff for this revision Revisions of this file
diff -r 732f7144ec81 -r 66f13188c26b PointingDevice/TouchScreen.cpp
--- a/PointingDevice/TouchScreen.cpp	Sat Nov 05 15:36:59 2016 +0000
+++ b/PointingDevice/TouchScreen.cpp	Sat Nov 05 20:24:59 2016 +0000
@@ -4,10 +4,37 @@
 
 #include "TouchScreen.h"
 
-TouchScreen::TouchScreen(RK043FN48H* display)
+//Serial pc(USBTX, USBRX);
+
+TouchScreen::TouchScreen(Display* display)
+{
+    _width = display != NULL ? display->DisplayWidth() - 1: 479;
+    _height =  display != NULL ? display->DisplayHeight() - 1: 271;
+}
+
+
+void TouchScreen::Init()
+{
+    TS_DISCO_F746NG::Init(_width, _height);
+}
+
+
+pPoint TouchScreen::DetectTouch()
 {
-    if(display != NULL)
-    {
+    GetState(&tsState);
+    // Debug message
+    //pc.printf(".");
+    
+    if(tsState.touchDetected > 0) {
+        touchPoint.X = tsState.touchX[0];
+        touchPoint.Y = tsState.touchY[0];
 
+        // Debug message
+        //pc.printf("Touch detected at (%d, %d)\r\n", touchPoint.X, touchPoint.Y);
+    
+        //ResetTouchData(&tsState);
+        return &touchPoint;
     }
+
+    return NULL;
 }
\ No newline at end of file
diff -r 732f7144ec81 -r 66f13188c26b PointingDevice/TouchScreen.h
--- a/PointingDevice/TouchScreen.h	Sat Nov 05 15:36:59 2016 +0000
+++ b/PointingDevice/TouchScreen.h	Sat Nov 05 20:24:59 2016 +0000
@@ -29,13 +29,34 @@
     /// <summary>
     /// Initializes a new instance of the <see cref="TouchScreen"/> class.
     /// </summary>
-    TouchScreen(RK043FN48H* display);
+    TouchScreen(Display* display);
 
     /// <summary>
     /// Finalizes an instance of the <see cref="TouchScreen"/> class.
     /// </summary>
     ~TouchScreen();
     
-    pPoint virtual DetectTouch(Window screen) = 0;
+  /**
+    * @brief  Initializes and configures the touch screen functionalities and 
+    *         configures all necessary hardware resources (GPIOs, I2C, clocks..).
+    */
+    void Init();
+         
+    /// <summary>
+    /// Detect a touch.
+    /// Returns a pointer to tpoch point position or NULL if touch was not detected. 
+    /// </summary>
+    pPoint DetectTouch();
+    
+    Point touchPoint;
+    
+private:
+    /// Maximum X size of the TS area on LCD
+    uint16_t _width;
+    
+    /// Maximum Y size of the TS area on LCD
+    uint16_t _height;
+    
+    TS_StateTypeDef tsState;
 };
 
diff -r 732f7144ec81 -r 66f13188c26b RadarDemo/RadarDemo.cpp
--- a/RadarDemo/RadarDemo.cpp	Sat Nov 05 15:36:59 2016 +0000
+++ b/RadarDemo/RadarDemo.cpp	Sat Nov 05 20:24:59 2016 +0000
@@ -8,14 +8,20 @@
 // Size of range control areas on the screen
 const int raSize = 50;
 
+// Button Press Timeout in miliseconds
+const uint32_t buttonPressTimeout = 400;
+
+
 RadarDemo::RadarDemo(Display* display) : Radar(display)
 {
+    _ts = new TouchScreen(display);
 }
 
 
 RadarDemo::~RadarDemo()
 {
     Radar::~Radar();
+    free(_ts);
 }
 
 
@@ -41,11 +47,13 @@
 
     // Force background redraw
     UnvalidateBackground();
+    
+    _buttonPressTime = 0;
 
     // Initialize touchscreen
-    ts.Init(((RK043FN48H*)GetDisplay())->DisplayWidth(), ((RK043FN48H*)GetDisplay())->DisplayHeight());
-
-    timer.Start();
+    _ts->Init();
+   
+    timer.Start();   
 }
 
 
@@ -57,11 +65,7 @@
     lastBeamAngle = currentBeamAngle;
     currentBeamAngle = timer.GetBeamAngle();
 
-    // TODO: 
-    if(DetectTouch(window))
-    {
-        // Set timeout for the next touch detection
-    }
+    CheckUserInput(window); 
 
     RK043FN48H* display = (RK043FN48H*)GetDisplay();
     if(NeedsRedraw()) {
@@ -136,32 +140,49 @@
 }
 
 
-bool RadarDemo::DetectTouch(Window screen)
+UserInputType RadarDemo::CheckUserInput(Window screen)
 {
-    TS_StateTypeDef tsState;
-
-    ts.GetState(&tsState);
+    pPoint p;
+    
+    // There is a timeout after pressing the button
+    if(TouchScreenDisabled())
+        return None;
+        
+    if ((p = _ts->DetectTouch()) != NULL) {
 
-    if (tsState.touchDetected > 0) {
-
-        if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] < screen.y1 + raSize) {
-            if(ChangeRange(1))
+        if(p->X > (screen.x2-raSize) && p->Y < (screen.y1 + raSize)) {
+            if(ChangeRange(1)) {
                 UnvalidateBackground();
+                SetTouchScreenTimeout();
+                
+                return ButtonPressed;
+            }
         }
 
-        else if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] > (screen.y2-raSize)) {
-            if(ChangeRange(-1))
+        if(p->X > (screen.x2-raSize) && p->Y > (screen.y2-raSize)) {
+            if(ChangeRange(-1)) {
                 UnvalidateBackground();
+                SetTouchScreenTimeout();
+                
+                return ButtonPressed;
+            }
         }
 
-        else {
-            SetCenter(tsState.touchX[0], tsState.touchY[0]);
-            UnvalidateBackground();
-        }
+        SetCenter(p->X, p->Y);
+        UnvalidateBackground();
         
-        ts.ResetTouchData(&tsState);
-        return true;
+        return SingleTouch;
     }
 
-    return false;
+    return None;
 }
+
+bool RadarDemo::TouchScreenDisabled()
+{
+    return _buttonPressTime + buttonPressTimeout > timer.GetRunningTime();
+}
+
+void RadarDemo::SetTouchScreenTimeout()
+{
+    _buttonPressTime = timer.GetRunningTime();
+}
diff -r 732f7144ec81 -r 66f13188c26b RadarDemo/RadarDemo.h
--- a/RadarDemo/RadarDemo.h	Sat Nov 05 15:36:59 2016 +0000
+++ b/RadarDemo/RadarDemo.h	Sat Nov 05 20:24:59 2016 +0000
@@ -39,9 +39,18 @@
 
 #pragma once
 
-#include "TS_DISCO_F746NG.h"
 #include "Radar.h"
 #include "RadarTimer.h"
+#include "TouchScreen.h"
+
+typedef enum EUserInputType
+{
+    None = 0,
+    SingleTouch =1,
+    MultiplyTouch = 2,
+    ButtonPressed = 3
+} UserInputType;
+
 
 /// <summary>
 /// Radar display demo class.
@@ -105,11 +114,10 @@
     void AddSampleTargets(int count);
 
     /// <summary>
-    /// Detect a touch on the screen in given area.
-    /// Returns true if the touch has been detected.
+    /// Detect a touch type on the screen in given area.
     /// </summary>
     /// <param name="screen">The screen window coordinates.</param>
-    bool DetectTouch(Window screen);
+    UserInputType CheckUserInput(Window screen);
     
     RadarTimer timer;
 
@@ -119,7 +127,11 @@
     float lastBeamAngle;
 
     bool _needsRedraw;
-
-    TS_DISCO_F746NG ts;
+    
+    TouchScreen* _ts;
+    uint32_t _buttonPressTime;
+    bool TouchScreenDisabled();  
+    void SetTouchScreenTimeout();  
+    
 };
 
diff -r 732f7144ec81 -r 66f13188c26b RadarDemo/RadarTimer.cpp
--- a/RadarDemo/RadarTimer.cpp	Sat Nov 05 15:36:59 2016 +0000
+++ b/RadarDemo/RadarTimer.cpp	Sat Nov 05 20:24:59 2016 +0000
@@ -1,14 +1,21 @@
+//
+// RadarTimer.cpp - Radar time subsystem simulator.
+//
+
 #include "RadarTimer.h"
 #include "Commons.h"
 
 RadarTimer::RadarTimer()
 {
-
+    // Set 10seconds as default value for scan period;
+    _scanPeriod = 10000;
+    _lastScanTime = 0;
 }
 
 RadarTimer::RadarTimer(uint32_t scanPeriod)
 {
     SetScanPeriod(scanPeriod);
+    _lastScanTime = 0;
 }
 
 
@@ -20,10 +27,12 @@
 void RadarTimer::Start()
 {
 #ifndef _SDL_timer_h
-   start();
+    start();
 #endif
+    _lastScanTime = GetRunningTime();
 }
 
+
 uint32_t RadarTimer::GetRunningTime()
 {
 #ifdef _SDL_timer_h
@@ -36,9 +45,9 @@
 
 void RadarTimer::RegisterScan()
 {
-    // Lock scan time  
+    // Lock scan time
     _runningTime = GetRunningTime();
-    
+
     // Reset scan time after one full turn
     if (_runningTime >= (_lastScanTime + _scanPeriod)) {
         _lastScanTime = _runningTime;
@@ -52,6 +61,6 @@
     if(angle >= (2 * M_PI)) {
         angle -= 2 * M_PI;
     }
-    
+
     return angle;
 }
\ No newline at end of file
diff -r 732f7144ec81 -r 66f13188c26b RadarDemo/RadarTimer.h
--- a/RadarDemo/RadarTimer.h	Sat Nov 05 15:36:59 2016 +0000
+++ b/RadarDemo/RadarTimer.h	Sat Nov 05 20:24:59 2016 +0000
@@ -1,8 +1,25 @@
-#include "mbed.h"
+/*
+    RadarTimer.h - Radar time subsystem simulator, inherits timer and implements beam angle calculation method.
+
+    Copyright(c) 2016 karpent at gmail.com, MIT License
+
+    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"),
+    to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+    and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
+
+    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+    THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/ 
 
 
+#include "mbed.h"
+
 /**
-  * @brief Radar time subsystem
+  * @brief Radar time subsystem simulator, inherits timer and implements beam angle calculation method.
   */
 class RadarTimer : public Timer
 {