adding new lococtrl interface

origin
steffen 6 years ago
parent d244d406b2
commit ebe25b2b87

@ -213,7 +213,7 @@ int InterfaceZ21::Loop(string interfacename) {
loconet_map[inloc].addr = loconet_last.addr; loconet_map[inloc].addr = loconet_last.addr;
} }
debug (0, "%s:%d Loc:%d Speed:%d\n", __FILE__, __LINE__, loconet_map[inloc].addr, inspeed); debug (0, "%s:%d Loc:%d Speed:%d", __FILE__, __LINE__, loconet_map[inloc].addr, inspeed);
if (loconet_map[inloc].addr > 0) { if (loconet_map[inloc].addr > 0) {
int speed = inspeed; int speed = inspeed;
server->LocomotiveAddrSpeed(interfacename, loconet_map[inloc].addr, speed); server->LocomotiveAddrSpeed(interfacename, loconet_map[inloc].addr, speed);
@ -230,8 +230,14 @@ int InterfaceZ21::Loop(string interfacename) {
if (inloc >= 0 && inloc <= INTF_Z21_LOCONET_MAXADDR) { if (inloc >= 0 && inloc <= INTF_Z21_LOCONET_MAXADDR) {
if (loconet_last.addr) loconet_map[inloc] = loconet_last; if (loconet_last.addr) loconet_map[inloc] = loconet_last;
debug (0, "%s:%d Loc:%d Function:%d\n", __FILE__, __LINE__, loconet_map[inloc].addr, infunc); debug (0, "%s:%d Loc:%d Function:%d", __FILE__, __LINE__, loconet_map[inloc].addr, infunc);
} }
debug (0, "%s:%d Loc:%d Function:%d", __FILE__, __LINE__, loconet_map[inloc].addr, infunc);
if (loconet_map[inloc].addr > 0) {
server->LocomotiveAddrFunction(interfacename, loconet_map[inloc].addr, infunc);
}
loconet_last.addr = 0; loconet_last.addr = 0;
} }
} }
@ -341,8 +347,8 @@ void InterfaceZ21::SetLocoSpeed(Locomotive *l, int step) {
else if (step > 0) { else if (step > 0) {
l->flags &= ~LOCO_F_REVERSE; l->flags &= ~LOCO_F_REVERSE;
} }
if (l->flags & LOCO_F_REVERSE) buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x80; if (l->flags & LOCO_F_REVERSE) buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x00;
else buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x00; else buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x80;
// //
// setup loc addr // setup loc addr

@ -341,9 +341,12 @@ void Interfaces::SetLocoSpeed(Locomotive *l, int speed) {
int i; int i;
int step = 0; int step = 0;
// TEST
// (l->steps-1) ?????
if (abs(speed) < l->vmin) step = 0; if (abs(speed) < l->vmin) step = 0;
if (abs(speed) >= l->vmin) step = (speed * (l->steps-1)) / l->vmax; if (abs(speed) >= l->vmin) step = (speed * (l->steps)) / l->vmax;
if (abs(speed) > l->vmax) step = l->steps-1; if (abs(speed) > l->vmax) step = l->steps;
l->speed = speed; l->speed = speed;

@ -206,6 +206,28 @@ int Locomotives::SetSpeed(string name, int speed) {
}; };
int Locomotives::SetReverse(string name, int reverse) {
int i;
Lock();
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
if (name.compare(locomotives[i].name) == 0) {
if (reverse)
locomotives[i].flags |= LOCO_F_REVERSE;
else
locomotives[i].flags &= ~LOCO_F_REVERSE;
server->interfaces.SetLocoSpeed(&locomotives[i], locomotives[i].speed);
break;
}
}
UnLock();
return 1;
};
int Locomotives::SetFunction(string name, int func, int value) { int Locomotives::SetFunction(string name, int func, int value) {
int i; int i;
@ -227,13 +249,21 @@ int Locomotives::SetFunction(string name, int func, int value) {
// //
// set values from bus... // set values from bus...
// //
int Locomotives::SetSpeedFromBus(string name, int addr, int speed) { int Locomotives::SetSpeedFromBus(string ifname, int addr, int speed) {
int i; int i;
JSONParse jp; JSONParse jp;
debug (0, "%s:%d SetSpeedFromBus IfName:%s Addr:%d Speed:%d", __FILE__, __LINE__, ifname.c_str(), addr, speed);
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) { for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
if (name.compare(locomotives[i].name) == 0 && locomotives[i].addr == addr) { if (ifname.compare(locomotives[i].ifname) == 0 && locomotives[i].addr == addr) {
locomotives[i].speed = speed; if (speed == 0) locomotives[i].speed = 0;
if (speed >= (127-((127/locomotives[i].steps)+1))) locomotives[i].speed = locomotives[i].vmax;
else {
printf ("\n\n Speed:%d test:%d", speed, (127-((127/locomotives[i].steps)+1)));
locomotives[i].speed = (1+speed) * locomotives[i].vmax / 127;
if (locomotives[i].speed > locomotives[i].vmax) locomotives[i].speed = locomotives[i].vmax;
}
jp.AddObject("locomotive",_GetJSON(i)); jp.AddObject("locomotive",_GetJSON(i));
if(network) network->ChangeListPushToAll(jp.ToString()); if(network) network->ChangeListPushToAll(jp.ToString());
@ -245,13 +275,14 @@ int Locomotives::SetSpeedFromBus(string name, int addr, int speed) {
}; };
int Locomotives::SetDirectionFromBus(string name, int addr, int reverse) { int Locomotives::SetFunctionFromBus(string ifname, int addr, int func) {
int i; int i;
JSONParse jp; JSONParse jp;
debug (0, "%s:%d SetDirectionFromBus IfName:%s Addr:%d function:%d", __FILE__, __LINE__, ifname.c_str(), addr, func);
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) { for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
if (name.compare(locomotives[i].name) == 0 && locomotives[i].addr == addr) { if (ifname.compare(locomotives[i].ifname) == 0 && locomotives[i].addr == addr) {
if (reverse) locomotives[i].flags |= LOCO_F_REVERSE; if (func & 32) locomotives[i].flags |= LOCO_F_REVERSE;
else locomotives[i].flags &= ~LOCO_F_REVERSE; else locomotives[i].flags &= ~LOCO_F_REVERSE;
jp.AddObject("locomotive",_GetJSON(i)); jp.AddObject("locomotive",_GetJSON(i));

@ -45,10 +45,11 @@ class Locomotives {
int Change(Locomotive *loco); int Change(Locomotive *loco);
int Delete(string name); int Delete(string name);
int SetSpeed(string name, int speed); int SetSpeed(string name, int speed);
int SetReverse(string name, int reverse);
int SetFunction(string name, int func, int value); int SetFunction(string name, int func, int value);
int SetSpeedFromBus (string name, int addr, int speed); int SetSpeedFromBus (string ifname, int addr, int speed);
int SetDirectionFromBus (string name, int addr, int speed); int SetFunctionFromBus (string ifname, int addr, int func);
JSONParse GetJSON(string name); JSONParse GetJSON(string name);
void GetJSONAll(JSONParse *json); void GetJSONAll(JSONParse *json);

@ -6,7 +6,7 @@
#define BUFFERSIZE 64000 #define BUFFERSIZE 64000
#define MAXWAITTIME 500 #define MAXWAITTIME 500
#define UNIX_SOCKET_FILE "/tmp/modelbahn.socket" #define UNIX_SOCKET_FILE "/tmp/modelbahn.socket"
#define DEFAULT_DATADIR "/home/steffen/Dokumente/Programmierung/Modelbahn/" #define DEFAULT_DATADIR "/home/steffen/Dokumente/Programmierung/modelbahn/"
#define SESSIONS_MAX 8 #define SESSIONS_MAX 8
#define REFERENCENAME_LEN 128 #define REFERENCENAME_LEN 128

@ -22,7 +22,6 @@
#include "modelbahn.h" #include "modelbahn.h"
#include "server.h" #include "server.h"
int Server::Start() { int Server::Start() {
int err; int err;
pthread_attr_t attr; pthread_attr_t attr;

@ -29,8 +29,40 @@
#include "sensor.h" #include "sensor.h"
#include "interface.h" #include "interface.h"
enum {
SSTATUS_STARTUP,
SSTATUS_RESET,
SSTATUS_MANUAL,
SSTATUS_AUTO,
SSTATUS_MAX
};
enum {
MRESET_STEP_INTERFACES,
MRESET_STEP_LOCOMOTIVES,
MRESET_STEP_TURNOUTS,
MRESET_STEP_SENSORS,
MRESET_STEP_MAX
};
struct {
int mr_step; // current step
int mr_timestamp; // timestamp ( needed for times )
int mr_idx; // mode index
} typedef ModeResetData;
class Server { class Server {
private: private:
int status; // holds the current mode
int status_progress; // to return a value from 0-100
string status_text; // some text
ModeResetData data_reset;
pthread_mutex_t mtx; pthread_mutex_t mtx;
pthread_t thread; pthread_t thread;
int thread_running; int thread_running;
@ -74,6 +106,24 @@ public:
int GetHeight() { return railways.GetHeight(); }; int GetHeight() { return railways.GetHeight(); };
int GetWidth() { return railways.GetWidth(); }; int GetWidth() { return railways.GetWidth(); };
//
// Load Save Part
int Load();
int Save();
//
// Reset dynamic data
// FIXME:
// FIXME:
// FIXME: finish me
// FIXME:
// FIXME:
JSONParse GetJSONServerStatus();
void ModeReset(); // mode Reset if finished will go to Manual
void ModeManual(); // Manual Mode
void ModeAuto(); // only allowed if Manual was set
void ModeError(); // will cut power and keep this mode, until reset or Mode Manual
///////////////////////////////////////// /////////////////////////////////////////
// Railway // Railway
int RailwayChange(Railway *rw) { return railways.Change(rw);}; int RailwayChange(Railway *rw) { return railways.Change(rw);};
@ -104,6 +154,7 @@ public:
JSONParse LocomotiveGetJSON(string name) { return locomotives.GetJSON(name); }; JSONParse LocomotiveGetJSON(string name) { return locomotives.GetJSON(name); };
int LocomotiveDelete(string name) { return locomotives.Delete(name); }; int LocomotiveDelete(string name) { return locomotives.Delete(name); };
int LocomotiveSetSpeed(string name, int speed) { return locomotives.SetSpeed(name, speed); }; int LocomotiveSetSpeed(string name, int speed) { return locomotives.SetSpeed(name, speed); };
int LocomotiveSetReverse(string name, int reverse) { return locomotives.SetReverse(name, reverse); };
int LocomotiveSetFunction(string name, int func, int value) { return locomotives.SetFunction(name, func, value); }; int LocomotiveSetFunction(string name, int func, int value) { return locomotives.SetFunction(name, func, value); };
///////////////////////////////////////// /////////////////////////////////////////
@ -118,16 +169,10 @@ public:
///////////////////////////////////////// /////////////////////////////////////////
// reports from interfaces // reports from interfaces
int LocomotiveAddrSpeed(string name, int addr, int speed) { return locomotives.SetSpeedFromBus(name, addr, speed); }; int LocomotiveAddrSpeed(string name, int addr, int speed) { return locomotives.SetSpeedFromBus(name, addr, speed); };
int LocomotiveAddrDirection(string name, int addr, int reverse) { return locomotives.SetDirectionFromBus(name, addr, reverse); }; int LocomotiveAddrFunction(string name, int addr, int func) { return locomotives.SetFunctionFromBus(name, addr, func); };
int TurnoutAddrMode(string name, int addr, int active) { turnouts.SetFromBus(name, addr, active); return 1; }; int TurnoutAddrMode(string name, int addr, int active) { turnouts.SetFromBus(name, addr, active); return 1; };
int SensorAddrChange(string name, int addr, int active) { sensors.SetFromBus(name, addr, active); return 1; }; int SensorAddrChange(string name, int addr, int active) { sensors.SetFromBus(name, addr, active); return 1; };
//
// Load Save Part
int Load();
int Save();
protected: protected:
static void *ThreadEntry (void *This) { ((Server*)This)->ThreadProcess(); return NULL;}; static void *ThreadEntry (void *This) { ((Server*)This)->ThreadProcess(); return NULL;};
}; };

@ -107,11 +107,15 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) {
debug (0, "* Session Poweroff"); debug (0, "* Session Poweroff");
server->PowerOnOff(0); server->PowerOnOff(0);
} }
else if (command.compare("save") == 0) { else if (command.compare("save") == 0) {
debug (0, "* Save All"); debug (0, "* Save All");
server->Save(); server->Save();
} }
else if (command.compare("resetdata") == 0) {
debug (0, "* Reset All Data");
#warning FIXME: continue here
// server->ResetData();
}
else if (command.compare("getall") == 0) { else if (command.compare("getall") == 0) {
json.Clear(); json.Clear();
server->GetJSONAll(&json); server->GetJSONAll(&json);
@ -377,6 +381,12 @@ void Session::SetJSONLocomotive(JSONParse *jp) {
debug (0, "%s:%d SetJSONLocomotive Element %s Speed:%d", __FILE__, __LINE__, loconame.c_str(), speed); debug (0, "%s:%d SetJSONLocomotive Element %s Speed:%d", __FILE__, __LINE__, loconame.c_str(), speed);
server->LocomotiveSetSpeed(loconame, speed); server->LocomotiveSetSpeed(loconame, speed);
} }
if (jloco.GetValueInt("reverse", &value) == 1) {
//
// reverse
debug (0, "%s:%d SetJSONLocomotive Element %s Reverse:%d", __FILE__, __LINE__, loconame.c_str(), value);
server->LocomotiveSetReverse(loconame, value);
}
if (jloco.GetValueInt("function", &func) == 1 && jloco.GetValueInt("value", &value) == 1) { if (jloco.GetValueInt("function", &func) == 1 && jloco.GetValueInt("value", &value) == 1) {
// //
// set function // set function

Loading…
Cancel
Save