| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2017 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
|
| 24 |
#include "huey-device.h" |
| 25 |
|
| 26 |
int |
| 27 |
main (int argc, char *argv[]) |
| 28 |
{ |
| 29 |
g_autoptr(GError) error = NULL; |
| 30 |
g_autoptr(GUsbContext) ctx = NULL; |
| 31 |
g_autoptr(GUsbDevice) device = NULL; |
| 32 |
|
| 33 |
/* check arguments */ |
| 34 |
if (argc != 2) { |
| 35 |
g_printerr ("command required, e.g. status, eeprom, sample\n"); |
| 36 |
return EXIT_FAILURE; |
| 37 |
} |
| 38 |
|
| 39 |
/* find and open device */ |
| 40 |
ctx = g_usb_context_new (&error); |
| 41 |
if (ctx == NULL) { |
| 42 |
g_printerr ("%s\n", error->message); |
| 43 |
return EXIT_FAILURE; |
| 44 |
} |
| 45 |
device = g_usb_context_find_by_vid_pid (ctx, HUEY_USB_VID, HUEY_USB_PID, &error); |
| 46 |
if (device == NULL) { |
| 47 |
g_printerr ("%s\n", error->message); |
| 48 |
return EXIT_FAILURE; |
| 49 |
} |
| 50 |
if (!huey_device_open (device, &error)) { |
| 51 |
g_printerr ("%s\n", error->message); |
| 52 |
return EXIT_FAILURE; |
| 53 |
} |
| 54 |
|
| 55 |
/* device status */ |
| 56 |
if (g_strcmp0 (argv[1], "status") == 0) { |
| 57 |
g_autofree gchar *status = NULL; |
| 58 |
status = huey_device_get_status (device, &error); |
| 59 |
if (status == NULL) { |
| 60 |
g_printerr ("failed to get status: %s\n", error->message); |
| 61 |
return EXIT_FAILURE; |
| 62 |
} |
| 63 |
g_print ("status = '%s'\n", status); |
| 64 |
return EXIT_SUCCESS; |
| 65 |
} |
| 66 |
|
| 67 |
/* sample the colors again and again */ |
| 68 |
if (g_strcmp0 (argv[1], "sample") == 0) { |
| 69 |
for (guint i = 0; i < 0xff; i++) { |
| 70 |
gdouble val = 0.f; |
| 71 |
if (!huey_device_take_sample (device, &val, &error)) { |
| 72 |
g_printerr ("failed to take sample: %s\n", error->message); |
| 73 |
return EXIT_FAILURE; |
| 74 |
} |
| 75 |
g_print ("val=%.3f\n", val); |
| 76 |
} |
| 77 |
return EXIT_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
/* get all the EEPROM */ |
| 81 |
if (g_strcmp0 (argv[1], "eeprom") == 0) { |
| 82 |
const gchar *data; |
| 83 |
gsize sz = 0; |
| 84 |
g_autoptr(GBytes) blob = NULL; |
| 85 |
|
| 86 |
/* read entire EEPROM space */ |
| 87 |
blob = huey_device_read_eeprom (device, &error); |
| 88 |
if (blob == NULL) { |
| 89 |
g_printerr ("failed to get EEPROM: %s\n", error->message); |
| 90 |
return EXIT_FAILURE; |
| 91 |
} |
| 92 |
/* write it out to a file */ |
| 93 |
data = g_bytes_get_data (blob, &sz); |
| 94 |
if (!g_file_set_contents ("huey2.bin", data, sz, &error)) { |
| 95 |
g_printerr ("failed to save file: %s\n", error->message); |
| 96 |
return EXIT_FAILURE; |
| 97 |
} |
| 98 |
return EXIT_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
/* command invalid */ |
| 102 |
g_printerr ("command not known\n"); |
| 103 |
return EXIT_FAILURE; |
| 104 |
} |