Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Git Source Tree
Root/
Source at commit e98ea2bfdb0dab5fa5b35c0da966eae7f50a6563 created 12 years 6 months ago. By Werner Almesberger, labsw/tool/: basic labsw control utility | |
---|---|
1 | /* |
2 | * tool/labsw.c - LABSW control tool |
3 | * |
4 | * Written 2011 by Werner Almesberger |
5 | * Copyright 2011 Werner Almesberger |
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 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <stdio.h> |
16 | #include <string.h> |
17 | #include <usb.h> |
18 | |
19 | #include "f32xbase/usb.h" |
20 | #include "labsw/ep0.h" |
21 | #include "labsw/usb-id.h" |
22 | |
23 | |
24 | #define FROM_DEV LABSW_FROM_DEV(0) |
25 | #define TO_DEV LABSW_TO_DEV(0) |
26 | |
27 | #define BUF_SIZE 256 |
28 | |
29 | |
30 | static void identify_labsw(usb_dev_handle *dev) |
31 | { |
32 | const struct usb_device *device = usb_device(dev); |
33 | uint8_t ids[3]; |
34 | char buf[BUF_SIZE+1]; /* +1 for terminating \0 */ |
35 | int res; |
36 | |
37 | printf("%04x:%04x ", |
38 | device->descriptor.idVendor, device->descriptor.idProduct); |
39 | |
40 | res = usb_control_msg(dev, FROM_DEV, LABSW_ID, 0, 0, |
41 | (char *) ids, sizeof(ids), 1000); |
42 | if (res < 0) { |
43 | fprintf(stderr, "LABSW_ID: %s\n", usb_strerror()); |
44 | exit(1); |
45 | } |
46 | |
47 | printf("protocol %u.%u hw %u\n", ids[0], ids[1], ids[2]); |
48 | |
49 | res = usb_control_msg(dev, FROM_DEV, LABSW_BUILD, 0, 0, |
50 | buf, sizeof(buf), 1000); |
51 | if (res < 0) { |
52 | fprintf(stderr, "LABSW_BUILD: %s\n", usb_strerror()); |
53 | exit(1); |
54 | } |
55 | buf[res] = 0; |
56 | printf("%10s%s\n", "", buf); |
57 | } |
58 | |
59 | |
60 | static void set(usb_dev_handle *dev, uint16_t value, uint16_t mask) |
61 | { |
62 | int res; |
63 | |
64 | res = usb_control_msg(dev, TO_DEV, LABSW_SET, value, mask, |
65 | NULL, 0, 1000); |
66 | if (res < 0) { |
67 | fprintf(stderr, "LABSW_SET: %s\n", usb_strerror()); |
68 | exit(1); |
69 | } |
70 | } |
71 | |
72 | |
73 | #define ON_BITS(ch) \ |
74 | (LABSW_CH##ch##_RELAY | LABSW_CH##ch##_OPT | LABSW_CH##ch##_R) |
75 | #define OFF_BITS(ch) LABSW_CH##ch##_G |
76 | #define CHAN_MASK(ch) (ON_BITS(ch) | OFF_BITS(ch)) |
77 | #define REMOTE_BITS LABSW_MAIN_R |
78 | #define REMOTE_MASK (LABSW_MAIN_R | LABSW_MAIN_G) |
79 | |
80 | |
81 | #define SET_CHAN(ch, on) \ |
82 | set(dev, (on ? ON_BITS(ch) : OFF_BITS(ch)) | REMOTE_BITS, \ |
83 | CHAN_MASK(ch) | REMOTE_MASK) |
84 | |
85 | |
86 | static void query(usb_dev_handle *dev) |
87 | { |
88 | uint8_t buf[2]; |
89 | int res; |
90 | |
91 | res = usb_control_msg(dev, FROM_DEV, LABSW_GET, 0, 0, |
92 | (char *) buf, sizeof(buf), 1000); |
93 | if (res < 0) { |
94 | fprintf(stderr, "LABSW_GET: %s\n", usb_strerror()); |
95 | exit(1); |
96 | } |
97 | if (res != 2) { |
98 | fprintf(stderr, "LABSW_GET: expected %u, got %d\n", |
99 | (unsigned) sizeof(buf), res); |
100 | exit(1); |
101 | } |
102 | printf("%u %u\n", buf[0], buf[1]); |
103 | } |
104 | |
105 | |
106 | int main(int argc, char **argv) |
107 | { |
108 | usb_dev_handle *dev; |
109 | int i; |
110 | |
111 | dev = open_usb(USB_VENDOR, USB_PRODUCT); |
112 | if (!dev) { |
113 | fprintf(stderr, ":-(\n"); |
114 | return 1; |
115 | } |
116 | |
117 | if (argc == 1) { |
118 | identify_labsw(dev); |
119 | return 0; |
120 | } |
121 | |
122 | for (i = 1; i != argc; i++) { |
123 | if (!strcmp(argv[i], "ch1=0")) |
124 | SET_CHAN(1, 0); |
125 | else if (!strcmp(argv[i], "ch1=1")) |
126 | SET_CHAN(1, 1); |
127 | else if (!strcmp(argv[i], "ch2=0")) |
128 | SET_CHAN(2, 0); |
129 | else if (!strcmp(argv[i], "ch2=1")) |
130 | SET_CHAN(2, 1); |
131 | else if (!strcmp(argv[i], "query")) |
132 | query(dev); |
133 | else if (!strcmp(argv[i], "local")) |
134 | set(dev, 0, 0); |
135 | else { |
136 | fprintf(stderr, "unrecognized command \"%s\"\n", |
137 | argv[i]); |
138 | exit(1); |
139 | } |
140 | } |
141 | return 0; |
142 | } |
143 |
Branches:
master