Skip to content

Commit

Permalink
Merge branch 'Broadcast' into EXRAILPlus
Browse files Browse the repository at this point in the history
  • Loading branch information
Asbelos committed Dec 16, 2021
2 parents 0947467 + 1b07d0a commit 8209207
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
37 changes: 7 additions & 30 deletions DCC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,38 +193,15 @@ void DCC::setFn( int cab, int16_t functionNumber, bool on) {
}
}

// Change function according to how button was pressed,
// typically in WiThrottle.
// Returns new state or -1 if nothing was changed.
int DCC::changeFn( int cab, int16_t functionNumber, bool pressed) {
int funcstate = -1;
if (cab<=0 || functionNumber>28) return funcstate;
// Flip function state
void DCC::changeFn( int cab, int16_t functionNumber) {
if (cab<=0 || functionNumber>28) return;
int reg = lookupSpeedTable(cab);
if (reg<0) return funcstate;

// Take care of functions:
// Imitate how many command stations do it: Button press is
// toggle but for F2 where it is momentary
if (reg<0) return;
unsigned long funcmask = (1UL<<functionNumber);
if (functionNumber == 2) {
// turn on F2 on press and off again at release of button
if (pressed) {
speedTable[reg].functions |= funcmask;
funcstate = 1;
} else {
speedTable[reg].functions &= ~funcmask;
funcstate = 0;
}
} else {
// toggle function on press, ignore release
if (pressed) {
speedTable[reg].functions ^= funcmask;
}
funcstate = (speedTable[reg].functions & funcmask)? 1 : 0;
}
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
speedTable[reg].functions ^= funcmask;
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
CommandDistributor::broadcastLoco(reg);
return funcstate;
}

int DCC::getFn( int cab, int16_t functionNumber) {
Expand Down Expand Up @@ -700,7 +677,7 @@ void DCC::updateLocoReminder(int loco, byte speedCode) {

// determine speed reg for this loco
int reg=lookupSpeedTable(loco);
if (reg>=0) {
if (reg>=0 && speedTable[reg].speedCode!=speedCode) {
speedTable[reg].speedCode = speedCode;
CommandDistributor::broadcastLoco(reg);
}
Expand Down
2 changes: 1 addition & 1 deletion DCC.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class DCC
static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue);
static void setFunction(int cab, byte fByte, byte eByte);
static void setFn(int cab, int16_t functionNumber, bool on);
static int changeFn(int cab, int16_t functionNumber, bool pressed);
static void changeFn(int cab, int16_t functionNumber);
static int getFn(int cab, int16_t functionNumber);
static uint32_t getFunctionMap(int cab);
static void updateGroupflags(byte &flags, int16_t functionNumber);
Expand Down
17 changes: 8 additions & 9 deletions WiThrottle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,29 @@ void WiThrottle::multithrottle(RingStream * stream, byte * cmd){
void WiThrottle::locoAction(RingStream * stream, byte* aval, char throttleChar, int cab){
// Note cab=-1 for all cabs in the consist called throttleChar.
// DIAG(F("Loco Action aval=%c%c throttleChar=%c, cab=%d"), aval[0],aval[1],throttleChar, cab);
(void) stream;
switch (aval[0]) {
case 'V': // Vspeed
{
int witSpeed=getInt(aval+1);
LOOPLOCOS(throttleChar, cab) {
mostRecentCab=myLocos[loco].cab;
DCC::setThrottle(myLocos[loco].cab, WiTToDCCSpeed(witSpeed), DCC::getThrottleDirection(myLocos[loco].cab));
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, witSpeed);
// SetThrottle will cause speed change broadcast
}
}
break;
case 'F': //F onOff function
{
bool funcstate;
case 'F': // Function key pressed/released
{
bool pressed=aval[1]=='1';
int fKey = getInt(aval+2);
if (fKey!=2 && !pressed) break; // ignore releases except key 2
LOOPLOCOS(throttleChar, cab) {
funcstate = DCC::changeFn(myLocos[loco].cab, fKey, pressed);
if(funcstate==0 || funcstate==1)
StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"), throttleChar, LorS(myLocos[loco].cab),
myLocos[loco].cab, funcstate, fKey);
}
if (fKey==2) DCC::setFn(myLocos[loco].cab,fKey, pressed);
else DCC::changeFn(myLocos[loco].cab, fKey);
}
break;
}
case 'q':
if (aval[1]=='V' || aval[1]=='R' ) { //qV or qR
// just flag the loco for broadcast and it will happen.
Expand Down
4 changes: 2 additions & 2 deletions WiThrottle.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class WiThrottle {
~WiThrottle();

static const int MAX_MY_LOCO=10; // maximum number of locos assigned to a single client
static const int HEARTBEAT_SECONDS=4; // heartbeat at 4secs to provide messaging transport
static const int ESTOP_SECONDS=8; // eStop if no incoming messages for more than 8secs
static const int HEARTBEAT_SECONDS=10; // heartbeat at 4secs to provide messaging transport
static const int ESTOP_SECONDS=20; // eStop if no incoming messages for more than 8secs
static WiThrottle* firstThrottle;
static int getInt(byte * cmd);
static int getLocoId(byte * cmd);
Expand Down

0 comments on commit 8209207

Please sign in to comment.