Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for additional scale and fixes according to the docs. #11

Merged
merged 3 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scales.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
// **NSCALES** should be kept updated with the length of the list.
//
#define NSCALES 6
#define NSCALES 8

//
// What is the number of the weighing result to show, as the first result may be incorrect (from the previous weighing)
Expand All @@ -37,5 +37,7 @@ uint16_t scales[NSCALES][2] = {\
// SANFORD Dymo 10 lb USB Postal Scale
{0x6096, 0x0158},
// Fairbanks Scales SCB-R9000
{0x0b67, 0x555e}
{0x0b67, 0x555e},
// Dymo-CoStar Corp. M25 Digital Postal Scale
{0x0922, 0x8004}
};
44 changes: 32 additions & 12 deletions usbscale.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ static libusb_device* find_scale(libusb_device**);
// program should read again (i.e. continue looping).
//
static int print_scale_data(unsigned char*);

//
// take device and fetch bEndpointAddress for the first endpoint
//
uint8_t get_first_endpoint_address(libusb_device* dev);

//
// **UNITS** is an array of all the unit abbreviations as set forth by *HID
// Point of Sale Usage Tables*, version 1.02, by the USB Implementers' Forum.
Expand Down Expand Up @@ -197,8 +203,7 @@ int main(void)
handle,
//bmRequestType => direction: in, type: class,
// recipient: interface
LIBUSB_ENDPOINT_IN | //LIBUSB_REQUEST_TYPE_CLASS |
LIBUSB_RECIPIENT_INTERFACE,
get_first_endpoint_address(dev),
data,
WEIGH_REPORT_SIZE, // length of data
&len,
Expand Down Expand Up @@ -276,16 +281,12 @@ static int print_scale_data(unsigned char* dat) {
uint8_t report = dat[0];
uint8_t status = dat[1];
uint8_t unit = dat[2];
uint8_t expt = dat[3];
double weight = (double)(dat[4] + (dat[5] << 8)) / 10;

if(expt != 255 && expt != 0) {
if (expt > 127) {
weight = weight * pow(10, expt-255);
} else {
weight = pow(weight, expt);
}
}
// Accoring to the docs, scaling applied to the data as a base ten exponent
int8_t expt = dat[3];
// convert to machine order at all times
double weight = (double) le16toh(dat[5] << 8 | dat[4]);
// since the expt is signed, we do not need no trickery
weight = weight * pow(10, expt);

//
// The scale's first byte, its "report", is always 3.
Expand Down Expand Up @@ -418,3 +419,22 @@ static libusb_device* find_scale(libusb_device **devs)
return NULL;
}

uint8_t get_first_endpoint_address(libusb_device* dev)
{
// default value
uint8_t endpoint_address = LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_INTERFACE; //| LIBUSB_RECIPIENT_ENDPOINT;

struct libusb_config_descriptor *config;
int r = libusb_get_config_descriptor(dev, 0, &config);
if (r == 0) {
// assuming we have only one endpoint
endpoint_address = config->interface[0].altsetting[0].endpoint[0].bEndpointAddress;
libusb_free_config_descriptor(config);
}

#ifdef DEBUG
printf("bEndpointAddress 0x%02x\n", endpoint_address);
#endif

return endpoint_address;
}