#ifndef _LOCOMOTIVE_H_ #define _LOCOMOTIVE_H_ #include "modelbahn.h" #include "server.h" #define LOCO_F_REVERSE 0x0001 #define LOCO_F_CARGO 0x0010 #define LOCO_F_CANREVERSE 0x0020 #define LOCO_F_SHORTTRAIN 0x0040 #define LOCO_F_AUTO 0x0100 #define LOCO_F_RANDOM 0x0200 #define LOCO_F_AUTOSTOP 0x0400 #define LOCO_TO_TURNOUT (50+TURNOUT_DEFAULT_ACTIVETIMEOUT) #define LOCO_TO_TRYAGAIN (1000) #define WAYDATA_LEN (16*REFERENCENAME_LEN) enum { LOCO_INT_UNDEF = 0, LOCO_INT_DCC14, LOCO_INT_DCC28, LOCO_INT_DCC128 }; enum { LOCO_OR_NOTHING = 0, LOCO_OR_SEARCH, // search way (to next block and lock way) LOCO_OR_PREPARE, // switch turnouts right way (one every 100ms) // if no turnout has to be set, continue LOCO_OR_ONTHEWAY, // locomotive is on the way // maybe prepare next block? // ASSIGN -> PREV, NEXT -> ASSIGN, NEXT <- (empty) // propabely searching next block (if DEST is set)? LOCO_OR_ENTERBLOCK, // got new block ready? // if NEXT is empty and way not AutoPrepareWay not finished... slow down LOCO_OR_STOPWAIT // stopping }; struct s_LocoAuto { int onroute; // LOCO_OR_..... struct timeval waituntil; // wait until this time for next action (tournout, waiting station...) }; struct s_Locomotive { char name[REFERENCENAME_LEN]; // name char ifname[REFERENCENAME_LEN]; // ref. of interface int addr; // address on bus int stepcode; // stepcoding LOCO_INT_DCC... int flags; // flags int vmin; // speed - below this we always set 0 int vslow; // speed - slow entry in trainstation int vmid; // speed - for normal trains (max for cargo trains, on automode) int vfast; // speed - for normal trains int vmax; // speed - maximum speed // dynamic data int speed; // current speed int64_t func; // function enabled ... light... char blockassign[REFERENCENAME_LEN]; // currently assigned block [+BLOCKREFNAME ... -BLOCKREFNAME] char blocknext[REFERENCENAME_LEN]; // next block to go to char blockprev[REFERENCENAME_LEN]; // prev block (mostly assigned block char blockdest[REFERENCENAME_LEN]; // destination block char auto_way[WAYDATA_LEN]; // route to way "b:+blockname,t:name:0,t:name:1,b:-blockname" int auto_onroute; // LOCO_OR_.... int auto_data; // just some data needed by some steps struct timeval auto_timenext; // timeval of the next active step } typedef Locomotive; class Locomotives { private: Locomotive *locomotives; int max; int changed; pthread_mutex_t mtx; int Lock(); int UnLock(); // not thread safe JSONParse _GetJSON(int idx); public: Locomotives(); ~Locomotives(); bool IsChanged() { return changed; } void ClearChanged() { changed = 0; }; int Change(Locomotive *loco); int Delete(string name); int SetSpeed(string name, int speed); int SetReverse(string name, int reverse); int SetFunction(string name, int func, int value); int Reset(string name); int SetMan(string name); int SetAutoMan(string name); int SetAuto(string name); int SetAutoRand(string name); int SetSpeedFromBus (string ifname, int addr, int speed); int SetFunctionFromBus (string ifname, int addr, int func); int SetDestination (string name, string block, int direction); int SetAssign (string name, string block, int direction); int AutoCheckWaySingleStep(string way, string locname, int *data); int Loop(); string GetName(int idx); JSONParse GetJSON(string name); void GetJSONAll(JSONParse *json); Locomotive GetLocomotiveFromJSON(JSONParse *j); int Test(string loco); }; #endif