You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
4.1 KiB
130 lines
4.1 KiB
|
|
#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_AUTOSHED 0x0200
|
|
#define LOCO_F_AUTORANDOM 0x0400
|
|
#define LOCO_F_AUTOSTOP 0x0800
|
|
|
|
#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 been 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_ENTERBLOCKNEXT, // got new block ready?
|
|
LOCO_OR_ENTERBLOCKSTOP, // got new block ready?
|
|
// if NEXT is empty and way not AutoPrepareWay not finished... slow down
|
|
LOCO_OR_STOPWAIT // stopping
|
|
};
|
|
|
|
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
|
|
char schedway[WAYDATA_LEN]; // scheduled way W:15,B:+B17,W30,B:-B21
|
|
|
|
// 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
|
|
|
|
int sched_step; // on automode this is the scheduled way step
|
|
char auto_way[WAYDATA_LEN]; // route to way "b:+blockname,t:name:0,t:name:1,b:-blockname"
|
|
char auto_wayold[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);
|
|
int SchedulerNextStep(Locomotive *loc); // automode, calculate next step
|
|
void SendUpdate(Locomotive *loc);
|
|
|
|
int Loco_SearchAndLock(Locomotive *loco);
|
|
int Loco_PrepareWay(Locomotive *loco);
|
|
int Loco_OnRoute(Locomotive *loco);
|
|
int Loco_BlockEnterStop(Locomotive *loco);
|
|
int Loco_BlockEnterNext(Locomotive *loco);
|
|
int Loco_BlockStopWait(Locomotive *loco);
|
|
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 SetModeMan(string name);
|
|
int SetModeAutoMan(string name);
|
|
int SetModeAuto(string name);
|
|
int SetModeAutoRand(string name);
|
|
int SetModeAutoShed(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 GetFlags(string name);
|
|
|
|
int AutoCheckWaySingleStep(string way, Locomotive *loc, int *data);
|
|
int Loop();
|
|
|
|
string GetName(int idx);
|
|
|
|
JSONParse GetJSON(string name);
|
|
void GetJSONAll(JSONParse *json);
|
|
Locomotive GetLocomotiveFromJSON(JSONParse *j);
|
|
};
|
|
|
|
|
|
#endif
|