diff --git a/application.c b/application.c index 8c954f5..372ccd5 100644 --- a/application.c +++ b/application.c @@ -1385,8 +1385,18 @@ void main(void) rtc_internal_tick_flag = false; // reset flag action = true; // action has been performed if (!interactive) { // periodically check cable when not in interactive mode - - // start cable detection + // check if there is a cable by testing the ground connection + bool ground_connected = usb_cables_test_ground(usb_connectors, LENGTH(usb_connectors), NULL); + if (!ground_connected) { // there is no cable + if (cable_current->connections_nb > 0) { // there was a cable before + 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)); + cable_clear(cable_current); // clear definition + } + goto test_end; + } + // there is a cable, start cable detection cable_clear(cable_next); // clear definition cable_detect(cable_next); // detect connected connectors // if there is a cable, we need to identify it further @@ -1398,106 +1408,106 @@ void main(void) // compare next to current cable if (cable_current->connections_nb != cable_next->connections_nb) { cable_changed = true; // note it changed, but don't do anything until change is confirmed a second time - } else { // same number of connections - // check if they are all the same connections (the search order is the same) - bool match = true; - for (uint16_t i = 0; i < cable_current->connections_nb && i < cable_next->connections_nb && match; i++) { - if (cable_current->connections[i][0] != cable_next->connections[i][0] || cable_current->connections[i][1] != cable_next->connections[i][1]) { - match = false; + goto test_end; + } + // it's a cable with the same number of connections + // check if they are all the same connections (the search order is the same) + bool match = true; + for (uint16_t i = 0; i < cable_current->connections_nb && i < cable_next->connections_nb && match; i++) { + if (cable_current->connections[i][0] != cable_next->connections[i][0] || cable_current->connections[i][1] != cable_next->connections[i][1]) { + match = false; + } + } + if (!match) { // not the same connections + 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_current); // check if there is a load + cable_issues(cable_current); // get the exact issues + lcd_hd44780_clear_display(); // clear display + 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_current->unconnected_nb[cable_current->cable_best] + cable_current->unspecified_nb[cable_current->cable_best]; + if (0 == issues) { + char* line2 = "perfect match"; + lcd_hd44780_write_line(true, line2, strlen(line2)); + } else { + 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); } } - if (!match) { // not the same connections - 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_current); // check if there is a load - cable_issues(cable_current); // get the exact issues - lcd_hd44780_clear_display(); // clear display - 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_current->unconnected_nb[cable_current->cable_best] + cable_current->unspecified_nb[cable_current->cable_best]; - if (0 == issues) { - char* line2 = "perfect match"; - lcd_hd44780_write_line(true, line2, strlen(line2)); - } else { - char* line2 = "closest match"; - lcd_hd44780_write_line(true, line2, strlen(line2)); - } + } 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 { - lcd_hd44780_write_line(false, "no matching", 11); - lcd_hd44780_write_line(true, "cable found", 11); - } - } - } 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"); + if (cable_current->load) { + snprintf(line, LENGTH(line), "with load"); } 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->shortname, usb_pins[cable_current->unconnected[i][0]].name, connector_to->shortname, 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->shortname, usb_pins[cable_current->unspecified[i][0]].name, connector_to->shortname, usb_pins[cable_current->unspecified[i][1]].name); - } - } else { // end reached - snprintf(line, LENGTH(line), "closest match"); - cable_message_i = 0; // restart + snprintf(line, LENGTH(line), "without load"); } } - uint8_t len = strlen(line); - for (uint8_t i = len; i < LENGTH(line) - 2; i++) { - line[i] = ' '; // put space at end of line + } 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->shortname, usb_pins[cable_current->unconnected[i][0]].name, connector_to->shortname, 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->shortname, usb_pins[cable_current->unspecified[i][0]].name, connector_to->shortname, usb_pins[cable_current->unspecified[i][1]].name); + } + } else { // end reached + snprintf(line, LENGTH(line), "closest match"); + cable_message_i = 0; // restart } - line[LENGTH(line) - 1] = '\0'; // end string - lcd_hd44780_write_line(true, line, strlen(line)); // write message } + 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 } } if (cable_changed) { @@ -1506,6 +1516,7 @@ void main(void) cable_current = cable_next; cable_next = cable_tmp; } +test_end:; } // !interactive while (!interactive && rtc_get_counter_val() >= last_connect_time + SHUTDOWN_TIMEOUT) { // time to shut down #if !DEBUG