Skip to content

Commit

Permalink
Add special train feature to track cherry blossom and other wrapped t…
Browse files Browse the repository at this point in the history
…rains
  • Loading branch information
LArkema committed Mar 23, 2024
1 parent 3809c4c commit 3ed5580
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 7 deletions.
34 changes: 34 additions & 0 deletions DCTransistor-Bidirectional/DCTransistor-Bidirectional.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ void setup() {
train_pos_filter["TrainPositions"][0]["CircuitId"] = true;
train_pos_filter["TrainPositions"][0]["DirectionNum"] = true;
train_pos_filter["TrainPositions"][0]["LineCode"] = true;

if(SPECIAL_TRAIN){
train_pos_filter["TrainPositions"][0]["TrainId"] = true;
}

//Leave setup and turn Web led yellow
#ifdef PRINT
Expand Down Expand Up @@ -166,6 +170,11 @@ void loop() {
DynamicJsonDocument doc(json_size);
DeserializationError error = deserializeJson(doc, https.getStream(), DeserializationOption::Filter(train_pos_filter));

//Constants to track the presence of special trains
uint8_t special_train_index = 0;
uint8_t special_train_dir = 0;
TrainLine* special_train_line = NULL;

//Check that JSON Deserialization didn't fail. If so, print errors and return.
if (error) {
strip.setPixelColor(WEB_LED, RD_HEX_COLOR);
Expand Down Expand Up @@ -261,10 +270,16 @@ void loop() {
const uint16_t circID = train["CircuitId"].as<unsigned int>();
const uint8_t train_dir = train["DirectionNum"].as<unsigned short>();
const char* train_line = train["LineCode"];
uint8_t trainID = 0;

if (SPECIAL_TRAIN){
trainID = train["TrainId"].as<unsigned int>();
}

const char line_char = train_line[0]; //get first character of line, as switch statements work on chars but not strings.

int res = 0; //store result of setting each train
TrainLine* cur_train_line = NULL; //store which TrainLine object has current line

#ifdef PRINT
Serial.printf("Line: %s, Direction: %d, Circuit: %d, ", train_line, train_dir, circID); //continued after station determined
Expand All @@ -275,33 +290,47 @@ void loop() {
{
case 'R':
res = redline->setTrainState(circID, train_dir-1);
cur_train_line = redline;
countr++;
break;
case 'B':
res = blueline->setTrainState(circID, train_dir-1);
cur_train_line=blueline;
countb++;
break;
case 'O':
res = orangeline->setTrainState(circID, train_dir-1);
cur_train_line=orangeline;
counto++;
break;
case 'S':
res = silverline->setTrainState(circID, train_dir-1);
cur_train_line=silverline;
counts++;
break;
case 'Y':
res = yellowline->setTrainState(circID, train_dir-1);
cur_train_line=yellowline;
county++;
break;
case 'G':
res = greenline->setTrainState(circID, train_dir-1);
cur_train_line=greenline;
countg++;
break;
default:
res = -1;
break;
}//end switch statement

if (SPECIAL_TRAIN == true){
if(SPECIAL_TRAIN_ID == trainID){
special_train_index = res;
special_train_line = cur_train_line;
special_train_dir = train_dir-1;
}
}

#ifdef PRINT
Serial.printf("Station Index: %d\n", res); //Finish debugging / output info
#endif
Expand Down Expand Up @@ -354,6 +383,11 @@ void loop() {

}//end loop through each LED

if(SPECIAL_TRAIN){
uint8_t special_led = special_train_line->getLEDForIndex(special_train_index, special_train_dir);
strip.setPixelColor(special_led, SPECIAL_TRAIN_HEX);
}

//Update the board with new state of the system
strip.show();

Expand Down
6 changes: 6 additions & 0 deletions DCTransistor-Bidirectional/TrainLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TrainLine {
uint16_t getOppCID(bool dir);
uint16_t getLastCID(bool dir);
int16_t getStationCircuit(uint8_t index, bool dir); //get circuitID of any given station
uint8_t getLEDForIndex(uint8_t index, uint8_t train_dir);

};//END TrainLine definitiong

Expand Down Expand Up @@ -435,6 +436,11 @@ uint32_t TrainLine::getLEDColor(){
return led_color;
}

// Get the LED Number for an index on the station's line
uint8_t TrainLine::getLEDForIndex(uint8_t index, uint8_t train_dir){
return station_leds[train_dir][index];
}

//Keep led on for last station in line for 3 cycles after arriving, then turn off. Call after looping through API data.
void TrainLine::setEndLED(){

Expand Down
19 changes: 15 additions & 4 deletions DCTransistor-Bidirectional/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <ESP8266httpUpdate.h>

//Version string. Changes with every software version
#define VERSION "1.1.18"
#define VERSION "1.1.19"

/*
* USER CONFIGURATION VALUES
Expand All @@ -34,7 +34,13 @@
#define AUTOUPDATE true

//Uncomment below line to print program text output to Serial output (requires attaching board to computer via USB cable)
#define PRINT
//#define PRINT

//Number of seconds to wait between requests to WMATA server (WMATA updates every ~20, per documentation)
#define WAIT_SEC 15

//Uncomment below line to print program text output to Serial output (requires attaching board to computer via USB cable)
//#define PRINT

//Number of seconds to wait between requests to WMATA server (WMATA updates every ~20, per documentation)
#define WAIT_SEC 15
Expand All @@ -43,6 +49,11 @@
#define WIFI_NAME "DCTransistor"
#define WIFI_PASSWORD "trainsareneat"

//Setup LED for special trains
#define SPECIAL_TRAIN true
#define SPECIAL_TRAIN_ID 167
#define SPECIAL_TRAIN_HEX 0x00F17EB1

// ---- LED Configuration Values ----
#define LED_BRIGHTNESS 3 //Range of 0-100. Can get very bright very fast

Expand Down Expand Up @@ -70,8 +81,8 @@
*/

//Web server certificate SHA1 fingerprints for TLS connections. Updated daily by update-fingerprints action
#define GITHUB_COM_FINGERPRINT "E7 03 5B CC 1C 18 77 1F 79 2F 90 86 6B 6C 1D F8 DF AA BD C0"
#define RAW_GITHUBUSERCONTENT_COM_FINGERPRINT "97 D8 C5 70 0F 12 24 6C 88 BC FA 06 7E 8C A7 4D A8 62 67 28"
#define GITHUB_COM_FINGERPRINT "A3 B5 9E 5F E8 84 EE 1F 34 D9 8E EF 85 8E 3F B6 62 AC 10 4A"
#define RAW_GITHUBUSERCONTENT_COM_FINGERPRINT "A1 46 14 C7 2A 1D 52 79 F6 AA 2B B2 C5 0A 3B D3 F5 02 06 75"
#define API_WMATA_COM_FINGERPRINT "99 E2 96 23 71 DD 13 88 D0 5F 0B 72 2C FA 69 87 7A 8C 1F 40"

/*
Expand Down
34 changes: 32 additions & 2 deletions DCTransistor/DCTransistor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ void setup() {
train_pos_filter["TrainPositions"][0]["CircuitId"] = true;
train_pos_filter["TrainPositions"][0]["DirectionNum"] = true;
train_pos_filter["TrainPositions"][0]["LineCode"] = true;

if(SPECIAL_TRAIN){
train_pos_filter["TrainPositions"][0]["TrainId"] = true;
}

//Leave setup and turn Web led yellow
#ifdef PRINT
Expand Down Expand Up @@ -151,6 +155,10 @@ void loop() {
DynamicJsonDocument doc(json_size);
DeserializationError error = deserializeJson(doc, https.getStream(), DeserializationOption::Filter(train_pos_filter));

//Constants to track the presence of special trains
uint8_t special_train_index = 0;
TrainLine* special_train_line = NULL;

//Check that JSON Deserialization didn't fail. If so, print errors and return.
if (error) {
strip.setPixelColor(WEB_LED, RD_HEX_COLOR);
Expand Down Expand Up @@ -239,10 +247,16 @@ void loop() {
const uint16_t circID = train["CircuitId"].as<unsigned int>();
const uint8_t train_dir = train["DirectionNum"].as<unsigned short>();
const char* train_line = train["LineCode"];
uint8_t trainID = 0;

if (SPECIAL_TRAIN){
trainID = train["TrainId"].as<unsigned int>();
}

const char line_char = train_line[0]; //get first character of line, as switch statements work on chars but not strings.

int res = 0; //store result of setting each train
TrainLine* cur_train_line = NULL; //store which TrainLine object has current line

#ifdef PRINT
Serial.printf("Line: %s, Direction: %d, Circuit: %d, ", train_line, train_dir, circID); //continued after station determined
Expand All @@ -253,26 +267,32 @@ void loop() {
{
case 'R':
res = redline->setTrainState(circID, train_dir-1);
cur_train_line = redline;
countr++;
break;
case 'B':
res = blueline->setTrainState(circID, train_dir-1);
cur_train_line=blueline;
countb++;
break;
case 'O':
res = orangeline->setTrainState(circID, train_dir-1);
cur_train_line=orangeline;
counto++;
break;
case 'S':
res = silverline->setTrainState(circID, train_dir-1);
cur_train_line=silverline;
counts++;
break;
case 'Y':
res = yellowline->setTrainState(circID, train_dir-1);
cur_train_line=yellowline;
county++;
break;
case 'G':
res = greenline->setTrainState(circID, train_dir-1);
cur_train_line=greenline;
countg++;
break;
default:
Expand All @@ -284,13 +304,18 @@ void loop() {
Serial.printf("Station Index: %d\n", res); //Finish debugging / output info
#endif

if (SPECIAL_TRAIN == true){
if(SPECIAL_TRAIN_ID == trainID){
special_train_index = res;
special_train_line = cur_train_line;
}
}

if (res == -1){countfail++;}

}//end if train is on a line
} //end loop through active trains



#ifdef PRINT
Serial.printf("Red Count: %d\n", countr);
Serial.printf("Blue Count: %d\n", countb);
Expand Down Expand Up @@ -333,6 +358,11 @@ void loop() {

}//end loop through each LED

if(SPECIAL_TRAIN){
uint8_t special_led = special_train_line->getLEDForIndex(special_train_index);
strip.setPixelColor(special_led, SPECIAL_TRAIN_HEX);
}

//Update the board with new state of the system
strip.show();

Expand Down
5 changes: 5 additions & 0 deletions DCTransistor/TrainLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TrainLine {
uint16_t getOppCID(bool dir);
uint16_t getLastCID(bool dir);
int16_t getStationCircuit(uint8_t index, bool dir); //get circuitID of any given station
uint8_t getLEDForIndex(uint8_t index);

};//END TrainLine definitiong

Expand Down Expand Up @@ -415,6 +416,10 @@ uint32_t TrainLine::getLEDColor(){
return led_color;
}

uint8_t TrainLine::getLEDForIndex(uint8_t index){
return station_leds[index];
}

//Keep led on for last station in line for 3 cycles after arriving, then turn off. Call after looping through API data.
void TrainLine::setEndLED(){

Expand Down
7 changes: 6 additions & 1 deletion DCTransistor/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <ESP8266httpUpdate.h>

//Version string. Changes with every software version
#define VERSION "1.1.18"
#define VERSION "1.1.19"

/*
* USER CONFIGURATION VALUES
Expand All @@ -43,6 +43,11 @@
#define WIFI_NAME "DCTransistor"
#define WIFI_PASSWORD "trainsareneat"

//Setup LED for special trains
#define SPECIAL_TRAIN true
#define SPECIAL_TRAIN_ID 167
#define SPECIAL_TRAIN_HEX 0x00F17EB1

// ---- LED Configuration Values ----
#define LED_BRIGHTNESS 3 //Range of 0-100. Can get very bright very fast

Expand Down

0 comments on commit 3ed5580

Please sign in to comment.