Miroslaw K. / Mbed 2 deprecated RadarDemo

Dependencies:   BSP_DISCO_F746NG Graphics mbed TS_DISCO_F746NG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RadarDemo.cpp Source File

RadarDemo.cpp

00001 //
00002 // RadarDemo.cpp - example of graphic commands usage to create 2D graphics.
00003 //
00004 
00005 #include "RadarDemo.h"
00006 #include "RK043FN48H.h"
00007 
00008 // Size of range control areas on the screen
00009 const int raSize = 50;
00010 
00011 // Button Press Timeout in miliseconds
00012 const uint32_t buttonPressTimeout = 400;
00013 
00014 
00015 RadarDemo::RadarDemo(Display* display) : Radar(display)
00016 {
00017     _ts = new TouchScreen(display);
00018 }
00019 
00020 
00021 RadarDemo::~RadarDemo()
00022 {
00023     Radar::~Radar();
00024     free(_ts);
00025 }
00026 
00027 
00028 void RadarDemo::Initialize()
00029 {
00030     // Set scan perion for 6 seconds
00031     timer.SetScanPeriod(6);
00032 
00033     // Run forever, to set limited time for the demo
00034     // use for example: demoTime = 10 * scanPeriod;
00035     demoTime = 0;
00036 
00037     // Put a number of sample targets on the display
00038     AddSampleTargets(30);
00039 
00040     // Use medium range, values 0..2 are available now
00041     SetRange(1);
00042 
00043     // Remark : Data member initializer is not allowed for ARM compiler,
00044     //          initialize data in class constructor.
00045     currentBeamAngle = 0;
00046     lastBeamAngle = currentBeamAngle;
00047 
00048     // Force background redraw
00049     UnvalidateBackground();
00050     
00051     _buttonPressTime = 0;
00052 
00053     // Initialize touchscreen
00054     _ts->Init();
00055    
00056     timer.Start();   
00057 }
00058 
00059 
00060 void RadarDemo::Render()
00061 {
00062     timer.RegisterScan();
00063 
00064     // Calculate actual beam position
00065     lastBeamAngle = currentBeamAngle;
00066     currentBeamAngle = timer.GetBeamAngle();
00067 
00068     CheckUserInput(window); 
00069 
00070     RK043FN48H* display = (RK043FN48H*)GetDisplay();
00071     if(NeedsRedraw()) {
00072 
00073         display->SetActiveLayer(Background);
00074         display->Clear();
00075 
00076         // Set draw color before calling draw method
00077         display->SetDrawColor(0x80, 0, 0, 0xFF);
00078         DrawRangeButtons(raSize);
00079 
00080         display->SetDrawColor(0x40, 0x40, 0x40, 0xFF);
00081         DrawMarkers();
00082 
00083         display->SetDrawColor(0x80, 0x80, 0x80, 0xFF);
00084         DrawBorder();
00085 
00086         display->SetActiveLayer(Foreground);
00087         _needsRedraw = false;
00088     }
00089 
00090     UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, timer.GetRunningTime());
00091 
00092     // Redraw foreground
00093     display->Clear();
00094     DrawTracks();
00095 
00096     DrawRadarBeam(currentBeamAngle);
00097 }
00098 
00099 
00100 bool RadarDemo::IsRunning()
00101 {
00102     return demoTime > 0 ? timer.GetRunningTime() <= demoTime : true;
00103 }
00104 
00105 
00106 void RadarDemo::AddSampleTargets(int count)
00107 {
00108     const float minSpeed = 200; // [km/h]
00109     const float maxSpeed = 800; // [km/h]
00110     Target *target;
00111 
00112     srand(timer.GetRunningTime());
00113 
00114     for (int i = 0; i<count; i++) {
00115         // Generate target parameters
00116         float angle = 2 * M_PI * rand() / (float)RAND_MAX;
00117         float range = GetMaxRange() * rand() / (float)RAND_MAX;
00118         float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX;
00119         float direction = 2 * M_PI * rand() / (float)RAND_MAX;
00120 
00121         // Create a new target
00122         target = new Target(i + 100, speed, direction);
00123         target->SetLocationAngular(range, angle);
00124 
00125         // Put target on the list
00126         targetsList.push_back(*target);
00127     }
00128 }
00129 
00130 
00131 void RadarDemo::UnvalidateBackground()
00132 {
00133     _needsRedraw = true;
00134 }
00135 
00136 
00137 bool RadarDemo::NeedsRedraw()
00138 {
00139     return _needsRedraw;
00140 }
00141 
00142 
00143 UserInputType RadarDemo::CheckUserInput(Window screen)
00144 {
00145     pPoint p;
00146     
00147     // There is a timeout after pressing the button
00148     if(TouchScreenDisabled())
00149         return None;
00150         
00151     if ((p = _ts->DetectTouch()) != NULL) {
00152 
00153         if(p->X > (screen.x2-raSize) && p->Y < (screen.y1 + raSize)) {
00154             if(ChangeRange(1)) {
00155                 UnvalidateBackground();
00156                 SetTouchScreenTimeout();
00157                 
00158                 return ButtonPressed;
00159             }
00160         }
00161 
00162         if(p->X > (screen.x2-raSize) && p->Y > (screen.y2-raSize)) {
00163             if(ChangeRange(-1)) {
00164                 UnvalidateBackground();
00165                 SetTouchScreenTimeout();
00166                 
00167                 return ButtonPressed;
00168             }
00169         }
00170 
00171         SetCenter(p->X, p->Y);
00172         UnvalidateBackground();
00173         
00174         return SingleTouch;
00175     }
00176 
00177     return None;
00178 }
00179 
00180 bool RadarDemo::TouchScreenDisabled()
00181 {
00182     return _buttonPressTime + buttonPressTimeout > timer.GetRunningTime();
00183 }
00184 
00185 void RadarDemo::SetTouchScreenTimeout()
00186 {
00187     _buttonPressTime = timer.GetRunningTime();
00188 }