mirror of
https://github.com/erjiang/usbscale.git
synced 2025-06-28 09:00:58 -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
|
LDLIBS=-lm -lusb-1.0
|
||||||
CFLAGS=-Os -Wall
|
CFLAGS=-Os -Wall -Wextra
|
||||||
DOCCO=docco
|
DOCCO=docco
|
||||||
|
|
||||||
usbscale: usbscale.c scales.h
|
usbscale: usbscale.c scales.h
|
||||||
|
52
usbscale.c
52
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
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stdint.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
|
#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.
|
// 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
|
// **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
|
// 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;
|
ssize_t cnt;
|
||||||
libusb_device* dev;
|
libusb_device* dev;
|
||||||
libusb_device_handle* handle;
|
libusb_device_handle* handle;
|
||||||
|
bool found_scale = false;
|
||||||
|
|
||||||
int weigh_count = WEIGH_COUNT -1;
|
int weigh_count = WEIGH_COUNT -1;
|
||||||
|
|
||||||
@ -127,15 +130,17 @@ int main(int argc, char **argv)
|
|||||||
return (int) cnt;
|
return (int) cnt;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Once we have the list, we use **find_scale** to loop through and match
|
// Once we have the list, we use **is_scale** to loop through and match
|
||||||
// every device against the scales.h list. **find_scale** will return the
|
// every device against the scales.h list. **is_scale** will return the
|
||||||
// first device that matches, or 0 if none of them matched.
|
// first device that matches, or 0 if none of them matched.
|
||||||
//
|
//
|
||||||
dev = find_scale(devs);
|
|
||||||
if(dev == 0) {
|
int i = 0;
|
||||||
fprintf(stderr, "No USB scale found on this computer.\n");
|
while ((dev = devs[i++]) != NULL) {
|
||||||
return -1;
|
if (!is_scale(dev)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
found_scale = true;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Once we have a pointer to the USB scale in question, we open it.
|
// 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
|
// The return code will be 0 for success or -1 for errors (see
|
||||||
// `libusb_init` above if it's neither 0 nor -1).
|
// `libusb_init` above if it's neither 0 nor -1).
|
||||||
//
|
//
|
||||||
|
if (scale_result != 0) {
|
||||||
return scale_result;
|
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
|
// 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
|
// 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
|
// return the first matching `libusb_device\*` or 0 if no matching device is
|
||||||
// found.
|
// 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;
|
struct libusb_device_descriptor desc;
|
||||||
int r = libusb_get_device_descriptor(dev, &desc);
|
int r = libusb_get_device_descriptor(dev, &desc);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
@ -434,13 +442,11 @@ static libusb_device* find_scale(libusb_device **devs)
|
|||||||
"Manufacturer: %s\n", string);
|
"Manufacturer: %s\n", string);
|
||||||
libusb_close(hand);
|
libusb_close(hand);
|
||||||
#endif
|
#endif
|
||||||
return dev;
|
return true;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return false;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t get_first_endpoint_address(libusb_device* dev)
|
uint8_t get_first_endpoint_address(libusb_device* dev)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user