Skip to content

Commit

Permalink
LocoTable addition (but no other changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
habazut committed Aug 15, 2024
1 parent 1f4b302 commit 7a67bee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
#define SHOWCPUSTATS false
#define SHOWCOMMAND true
#define SHOWBINARY false
#define SHOWTABLE false

#if defined(ESP32) || defined(ESP8266) || defined(ESP_PLATFORM)
#define INTERRUPT_SAFE IRAM_ATTR
Expand Down
15 changes: 12 additions & 3 deletions DCCInspector-EX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ bool showBitLengths = SHOWBITLENGTHS;
bool showCpuStats = SHOWCPUSTATS;
bool showCommand = SHOWCOMMAND;
bool showBinary = SHOWBINARY;
bool showTable = SHOWTABLE;

byte inputPacket = 0; // Index of next packet to be analysed in dccPacket array
byte pktByteCount = 0;
Expand Down Expand Up @@ -317,7 +318,9 @@ void loop() {
DCCStatistics.writeFullStatistics(stats, showCpuStats, showBitLengths);
Serial.println("--");
}

if (showTable)
LocoTable::dumpTable(&Serial);

// Output short version of DCC statistics to a buffer
// for use by OLED
#if defined(USE_OLED)
Expand Down Expand Up @@ -866,8 +869,7 @@ void DecodePacket(Print &output, int inputPacket, bool newPeriod) {
sprintf(str, BYTE_TO_BINARY_PATTERN5, BYTE_TO_BINARY5(instrByte1));
sbTemp.print(str);
}
locoInfoChanged = LocoTable::updateFunc(decoderAddress, instrByte1, 0)
|| LocoTable::updateFunc(decoderAddress, instrByte1, 1);
locoInfoChanged = LocoTable::updateFunc(decoderAddress, instrByte1, 1);
if (showCommand && locoInfoChanged)
sprintf(commandBuffer, "<f %d %d>", decoderAddress, 128 + (instrByte1 & 0B11111));
break;
Expand Down Expand Up @@ -1141,6 +1143,12 @@ bool processCommands() {
Serial.print(F("show Cpu stats = "));
Serial.println(showCpuStats);
break;
case 't':
case 'T':
showTable = !showTable;
Serial.print(F("show table = "));
Serial.println(showTable);
break;
case 'i':
#if defined(USE_HTTPSERVER)
case 'I':
Expand All @@ -1165,6 +1173,7 @@ bool processCommands() {
Serial.println(F("h = show heartbeat toggle"));
Serial.println(F("l = show locomotive packets toggle"));
Serial.println(F("p = show binary Pattern"));
Serial.println(F("t = show table"));
Serial.println(
F("s = set NMRA compliance strictness "
"(0=none,1=decoder,2=controller)"));
Expand Down
33 changes: 27 additions & 6 deletions LocoTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
#include "LocoTable.h"

LocoTable::LOCO LocoTable::speedTable[MAX_LOCOS];
LocoTable::LOCO LocoTable::speedTable[MAX_LOCOS] = { {0,0,0,0,0,0} };
int LocoTable::highestUsedReg = 0;

int LocoTable::lookupSpeedTable(int locoId, bool autoCreate) {
Expand Down Expand Up @@ -65,6 +65,7 @@ bool LocoTable::updateLoco(int loco, byte speedCode) {
// determine speed reg for this loco
int reg=lookupSpeedTable(loco, false);
if (reg>=0) {
speedTable[reg].speedcounter++;
if (speedTable[reg].speedCode!=speedCode) {
speedTable[reg].speedCode = speedCode;
return true;
Expand All @@ -91,14 +92,15 @@ bool LocoTable::updateFunc(int loco, byte func, int shift) {
} else {
newfunc = previous = speedTable[reg].functions;
}

if(shift == 0) { // special case for light

speedTable[reg].funccounter++;

if(shift == 1) { // special case for light
newfunc &= ~1UL;
newfunc |= ((func & 0B10000) >> 4);
} else {
newfunc &= ~(0B1111UL << shift);
newfunc |= ((func & 0B1111) << shift);
}
newfunc &= ~(0B1111UL << shift);
newfunc |= ((func & 0B1111) << shift);

if (newfunc != previous) {
speedTable[reg].functions = newfunc;
Expand All @@ -107,3 +109,22 @@ bool LocoTable::updateFunc(int loco, byte func, int shift) {
return retval;
}

void LocoTable::dumpTable(Stream *output) {
output->print("\n-----------Table---------\n");
for (byte reg = 0; reg <= highestUsedReg; reg++) {
if (speedTable[reg].loco != 0) {
output->print(speedTable[reg].loco);
output->print(' ');
output->print(speedTable[reg].speedCode);
output->print(' ');
output->print(speedTable[reg].functions);
output->print(" #funcpacks:");
output->print(speedTable[reg].funccounter);
output->print(" #speedpacks:");
output->print(speedTable[reg].speedcounter);
speedTable[reg].funccounter = 0;
speedTable[reg].speedcounter = 0;
output->print('\n');
}
}
}
4 changes: 4 additions & 0 deletions LocoTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "StringBuilder.h"

#define MAX_LOCOS 128

Expand All @@ -27,6 +28,7 @@ class LocoTable {
static int lookupSpeedTable(int locoId, bool autoCreate);
static bool updateLoco(int loco, byte speedCode);
static bool updateFunc(int loco, byte func, int shift);
static void dumpTable(Stream *output);

private:
struct LOCO
Expand All @@ -35,6 +37,8 @@ class LocoTable {
byte speedCode;
byte groupFlags;
unsigned long functions;
unsigned int funccounter;
unsigned int speedcounter;
};
static LOCO speedTable[MAX_LOCOS];
static int highestUsedReg;
Expand Down

0 comments on commit 7a67bee

Please sign in to comment.