Skip to content

Commit

Permalink
Feat: support new specs in display
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianim committed Mar 24, 2023
1 parent 2d30297 commit e582b04
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
8 changes: 8 additions & 0 deletions display/include/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ namespace timer {
struct TimerSegment {
bool valid;
unsigned long time;
unsigned long count_to;
bool sound;
uint32_t color;
};

enum class PreStartBehaviour { SHOW_ZERO, RUN_NORMALLY };

struct DisplayOptions {
PreStartBehaviour pre_start_behaviour;
};

struct TimerData {
bool valid;
bool repeat;
TIME start_at;
TIME stop_at;
DisplayOptions display_options;
TimerSegment segments[10];
};

Expand Down
3 changes: 3 additions & 0 deletions display/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ framework = arduino
monitor_speed = 115200
monitor_filters = esp8266_exception_decoder
build_type = debug
upload_port = 10.37.91.251/u
extra_scripts = upload_script.py
upload_protocol = custom
lib_deps =
bblanchon/ArduinoJson@^6.18.2
adafruit/Adafruit GFX Library@^1.10.10
Expand Down
76 changes: 46 additions & 30 deletions display/src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void _handleNewOffset(TIME offset) {
}

_timeOffset = sum / validOffsets;
Serial.printf("New offset: %llu\n", _timeOffset);
}

uint32_t _parseHexColor(const char *c) {
Expand All @@ -69,38 +68,57 @@ uint32_t _parseHexColor(const char *c) {
return color;
}

void _handleMessage(JsonDocument &doc) {
String type = doc["type"];
if (type == "Timer") {
_resetTimerData();
void _loadDisplayOptions(JsonObject &data) {
String pre_start_behaviour = data["pre_start_behaviour"];
if (pre_start_behaviour == "ShowZero") {
_timerData.display_options.pre_start_behaviour =
timer::PreStartBehaviour::SHOW_ZERO;
} else if (pre_start_behaviour == "RunNormally") {
_timerData.display_options.pre_start_behaviour =
timer::PreStartBehaviour::RUN_NORMALLY;
}
}

JsonObject data = doc["data"];
if (data["segments"].size() > 10) {
return;
}
_timerData.valid = true;
_timerData.repeat = data["repeat"];
_timerData.start_at = data["start_at"].as<TIME>();
void _loadTimerData(JsonObject &data) {
_resetTimerData();

if (data["stop_at"].isNull()) {
_timerData.stop_at = 0;
if (data["segments"].size() > 10) {
return;
}
_timerData.valid = true;
_timerData.repeat = data["repeat"];
_timerData.start_at = data["start_at"].as<TIME>();

if (data["stop_at"].isNull()) {
_timerData.stop_at = 0;
} else {
_timerData.stop_at = data["stop_at"].as<TIME>();
}

JsonObject display_options = data["display_options"];
_loadDisplayOptions(display_options);

JsonArray segments = data["segments"];
for (size_t i = 0; i < segments.size() && i < 10; i++) {
JsonObject segment = segments[i];
_timerData.segments[i].valid = true;
_timerData.segments[i].time = segment["time"].as<TIME>();
_timerData.segments[i].count_to = segment["count_to"].as<TIME>();
_timerData.segments[i].sound = segment["sound"];

if (segment["color"].isNull()) {
_timerData.segments[i].color = 0xffffff;
} else {
_timerData.stop_at = data["stop_at"].as<TIME>();
_timerData.segments[i].color = _parseHexColor(segment["color"]);
}
}
}

JsonArray segments = data["segments"];
for (size_t i = 0; i < segments.size() && i < 10; i++) {
JsonObject segment = segments[i];
_timerData.segments[i].valid = true;
_timerData.segments[i].time = segment["time"].as<TIME>() + 1000;
_timerData.segments[i].sound = segment["sound"];

if (segment["color"].isNull()) {
_timerData.segments[i].color = 0xffffff;
} else {
_timerData.segments[i].color = _parseHexColor(segment["color"]);
}
}
void _handleMessage(JsonDocument &doc) {
String type = doc["type"];
if (type == "Timer") {
JsonObject data = doc["data"];
_loadTimerData(data);
} else if (type == "Timestamp") {
unsigned long now = millis();
unsigned long getTimeRoundtrip = now - _lastGetTimeSent;
Expand Down Expand Up @@ -132,8 +150,6 @@ void _webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
break;
}
case WStype_TEXT: {
Serial.printf("[WSc] get text: %s\n", payload);

_doc.clear();
DeserializationError error = deserializeJson(_doc, payload);
if (error) {
Expand Down
6 changes: 5 additions & 1 deletion display/src/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ ActiveSegment calculateCurrentSegment(TimerData timerData, TIME timeOffset) {
}

TIME currentTime = (TIME)millis() + timeOffset;
if (currentTime < timerData.start_at) {
if (currentTime < timerData.start_at &&
timerData.display_options.pre_start_behaviour ==
PreStartBehaviour::SHOW_ZERO) {
return {0, 0xfff};
}

Expand Down Expand Up @@ -39,6 +41,8 @@ ActiveSegment calculateCurrentSegment(TimerData timerData, TIME timeOffset) {
currentSegmentIndex++;
}

timeInCurrentSegment += timerData.segments[currentSegmentIndex - 1].count_to;

return {timeInCurrentSegment / 1000,
timerData.segments[currentSegmentIndex - 1].color};
}
Expand Down
2 changes: 2 additions & 0 deletions display/upload_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Import("env")
env.Replace(UPLOADCMD="curl -v -F image=@$SOURCE $UPLOAD_PORT")

0 comments on commit e582b04

Please sign in to comment.