-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterwheel.c
85 lines (68 loc) · 1.87 KB
/
filterwheel.c
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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "hidapi/hidapi.h"
#include "comms.h"
#include "filterwheel.h"
int main(int argc, char* argv[]) {
unsigned char response;
unsigned char buf[2];
struct hid_device_info *devs, *cur_dev;
struct filter_options opts;
opts = parse_options(argc, argv);
hid_device *dev = hid_open(0x1278, 0x0920, NULL);
if (dev == NULL) {
printf("An error occured: Could not open device.\n" );
return 1;
}
if (opts.op == GET_FILTER) {
printf("%d\n", current_filter(dev));
} else if (opts.op == CHANGE_FILTER){
change_filter(opts.filter, dev);
}
hid_close(dev);
hid_exit();
return 0;
}
struct filter_options parse_options(int argc, char* argv[]) {
struct filter_options opts = {CHANGE_FILTER, 0};
char c;
int opt_index = 0;
static struct option long_options[] = {
{"get", 0, NULL, 'g'},
{"help", 0, NULL, 'h'},
};
while ((c = getopt_long(argc, argv, "gh", long_options, &opt_index)) != -1 ) {
switch (c) {
case 'g':
opts.op = GET_FILTER;
break;
case 'h':
print_help_and_exit(0);
case '?':
printf("Unknown option: %d \n", c);
print_help_and_exit(1);
default:
break;
}
}
if (opts.op == CHANGE_FILTER) {
if (argc != 2) {
printf("Incorrect number of arguments!");
print_help_and_exit(1);
}
opts.filter = atoi(argv[argc -1]);
if (opts.filter == 0) {
printf("Filter number must be a natural number.");
print_help_and_exit(1);
}
}
return opts;
}
void print_help_and_exit(char exit_status) {
printf("usage: filterwheel [-g] [-h] [filter_number]\n");
printf("\tfilter_number: The number of the filter that you want to use\n");
printf("\t-g, --get: Get the current filter\n");
printf("\t-h, --help: Print this help message\n");
exit(exit_status);
}