summaryrefslogtreecommitdiff
path: root/src/sensors/huey2/huey-tool.c
blob: ce55ab05ea36e0925db2d23382c239826cf87ff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>

#include "huey-device.h"

int
main (int argc, char *argv[])
{
	g_autoptr(GError) error = NULL;
	g_autoptr(GUsbContext) ctx = NULL;
	g_autoptr(GUsbDevice) device = NULL;

	/* check arguments */
	if (argc != 2) {
		g_printerr ("command required, e.g. status, eeprom, sample\n");
		return EXIT_FAILURE;
	}

	/* find and open device */
	ctx = g_usb_context_new (&error);
	if (ctx == NULL) {
		g_printerr ("%s\n", error->message);
		return EXIT_FAILURE;
	}
	device = g_usb_context_find_by_vid_pid (ctx, HUEY_USB_VID, HUEY_USB_PID, &error);
	if (device == NULL) {
		g_printerr ("%s\n", error->message);
		return EXIT_FAILURE;
	}
	if (!huey_device_open (device, &error)) {
		g_printerr ("%s\n", error->message);
		return EXIT_FAILURE;
	}

	/* device status */
	if (g_strcmp0 (argv[1], "status") == 0) {
		g_autofree gchar *status = NULL;
		status = huey_device_get_status (device, &error);
		if (status == NULL) {
			g_printerr ("failed to get status: %s\n", error->message);
			return EXIT_FAILURE;
		}
		g_print ("status = '%s'\n", status);
		return EXIT_SUCCESS;
	}

	/* sample the colors again and again */
	if (g_strcmp0 (argv[1], "sample") == 0) {
		for (guint i = 0; i < 0xff; i++) {
			gdouble val = 0.f;
			if (!huey_device_take_sample (device, &val, &error)) {
				g_printerr ("failed to take sample: %s\n", error->message);
				return EXIT_FAILURE;
			}
			g_print ("val=%.3f\n", val);
		}
		return EXIT_SUCCESS;
	}

	/* get all the EEPROM */
	if (g_strcmp0 (argv[1], "eeprom") == 0) {
		const gchar *data;
		gsize sz = 0;
		g_autoptr(GBytes) blob = NULL;

		/* read entire EEPROM space */
		blob = huey_device_read_eeprom (device, &error);
		if (blob == NULL) {
			g_printerr ("failed to get EEPROM: %s\n", error->message);
			return EXIT_FAILURE;
		}
		/* write it out to a file */
		data = g_bytes_get_data (blob, &sz);
		if (!g_file_set_contents ("huey2.bin", data, sz, &error)) {
			g_printerr ("failed to save file: %s\n", error->message);
			return EXIT_FAILURE;
		}
		return EXIT_SUCCESS;
	}

	/* command invalid */
	g_printerr ("command not known\n");
	return EXIT_FAILURE;
}