NerfUS / Mbed 2 deprecated NerfUSTarget

Dependencies:   LedController mbed-rtos mbed NerfUSXbee Servomotor TargetManager

Fork of NerfUS by NerfUS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TargetManager.cpp Source File

TargetManager.cpp

00001 #include "TargetManager.hpp"
00002 #include <cassert>
00003 
00004 TargetManager::TargetManager(std::vector<TargetInterface*>& targets,
00005         std::vector<NerfusTickerInterface*>& nerfus_tickers) :
00006     targets(targets),
00007     nerfus_tickers(nerfus_tickers)
00008 {
00009     for(int i=0; i<targets.size(); i++)
00010     {
00011         is_active_target.push_back(false);
00012     }
00013 }
00014 
00015 void TargetManager::execute(const TargetInfo& target_info)
00016 {
00017     assert(0 <= target_info.id && target_info.id < targets.size() && "ID is out of range");
00018     TargetInterface& target = *(targets[target_info.id]);
00019 
00020     if(target_info.type == TARGET_TYPE_ALLY)
00021     {
00022         target.ally_command();
00023     }
00024     else if(target_info.type == TARGET_TYPE_ENEMY)
00025     {
00026         target.enemy_command();
00027     }
00028     else
00029     {
00030         assert(false && "Target type invalid");
00031     }
00032 
00033     is_active_target[target_info.id] = true;
00034     nerfus_tickers[target_info.id]->start(target_info.timeout_ms);
00035 }
00036 
00037 void TargetManager::execute(const std::vector<uint8_t>& target_info_bytes, int *address)
00038 {
00039     const int id = target_info_bytes[0];
00040     const TargetType type = (target_info_bytes[1] == 0) ? TARGET_TYPE_ALLY : TARGET_TYPE_ENEMY;
00041     const int timeout_msb = target_info_bytes[2];
00042     const int timeout_lsb = target_info_bytes[3];
00043     const int timeout = (timeout_msb << 8) + timeout_lsb;
00044     const TargetInfo target_info = make_TargetInfo(id, type, timeout);
00045     execute(target_info);
00046 }
00047 
00048 void TargetManager::target_hit(int target_number)
00049 {
00050     const int time_ms = nerfus_tickers[target_number]->get_time_ms();
00051     if(is_active_target[target_number] && time_ms > 750)
00052     {
00053         is_active_target[target_number] = false;
00054         targets[target_number]->hit(time_ms);
00055         nerfus_tickers[target_number]->stop();
00056     }
00057 }
00058 
00059 void TargetManager::target_missed(int target_number)
00060 {
00061     if(is_active_target[target_number])
00062     {
00063         is_active_target[target_number] = false;
00064         targets[target_number]->timeout(nerfus_tickers[target_number]->get_time_ms());
00065         nerfus_tickers[target_number]->stop();
00066     }
00067 }