Optimaze with new mbed os for study
Dependencies: TS_DISCO_F746NG BSP_DISCO_F746NG Graphics
RadarDemo/RadarDemo.cpp@5:64cafb3c6e7b, 2019-08-23 (annotated)
- Committer:
- ngtkien
- Date:
- Fri Aug 23 16:59:22 2019 +0000
- Revision:
- 5:64cafb3c6e7b
- Parent:
- 4:66f13188c26b
KienNgo optimaze
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 | 4:66f13188c26b | 11 | // Button Press Timeout in miliseconds |
karpent | 4:66f13188c26b | 12 | const uint32_t buttonPressTimeout = 400; |
karpent | 4:66f13188c26b | 13 | |
karpent | 4:66f13188c26b | 14 | |
karpent | 0:d8b9955d2b36 | 15 | RadarDemo::RadarDemo(Display* display) : Radar(display) |
karpent | 0:d8b9955d2b36 | 16 | { |
karpent | 4:66f13188c26b | 17 | _ts = new TouchScreen(display); |
karpent | 0:d8b9955d2b36 | 18 | } |
karpent | 0:d8b9955d2b36 | 19 | |
karpent | 0:d8b9955d2b36 | 20 | |
karpent | 0:d8b9955d2b36 | 21 | RadarDemo::~RadarDemo() |
karpent | 0:d8b9955d2b36 | 22 | { |
karpent | 0:d8b9955d2b36 | 23 | Radar::~Radar(); |
karpent | 4:66f13188c26b | 24 | free(_ts); |
karpent | 0:d8b9955d2b36 | 25 | } |
karpent | 0:d8b9955d2b36 | 26 | |
karpent | 0:d8b9955d2b36 | 27 | |
karpent | 0:d8b9955d2b36 | 28 | void RadarDemo::Initialize() |
karpent | 0:d8b9955d2b36 | 29 | { |
karpent | 1:5e49b46de1b0 | 30 | // Set scan perion for 6 seconds |
karpent | 3:732f7144ec81 | 31 | timer.SetScanPeriod(6); |
karpent | 0:d8b9955d2b36 | 32 | |
karpent | 1:5e49b46de1b0 | 33 | // Run forever, to set limited time for the demo |
karpent | 1:5e49b46de1b0 | 34 | // use for example: demoTime = 10 * scanPeriod; |
karpent | 1:5e49b46de1b0 | 35 | demoTime = 0; |
karpent | 1:5e49b46de1b0 | 36 | |
karpent | 1:5e49b46de1b0 | 37 | // Put a number of sample targets on the display |
karpent | 2:8db224cc1fcb | 38 | AddSampleTargets(30); |
karpent | 0:d8b9955d2b36 | 39 | |
karpent | 1:5e49b46de1b0 | 40 | // Use medium range, values 0..2 are available now |
karpent | 0:d8b9955d2b36 | 41 | SetRange(1); |
karpent | 1:5e49b46de1b0 | 42 | |
karpent | 0:d8b9955d2b36 | 43 | // Remark : Data member initializer is not allowed for ARM compiler, |
karpent | 1:5e49b46de1b0 | 44 | // initialize data in class constructor. |
karpent | 0:d8b9955d2b36 | 45 | currentBeamAngle = 0; |
karpent | 0:d8b9955d2b36 | 46 | lastBeamAngle = currentBeamAngle; |
karpent | 0:d8b9955d2b36 | 47 | |
karpent | 1:5e49b46de1b0 | 48 | // Force background redraw |
karpent | 1:5e49b46de1b0 | 49 | UnvalidateBackground(); |
karpent | 4:66f13188c26b | 50 | |
karpent | 4:66f13188c26b | 51 | _buttonPressTime = 0; |
karpent | 1:5e49b46de1b0 | 52 | |
karpent | 2:8db224cc1fcb | 53 | // Initialize touchscreen |
karpent | 4:66f13188c26b | 54 | _ts->Init(); |
karpent | 4:66f13188c26b | 55 | |
karpent | 4:66f13188c26b | 56 | timer.Start(); |
karpent | 0:d8b9955d2b36 | 57 | } |
karpent | 0:d8b9955d2b36 | 58 | |
karpent | 0:d8b9955d2b36 | 59 | |
karpent | 0:d8b9955d2b36 | 60 | void RadarDemo::Render() |
karpent | 0:d8b9955d2b36 | 61 | { |
karpent | 3:732f7144ec81 | 62 | timer.RegisterScan(); |
karpent | 0:d8b9955d2b36 | 63 | |
karpent | 0:d8b9955d2b36 | 64 | // Calculate actual beam position |
karpent | 0:d8b9955d2b36 | 65 | lastBeamAngle = currentBeamAngle; |
karpent | 3:732f7144ec81 | 66 | currentBeamAngle = timer.GetBeamAngle(); |
karpent | 0:d8b9955d2b36 | 67 | |
karpent | 4:66f13188c26b | 68 | CheckUserInput(window); |
karpent | 2:8db224cc1fcb | 69 | |
karpent | 3:732f7144ec81 | 70 | RK043FN48H* display = (RK043FN48H*)GetDisplay(); |
karpent | 0:d8b9955d2b36 | 71 | if(NeedsRedraw()) { |
karpent | 0:d8b9955d2b36 | 72 | |
karpent | 0:d8b9955d2b36 | 73 | display->SetActiveLayer(Background); |
karpent | 0:d8b9955d2b36 | 74 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 75 | |
karpent | 2:8db224cc1fcb | 76 | // Set draw color before calling draw method |
karpent | 2:8db224cc1fcb | 77 | display->SetDrawColor(0x80, 0, 0, 0xFF); |
karpent | 2:8db224cc1fcb | 78 | DrawRangeButtons(raSize); |
karpent | 2:8db224cc1fcb | 79 | |
karpent | 0:d8b9955d2b36 | 80 | display->SetDrawColor(0x40, 0x40, 0x40, 0xFF); |
karpent | 0:d8b9955d2b36 | 81 | DrawMarkers(); |
karpent | 0:d8b9955d2b36 | 82 | |
karpent | 0:d8b9955d2b36 | 83 | display->SetDrawColor(0x80, 0x80, 0x80, 0xFF); |
karpent | 0:d8b9955d2b36 | 84 | DrawBorder(); |
karpent | 0:d8b9955d2b36 | 85 | |
karpent | 0:d8b9955d2b36 | 86 | display->SetActiveLayer(Foreground); |
karpent | 0:d8b9955d2b36 | 87 | _needsRedraw = false; |
karpent | 0:d8b9955d2b36 | 88 | } |
karpent | 1:5e49b46de1b0 | 89 | |
karpent | 3:732f7144ec81 | 90 | UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, timer.GetRunningTime()); |
karpent | 2:8db224cc1fcb | 91 | |
karpent | 1:5e49b46de1b0 | 92 | // Redraw foreground |
karpent | 0:d8b9955d2b36 | 93 | display->Clear(); |
karpent | 0:d8b9955d2b36 | 94 | DrawTracks(); |
karpent | 1:5e49b46de1b0 | 95 | |
karpent | 0:d8b9955d2b36 | 96 | DrawRadarBeam(currentBeamAngle); |
karpent | 0:d8b9955d2b36 | 97 | } |
karpent | 0:d8b9955d2b36 | 98 | |
karpent | 0:d8b9955d2b36 | 99 | |
karpent | 0:d8b9955d2b36 | 100 | bool RadarDemo::IsRunning() |
karpent | 0:d8b9955d2b36 | 101 | { |
karpent | 3:732f7144ec81 | 102 | return demoTime > 0 ? timer.GetRunningTime() <= demoTime : true; |
karpent | 0:d8b9955d2b36 | 103 | } |
karpent | 0:d8b9955d2b36 | 104 | |
karpent | 0:d8b9955d2b36 | 105 | |
karpent | 0:d8b9955d2b36 | 106 | void RadarDemo::AddSampleTargets(int count) |
karpent | 0:d8b9955d2b36 | 107 | { |
karpent | 0:d8b9955d2b36 | 108 | const float minSpeed = 200; // [km/h] |
karpent | 0:d8b9955d2b36 | 109 | const float maxSpeed = 800; // [km/h] |
karpent | 0:d8b9955d2b36 | 110 | Target *target; |
karpent | 0:d8b9955d2b36 | 111 | |
karpent | 3:732f7144ec81 | 112 | srand(timer.GetRunningTime()); |
karpent | 0:d8b9955d2b36 | 113 | |
karpent | 0:d8b9955d2b36 | 114 | for (int i = 0; i<count; i++) { |
karpent | 0:d8b9955d2b36 | 115 | // Generate target parameters |
karpent | 0:d8b9955d2b36 | 116 | float angle = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 117 | float range = GetMaxRange() * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 118 | float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 119 | float direction = 2 * M_PI * rand() / (float)RAND_MAX; |
karpent | 0:d8b9955d2b36 | 120 | |
karpent | 0:d8b9955d2b36 | 121 | // Create a new target |
karpent | 0:d8b9955d2b36 | 122 | target = new Target(i + 100, speed, direction); |
karpent | 0:d8b9955d2b36 | 123 | target->SetLocationAngular(range, angle); |
karpent | 0:d8b9955d2b36 | 124 | |
karpent | 0:d8b9955d2b36 | 125 | // Put target on the list |
karpent | 0:d8b9955d2b36 | 126 | targetsList.push_back(*target); |
karpent | 0:d8b9955d2b36 | 127 | } |
karpent | 0:d8b9955d2b36 | 128 | } |
karpent | 0:d8b9955d2b36 | 129 | |
karpent | 0:d8b9955d2b36 | 130 | |
karpent | 0:d8b9955d2b36 | 131 | void RadarDemo::UnvalidateBackground() |
karpent | 0:d8b9955d2b36 | 132 | { |
karpent | 1:5e49b46de1b0 | 133 | _needsRedraw = true; |
karpent | 0:d8b9955d2b36 | 134 | } |
karpent | 0:d8b9955d2b36 | 135 | |
karpent | 0:d8b9955d2b36 | 136 | |
karpent | 0:d8b9955d2b36 | 137 | bool RadarDemo::NeedsRedraw() |
karpent | 0:d8b9955d2b36 | 138 | { |
karpent | 0:d8b9955d2b36 | 139 | return _needsRedraw; |
karpent | 0:d8b9955d2b36 | 140 | } |
karpent | 0:d8b9955d2b36 | 141 | |
karpent | 2:8db224cc1fcb | 142 | |
karpent | 4:66f13188c26b | 143 | UserInputType RadarDemo::CheckUserInput(Window screen) |
karpent | 2:8db224cc1fcb | 144 | { |
karpent | 4:66f13188c26b | 145 | pPoint p; |
karpent | 4:66f13188c26b | 146 | |
karpent | 4:66f13188c26b | 147 | // There is a timeout after pressing the button |
karpent | 4:66f13188c26b | 148 | if(TouchScreenDisabled()) |
karpent | 4:66f13188c26b | 149 | return None; |
karpent | 4:66f13188c26b | 150 | |
karpent | 4:66f13188c26b | 151 | if ((p = _ts->DetectTouch()) != NULL) { |
karpent | 2:8db224cc1fcb | 152 | |
karpent | 4:66f13188c26b | 153 | if(p->X > (screen.x2-raSize) && p->Y < (screen.y1 + raSize)) { |
karpent | 4:66f13188c26b | 154 | if(ChangeRange(1)) { |
karpent | 2:8db224cc1fcb | 155 | UnvalidateBackground(); |
karpent | 4:66f13188c26b | 156 | SetTouchScreenTimeout(); |
karpent | 4:66f13188c26b | 157 | |
karpent | 4:66f13188c26b | 158 | return ButtonPressed; |
karpent | 4:66f13188c26b | 159 | } |
karpent | 2:8db224cc1fcb | 160 | } |
karpent | 2:8db224cc1fcb | 161 | |
karpent | 4:66f13188c26b | 162 | if(p->X > (screen.x2-raSize) && p->Y > (screen.y2-raSize)) { |
karpent | 4:66f13188c26b | 163 | if(ChangeRange(-1)) { |
karpent | 2:8db224cc1fcb | 164 | UnvalidateBackground(); |
karpent | 4:66f13188c26b | 165 | SetTouchScreenTimeout(); |
karpent | 4:66f13188c26b | 166 | |
karpent | 4:66f13188c26b | 167 | return ButtonPressed; |
karpent | 4:66f13188c26b | 168 | } |
karpent | 2:8db224cc1fcb | 169 | } |
karpent | 2:8db224cc1fcb | 170 | |
karpent | 4:66f13188c26b | 171 | SetCenter(p->X, p->Y); |
karpent | 4:66f13188c26b | 172 | UnvalidateBackground(); |
karpent | 2:8db224cc1fcb | 173 | |
karpent | 4:66f13188c26b | 174 | return SingleTouch; |
karpent | 2:8db224cc1fcb | 175 | } |
karpent | 2:8db224cc1fcb | 176 | |
karpent | 4:66f13188c26b | 177 | return None; |
karpent | 2:8db224cc1fcb | 178 | } |
karpent | 4:66f13188c26b | 179 | |
karpent | 4:66f13188c26b | 180 | bool RadarDemo::TouchScreenDisabled() |
karpent | 4:66f13188c26b | 181 | { |
karpent | 4:66f13188c26b | 182 | return _buttonPressTime + buttonPressTimeout > timer.GetRunningTime(); |
karpent | 4:66f13188c26b | 183 | } |
karpent | 4:66f13188c26b | 184 | |
karpent | 4:66f13188c26b | 185 | void RadarDemo::SetTouchScreenTimeout() |
karpent | 4:66f13188c26b | 186 | { |
karpent | 4:66f13188c26b | 187 | _buttonPressTime = timer.GetRunningTime(); |
karpent | 4:66f13188c26b | 188 | } |