Optimaze with new mbed os for study
Dependencies: TS_DISCO_F746NG BSP_DISCO_F746NG Graphics
RadarDemo/RadarDemo.cpp@0:d8b9955d2b36, 2016-11-04 (annotated)
- Committer:
- karpent
- Date:
- Fri Nov 04 01:02:37 2016 +0000
- Revision:
- 0:d8b9955d2b36
- Child:
- 1:5e49b46de1b0
Initial revision
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 | 0:d8b9955d2b36 | 8 | RadarDemo::RadarDemo(Display* display) : Radar(display) |
karpent | 0:d8b9955d2b36 | 9 | { |
karpent | 0:d8b9955d2b36 | 10 | } |
karpent | 0:d8b9955d2b36 | 11 | |
karpent | 0:d8b9955d2b36 | 12 | |
karpent | 0:d8b9955d2b36 | 13 | RadarDemo::~RadarDemo() |
karpent | 0:d8b9955d2b36 | 14 | { |
karpent | 0:d8b9955d2b36 | 15 | #ifndef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 16 | t.stop(); |
karpent | 0:d8b9955d2b36 | 17 | #endif |
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 | 0:d8b9955d2b36 | 24 | _needsRedraw = true; |
karpent | 0:d8b9955d2b36 | 25 | |
karpent | 0:d8b9955d2b36 | 26 | scanPeriod = 6000; |
karpent | 0:d8b9955d2b36 | 27 | demoTime = 0; //2 * scanPeriod; |
karpent | 0:d8b9955d2b36 | 28 | |
karpent | 0:d8b9955d2b36 | 29 | AddSampleTargets(100); |
karpent | 0:d8b9955d2b36 | 30 | |
karpent | 0:d8b9955d2b36 | 31 | SetRange(1); |
karpent | 0:d8b9955d2b36 | 32 | //SetCenter(0, 0); |
karpent | 0:d8b9955d2b36 | 33 | |
karpent | 0:d8b9955d2b36 | 34 | // Remark : Data member initializer is not allowed for ARM compiler, |
karpent | 0:d8b9955d2b36 | 35 | // initialize data in class constructor. |
karpent | 0:d8b9955d2b36 | 36 | runningTime = 0; |
karpent | 0:d8b9955d2b36 | 37 | lastScanTime = runningTime; |
karpent | 0:d8b9955d2b36 | 38 | currentBeamAngle = 0; |
karpent | 0:d8b9955d2b36 | 39 | lastBeamAngle = currentBeamAngle; |
karpent | 0:d8b9955d2b36 | 40 | |
karpent | 0:d8b9955d2b36 | 41 | #ifndef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 42 | t.start(); |
karpent | 0:d8b9955d2b36 | 43 | #endif |
karpent | 0:d8b9955d2b36 | 44 | } |
karpent | 0:d8b9955d2b36 | 45 | |
karpent | 0:d8b9955d2b36 | 46 | |
karpent | 0:d8b9955d2b36 | 47 | void RadarDemo::Render() |
karpent | 0:d8b9955d2b36 | 48 | { |
karpent | 0:d8b9955d2b36 | 49 | // Reset scan time after one full turn |
karpent | 0:d8b9955d2b36 | 50 | if (runningTime >= (lastScanTime + scanPeriod)) { |
karpent | 0:d8b9955d2b36 | 51 | lastScanTime = runningTime; |
karpent | 0:d8b9955d2b36 | 52 | } |
karpent | 0:d8b9955d2b36 | 53 | |
karpent | 0:d8b9955d2b36 | 54 | // Calculate actual beam position |
karpent | 0:d8b9955d2b36 | 55 | lastBeamAngle = currentBeamAngle; |
karpent | 0:d8b9955d2b36 | 56 | |
karpent | 0:d8b9955d2b36 | 57 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 58 | uint32_t msTime = SDL_GetTicks(); |
karpent | 0:d8b9955d2b36 | 59 | #else |
karpent | 0:d8b9955d2b36 | 60 | int msTime = t.read_ms(); |
karpent | 0:d8b9955d2b36 | 61 | #endif |
karpent | 0:d8b9955d2b36 | 62 | currentBeamAngle = 2 * M_PI * (msTime - lastScanTime) / (float)scanPeriod; |
karpent | 0:d8b9955d2b36 | 63 | if(currentBeamAngle >= (2 * M_PI)) { |
karpent | 0:d8b9955d2b36 | 64 | currentBeamAngle -= 2 * M_PI; |
karpent | 0:d8b9955d2b36 | 65 | } |
karpent | 0:d8b9955d2b36 | 66 | |
karpent | 0:d8b9955d2b36 | 67 | //pc.printf("Sector :%3.1f, %3.1f\r\n", lastBeamAngle*180.f/M_PI, currentBeamAngle*180.f / M_PI) ; |
karpent | 0:d8b9955d2b36 | 68 | //pc.printf("Time : ms=%dms, r=%dms, l=%dms\r\n", msTime, runningTime, lastScanTime) ; |
karpent | 0:d8b9955d2b36 | 69 | |
karpent | 0:d8b9955d2b36 | 70 | RK043FN48H* display = (RK043FN48H*)GetDisplay(); |
karpent | 0:d8b9955d2b36 | 71 | |
karpent | 0:d8b9955d2b36 | 72 | if(NeedsRedraw()) { |
karpent | 0:d8b9955d2b36 | 73 | |
karpent | 0:d8b9955d2b36 | 74 | display->SetActiveLayer(Background); |
karpent | 0:d8b9955d2b36 | 75 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 76 | |
karpent | 0:d8b9955d2b36 | 77 | // Set color for markers |
karpent | 0:d8b9955d2b36 | 78 | display->SetDrawColor(0x40, 0x40, 0x40, 0xFF); |
karpent | 0:d8b9955d2b36 | 79 | DrawMarkers(); |
karpent | 0:d8b9955d2b36 | 80 | |
karpent | 0:d8b9955d2b36 | 81 | display->SetDrawColor(0x80, 0x80, 0x80, 0xFF); |
karpent | 0:d8b9955d2b36 | 82 | DrawBorder(); |
karpent | 0:d8b9955d2b36 | 83 | |
karpent | 0:d8b9955d2b36 | 84 | display->SetActiveLayer(Foreground); |
karpent | 0:d8b9955d2b36 | 85 | _needsRedraw = false; |
karpent | 0:d8b9955d2b36 | 86 | |
karpent | 0:d8b9955d2b36 | 87 | } |
karpent | 0:d8b9955d2b36 | 88 | |
karpent | 0:d8b9955d2b36 | 89 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 90 | runningTime = SDL_GetTicks(); |
karpent | 0:d8b9955d2b36 | 91 | #else |
karpent | 0:d8b9955d2b36 | 92 | runningTime = t.read_ms(); |
karpent | 0:d8b9955d2b36 | 93 | #endif |
karpent | 0:d8b9955d2b36 | 94 | UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, runningTime); |
karpent | 0:d8b9955d2b36 | 95 | |
karpent | 0:d8b9955d2b36 | 96 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 97 | DrawTracks(); |
karpent | 0:d8b9955d2b36 | 98 | DrawRadarBeam(currentBeamAngle); |
karpent | 0:d8b9955d2b36 | 99 | } |
karpent | 0:d8b9955d2b36 | 100 | |
karpent | 0:d8b9955d2b36 | 101 | |
karpent | 0:d8b9955d2b36 | 102 | bool RadarDemo::IsRunning() |
karpent | 0:d8b9955d2b36 | 103 | { |
karpent | 0:d8b9955d2b36 | 104 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 105 | runningTime = SDL_GetTicks(); |
karpent | 0:d8b9955d2b36 | 106 | #else |
karpent | 0:d8b9955d2b36 | 107 | runningTime = t.read_ms(); |
karpent | 0:d8b9955d2b36 | 108 | #endif |
karpent | 0:d8b9955d2b36 | 109 | |
karpent | 0:d8b9955d2b36 | 110 | return demoTime > 0 ? runningTime <= demoTime : true; |
karpent | 0:d8b9955d2b36 | 111 | } |
karpent | 0:d8b9955d2b36 | 112 | |
karpent | 0:d8b9955d2b36 | 113 | |
karpent | 0:d8b9955d2b36 | 114 | void RadarDemo::AddSampleTargets(int count) |
karpent | 0:d8b9955d2b36 | 115 | { |
karpent | 0:d8b9955d2b36 | 116 | const float minSpeed = 200; // [km/h] |
karpent | 0:d8b9955d2b36 | 117 | const float maxSpeed = 800; // [km/h] |
karpent | 0:d8b9955d2b36 | 118 | Target *target; |
karpent | 0:d8b9955d2b36 | 119 | |
karpent | 0:d8b9955d2b36 | 120 | #ifdef _SDL_timer_h |
karpent | 0:d8b9955d2b36 | 121 | srand(SDL_GetTicks()); |
karpent | 0:d8b9955d2b36 | 122 | #else |
karpent | 0:d8b9955d2b36 | 123 | srand(t.read_us()); |
karpent | 0:d8b9955d2b36 | 124 | #endif |
karpent | 0:d8b9955d2b36 | 125 | |
karpent | 0:d8b9955d2b36 | 126 | for (int i = 0; i<count; i++) { |
karpent | 0:d8b9955d2b36 | 127 | // Generate target parameters |
karpent | 0:d8b9955d2b36 | 128 | float angle = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 129 | float range = GetMaxRange() * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 130 | float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 131 | float direction = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 132 | |
karpent | 0:d8b9955d2b36 | 133 | // Create a new target |
karpent | 0:d8b9955d2b36 | 134 | target = new Target(i + 100, speed, direction); |
karpent | 0:d8b9955d2b36 | 135 | target->SetLocationAngular(range, angle); |
karpent | 0:d8b9955d2b36 | 136 | |
karpent | 0:d8b9955d2b36 | 137 | // Put target on the list |
karpent | 0:d8b9955d2b36 | 138 | targetsList.push_back(*target); |
karpent | 0:d8b9955d2b36 | 139 | } |
karpent | 0:d8b9955d2b36 | 140 | } |
karpent | 0:d8b9955d2b36 | 141 | |
karpent | 0:d8b9955d2b36 | 142 | |
karpent | 0:d8b9955d2b36 | 143 | void RadarDemo::UnvalidateBackground() |
karpent | 0:d8b9955d2b36 | 144 | { |
karpent | 0:d8b9955d2b36 | 145 | _needsRedraw = false; |
karpent | 0:d8b9955d2b36 | 146 | } |
karpent | 0:d8b9955d2b36 | 147 | |
karpent | 0:d8b9955d2b36 | 148 | |
karpent | 0:d8b9955d2b36 | 149 | bool RadarDemo::NeedsRedraw() |
karpent | 0:d8b9955d2b36 | 150 | { |
karpent | 0:d8b9955d2b36 | 151 | return _needsRedraw; |
karpent | 0:d8b9955d2b36 | 152 | } |
karpent | 0:d8b9955d2b36 | 153 |