NerfUS mobile node that manages a target for the Nerf gun firing range
Dependencies: LedController mbed-rtos mbed NerfUSXbee Servomotor TargetManager
Fork of NerfUS by
source/TargetManager.cpp@35:7ee45f6d6484, 2017-03-30 (annotated)
- Committer:
- Maxime Dupuis
- Date:
- Thu Mar 30 15:06:01 2017 -0400
- Revision:
- 35:7ee45f6d6484
- Parent:
- 34:85994e0501fb
- Child:
- 36:f55ce07292c9
Receive TargetInfo in bytes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Maxime Dupuis |
28:8fc53a870691 | 1 | #include "TargetManager.hpp" |
Maxime Dupuis |
28:8fc53a870691 | 2 | |
Maxime Dupuis |
30:412a779cf607 | 3 | TargetManager::TargetManager(std::vector<TargetInterface*>& targets, |
Maxime Dupuis |
30:412a779cf607 | 4 | NerfusTickerInterface& nerfus_ticker) : |
Maxime Dupuis |
30:412a779cf607 | 5 | targets(targets), |
Maxime Dupuis |
30:412a779cf607 | 6 | nerfus_ticker(nerfus_ticker) |
Maxime Dupuis |
30:412a779cf607 | 7 | { |
Maxime Dupuis |
32:50b777bab5a4 | 8 | for(int i=0; i<targets.size(); i++) |
Maxime Dupuis |
32:50b777bab5a4 | 9 | { |
Maxime Dupuis |
32:50b777bab5a4 | 10 | is_active_target.push_back(false); |
Maxime Dupuis |
32:50b777bab5a4 | 11 | } |
Maxime Dupuis |
32:50b777bab5a4 | 12 | } |
Maxime Dupuis |
30:412a779cf607 | 13 | |
Maxime Dupuis |
34:85994e0501fb | 14 | void TargetManager::execute(const TargetInfo& target_info) |
Maxime Dupuis |
32:50b777bab5a4 | 15 | { |
Maxime Dupuis |
34:85994e0501fb | 16 | TargetInterface& target = *(targets[target_info.id]); |
Maxime Dupuis |
32:50b777bab5a4 | 17 | |
Maxime Dupuis |
34:85994e0501fb | 18 | if(target_info.type == TARGET_TYPE_ALLY) |
Maxime Dupuis |
34:85994e0501fb | 19 | { |
Maxime Dupuis |
34:85994e0501fb | 20 | target.ally_command(); |
Maxime Dupuis |
34:85994e0501fb | 21 | } |
Maxime Dupuis |
34:85994e0501fb | 22 | else |
Maxime Dupuis |
34:85994e0501fb | 23 | { |
Maxime Dupuis |
34:85994e0501fb | 24 | target.enemy_command(); |
Maxime Dupuis |
34:85994e0501fb | 25 | } |
Maxime Dupuis |
32:50b777bab5a4 | 26 | |
Maxime Dupuis |
34:85994e0501fb | 27 | is_active_target[target_info.id] = true; |
Maxime Dupuis |
32:50b777bab5a4 | 28 | |
Maxime Dupuis |
34:85994e0501fb | 29 | nerfus_ticker.start(target_info.timeout_ms); |
Maxime Dupuis |
30:412a779cf607 | 30 | } |
Maxime Dupuis |
30:412a779cf607 | 31 | |
Maxime Dupuis |
35:7ee45f6d6484 | 32 | void TargetManager::execute(const std::vector<uint8_t>& target_info_bytes) |
Maxime Dupuis |
35:7ee45f6d6484 | 33 | { |
Maxime Dupuis |
35:7ee45f6d6484 | 34 | const int id = target_info_bytes[1]; |
Maxime Dupuis |
35:7ee45f6d6484 | 35 | const TargetType type = (target_info_bytes[2] == 0) ? TARGET_TYPE_ALLY : TARGET_TYPE_ENEMY; |
Maxime Dupuis |
35:7ee45f6d6484 | 36 | const int timeout_msb = target_info_bytes[3]; |
Maxime Dupuis |
35:7ee45f6d6484 | 37 | const int timeout_lsb = target_info_bytes[4]; |
Maxime Dupuis |
35:7ee45f6d6484 | 38 | const int timeout = (timeout_msb << 8) + timeout_lsb; |
Maxime Dupuis |
35:7ee45f6d6484 | 39 | const TargetInfo target_info = make_TargetInfo(id, type, timeout); |
Maxime Dupuis |
35:7ee45f6d6484 | 40 | execute(target_info); |
Maxime Dupuis |
35:7ee45f6d6484 | 41 | } |
Maxime Dupuis |
35:7ee45f6d6484 | 42 | |
Maxime Dupuis |
28:8fc53a870691 | 43 | void TargetManager::target_hit(int target_number) |
Maxime Dupuis |
28:8fc53a870691 | 44 | { |
Maxime Dupuis |
32:50b777bab5a4 | 45 | if(is_active_target[target_number]) |
Maxime Dupuis |
32:50b777bab5a4 | 46 | { |
Maxime Dupuis |
32:50b777bab5a4 | 47 | targets[target_number]->hit(nerfus_ticker.get_time_ms()); |
Maxime Dupuis |
32:50b777bab5a4 | 48 | nerfus_ticker.stop(); |
Maxime Dupuis |
32:50b777bab5a4 | 49 | is_active_target[target_number] = false; |
Maxime Dupuis |
32:50b777bab5a4 | 50 | } |
Maxime Dupuis |
28:8fc53a870691 | 51 | } |
Maxime Dupuis |
28:8fc53a870691 | 52 | |
Maxime Dupuis |
28:8fc53a870691 | 53 | void TargetManager::target_missed(int target_number) |
Maxime Dupuis |
28:8fc53a870691 | 54 | { |
Maxime Dupuis |
32:50b777bab5a4 | 55 | if(is_active_target[target_number]) |
Maxime Dupuis |
32:50b777bab5a4 | 56 | { |
Maxime Dupuis |
32:50b777bab5a4 | 57 | targets[target_number]->timeout(nerfus_ticker.get_time_ms()); |
Maxime Dupuis |
32:50b777bab5a4 | 58 | nerfus_ticker.stop(); |
Maxime Dupuis |
32:50b777bab5a4 | 59 | is_active_target[target_number] = false; |
Maxime Dupuis |
32:50b777bab5a4 | 60 | } |
Maxime Dupuis |
28:8fc53a870691 | 61 | } |
Maxime Dupuis |
28:8fc53a870691 | 62 |