Optimaze with new mbed os for study
Dependencies: TS_DISCO_F746NG BSP_DISCO_F746NG Graphics
RadarDemo/RadarDemo.cpp@3:732f7144ec81, 2016-11-05 (annotated)
- Committer:
- karpent
- Date:
- Sat Nov 05 15:36:59 2016 +0000
- Revision:
- 3:732f7144ec81
- Parent:
- 2:8db224cc1fcb
- Child:
- 4:66f13188c26b
RadarTimer class added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
karpent | 0:d8b9955d2b36 | 1 | // |
karpent | 0:d8b9955d2b36 | 2 | // RadarDemo.cpp - example of graphic commands usage to create 2D graphics. |
karpent | 0:d8b9955d2b36 | 3 | // |
karpent | 0:d8b9955d2b36 | 4 | |
karpent | 0:d8b9955d2b36 | 5 | #include "RadarDemo.h" |
karpent | 0:d8b9955d2b36 | 6 | #include "RK043FN48H.h" |
karpent | 0:d8b9955d2b36 | 7 | |
karpent | 2:8db224cc1fcb | 8 | // Size of range control areas on the screen |
karpent | 2:8db224cc1fcb | 9 | const int raSize = 50; |
karpent | 2:8db224cc1fcb | 10 | |
karpent | 0:d8b9955d2b36 | 11 | RadarDemo::RadarDemo(Display* display) : Radar(display) |
karpent | 0:d8b9955d2b36 | 12 | { |
karpent | 0:d8b9955d2b36 | 13 | } |
karpent | 0:d8b9955d2b36 | 14 | |
karpent | 0:d8b9955d2b36 | 15 | |
karpent | 0:d8b9955d2b36 | 16 | RadarDemo::~RadarDemo() |
karpent | 0:d8b9955d2b36 | 17 | { |
karpent | 0:d8b9955d2b36 | 18 | Radar::~Radar(); |
karpent | 0:d8b9955d2b36 | 19 | } |
karpent | 0:d8b9955d2b36 | 20 | |
karpent | 0:d8b9955d2b36 | 21 | |
karpent | 0:d8b9955d2b36 | 22 | void RadarDemo::Initialize() |
karpent | 0:d8b9955d2b36 | 23 | { |
karpent | 1:5e49b46de1b0 | 24 | // Set scan perion for 6 seconds |
karpent | 3:732f7144ec81 | 25 | timer.SetScanPeriod(6); |
karpent | 0:d8b9955d2b36 | 26 | |
karpent | 1:5e49b46de1b0 | 27 | // Run forever, to set limited time for the demo |
karpent | 1:5e49b46de1b0 | 28 | // use for example: demoTime = 10 * scanPeriod; |
karpent | 1:5e49b46de1b0 | 29 | demoTime = 0; |
karpent | 1:5e49b46de1b0 | 30 | |
karpent | 1:5e49b46de1b0 | 31 | // Put a number of sample targets on the display |
karpent | 2:8db224cc1fcb | 32 | AddSampleTargets(30); |
karpent | 0:d8b9955d2b36 | 33 | |
karpent | 1:5e49b46de1b0 | 34 | // Use medium range, values 0..2 are available now |
karpent | 0:d8b9955d2b36 | 35 | SetRange(1); |
karpent | 1:5e49b46de1b0 | 36 | |
karpent | 0:d8b9955d2b36 | 37 | // Remark : Data member initializer is not allowed for ARM compiler, |
karpent | 1:5e49b46de1b0 | 38 | // initialize data in class constructor. |
karpent | 0:d8b9955d2b36 | 39 | currentBeamAngle = 0; |
karpent | 0:d8b9955d2b36 | 40 | lastBeamAngle = currentBeamAngle; |
karpent | 0:d8b9955d2b36 | 41 | |
karpent | 1:5e49b46de1b0 | 42 | // Force background redraw |
karpent | 1:5e49b46de1b0 | 43 | UnvalidateBackground(); |
karpent | 1:5e49b46de1b0 | 44 | |
karpent | 2:8db224cc1fcb | 45 | // Initialize touchscreen |
karpent | 2:8db224cc1fcb | 46 | ts.Init(((RK043FN48H*)GetDisplay())->DisplayWidth(), ((RK043FN48H*)GetDisplay())->DisplayHeight()); |
karpent | 2:8db224cc1fcb | 47 | |
karpent | 3:732f7144ec81 | 48 | timer.Start(); |
karpent | 0:d8b9955d2b36 | 49 | } |
karpent | 0:d8b9955d2b36 | 50 | |
karpent | 0:d8b9955d2b36 | 51 | |
karpent | 0:d8b9955d2b36 | 52 | void RadarDemo::Render() |
karpent | 0:d8b9955d2b36 | 53 | { |
karpent | 3:732f7144ec81 | 54 | timer.RegisterScan(); |
karpent | 0:d8b9955d2b36 | 55 | |
karpent | 0:d8b9955d2b36 | 56 | // Calculate actual beam position |
karpent | 0:d8b9955d2b36 | 57 | lastBeamAngle = currentBeamAngle; |
karpent | 3:732f7144ec81 | 58 | currentBeamAngle = timer.GetBeamAngle(); |
karpent | 0:d8b9955d2b36 | 59 | |
karpent | 3:732f7144ec81 | 60 | // TODO: |
karpent | 2:8db224cc1fcb | 61 | if(DetectTouch(window)) |
karpent | 2:8db224cc1fcb | 62 | { |
karpent | 2:8db224cc1fcb | 63 | // Set timeout for the next touch detection |
karpent | 2:8db224cc1fcb | 64 | } |
karpent | 2:8db224cc1fcb | 65 | |
karpent | 3:732f7144ec81 | 66 | RK043FN48H* display = (RK043FN48H*)GetDisplay(); |
karpent | 0:d8b9955d2b36 | 67 | if(NeedsRedraw()) { |
karpent | 0:d8b9955d2b36 | 68 | |
karpent | 0:d8b9955d2b36 | 69 | display->SetActiveLayer(Background); |
karpent | 0:d8b9955d2b36 | 70 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 71 | |
karpent | 2:8db224cc1fcb | 72 | // Set draw color before calling draw method |
karpent | 2:8db224cc1fcb | 73 | display->SetDrawColor(0x80, 0, 0, 0xFF); |
karpent | 2:8db224cc1fcb | 74 | DrawRangeButtons(raSize); |
karpent | 2:8db224cc1fcb | 75 | |
karpent | 0:d8b9955d2b36 | 76 | display->SetDrawColor(0x40, 0x40, 0x40, 0xFF); |
karpent | 0:d8b9955d2b36 | 77 | DrawMarkers(); |
karpent | 0:d8b9955d2b36 | 78 | |
karpent | 0:d8b9955d2b36 | 79 | display->SetDrawColor(0x80, 0x80, 0x80, 0xFF); |
karpent | 0:d8b9955d2b36 | 80 | DrawBorder(); |
karpent | 0:d8b9955d2b36 | 81 | |
karpent | 0:d8b9955d2b36 | 82 | display->SetActiveLayer(Foreground); |
karpent | 0:d8b9955d2b36 | 83 | _needsRedraw = false; |
karpent | 0:d8b9955d2b36 | 84 | } |
karpent | 1:5e49b46de1b0 | 85 | |
karpent | 3:732f7144ec81 | 86 | UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, timer.GetRunningTime()); |
karpent | 2:8db224cc1fcb | 87 | |
karpent | 1:5e49b46de1b0 | 88 | // Redraw foreground |
karpent | 0:d8b9955d2b36 | 89 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 90 | DrawTracks(); |
karpent | 1:5e49b46de1b0 | 91 | |
karpent | 0:d8b9955d2b36 | 92 | DrawRadarBeam(currentBeamAngle); |
karpent | 0:d8b9955d2b36 | 93 | } |
karpent | 0:d8b9955d2b36 | 94 | |
karpent | 0:d8b9955d2b36 | 95 | |
karpent | 0:d8b9955d2b36 | 96 | bool RadarDemo::IsRunning() |
karpent | 0:d8b9955d2b36 | 97 | { |
karpent | 3:732f7144ec81 | 98 | return demoTime > 0 ? timer.GetRunningTime() <= demoTime : true; |
karpent | 0:d8b9955d2b36 | 99 | } |
karpent | 0:d8b9955d2b36 | 100 | |
karpent | 0:d8b9955d2b36 | 101 | |
karpent | 0:d8b9955d2b36 | 102 | void RadarDemo::AddSampleTargets(int count) |
karpent | 0:d8b9955d2b36 | 103 | { |
karpent | 0:d8b9955d2b36 | 104 | const float minSpeed = 200; // [km/h] |
karpent | 0:d8b9955d2b36 | 105 | const float maxSpeed = 800; // [km/h] |
karpent | 0:d8b9955d2b36 | 106 | Target *target; |
karpent | 0:d8b9955d2b36 | 107 | |
karpent | 3:732f7144ec81 | 108 | srand(timer.GetRunningTime()); |
karpent | 0:d8b9955d2b36 | 109 | |
karpent | 0:d8b9955d2b36 | 110 | for (int i = 0; i<count; i++) { |
karpent | 0:d8b9955d2b36 | 111 | // Generate target parameters |
karpent | 0:d8b9955d2b36 | 112 | float angle = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 113 | float range = GetMaxRange() * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 114 | float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 115 | float direction = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 116 | |
karpent | 0:d8b9955d2b36 | 117 | // Create a new target |
karpent | 0:d8b9955d2b36 | 118 | target = new Target(i + 100, speed, direction); |
karpent | 0:d8b9955d2b36 | 119 | target->SetLocationAngular(range, angle); |
karpent | 0:d8b9955d2b36 | 120 | |
karpent | 0:d8b9955d2b36 | 121 | // Put target on the list |
karpent | 0:d8b9955d2b36 | 122 | targetsList.push_back(*target); |
karpent | 0:d8b9955d2b36 | 123 | } |
karpent | 0:d8b9955d2b36 | 124 | } |
karpent | 0:d8b9955d2b36 | 125 | |
karpent | 0:d8b9955d2b36 | 126 | |
karpent | 0:d8b9955d2b36 | 127 | void RadarDemo::UnvalidateBackground() |
karpent | 0:d8b9955d2b36 | 128 | { |
karpent | 1:5e49b46de1b0 | 129 | _needsRedraw = true; |
karpent | 0:d8b9955d2b36 | 130 | } |
karpent | 0:d8b9955d2b36 | 131 | |
karpent | 0:d8b9955d2b36 | 132 | |
karpent | 0:d8b9955d2b36 | 133 | bool RadarDemo::NeedsRedraw() |
karpent | 0:d8b9955d2b36 | 134 | { |
karpent | 0:d8b9955d2b36 | 135 | return _needsRedraw; |
karpent | 0:d8b9955d2b36 | 136 | } |
karpent | 0:d8b9955d2b36 | 137 | |
karpent | 2:8db224cc1fcb | 138 | |
karpent | 2:8db224cc1fcb | 139 | bool RadarDemo::DetectTouch(Window screen) |
karpent | 2:8db224cc1fcb | 140 | { |
karpent | 2:8db224cc1fcb | 141 | TS_StateTypeDef tsState; |
karpent | 2:8db224cc1fcb | 142 | |
karpent | 2:8db224cc1fcb | 143 | ts.GetState(&tsState); |
karpent | 2:8db224cc1fcb | 144 | |
karpent | 3:732f7144ec81 | 145 | if (tsState.touchDetected > 0) { |
karpent | 2:8db224cc1fcb | 146 | |
karpent | 2:8db224cc1fcb | 147 | if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] < screen.y1 + raSize) { |
karpent | 2:8db224cc1fcb | 148 | if(ChangeRange(1)) |
karpent | 2:8db224cc1fcb | 149 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 150 | } |
karpent | 2:8db224cc1fcb | 151 | |
karpent | 2:8db224cc1fcb | 152 | else if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] > (screen.y2-raSize)) { |
karpent | 2:8db224cc1fcb | 153 | if(ChangeRange(-1)) |
karpent | 2:8db224cc1fcb | 154 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 155 | } |
karpent | 2:8db224cc1fcb | 156 | |
karpent | 2:8db224cc1fcb | 157 | else { |
karpent | 2:8db224cc1fcb | 158 | SetCenter(tsState.touchX[0], tsState.touchY[0]); |
karpent | 2:8db224cc1fcb | 159 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 160 | } |
karpent | 2:8db224cc1fcb | 161 | |
karpent | 2:8db224cc1fcb | 162 | ts.ResetTouchData(&tsState); |
karpent | 2:8db224cc1fcb | 163 | return true; |
karpent | 2:8db224cc1fcb | 164 | } |
karpent | 2:8db224cc1fcb | 165 | |
karpent | 2:8db224cc1fcb | 166 | return false; |
karpent | 2:8db224cc1fcb | 167 | } |