Skip to content

Commit 3250775

Browse files
committed
Added low battery warning
1 parent 95ebddf commit 3250775

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

owonb35.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535

3636
#include <gattlib.h>
3737

38+
#define VERSION "1.1.1"
39+
40+
int quiet = FALSE;
41+
3842
#define BLE_SCAN_TIMEOUT 4
3943

4044
GMainLoop *loop;
@@ -53,6 +57,8 @@ int units = 0;
5357

5458
int show_units = TRUE;
5559

60+
int low_battery = FALSE;
61+
5662
unsigned long start_time = 0;
5763

5864
void print_timestamp() {
@@ -208,7 +214,7 @@ void print_units(int scale, int function) {
208214

209215
void print_type(uint16_t type) {
210216

211-
// Clear auto flag
217+
// Clear auto and low battery flags
212218
type = type & 0x33;
213219

214220
switch (type) {
@@ -267,6 +273,17 @@ void notification_handler(const uuid_t* uuid, const uint8_t* data, size_t data_l
267273
measurement = -1 * (float)(reading[2] & 0x7fff) / pow(10.0, decimal);
268274
}
269275

276+
if (reading[1] & 0x08) {
277+
if (!low_battery) {
278+
fprintf(stderr, "LOW BATTERY\n");
279+
}
280+
281+
if (low_battery++ > 17) low_battery = 0;
282+
283+
} else {
284+
low_battery = FALSE;
285+
}
286+
270287
switch (format) {
271288
case space:
272289
case csv:
@@ -338,6 +355,8 @@ static void usage(char *argv[]) {
338355
printf("\t-M\t\t Scale measurements to mega units\n");
339356
printf("\t-x\t\t Output just the measurement without the units or type for use with feedgnuplot\n");
340357
printf("\t<device_address> Address of Owon multimeter to connect\n");
358+
printf("\t-q\t\t Quiet - no status output\n");
359+
printf("\t-V\t\t Display version\n");
341360
printf("\t\t\t otherwise will connect to first meter found if not specified\n");
342361

343362
}
@@ -347,7 +366,7 @@ static void ble_discovered_device(const char* addr, const char* name) {
347366

348367
if ((name != NULL) && (strcmp(BDM, name) == 0) && (address == NULL)) {
349368

350-
fprintf(stderr, "Connecting to %s\n", addr);
369+
if (!quiet) fprintf(stderr, "Connecting to %s\n", addr);
351370

352371
address = malloc(18);
353372
strcpy(address,addr);
@@ -435,6 +454,16 @@ int main(int argc, char *argv[]) {
435454
usage(argv);
436455
return 0;
437456

457+
case 'q':
458+
quiet = TRUE;
459+
break;
460+
461+
case 'V':
462+
printf("%s version ", argv[0]);
463+
printf(VERSION);
464+
printf("\n");
465+
return 0;
466+
438467
default:
439468
fprintf(stderr, "Unknown option %s\n\n", argv[argi]);
440469
usage(argv);
@@ -450,7 +479,7 @@ int main(int argc, char *argv[]) {
450479
if (scan) {
451480

452481
do {
453-
fprintf(stderr, "Scanning...\n");
482+
if (!quiet) fprintf(stderr, "Scanning...\n");
454483

455484
ret = gattlib_adapter_open(adapter_name, &adapter);
456485
if (ret) {
@@ -465,12 +494,12 @@ int main(int argc, char *argv[]) {
465494
}
466495
gattlib_adapter_scan_disable(adapter);
467496

468-
fprintf(stderr, "Scan completed\n");
497+
if (!quiet) fprintf(stderr, "Scan completed\n");
469498

470499
gattlib_adapter_close(adapter);
471500

472501
if (address == NULL) {
473-
fprintf(stderr, "Multimeter device not found.\n");
502+
if (!quiet) fprintf(stderr, "Multimeter device not found.\n");
474503
sleep(2);
475504
}
476505

@@ -505,6 +534,6 @@ int main(int argc, char *argv[]) {
505534

506535
g_main_loop_unref(loop);
507536
gattlib_disconnect(connection);
508-
fprintf(stderr,"Done\n");
537+
if (!quiet) fprintf(stderr,"Done\n");
509538
return 0;
510539
}

0 commit comments

Comments
 (0)