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
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 */
18 void usage(int argc
, const char** argv
) {
19 printf("%s <command> <arg>\n\tchange state of radioSHARK\n\n", argv
[0]);
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 */
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
;
39 unsigned int intensity
;
41 if (strcmp(argv
[1], "-fm") == 0) {
42 /* Tune to an FM frequency */
46 encodedFreq
= ((freq
* 1000) + 10700) / 12.5;
48 PACKET
[2] = (encodedFreq
>> 8) & 0xFF;
49 PACKET
[3] = encodedFreq
& 0xFF;
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 */
59 freq
= (float)atoi(argv
[2]);
60 encodedFreq
= ((unsigned short)freq
) + 450;
62 PACKET
[2] = (encodedFreq
>> 8) & 0xFF;
63 PACKET
[3] = encodedFreq
& 0xFF;
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]);
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]);
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
;
86 /* Bad command - display the program's usage instructions */
95 /* Turn libhid debugging on if requested. See include/debug.h for possible values. */
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 */
104 if (ret
!= HID_RET_SUCCESS
) {
105 fprintf(stderr
, "hid_init failed with return code %d\n", ret
);
109 /* Initialize the hid object */
110 hid
= hid_new_HIDInterface();
112 fprintf(stderr
, "hid_new_HIDInterface() failed, out of memory?\n");
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
);
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
);
134 /* Delete the hid object */
135 hid_delete_HIDInterface(&hid
);
137 /* Clean up the hid library */
139 if (ret
!= HID_RET_SUCCESS
) {
140 fprintf(stderr
, "hid_cleanup failed with return code %d\n", ret
);