Optimaze with new mbed os for study
Dependencies: TS_DISCO_F746NG BSP_DISCO_F746NG Graphics
RadarDemo/RadarTimer.cpp@4:66f13188c26b, 2016-11-05 (annotated)
- Committer:
- karpent
- Date:
- Sat Nov 05 20:24:59 2016 +0000
- Revision:
- 4:66f13188c26b
- Parent:
- 3:732f7144ec81
Code refactoring, 400ms timeout added when button is pressed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
karpent | 4:66f13188c26b | 1 | // |
karpent | 4:66f13188c26b | 2 | // RadarTimer.cpp - Radar time subsystem simulator. |
karpent | 4:66f13188c26b | 3 | // |
karpent | 4:66f13188c26b | 4 | |
karpent | 3:732f7144ec81 | 5 | #include "RadarTimer.h" |
karpent | 3:732f7144ec81 | 6 | #include "Commons.h" |
karpent | 3:732f7144ec81 | 7 | |
karpent | 3:732f7144ec81 | 8 | RadarTimer::RadarTimer() |
karpent | 3:732f7144ec81 | 9 | { |
karpent | 4:66f13188c26b | 10 | // Set 10seconds as default value for scan period; |
karpent | 4:66f13188c26b | 11 | _scanPeriod = 10000; |
karpent | 4:66f13188c26b | 12 | _lastScanTime = 0; |
karpent | 3:732f7144ec81 | 13 | } |
karpent | 3:732f7144ec81 | 14 | |
karpent | 3:732f7144ec81 | 15 | RadarTimer::RadarTimer(uint32_t scanPeriod) |
karpent | 3:732f7144ec81 | 16 | { |
karpent | 3:732f7144ec81 | 17 | SetScanPeriod(scanPeriod); |
karpent | 4:66f13188c26b | 18 | _lastScanTime = 0; |
karpent | 3:732f7144ec81 | 19 | } |
karpent | 3:732f7144ec81 | 20 | |
karpent | 3:732f7144ec81 | 21 | |
karpent | 3:732f7144ec81 | 22 | void RadarTimer::SetScanPeriod(uint32_t scanPeriod) |
karpent | 3:732f7144ec81 | 23 | { |
karpent | 3:732f7144ec81 | 24 | _scanPeriod = scanPeriod * 1000; |
karpent | 3:732f7144ec81 | 25 | } |
karpent | 3:732f7144ec81 | 26 | |
karpent | 3:732f7144ec81 | 27 | void RadarTimer::Start() |
karpent | 3:732f7144ec81 | 28 | { |
karpent | 3:732f7144ec81 | 29 | #ifndef _SDL_timer_h |
karpent | 4:66f13188c26b | 30 | start(); |
karpent | 3:732f7144ec81 | 31 | #endif |
karpent | 4:66f13188c26b | 32 | _lastScanTime = GetRunningTime(); |
karpent | 3:732f7144ec81 | 33 | } |
karpent | 3:732f7144ec81 | 34 | |
karpent | 4:66f13188c26b | 35 | |
karpent | 3:732f7144ec81 | 36 | uint32_t RadarTimer::GetRunningTime() |
karpent | 3:732f7144ec81 | 37 | { |
karpent | 3:732f7144ec81 | 38 | #ifdef _SDL_timer_h |
karpent | 3:732f7144ec81 | 39 | return SDL_GetTicks(); |
karpent | 3:732f7144ec81 | 40 | #else |
karpent | 3:732f7144ec81 | 41 | return read_ms(); |
karpent | 3:732f7144ec81 | 42 | #endif |
karpent | 3:732f7144ec81 | 43 | } |
karpent | 3:732f7144ec81 | 44 | |
karpent | 3:732f7144ec81 | 45 | |
karpent | 3:732f7144ec81 | 46 | void RadarTimer::RegisterScan() |
karpent | 3:732f7144ec81 | 47 | { |
karpent | 4:66f13188c26b | 48 | // Lock scan time |
karpent | 3:732f7144ec81 | 49 | _runningTime = GetRunningTime(); |
karpent | 4:66f13188c26b | 50 | |
karpent | 3:732f7144ec81 | 51 | // Reset scan time after one full turn |
karpent | 3:732f7144ec81 | 52 | if (_runningTime >= (_lastScanTime + _scanPeriod)) { |
karpent | 3:732f7144ec81 | 53 | _lastScanTime = _runningTime; |
karpent | 3:732f7144ec81 | 54 | } |
karpent | 3:732f7144ec81 | 55 | } |
karpent | 3:732f7144ec81 | 56 | |
karpent | 3:732f7144ec81 | 57 | |
karpent | 3:732f7144ec81 | 58 | float RadarTimer::GetBeamAngle() |
karpent | 3:732f7144ec81 | 59 | { |
karpent | 3:732f7144ec81 | 60 | float angle = 2 * M_PI * (GetRunningTime() - _lastScanTime) / (float)_scanPeriod; |
karpent | 3:732f7144ec81 | 61 | if(angle >= (2 * M_PI)) { |
karpent | 3:732f7144ec81 | 62 | angle -= 2 * M_PI; |
karpent | 3:732f7144ec81 | 63 | } |
karpent | 4:66f13188c26b | 64 | |
karpent | 3:732f7144ec81 | 65 | return angle; |
karpent | 3:732f7144ec81 | 66 | } |