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