Try using every scale found

This commit is contained in:
Eric Jiang 2022-04-16 15:30:21 -07:00
parent 1d3a7f5ea5
commit a26fd9eb55
2 changed files with 185 additions and 179 deletions

View File

@ -1,5 +1,5 @@
LDLIBS=-lm -lusb-1.0
CFLAGS=-Os -Wall
CFLAGS=-Os -Wall -Wextra
DOCCO=docco
usbscale: usbscale.c scales.h

View File

@ -25,6 +25,8 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdint.h>
@ -54,10 +56,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define CONTROL_REPORT_SIZE 2
//
// **find_scale** takes a libusb device list and finds the first USB device
// **is_scale** takes a libusb device list and finds the first USB device
// that matches a device listed in scales.h.
//
static libusb_device* find_scale(libusb_device**);
static bool is_scale(libusb_device*);
//
// **print_scale_data** takes the 6-byte output from the scale and interprets
// it, printing out the result to the screen. It also returns a 1 if the
@ -103,6 +105,7 @@ int main(int argc, char **argv)
ssize_t cnt;
libusb_device* dev;
libusb_device_handle* handle;
bool found_scale = false;
int weigh_count = WEIGH_COUNT -1;
@ -127,15 +130,17 @@ int main(int argc, char **argv)
return (int) cnt;
//
// Once we have the list, we use **find_scale** to loop through and match
// every device against the scales.h list. **find_scale** will return the
// Once we have the list, we use **is_scale** to loop through and match
// every device against the scales.h list. **is_scale** will return the
// first device that matches, or 0 if none of them matched.
//
dev = find_scale(devs);
if(dev == 0) {
fprintf(stderr, "No USB scale found on this computer.\n");
return -1;
int i = 0;
while ((dev = devs[i++]) != NULL) {
if (!is_scale(dev)) {
continue;
}
found_scale = true;
//
// Once we have a pointer to the USB scale in question, we open it.
@ -274,8 +279,19 @@ int main(int argc, char **argv)
// The return code will be 0 for success or -1 for errors (see
// `libusb_init` above if it's neither 0 nor -1).
//
if (scale_result != 0) {
return scale_result;
}
}
//
// if no scales were found, make sure to show a message
//
if(!found_scale) {
fprintf(stderr, "No USB scale found on this computer.\n");
return -1;
}
return 0;
}
//
// print_scale_data
@ -372,25 +388,17 @@ static int print_scale_data(unsigned char* dat) {
}
//
// find_scale
// is_scale
// ----------
//
// **find_scale** takes a `libusb_device\*\*` list and loop through it,
// **is_scale** takes a `libusb_device\*\*` list and loop through it,
// matching each device's vendor and product IDs to the scales.h list. It
// return the first matching `libusb_device\*` or 0 if no matching device is
// found.
//
static libusb_device* find_scale(libusb_device **devs)
static bool is_scale(libusb_device *dev)
{
int i = 0;
libusb_device* dev = 0;
//
// Loop through each USB device, and for each device, loop through the
// scales list to see if it's one of our listed scales.
//
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
@ -434,13 +442,11 @@ static libusb_device* find_scale(libusb_device **devs)
"Manufacturer: %s\n", string);
libusb_close(hand);
#endif
return dev;
return true;
break;
}
}
}
return NULL;
return false;
}
uint8_t get_first_endpoint_address(libusb_device* dev)