Optimaze with new mbed os for study
Dependencies: TS_DISCO_F746NG BSP_DISCO_F746NG Graphics
RadarDemo/RadarDemo.cpp@2:8db224cc1fcb, 2016-11-04 (annotated)
- Committer:
- karpent
- Date:
- Fri Nov 04 23:07:04 2016 +0000
- Revision:
- 2:8db224cc1fcb
- Parent:
- 1:5e49b46de1b0
- Child:
- 3:732f7144ec81
Touchscreen support added to change the range and center location.
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 | #ifndef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 19 | t.stop(); |
karpent | 0:d8b9955d2b36 | 20 | #endif |
karpent | 0:d8b9955d2b36 | 21 | Radar::~Radar(); |
karpent | 0:d8b9955d2b36 | 22 | } |
karpent | 0:d8b9955d2b36 | 23 | |
karpent | 0:d8b9955d2b36 | 24 | |
karpent | 0:d8b9955d2b36 | 25 | void RadarDemo::Initialize() |
karpent | 0:d8b9955d2b36 | 26 | { |
karpent | 1:5e49b46de1b0 | 27 | // Set scan perion for 6 seconds |
karpent | 0:d8b9955d2b36 | 28 | scanPeriod = 6000; |
karpent | 0:d8b9955d2b36 | 29 | |
karpent | 1:5e49b46de1b0 | 30 | // Run forever, to set limited time for the demo |
karpent | 1:5e49b46de1b0 | 31 | // use for example: demoTime = 10 * scanPeriod; |
karpent | 1:5e49b46de1b0 | 32 | demoTime = 0; |
karpent | 1:5e49b46de1b0 | 33 | |
karpent | 1:5e49b46de1b0 | 34 | // Put a number of sample targets on the display |
karpent | 2:8db224cc1fcb | 35 | AddSampleTargets(30); |
karpent | 0:d8b9955d2b36 | 36 | |
karpent | 1:5e49b46de1b0 | 37 | // Use medium range, values 0..2 are available now |
karpent | 0:d8b9955d2b36 | 38 | SetRange(1); |
karpent | 1:5e49b46de1b0 | 39 | |
karpent | 0:d8b9955d2b36 | 40 | // Remark : Data member initializer is not allowed for ARM compiler, |
karpent | 1:5e49b46de1b0 | 41 | // initialize data in class constructor. |
karpent | 0:d8b9955d2b36 | 42 | runningTime = 0; |
karpent | 0:d8b9955d2b36 | 43 | lastScanTime = runningTime; |
karpent | 0:d8b9955d2b36 | 44 | currentBeamAngle = 0; |
karpent | 0:d8b9955d2b36 | 45 | lastBeamAngle = currentBeamAngle; |
karpent | 0:d8b9955d2b36 | 46 | |
karpent | 1:5e49b46de1b0 | 47 | // Force background redraw |
karpent | 1:5e49b46de1b0 | 48 | UnvalidateBackground(); |
karpent | 1:5e49b46de1b0 | 49 | |
karpent | 2:8db224cc1fcb | 50 | // Initialize touchscreen |
karpent | 2:8db224cc1fcb | 51 | ts.Init(((RK043FN48H*)GetDisplay())->DisplayWidth(), ((RK043FN48H*)GetDisplay())->DisplayHeight()); |
karpent | 2:8db224cc1fcb | 52 | |
karpent | 0:d8b9955d2b36 | 53 | #ifndef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 54 | t.start(); |
karpent | 0:d8b9955d2b36 | 55 | #endif |
karpent | 0:d8b9955d2b36 | 56 | } |
karpent | 0:d8b9955d2b36 | 57 | |
karpent | 0:d8b9955d2b36 | 58 | |
karpent | 0:d8b9955d2b36 | 59 | void RadarDemo::Render() |
karpent | 0:d8b9955d2b36 | 60 | { |
karpent | 0:d8b9955d2b36 | 61 | // Reset scan time after one full turn |
karpent | 0:d8b9955d2b36 | 62 | if (runningTime >= (lastScanTime + scanPeriod)) { |
karpent | 0:d8b9955d2b36 | 63 | lastScanTime = runningTime; |
karpent | 0:d8b9955d2b36 | 64 | } |
karpent | 0:d8b9955d2b36 | 65 | |
karpent | 0:d8b9955d2b36 | 66 | // Calculate actual beam position |
karpent | 0:d8b9955d2b36 | 67 | lastBeamAngle = currentBeamAngle; |
karpent | 0:d8b9955d2b36 | 68 | |
karpent | 0:d8b9955d2b36 | 69 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 70 | uint32_t msTime = SDL_GetTicks(); |
karpent | 0:d8b9955d2b36 | 71 | #else |
karpent | 0:d8b9955d2b36 | 72 | int msTime = t.read_ms(); |
karpent | 0:d8b9955d2b36 | 73 | #endif |
karpent | 0:d8b9955d2b36 | 74 | currentBeamAngle = 2 * M_PI * (msTime - lastScanTime) / (float)scanPeriod; |
karpent | 0:d8b9955d2b36 | 75 | if(currentBeamAngle >= (2 * M_PI)) { |
karpent | 0:d8b9955d2b36 | 76 | currentBeamAngle -= 2 * M_PI; |
karpent | 0:d8b9955d2b36 | 77 | } |
karpent | 0:d8b9955d2b36 | 78 | |
karpent | 0:d8b9955d2b36 | 79 | RK043FN48H* display = (RK043FN48H*)GetDisplay(); |
karpent | 0:d8b9955d2b36 | 80 | |
karpent | 2:8db224cc1fcb | 81 | if(DetectTouch(window)) |
karpent | 2:8db224cc1fcb | 82 | { |
karpent | 2:8db224cc1fcb | 83 | // Set timeout for the next touch detection |
karpent | 2:8db224cc1fcb | 84 | } |
karpent | 2:8db224cc1fcb | 85 | |
karpent | 0:d8b9955d2b36 | 86 | if(NeedsRedraw()) { |
karpent | 0:d8b9955d2b36 | 87 | |
karpent | 0:d8b9955d2b36 | 88 | display->SetActiveLayer(Background); |
karpent | 0:d8b9955d2b36 | 89 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 90 | |
karpent | 2:8db224cc1fcb | 91 | // Set draw color before calling draw method |
karpent | 2:8db224cc1fcb | 92 | display->SetDrawColor(0x80, 0, 0, 0xFF); |
karpent | 2:8db224cc1fcb | 93 | DrawRangeButtons(raSize); |
karpent | 2:8db224cc1fcb | 94 | |
karpent | 0:d8b9955d2b36 | 95 | display->SetDrawColor(0x40, 0x40, 0x40, 0xFF); |
karpent | 0:d8b9955d2b36 | 96 | DrawMarkers(); |
karpent | 0:d8b9955d2b36 | 97 | |
karpent | 0:d8b9955d2b36 | 98 | display->SetDrawColor(0x80, 0x80, 0x80, 0xFF); |
karpent | 0:d8b9955d2b36 | 99 | DrawBorder(); |
karpent | 0:d8b9955d2b36 | 100 | |
karpent | 0:d8b9955d2b36 | 101 | display->SetActiveLayer(Foreground); |
karpent | 0:d8b9955d2b36 | 102 | _needsRedraw = false; |
karpent | 0:d8b9955d2b36 | 103 | |
karpent | 0:d8b9955d2b36 | 104 | } |
karpent | 1:5e49b46de1b0 | 105 | |
karpent | 0:d8b9955d2b36 | 106 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 107 | runningTime = SDL_GetTicks(); |
karpent | 0:d8b9955d2b36 | 108 | #else |
karpent | 0:d8b9955d2b36 | 109 | runningTime = t.read_ms(); |
karpent | 0:d8b9955d2b36 | 110 | #endif |
karpent | 2:8db224cc1fcb | 111 | |
karpent | 0:d8b9955d2b36 | 112 | UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, runningTime); |
karpent | 2:8db224cc1fcb | 113 | |
karpent | 1:5e49b46de1b0 | 114 | // Redraw foreground |
karpent | 0:d8b9955d2b36 | 115 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 116 | DrawTracks(); |
karpent | 1:5e49b46de1b0 | 117 | |
karpent | 0:d8b9955d2b36 | 118 | DrawRadarBeam(currentBeamAngle); |
karpent | 0:d8b9955d2b36 | 119 | } |
karpent | 0:d8b9955d2b36 | 120 | |
karpent | 0:d8b9955d2b36 | 121 | |
karpent | 0:d8b9955d2b36 | 122 | bool RadarDemo::IsRunning() |
karpent | 0:d8b9955d2b36 | 123 | { |
karpent | 0:d8b9955d2b36 | 124 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 125 | runningTime = SDL_GetTicks(); |
karpent | 0:d8b9955d2b36 | 126 | #else |
karpent | 0:d8b9955d2b36 | 127 | runningTime = t.read_ms(); |
karpent | 0:d8b9955d2b36 | 128 | #endif |
karpent | 0:d8b9955d2b36 | 129 | |
karpent | 0:d8b9955d2b36 | 130 | return demoTime > 0 ? runningTime <= demoTime : true; |
karpent | 0:d8b9955d2b36 | 131 | } |
karpent | 0:d8b9955d2b36 | 132 | |
karpent | 0:d8b9955d2b36 | 133 | |
karpent | 0:d8b9955d2b36 | 134 | void RadarDemo::AddSampleTargets(int count) |
karpent | 0:d8b9955d2b36 | 135 | { |
karpent | 0:d8b9955d2b36 | 136 | const float minSpeed = 200; // [km/h] |
karpent | 0:d8b9955d2b36 | 137 | const float maxSpeed = 800; // [km/h] |
karpent | 0:d8b9955d2b36 | 138 | Target *target; |
karpent | 0:d8b9955d2b36 | 139 | |
karpent | 0:d8b9955d2b36 | 140 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 141 | srand(SDL_GetTicks()); |
karpent | 0:d8b9955d2b36 | 142 | #else |
karpent | 0:d8b9955d2b36 | 143 | srand(t.read_us()); |
karpent | 0:d8b9955d2b36 | 144 | #endif |
karpent | 0:d8b9955d2b36 | 145 | |
karpent | 0:d8b9955d2b36 | 146 | for (int i = 0; i<count; i++) { |
karpent | 0:d8b9955d2b36 | 147 | // Generate target parameters |
karpent | 0:d8b9955d2b36 | 148 | float angle = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 149 | float range = GetMaxRange() * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 150 | float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 151 | float direction = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 152 | |
karpent | 0:d8b9955d2b36 | 153 | // Create a new target |
karpent | 0:d8b9955d2b36 | 154 | target = new Target(i + 100, speed, direction); |
karpent | 0:d8b9955d2b36 | 155 | target->SetLocationAngular(range, angle); |
karpent | 0:d8b9955d2b36 | 156 | |
karpent | 0:d8b9955d2b36 | 157 | // Put target on the list |
karpent | 0:d8b9955d2b36 | 158 | targetsList.push_back(*target); |
karpent | 0:d8b9955d2b36 | 159 | } |
karpent | 0:d8b9955d2b36 | 160 | } |
karpent | 0:d8b9955d2b36 | 161 | |
karpent | 0:d8b9955d2b36 | 162 | |
karpent | 0:d8b9955d2b36 | 163 | void RadarDemo::UnvalidateBackground() |
karpent | 0:d8b9955d2b36 | 164 | { |
karpent | 1:5e49b46de1b0 | 165 | _needsRedraw = true; |
karpent | 0:d8b9955d2b36 | 166 | } |
karpent | 0:d8b9955d2b36 | 167 | |
karpent | 0:d8b9955d2b36 | 168 | |
karpent | 0:d8b9955d2b36 | 169 | bool RadarDemo::NeedsRedraw() |
karpent | 0:d8b9955d2b36 | 170 | { |
karpent | 0:d8b9955d2b36 | 171 | return _needsRedraw; |
karpent | 0:d8b9955d2b36 | 172 | } |
karpent | 0:d8b9955d2b36 | 173 | |
karpent | 2:8db224cc1fcb | 174 | |
karpent | 2:8db224cc1fcb | 175 | bool RadarDemo::DetectTouch(Window screen) |
karpent | 2:8db224cc1fcb | 176 | { |
karpent | 2:8db224cc1fcb | 177 | TS_StateTypeDef tsState; |
karpent | 2:8db224cc1fcb | 178 | |
karpent | 2:8db224cc1fcb | 179 | ts.GetState(&tsState); |
karpent | 2:8db224cc1fcb | 180 | |
karpent | 2:8db224cc1fcb | 181 | if (tsState.touchDetected) { |
karpent | 2:8db224cc1fcb | 182 | |
karpent | 2:8db224cc1fcb | 183 | if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] < screen.y1 + raSize) { |
karpent | 2:8db224cc1fcb | 184 | if(ChangeRange(1)) |
karpent | 2:8db224cc1fcb | 185 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 186 | } |
karpent | 2:8db224cc1fcb | 187 | |
karpent | 2:8db224cc1fcb | 188 | else if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] > (screen.y2-raSize)) { |
karpent | 2:8db224cc1fcb | 189 | if(ChangeRange(-1)) |
karpent | 2:8db224cc1fcb | 190 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 191 | } |
karpent | 2:8db224cc1fcb | 192 | |
karpent | 2:8db224cc1fcb | 193 | else { |
karpent | 2:8db224cc1fcb | 194 | SetCenter(tsState.touchX[0], tsState.touchY[0]); |
karpent | 2:8db224cc1fcb | 195 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 196 | } |
karpent | 2:8db224cc1fcb | 197 | |
karpent | 2:8db224cc1fcb | 198 | ts.ResetTouchData(&tsState); |
karpent | 2:8db224cc1fcb | 199 | return true; |
karpent | 2:8db224cc1fcb | 200 | } |
karpent | 2:8db224cc1fcb | 201 | |
karpent | 2:8db224cc1fcb | 202 | return false; |
karpent | 2:8db224cc1fcb | 203 | } |