updated on Thu Jan 5 13:17:10 UTC 2012
[aur-mirror.git] / radioshark / shark.c
blobf57bb9768ee22012530e819b9e1a00294ede033a
1 /* This souce code written my Michael Rolig (email: michael_rolig@alumni.macalester.edu)
2 * This can be considered to be in the public domain
3 */
5 #define DEBUG false /* Set true for copious debugging output */
6 #define SHARK_VENDID 0x077d /* Griffin's Vendor ID */
7 #define SHARK_DEVID 0x627a /* The radioSHARK's Device ID */
9 #define READ_EP 0x5 /* libhid read command? */
10 #define WRITE_EP 0x5 /* libhid write command? */
11 #define SEND_PACKET_LENGTH 6 /* size of an instruction packet */
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <hid.h>
18 void usage(int argc, const char** argv) {
19 printf("%s <command> <arg>\n\tchange state of radioSHARK\n\n", argv[0]);
20 printf("commands:\n"
21 " -fm <freqeuncy> : set FM frequency, e.g. '-fm 91.5'\n"
22 " -am <frequency> : set AM frequency, e.g. '-am 730'\n"
23 " -blue <intensity> : turn on blue LED (0-127) '-blue 127'\n"
24 " -bblue <freqency> : turn on blue LED pulsing (0-127) '-bblue 64'\n"
25 " -red <0/1> : turn on/off red LED '-red 1'\n");
28 int main(int argc, const char** argv) {
30 /* Declare variables used later */
31 hid_return ret;
32 HIDInterface* hid;
33 HIDInterfaceMatcher matcher = { SHARK_VENDID, SHARK_DEVID, NULL, NULL, 0 };
35 /* Build the instruction packet to send to the shark */
36 unsigned char PACKET[SEND_PACKET_LENGTH] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
37 unsigned short encodedFreq;
38 float freq;
39 unsigned int intensity;
40 if (argc == 3) {
41 if (strcmp(argv[1], "-fm") == 0) {
42 /* Tune to an FM frequency */
43 PACKET[0] = 0xC0;
44 encodedFreq = 0;
45 freq = atof(argv[2]);
46 encodedFreq = ((freq * 1000) + 10700) / 12.5;
47 encodedFreq += 3;
48 PACKET[2] = (encodedFreq >> 8) & 0xFF;
49 PACKET[3] = encodedFreq & 0xFF;
50 if (DEBUG) {
51 printf("band = fm\n");
52 printf("freq = %.1f\n", freq);
53 printf("encoded freq = 0x%x\n", (unsigned int)encodedFreq);
55 } else if (strcmp(argv[1], "-am") == 0) {
56 /* Tune to an AM frequency */
57 PACKET[0] = 0xC0;
58 encodedFreq = 0;
59 freq = (float)atoi(argv[2]);
60 encodedFreq = ((unsigned short)freq) + 450;
61 PACKET[1] = 0x12;
62 PACKET[2] = (encodedFreq >> 8) & 0xFF;
63 PACKET[3] = encodedFreq & 0xFF;
64 if (DEBUG) {
65 printf("band = am\n");
66 printf("freq = %d\n", (unsigned int)freq);
67 printf("encoded freq = 0x%x\n", (unsigned int)encodedFreq);
69 } else if (strcmp(argv[1], "-blue") == 0) {
70 /* Adjust the blue LED */
71 intensity = atoi(argv[2]);
72 PACKET[0] = 0xA0;
73 PACKET[1] = (char)intensity;
74 } else if (strcmp(argv[1], "-bblue") == 0) {
75 /* Adjust the blue LED's pulsing rate */
76 intensity = atoi(argv[2]);
77 PACKET[0] = 0xA1;
78 PACKET[1] = (char)intensity;
79 } else if (strcmp(argv[1], "-red") == 0) {
80 /* Toggle the red LED */
81 intensity = atoi(argv[2]);
82 if (intensity) PACKET[0] = 0xA9;
83 else PACKET[0] = 0xA8;
84 PACKET[1] = (char)intensity;
85 } else {
86 /* Bad command - display the program's usage instructions */
87 usage(argc, argv);
88 exit(1);
90 } else {
91 usage(argc, argv);
92 exit(1);
95 /* Turn libhid debugging on if requested. See include/debug.h for possible values. */
96 if (DEBUG) {
97 hid_set_debug(HID_DEBUG_ALL);
98 hid_set_debug_stream(stderr);
99 hid_set_usb_debug(0); /* passed directly to libusb */
102 /* Initialize the hid library */
103 ret = hid_init();
104 if (ret != HID_RET_SUCCESS) {
105 fprintf(stderr, "hid_init failed with return code %d\n", ret);
106 return 1;
109 /* Initialize the hid object */
110 hid = hid_new_HIDInterface();
111 if (hid == 0) {
112 fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
113 return 1;
116 /* Open the shark */
117 ret = hid_force_open(hid, 2, &matcher, 3);
118 if (ret != HID_RET_SUCCESS) {
119 fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
120 return 1;
123 /* Send the instruction packet constructed above to the Shark */
124 ret = hid_interrupt_write(hid, WRITE_EP, (char*)PACKET, SEND_PACKET_LENGTH, 10000);
125 if (ret != HID_RET_SUCCESS) fprintf(stderr, "hid_interrupt_write failed with return code %d\n", ret);
127 /* Close the shark */
128 ret = hid_close(hid);
129 if (ret != HID_RET_SUCCESS) {
130 fprintf(stderr, "hid_close failed with return code %d\n", ret);
131 return 1;
134 /* Delete the hid object */
135 hid_delete_HIDInterface(&hid);
137 /* Clean up the hid library */
138 ret = hid_cleanup();
139 if (ret != HID_RET_SUCCESS) {
140 fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
141 return 1;
144 return 0;