Date:2011-09-06 09:21:36 (8 years 9 days ago)
Author:Werner Almesberger
Commit:e98ea2bfdb0dab5fa5b35c0da966eae7f50a6563
Message:labsw/tool/: basic labsw control utility

Files: labsw/tool/Makefile (1 diff)
labsw/tool/README (1 diff)
labsw/tool/labsw.c (1 diff)

Change Details

labsw/tool/Makefile
1#
2# tool/Makefile - Build the 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
14F32XBASE = ../../../f32xbase
15
16MAIN = labsw
17OBJS += $(F32XBASE)/lib/usb.o
18
19include $(F32XBASE)/lib/Makefile.common
20
21CFLAGS += -I$(F32XBASE)/include -I../fw/include -I../include
labsw/tool/README
1Lab Switch control utility
2--------------------------
3
4If invoked without arguments, labsw prints the firmware version and
5build date.
6
7The following commands can be given on the command line:
8
9ch1=0, ch1=1, ch2=0, ch2=1
10    turn the respective channel on or off. Both relay and
11    opto-coupler are switched. Also enters remote mode.
12
13query
14    read the buttons and opto-coupler inputs (rear panel)
15
16local
17    exit remote mode
18
19The Lab Switch starts in local mode, where the CH1/CH2 buttons
20toggle the respective channel. The main button turns off both
21channels. Local mode is indicated by a green main LED.
22
23If a channel is set via USB, the Lab Switch enters remote mode,
24indicated by a red main LED. In remote mode, the channel buttons
25are ignored. Pressing the main button exits remote mode and
26turns both channels off.
27
28The lab switch utility currently lacks the following features:
29- access to the rear opto-coupler outputs,
30- switching CH1/CH2 relay and opto-coupler independently,
31- direct control of LEDs.
32
33Examples:
34
35# labsw ch1=1
36
37Turns channel 1 on and enters remote mode.
38
39# labsw ch1=0 ch2=0 local
40
41Turns channels 1 and 2 off and exits remote mode. This is
42equivalent to pressing the main button.
labsw/tool/labsw.c
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
30static 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
60static 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
86static 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
106int 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}

Archive Download the corresponding diff file

Branches:
master



interactive