Skip to content

Commit 9ed1cb5

Browse files
committed
Add human-readable ounce display
1 parent 23888ab commit 9ed1cb5

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ error messages will be sent to `stderr`. An exit code of 0 means that a scale
2424
was found and a weight was successfully read. Any other error code indicates
2525
that a weight reading was unavailable.
2626

27+
Use the `-h`/`--human` flag to format ounce measurements as pounds and
28+
ounces, e.g. `1 lbs 4 oz` instead of `20 oz`.
29+
2730
## Zeroing the scale
2831

2932
There is somewhat-experimental support for sending a tare command to the scale.

usbscale.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static libusb_device* find_nth_scale(libusb_device**, int);
7373
// it, printing out the result to the screen. It also returns a 1 if the
7474
// program should read again (i.e. continue looping).
7575
//
76-
static int print_scale_data(unsigned char*);
76+
static int print_scale_data(unsigned char*, bool);
7777

7878
//
7979
// take device and fetch bEndpointAddress for the first endpoint
@@ -107,20 +107,22 @@ const char* UNITS[13] = {
107107
};
108108

109109
// Setup argument parsing
110-
const char *argp_program_version = "usbscale 0.2";
110+
const char *argp_program_version = "usbscale 0.3";
111111
const char *argp_program_bug_address = "<https://www.github.com/erjiang/usbscale/issues>";
112112
static char doc[] = "Read weight from a USB scale\n"
113113
"The `zero' command will request the scale to reset to zero (not supported by all scales).\n";
114114
static char args_doc[] = "[zero]";
115115
static struct argp_option options[] = {
116116
{ "index", 'i', "INDEX", 0, "Index of scale to read (default: 1)" },
117+
{ "human", 'h', 0, 0, "Print weight in lbs and oz when units are oz" },
117118
{ 0 }
118119
};
119120

120121
// setup argp to get index
121122
struct arguments {
122123
int index;
123124
bool tare;
125+
bool human;
124126
};
125127

126128
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
@@ -133,6 +135,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
133135
argp_usage(state);
134136
}
135137
break;
138+
case 'h':
139+
arguments->human = true;
140+
break;
136141
case ARGP_KEY_ARG:
137142
if (strcmp(arg, "zero") == 0) {
138143
arguments->tare = true;
@@ -162,6 +167,7 @@ int main(int argc, char **argv)
162167
// By default, get the first scale's weight
163168
arguments.index = 1;
164169
arguments.tare = false;
170+
arguments.human = false;
165171
argp_parse(&argp, argc, argv, 0, 0, &arguments);
166172

167173
libusb_device **devs;
@@ -314,7 +320,7 @@ int main(int argc, char **argv)
314320
}
315321
#endif
316322
if (weigh_count < 1) {
317-
scale_result = print_scale_data(data);
323+
scale_result = print_scale_data(data, arguments.human);
318324
if(scale_result != 1)
319325
break;
320326
}
@@ -351,14 +357,15 @@ int main(int argc, char **argv)
351357
// ----------------
352358
//
353359
// **print_scale_data** takes the 6 bytes of binary data sent by the scale and
354-
// interprets and prints it out.
360+
// interprets and prints it out. If `human` is true and the units are ounces,
361+
// it will output the weight as pounds and ounces.
355362
//
356363
// **Returns:** `0` if weight data was successfully read, `1` if the data
357364
// indicates that more data needs to be read (i.e. keep looping), and `-1` if
358365
// the scale data indicates that some error occurred and that the program
359366
// should terminate.
360367
//
361-
static int print_scale_data(unsigned char* dat) {
368+
static int print_scale_data(unsigned char* dat, bool human) {
362369

363370
//
364371
// We keep around `lastStatus` so that we're not constantly printing the
@@ -414,7 +421,13 @@ static int print_scale_data(unsigned char* dat) {
414421
// the `UNITS` lookup table for unit names.
415422
//
416423
case 0x04:
417-
printf("%g %s\n", weight, UNITS[unit]);
424+
if (human && unit == 11) {
425+
int lbs = (int)floor(weight / 16.0);
426+
double oz = weight - (lbs * 16.0);
427+
printf("%d lbs %g oz\n", lbs, oz);
428+
} else {
429+
printf("%g %s\n", weight, UNITS[unit]);
430+
}
418431
return 0;
419432
case 0x05:
420433
if(status != lastStatus)

0 commit comments

Comments
 (0)