Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit bbf68f6

Browse files
committed
Refactoring
1 parent fac2b8b commit bbf68f6

File tree

7 files changed

+38
-62
lines changed

7 files changed

+38
-62
lines changed

display_7segment.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,21 @@ void Rocket7SegmentDisplay::loop() {
4343
write(data);
4444

4545
} else {
46+
launch_type *launch = settings.getLaunch();
47+
if (launch == NULL) {
48+
write("LOADING ");
49+
return;
50+
}
4651

52+
int32_t launch_time = atol(launch->launch_time);
53+
54+
int32_t time = launch_time - settings.time_downloaded - (millis() - httpClient.info_downloaded_millis) / 1000;
55+
4756
if ((millis() - selected_launch_changed_millis) < 2000) {
48-
write(data);
57+
write(launch->rocket);
4958
return;
5059
}
5160

52-
int32_t time = launch_at_seconds - settings.time_downloaded - (millis() - httpClient.info_downloaded_millis) / 1000;
5361
boolean negative = false;
5462
if (time < 0) {
5563
negative = true;
@@ -62,11 +70,11 @@ void Rocket7SegmentDisplay::loop() {
6270
uint32_t days = (time / 60 / 60 / 24);
6371

6472
if (seconds == 59) {
65-
write(data);
73+
write(launch->rocket);
6674
return;
6775
}
6876

69-
if (settings.launch.time_status == 'T') {
77+
if (launch->time_status == 'T') {
7078
ledControl.setDigit(0, 0, seconds % 10, false);
7179
ledControl.setDigit(0, 1, seconds / 10, false);
7280

@@ -118,11 +126,8 @@ void Rocket7SegmentDisplay::setMessage(const __FlashStringHelper *message8Chars)
118126
loop();
119127
}
120128

121-
void Rocket7SegmentDisplay::setLaunch(int32_t launch_at_seconds, char* name8Chars) {
129+
void Rocket7SegmentDisplay::setLaunch() {
122130
show_launch = true;
123-
124-
this->launch_at_seconds = launch_at_seconds;
125-
memcpy(data, name8Chars, 8);
126131
}
127132

128133
void Rocket7SegmentDisplay::write(char* message8Chars) {

displays.cpp

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,59 +33,40 @@ void Displays::loop() {
3333
rocket7SegmentDisplay.loop();
3434
#endif
3535

36-
int selected_current = -1;
36+
int show_launch_id = -1;
3737

3838
if (settings.selected_launch_id == SELECTED_IP) {
3939

4040
} else if (settings.selected_launch_id == SELECTED_NEXT) {
4141

42-
selected_current = settings.launches[0].launch_id;
42+
show_launch_id = settings.launches[0].launch_id;
4343

4444
} else if (settings.selected_launch_id == SELECTED_CYCLE) {
4545

46-
selected_current = settings.launches[(millis() / SELECTED_CYCLE_DELAY_MILLIS) % settings.launch_count].launch_id;
46+
show_launch_id = settings.launches[(millis() / SELECTED_CYCLE_DELAY_MILLIS) % settings.launch_count].launch_id;
4747

4848
} else {
4949

50-
selected_current = settings.selected_launch_id;
50+
show_launch_id = settings.selected_launch_id;
5151

5252
}
5353

54-
if (settings.selected_launch != selected_current) {
55-
settings.selected_launch = selected_current;
56-
54+
if (settings.show_launch_id != show_launch_id) {
5755
selected_launch_changed_millis = millis();
5856

59-
settings.loadLaunch(settings.selected_launch);
57+
settings.setLaunch(show_launch_id);
6058

6159
}
6260

63-
int32_t launch_time = atol(settings.launch.launch_time);
64-
65-
int32_t seconds_left = launch_time - settings.time_downloaded - (millis() - httpClient.info_downloaded_millis) / 1000;
66-
6761
if (settings.selected_launch_id == SELECTED_CYCLE && (millis() - button_menu_millis) < MENU_BUTTON_SHOW_MENU_MILLIS) {
6862
setMessage(F("CYCLE "));
69-
//show_seconds_left_digit_display(seconds_left, false);
7063

7164
} else if (settings.selected_launch_id == SELECTED_NEXT && (millis() - button_menu_millis) < MENU_BUTTON_SHOW_MENU_MILLIS) {
7265
setMessage(F("UPCOMING "));
73-
//show_seconds_left_digit_display(seconds_left, false);
74-
75-
} else if (
76-
settings.launch.launch_time == 0
77-
|| seconds_left % 60 == 59
78-
|| (millis() - selected_launch_changed_millis) < MENU_BUTTON_SHOW_NAME_MILLIS) {
79-
//write(settings.launch.rocket);
80-
81-
//show_seconds_left_digit_display(seconds_left, false);
8266

83-
rocket7SegmentDisplay.setLaunch(launch_time, settings.launch.rocket);
8467
} else {
85-
86-
//show_seconds_left_digit_display(seconds_left, true);
87-
88-
rocket7SegmentDisplay.setLaunch(launch_time, settings.launch.rocket);
68+
69+
rocket7SegmentDisplay.setLaunch();
8970
}
9071

9172
//show_ip_digit_display();
@@ -108,14 +89,16 @@ void show_seconds_left_digit_display(int32_t time, boolean onDigitDisplay, char*
10889

10990
//char buf[16 + 1] = {0};
11091

92+
/*
11193
if (settings.launch.time_status == TIME_STATUS_TIME) {
11294
sprintf(buf, "%2d days %02d:%02d:%02d", days, hours, minutes, seconds);
11395
} else if (settings.launch.time_status == TIME_STATUS_DAY) {
11496
sprintf(buf, "%2d days", days);
11597
} else if (settings.launch.time_status == TIME_STATUS_MONTH) {
11698
sprintf(buf, "%2d days", days); //TODO months
11799
}
118-
100+
*/
101+
119102
}
120103

121104
void Displays::setMessage(const __FlashStringHelper *string) {

displays.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ class Rocket7SegmentDisplay {
1414
void loop();
1515
void setMessage(char* message8Chars);
1616
void setMessage(const __FlashStringHelper *message8Chars);
17-
void setLaunch(int32_t launch_at_seconds, char* name8Chars);
17+
void setLaunch();
1818
private:
1919
LedControl ledControl;
2020
void write(char* message8Chars);
2121

2222
char data[8];
23-
int32_t launch_at_seconds;
2423
bool show_launch;
2524

2625
};

http.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static void http_client_got_response (uint8_t status, uint16_t off, uint16_t len
4444
//memcpy(settings.launches + offset, response + 1, len);
4545
httpClient.info_downloaded_millis = millis();
4646

47-
settings.loadLaunch(settings.selected_launch_id);
47+
settings.setLaunch(settings.selected_launch_id);
4848
//Serial.println(settings.launch.rocket);
4949

5050
httpClient.next_try_millis = millis() + SUCCESS_REQUEST_RATE;

launchtime.ino

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ void setup () {
3939
settings.loadFromEEPROM();
4040
}
4141

42-
Serial.println("--");
43-
Serial.println(settings.selected_launch_id);
44-
Serial.println("--");
45-
4642
if (digitalRead(PIN_BUTTON_MENU) == LOW) {
4743
demo_mode = true;
4844
}
@@ -110,10 +106,11 @@ void process_buttons() {
110106
} else {
111107

112108
int index = settings.getIndex(settings.selected_launch_id, settings.launch_count);
109+
index++;
113110
if (index >= settings.launch_count) {
114111
settings.selected_launch_id = SELECTED_IP;
115112
} else {
116-
settings.selected_launch_id = settings.launches[index + 1].launch_id;
113+
settings.selected_launch_id = settings.launches[index].launch_id;
117114
}
118115

119116
}

settings.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ int Settings::getIndex(int launch_id, int return_if_not_found) {
8686

8787
}
8888

89-
void Settings::loadLaunch(int launch_id) {
90-
89+
launch_type *Settings::getLaunch() {
9190
for(int i = 0; i < launch_count; i++) {
92-
if (launches[i].launch_id == launch_id) {
93-
launch = launches[i];
94-
break;
91+
if (launches[i].launch_id == show_launch_id) {
92+
return &launches[i];
9593
}
9694
}
97-
95+
return NULL;
9896
}
9997

98+
void Settings::setLaunch(int launch_id) {
99+
show_launch_id = launch_id;
100+
}
100101

settings.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ const char TIME_STATUS_MONTH = 'M';
1414
const char TIME_STATUS_DAY = 'D';
1515
const char TIME_STATUS_TIME = 'T';
1616

17-
18-
19-
/*
20-
struct launch_type {
21-
int32_t seconds_left;
22-
char name[9];
23-
};
24-
*/
25-
2617
struct launch_type {
2718
char launch_id[6 + 1];
2819
char launch_status;
@@ -50,12 +41,12 @@ class Settings {
5041
//Things not to save
5142
uint8_t launch_count = 1;
5243

53-
launch_type launch;
5444
int getIndex(int launch_id, int return_if_not_found);
55-
void loadLaunch(int index);
45+
void setLaunch(int launch_id);
46+
launch_type *getLaunch();
5647
void processApiResponse(int index, uint8_t data);
5748

58-
uint32_t selected_launch = 0; //selected launch index
49+
int32_t show_launch_id = -1;
5950

6051
uint32_t time_downloaded;
6152

0 commit comments

Comments
 (0)