mirror of
https://github.com/erjiang/usbscale.git
synced 2025-06-29 01:20:59 -06:00
Try using every scale found
This commit is contained in:
parent
1d3a7f5ea5
commit
a26fd9eb55
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
LDLIBS=-lm -lusb-1.0
|
||||
CFLAGS=-Os -Wall
|
||||
CFLAGS=-Os -Wall -Wextra
|
||||
DOCCO=docco
|
||||
|
||||
usbscale: usbscale.c scales.h
|
||||
|
64
usbscale.c
64
usbscale.c
@ -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.
|
||||
@ -158,9 +163,9 @@ int main(int argc, char **argv)
|
||||
// On Linux, we typically need to detach the kernel driver so that we can
|
||||
// handle this USB device. We are a userspace tool, after all.
|
||||
//
|
||||
#ifdef __linux__
|
||||
#ifdef __linux__
|
||||
libusb_detach_kernel_driver(handle, 0);
|
||||
#endif
|
||||
#endif
|
||||
//
|
||||
// Finally, we can claim the interface to this device and begin I/O.
|
||||
//
|
||||
@ -238,12 +243,12 @@ int main(int argc, char **argv)
|
||||
// received to **print_scale_data**.
|
||||
//
|
||||
if(r == 0) {
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUG
|
||||
int i;
|
||||
for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
|
||||
printf("%x\n", data[i]);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
if (weigh_count < 1) {
|
||||
scale_result = print_scale_data(data);
|
||||
if(scale_result != 1)
|
||||
@ -263,9 +268,9 @@ int main(int argc, char **argv)
|
||||
// detached earlier, close the handle to the device, free the device list
|
||||
// that we retrieved, and exit libusb.
|
||||
//
|
||||
#ifdef __linux__
|
||||
#ifdef __linux__
|
||||
libusb_attach_kernel_driver(handle, 0);
|
||||
#endif
|
||||
#endif
|
||||
libusb_close(handle);
|
||||
libusb_free_device_list(devs, 1);
|
||||
libusb_exit(NULL);
|
||||
@ -274,7 +279,18 @@ 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;
|
||||
}
|
||||
|
||||
//
|
||||
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user