application: improve cable find action

This commit is contained in:
King Kévin 2019-12-09 20:26:50 +01:00
parent 8b8dfa5141
commit 20e02c91ad
1 changed files with 55 additions and 13 deletions

View File

@ -174,7 +174,7 @@ static void command_cables(void* argument)
for (uint8_t cable = 0; cable < LENGTH(usb_cables); cable++) { // test every cable
uint8_t pair_defined, pair_undefined, pair_disconnected;
bool result = usb_cables_check_cable(&usb_cables[cable], &pair_defined, &pair_undefined, &pair_disconnected);
printf("%s: %s (defined=%u, undefined=%u, disconnected=%u:)\n", result ? "OK" : "KO", usb_cables[cable].name, pair_defined, pair_undefined, pair_disconnected);
printf("%s: %s (defined=%u, undefined=%u, disconnected=%u)\n", result ? "OK" : "KO", usb_cables[cable].name, pair_defined, pair_undefined, pair_disconnected);
}
usb_pins_float(); // put all pins back in safe floating state
@ -192,7 +192,8 @@ static void command_find(void* argument)
// figure out which connectors are used
bool connected[LENGTH(usb_connectors)];
usb_cables_check_inter(usb_connectors, LENGTH(usb_connectors), connected);
//usb_cables_check_inter(usb_connectors, LENGTH(usb_connectors), connected);
usb_cables_check_ground(usb_connectors, LENGTH(usb_connectors), connected);
uint8_t connected_nb = 0;
printf("connectors:\n");
for (uint8_t i = 0; i < LENGTH(connected); i++) {
@ -202,10 +203,11 @@ static void command_find(void* argument)
}
}
// find matching cable
uint8_t matches = 0; // number of matching cables
printf("matching cables:\n");
// find cable with matching connector set
uint8_t matches = 0; // number of cables matching the connector set
bool cable_connectors[LENGTH(usb_cables)]; // which cable matches the connector set
for (uint8_t cable = 0; cable < LENGTH(usb_cables); cable++) {
cable_connectors[cable] = false; // start with not matching, and test if it matches
// ensure we have the same number of connections as the cable
if (usb_cables[cable].connectors_nb != connected_nb) {
continue;
@ -241,18 +243,58 @@ static void command_find(void* argument)
match = false;
}
}
if (!match) {
continue;
}
// the connector match
uint8_t pair_defined, pair_undefined, pair_disconnected;
match = usb_cables_check_cable(&usb_cables[cable], &pair_defined, &pair_undefined, &pair_disconnected);
cable_connectors[cable] = match;
if (match) {
matches++;
printf("- %s\n", usb_cables[cable].name);
}
}
printf("%u matching cable(s) found\n", matches++);
// test how well the pins of the cables with matching connectors match
printf("found %u cable(s) with matching connectors%s\n", matches, matches > 0 ? ":" : "");
if (0 == matches) {
return;
}
matches = 0; // number of matching cables
uint8_t cable_score[LENGTH(usb_cables)]; // how close the cable matches (0 = perfect match)
for (uint8_t cable = 0; cable < LENGTH(usb_cables); cable++) {
cable_score[cable] = 0xff; // initialise with worst score
if (!cable_connectors[cable]) { // skip if the cable connectors do not match
continue;
}
// match cable
uint8_t pair_defined, pair_undefined, pair_disconnected;
bool match = usb_cables_check_cable(&usb_cables[cable], &pair_defined, &pair_undefined, &pair_disconnected);
printf("- %s: %s (defined=%u, undefined=%u, disconnected=%u)\n", match ? "OK" : "KO", usb_cables[cable].name, pair_defined, pair_undefined, pair_disconnected);
cable_score[LENGTH(usb_cables)] = usb_cables[cable].pin_pairs_nb - pair_defined + pair_undefined + pair_disconnected; // calculate score
if (match) {
matches++;
}
}
printf("%u matching cable(s) found\n", matches);
// find best matching cable
if (matches > 0) { // we already found a perfect matching cable
return;
}
uint8_t best_score = 0xff;
for (uint8_t cable = 0; cable < LENGTH(usb_cables); cable++) {
if (!cable_connectors[cable]) { // skip if the cable connectors do not match
continue;
}
if (cable_score[cable] < best_score) {
best_score = cable_score[cable];
}
}
printf("closest matching cable(s):\n");
for (uint8_t cable = 0; cable < LENGTH(usb_cables); cable++) {
if (!cable_connectors[cable]) { // skip if the cable connectors do not match
continue;
}
if (cable_score[cable] == best_score) {
printf("- %s\n", usb_cables[cable].name);
}
}
usb_pins_float(); // put all pins back in safe floating state
}