Kyle Lemons / Mbed 2 deprecated HvZ

Dependencies:   XBeeLib mbed HvZAlphaNumLib HvZServerLib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers iHvZ.hpp Source File

iHvZ.hpp

00001 #ifndef _iHvZ
00002 #define _iHvZ
00003 
00004 #include "mbed.h"
00005 
00006 
00007 #include <map>
00008 #include <string>
00009 #include <sstream>
00010 #include <fstream>
00011 class iHvZ;
00012 
00013 #define STUN_STATUS_LED LED4
00014 #define STUN_DEFAULT_DURATION (15)
00015 #define STUN_INCUBATION_TIME (30)
00016 #include "Stun.hpp"
00017 
00018 #define _XBEE_DEBUG 0
00019 #define XBEE_PINS_DIN_DOUT_RST_ON p28,p27,p26,p20
00020 #include "XBee.hpp"
00021 
00022 #define TAG_TIMEOUT_TIME 5
00023 #define TAG_INPUT p23
00024 #include "Tag.hpp"
00025 
00026 #define ALPHA_NUM_DISPLAY_PINS p13, p10, p8, p11, p9, p15, p6, p14, p5, p12, p7
00027 #include "AlphaNumDisplay.hpp"
00028 
00029 #define ETH_PIN p21
00030 #include "udp.hpp"
00031 
00032 typedef enum
00033 {
00034   STATUS_HUMAN = 0,
00035   STATUS_ZOMBIE
00036 } Status;
00037 
00038 class iHvZ {
00039 private: 
00040     // Device configuration
00041     string   m_id;              //< The ID (8-byte DeviceID or 16-bit MAC Address) of this iHvZ device
00042     Stun     m_stun;            //< The stun status class
00043     XBee     m_xb;              //< The XBee device
00044     Tag      m_tag;             //< The device-to-device tagging
00045     AlphaNumDisplay m_alphanum;  //< The alphanumeric display 
00046     UDP      m_server;          //< The UDP server class
00047     
00048     // Game configuration
00049     unsigned m_stun_duration;   //< The current stun duration time
00050     unsigned m_incubation_time; //< The zombie incubation time
00051     
00052     // Tag IDs
00053     vector<string> m_tagids;   //< The list of IDs (8-byte TagIDs) given out when stunned or tagged (antidotes, etc can add to this list)
00054         
00055     // List of TagIDs of victims
00056     vector<string> m_victimids;
00057     
00058 public:
00059     /// Start an iHvZ game with the given ID (should start out as the MAC address)
00060     inline iHvZ(string id)
00061     : m_id(id),
00062       m_stun(STUN_STATUS_LED),
00063       m_xb(id, XBEE_PINS_DIN_DOUT_RST_ON),
00064       m_tag(this, TAG_INPUT),
00065       m_server(this, ETH_PIN),
00066       m_alphanum(ALPHA_NUM_DISPLAY_PINS),
00067       m_stun_duration(STUN_DEFAULT_DURATION),
00068       m_incubation_time(STUN_INCUBATION_TIME)
00069     {
00070         srand(time(NULL));
00071     }
00072     
00073     inline string itoa(int i)
00074     {
00075         static char buf[12];
00076         sprintf(buf, "%d", i);
00077         return string(buf);
00078     }
00079     
00080     inline int atoi(string s)
00081     {
00082         int i;
00083         sscanf(s.c_str(), "%d", &i);
00084         return i;
00085     }
00086     
00087     /* Filesystem save and load */
00088     /** Save the iHvZ state to iHvZ.cfg on the filesystem */
00089     inline bool save()
00090     {
00091         LocalFileSystem lfs("usb");
00092         Serial usb(USBTX,USBRX);
00093         ofstream statefile("/usb/iHvZ.cfg");
00094         map<string,string> params;
00095         int writecount = 0;
00096         
00097         // Make sure the file opened
00098         if (!statefile) return false;
00099         
00100         // Set the parameters
00101         //params["mode"] = (m_status==STATUS_HUMAN)?"HUMAN":"ZOMBIE";
00102         params["deviceid"] = m_id;
00103         params["stundur"] = itoa(m_stun_duration);
00104         params["inctime"] = itoa(m_incubation_time);
00105         params["tagcount"] = itoa(m_tagids.size());
00106         for (int i = 0; i < m_tagids.size(); ++i)
00107         {
00108             params["tagid[" + itoa(i) + "]"] = m_tagids[i];
00109         }
00110         params["victimcount"] = itoa(m_victimids.size());
00111         for (int i = 0; i < m_victimids.size(); ++i)
00112         {
00113             params["victim[" + itoa(i) + "]"] = m_victimids[i];
00114         }
00115         
00116         
00117         // Write to file
00118         for (map<string,string>::iterator iter = params.begin(); iter != params.end(); ++iter)
00119         {
00120             statefile << iter->first << "=" << iter->second << endl;
00121             ++writecount;
00122         }
00123         
00124         // Write status to USB
00125         usb.printf("Successfully wrote %d parameters to iHvZ.cfg\r\n", writecount);
00126         
00127         // Update the display (in case stuff changed)
00128         m_alphanum.display(m_tagids.size()==0?'Z':'H');
00129         
00130         // Success
00131         return true;
00132     }
00133     
00134     /* Load the iHvZ state from iHvZ.cfg on the filesystem */
00135     inline bool load()
00136     {
00137         LocalFileSystem lfs("usb");
00138         Serial usb(USBTX,USBRX);
00139         ifstream statefile("/usb/iHvZ.cfg");
00140         map<string,string> params;
00141         int readcount = 0;
00142         
00143         // Make sure the file opened
00144         if (statefile)
00145         {
00146             // Read in the lines of the file
00147             string line;
00148             while (getline(statefile, line))
00149             {
00150                 int eqsign = line.find('=');
00151                 if (eqsign == string::npos) continue;
00152                 string param = line.substr(0,eqsign);
00153                 string value = line.substr(eqsign+1);
00154                 params[param] = value;
00155                 ++readcount;
00156             }
00157             
00158             // Read static parameters
00159             m_id = params["deviceid"];
00160             m_stun_duration = atoi(params["stundur"]);
00161             m_incubation_time = atoi(params["inctime"]);
00162             
00163             // Read lives
00164             int tagcnt = atoi(params["tagcount"]);
00165             m_tagids.clear();
00166             m_tagids.reserve(tagcnt);
00167             for (int i = 0; i < tagcnt; ++i)
00168             {
00169                 m_tagids.push_back( params["tagid[" + itoa(i) + "]"] );
00170             }
00171             
00172             // Read victims
00173             int victimcnt = atoi(params["victimcount"]);
00174             m_victimids.clear();
00175             m_victimids.reserve(victimcnt);
00176             for (int i = 0; i < victimcnt; ++i)
00177             {
00178                 m_victimids.push_back( params["victim[" + itoa(i) + "]"] );
00179             }
00180         
00181             usb.printf("Successfully read %d parameters from /usb/iHvZ.cfg\r\n", readcount);
00182         }
00183         else
00184         {
00185             usb.printf("Unable to read /usb/iHvZ.cfg\r\n");
00186         }
00187         m_alphanum.display(m_tagids.size()==0?'Z':'H');
00188         
00189         // Success or failure
00190         return readcount > 0;
00191     }
00192     
00193     
00194     /* Getters */
00195     /// Get the status
00196     inline bool status() { return m_tagids.size() ? STATUS_HUMAN : STATUS_ZOMBIE; }
00197     /// Get the id
00198     inline string id() { return m_id; }
00199     /// Get the next tagid
00200     inline string life()
00201     {
00202         if (m_tagids.size() == 0) return m_id;
00203         return m_tagids.back();
00204     }
00205     /// Get the victims vector
00206     inline vector<string> &get_victims() { return m_victimids; }
00207     
00208     /// Get stun time
00209     inline unsigned stun_duration() { return m_stun_duration; }    
00210     /// Get incubation time
00211     inline unsigned incubation_time() { return m_incubation_time; }
00212     /// Get the stun tracker
00213     inline Stun &stun_tracker() { return m_stun; }
00214     /// Get the tag tracker
00215     inline Tag &tag_tracker() { return m_tag; }
00216     /// Get the xbee device
00217     inline XBee &xbee_device() { return m_xb; }
00218     /// Get the UDP device
00219     //inline UDP &udp_device() { return m_server; }
00220     /// Get the alphanum device
00221     inline AlphaNumDisplay &alphanumdisplay_device() { return m_alphanum; }
00222     
00223     
00224     /* Setters */
00225     /// Set the id
00226     inline void id(string newid) { m_id = newid; m_xb.uid(newid); }
00227     /// Add a tagid
00228     inline void life(string newtagid) { 
00229         m_tagids.push_back(newtagid); 
00230         
00231         m_alphanum.display('H');
00232     }
00233     /// Set stun time
00234     inline void stun_duration(unsigned stun) { m_stun_duration = stun; }    
00235     /// Set incubation time
00236     inline void incubation_time(unsigned incubation) { m_incubation_time = incubation; }
00237 
00238     /* Meta-actions */
00239     inline void tagged()
00240     {
00241         if (status() == STATUS_ZOMBIE) return;
00242         // Should we have an admin flag?
00243         
00244         // Use up the next player life or antidote
00245         if (m_tagids.size() > 0) m_tagids.pop_back();
00246         
00247         // If the player still has lives, they are fine
00248         if (m_tagids.size() > 0) return;
00249         
00250         // This player is now a zombie!
00251         m_stun.stun(m_incubation_time);
00252 
00253         // Save the stun to prevent power cycle cheating
00254         save();
00255         wait(1);
00256         m_alphanum.display('Z');
00257     }
00258     
00259     // Register the tagging of the given tagID by this device
00260     inline void register_tag(string tagid) { m_victimids.push_back(tagid); }
00261     
00262     // Clear the victim list
00263     inline void clear_victims() { m_victimids.clear(); }
00264     inline void clear_lives() { m_tagids.clear(); }
00265     
00266     inline void stunned()
00267     {
00268         if (status() == STATUS_HUMAN) return;
00269         
00270         // This zombie is now stunned
00271         m_stun.stun(m_stun_duration);
00272         
00273         // Save the stun to prevent power cycle cheating
00274         save();
00275     }
00276     
00277     inline void register_stun(string tagid) {}
00278 };
00279 
00280 
00281 #endif