application: print check result on LCD

This commit is contained in:
King Kévin 2019-12-30 17:19:48 +01:00
parent a58df32707
commit 6b391af92a
1 changed files with 86 additions and 21 deletions

View File

@ -59,7 +59,7 @@
/** number of RTC ticks per second
* @note use integer divider of oscillator to keep second precision
*/
#define RTC_TICKS_SECOND 4
#define RTC_TICKS_SECOND 10
/** RTC time when device is started */
static time_t time_start = 0;
@ -70,8 +70,8 @@ static time_t time_start = 0;
volatile bool rtc_internal_tick_flag = false; /**< flag set when internal RTC ticked */
/** @} */
/** activity timeout before switching off (in seconds) */
#define SHUTDOWN_TIMEOUT 10
/** activity timeout before switching off (in RTC ticks) */
#define SHUTDOWN_TIMEOUT (60 * RTC_TICKS_SECOND)
// ====================
// = common functions =
@ -1308,8 +1308,8 @@ void main(void)
#endif
// setup RTC
// the USB cable tester does not have a dedicated external 32.678 kHz LSE oscillator
rtc_auto_awake(RCC_HSE, 8000000 / 128 / RTC_TICKS_SECOND - 1); // use High Speed External oscillator (8 MHz / 128) as RTC clock (VBAT can't be used to keep the RTC running)
rtc_auto_awake(RCC_HSE, 8000000 / 128 - 1); // use High Speed External oscillator (8 MHz / 128) as RTC clock (VBAT can't be used to keep the RTC running)
rtc_interrupt_enable(RTC_SEC); // enable RTC interrupt on "seconds"
nvic_enable_irq(NVIC_RTC_IRQ); // allow the RTC to interrupt
time_start = rtc_get_counter_val(); // get start time from internal RTC
@ -1362,6 +1362,9 @@ void main(void)
cable_clear(cable_next); // initialize rest of cable structure
bool cable_changed = false; // if the next cable is not the same as the current
uint16_t cable_message_i = 0; // the message index of the last cable message to be displayed
uint32_t cable_message_t = last_connect_time; // the time stamp of the last message update
while (true) { // infinite loop
iwdg_reset(); // kick the dog
if (user_input_available) { // user input is available
@ -1407,40 +1410,102 @@ void main(void)
cable_changed = true;
} else if (cable_changed) { // it's the same cable, and it has been confirmed a second time
cable_changed = false; // remember it's the same cable
last_connect_time = rtc_get_counter_val(); // update last connect time to restart the timeout
if (0 == cable_current->connections_nb) { // no cable plugged in
lcd_hd44780_clear_display(); // be sure the display is cleared
lcd_hd44780_write_line(false, lcd_default_line1, strlen(lcd_default_line1));
lcd_hd44780_write_line(true, lcd_default_line2, strlen(lcd_default_line2));
} else { // there is a new confirmed cable
cable_load(cable_next); // check if there is a load
cable_issues(cable_next); // get the exact issues
cable_load(cable_current); // check if there is a load
cable_issues(cable_current); // get the exact issues
lcd_hd44780_clear_display(); // clear display
if (cable_next->cable_best < LENGTH(usb_cables) && cable_next->cable_best < LENGTH(cable_next->unconnected_nb) && cable_next->cable_best < LENGTH(cable_next->unspecified_nb)) {
const struct usb_cable_t* usb_cable = &usb_cables[cable_next->cable_best];
if (cable_current->cable_best < LENGTH(usb_cables) && cable_current->cable_best < LENGTH(cable_current->unconnected_nb) && cable_current->cable_best < LENGTH(cable_current->unspecified_nb)) {
const struct usb_cable_t* usb_cable = &usb_cables[cable_current->cable_best];
lcd_hd44780_write_line(false, usb_cable->name, strlen(usb_cable->name));
uint16_t issues = cable_next->unconnected_nb[cable_next->cable_best] + cable_next->unspecified_nb[cable_next->cable_best];
uint16_t issues = cable_current->unconnected_nb[cable_current->cable_best] + cable_current->unspecified_nb[cable_current->cable_best];
if (0 == issues) {
lcd_hd44780_write_line(true, "perfect match", 13);
char* line2 = "perfect match";
lcd_hd44780_write_line(true, line2, strlen(line2));
} else {
char score_str[17];
uint8_t str_len = snprintf(score_str, LENGTH(score_str), "issues: %u", issues);
if (str_len) {
lcd_hd44780_write_line(true, score_str, str_len);
}
char* line2 = "closest match";
lcd_hd44780_write_line(true, line2, strlen(line2));
}
} else {
lcd_hd44780_write_line(false, "no matching", 11);
lcd_hd44780_write_line(true, "cable found", 11);
}
}
} else { // the cable did not change
// nothing to do, wait for cable change
} else if (cable_current->cable_best < LENGTH(usb_cables) && cable_current->cable_best < LENGTH(cable_current->unconnected_nb) && cable_current->cable_best < LENGTH(cable_current->unspecified_nb)) { // the cable did not change, and there is a valid cable plugged in
// fix time (RTC integer overflow is possible, but only happens after 13 years without reset)
if (cable_message_t < last_connect_time) {
cable_message_t = last_connect_time;
cable_message_i = 0;
}
if (rtc_get_counter_val() >= cable_message_t + RTC_TICKS_SECOND) { // at least a second passed since last message
cable_message_t = rtc_get_counter_val(); // remember message has been displayed
cable_message_i++; // we will display the next message
char line[17] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'}; // line to display
if (0 == cable_current->unconnected_nb[cable_current->cable_best] && 0 == cable_current->unspecified_nb[cable_current->cable_best]) { // it's a perfect match
if (0 == cable_message_i % 2) { // which of the two messages to display
snprintf(line, LENGTH(line), "perfect match");
} else {
if (cable_current->load) {
snprintf(line, LENGTH(line), "with load");
} else {
snprintf(line, LENGTH(line), "without load");
}
}
} else { // not a perfect match
if (0 == cable_message_i) {
snprintf(line, LENGTH(line), "closest match");
} else if (1 == cable_message_i) {
if (cable_current->load) {
snprintf(line, LENGTH(line), "with load");
} else {
snprintf(line, LENGTH(line), "without load");
}
} else if (2 == cable_message_i) {
uint16_t issues = cable_next->unconnected_nb[cable_next->cable_best] + cable_next->unspecified_nb[cable_next->cable_best];
snprintf(line, LENGTH(line), "issues: %u", issues);
} else if (3 == cable_message_i) {
snprintf(line, LENGTH(line), "unconnected: %u", cable_next->unconnected_nb[cable_next->cable_best]);
} else if (cable_message_i < 4U + cable_next->unconnected_nb[cable_next->cable_best]) {
uint16_t i = cable_message_i - 4U;
const struct usb_connector_t* connector_from = usb_cables_get_connector(cable_current->unconnected[i][0]);
const struct usb_connector_t* connector_to = usb_cables_get_connector(cable_current->unconnected[i][1]);
if (NULL != connector_from && NULL != connector_to) {
snprintf(line, LENGTH(line), "%s_%s %s_%s", connector_from->name, usb_pins[cable_current->unconnected[i][0]].name, connector_to->name, usb_pins[cable_current->unconnected[i][1]].name);
}
} else if (cable_message_i == 4U + cable_next->unconnected_nb[cable_next->cable_best]) {
snprintf(line, LENGTH(line), "unspecified: %u", cable_next->unspecified_nb[cable_next->cable_best]);
} else if (cable_message_i < 5U + cable_next->unconnected_nb[cable_next->cable_best] + cable_next->unspecified_nb[cable_next->cable_best]) {
uint16_t i = cable_message_i - 5U - cable_next->unconnected_nb[cable_next->cable_best];
const struct usb_connector_t* connector_from = usb_cables_get_connector(cable_current->unspecified[i][0]);
const struct usb_connector_t* connector_to = usb_cables_get_connector(cable_current->unspecified[i][1]);
if (NULL != connector_from && NULL != connector_to) {
snprintf(line, LENGTH(line), "%s_%s %s_%s", connector_from->name, usb_pins[cable_current->unspecified[i][0]].name, connector_to->name, usb_pins[cable_current->unspecified[i][1]].name);
}
} else { // end reached
snprintf(line, LENGTH(line), "closest match");
cable_message_i = 0; // restart
}
}
uint8_t len = strlen(line);
for (uint8_t i = len; i < LENGTH(line) - 2; i++) {
line[i] = ' '; // put space at end of line
}
line[LENGTH(line) - 1] = '\0'; // end string
lcd_hd44780_write_line(true, line, strlen(line)); // write message
}
}
}
// next cable because the current one (reuse allocated current for the next)
struct cable_t* cable_tmp = cable_current;
cable_current = cable_next;
cable_next = cable_tmp;
if (cable_changed) {
// next cable because the current one (reuse allocated current for the next)
struct cable_t* cable_tmp = cable_current;
cable_current = cable_next;
cable_next = cable_tmp;
}
} // !interactive
while (!interactive && rtc_get_counter_val() >= last_connect_time + SHUTDOWN_TIMEOUT) { // time to shut down
#if !DEBUG