CalcProgrammer1

Corsair K70 RGB Rainbow Linux

Oct 9th, 2014
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <usb.h>
  4. #include <math.h>
  5.  
  6. static struct usb_device *find_device(uint16_t vendor, uint16_t product);
  7.  
  8. static void update_keyboard();
  9.  
  10. static void set_led( int x, int y, int r, int g, int b );
  11.  
  12. char red_val[144];
  13. char grn_val[144];
  14. char blu_val[144];
  15.  
  16. char data_pkt[5][64] = { 0 };
  17.  
  18. unsigned char position_map[] = {
  19. 137, 8, 20,255,
  20. 0, 12, 24, 36, 48, 60, 72, 84, 96,108,120,132, 6, 18, 30, 42, 32, 44, 56, 68,255,
  21. 1, 13, 25, 37, 49, 61, 73, 85, 97,109,121,133, 7, 31, 54, 66, 78, 80, 92,104,116,255,
  22. 2, 14, 26, 38, 50, 62, 74, 86, 98,110,122,134, 90,102, 43, 55, 67, 9, 21, 33,128,255,
  23. 3, 15, 27, 39, 51, 63, 75, 87, 99,111,123,135,126, 57, 69, 81,128,255,
  24. 4, 28, 40, 52, 64, 76, 88,100,112,124,136, 79,103, 93,105,117,140,255,
  25. 5, 17, 29, 53, 89,101,113, 91,115,127,139,129,141,140,255,
  26. };
  27.  
  28. float size_map[] = {
  29. -15.5, 1, 1, -2.5, 1, -2, 0,
  30. 1, -.5, 1, 1, 1, 1, -.75, 1, 1, 1, 1, -.75, 1, 1, 1, 1, -.5, 1, 1, 1, -.5, 1, 1, 1, 1, 0,
  31. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, -.5, 1, 1, 1, -.5, 1, 1, 1, 1, 0,
  32. 1.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.5, -.5, 1, 1, 1, -.5, 1, 1, 1, 1, 0,
  33. 1.75, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.25, -4, 1, 1, 1, 1, 0,
  34. 2.25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.75, -1.5, 1, -1.5, 1, 1, 1, 1, 0,
  35. 1.5, 1, 1.25, 6.5, 1.25, 1, 1, 1.5, -.5, 1, 1, 1, -.5, 2, 1, 1, 0,
  36. };
  37.  
  38. unsigned char led_matrix[7][92];
  39.  
  40. struct usb_device *dev;
  41. struct usb_dev_handle *handle;
  42.  
  43. int main(int argc, char **argv)
  44. {
  45.  
  46. int bytes_written = 0;
  47. int status = 0;
  48.  
  49. printf("Searching for Corsair K70 RGB keyboard...\n");
  50.  
  51. dev = find_device(0x1B1C, 0x1B13);
  52.  
  53. if(dev == NULL)
  54. {
  55. printf("Corsair K70 RGB keyboard not detected :(\n");
  56. return 1;
  57. }
  58. else
  59. {
  60. printf("Corsair K70 RGB keyboard detected successfully :)\n");
  61. }
  62.  
  63. handle = usb_open(dev);
  64.  
  65. if(handle == NULL)
  66. {
  67. printf("USB device handle did not open :(\n");
  68. return 1;
  69. }
  70. else
  71. {
  72. printf("USB device handle opened successfully :)\n");
  73. }
  74.  
  75. status = usb_claim_interface(handle, 3);
  76.  
  77. status = usb_detach_kernel_driver_np(handle, 3);
  78.  
  79. if(status == 0)
  80. {
  81. printf("USB interface claimed successfully :)\n");
  82. }
  83. else
  84. {
  85. printf("USB interface claim failed with error %d :(\n", status);
  86. return 1;
  87. }
  88.  
  89. // Construct XY lookup table
  90. unsigned char *keys = position_map;
  91. float *sizes = size_map;
  92. for (int y = 0; y < 7; y++)
  93. {
  94. unsigned char key;
  95. int size = 0;
  96.  
  97. for (int x = 0; x < 92; x++)
  98. {
  99. if (size == 0)
  100. {
  101. float sizef = *sizes++;
  102. if (sizef < 0)
  103. {
  104. size = -sizef * 4;
  105. key = 255;
  106. }
  107. else
  108. {
  109. key = *keys++;
  110. size = sizef * 4;
  111. }
  112. }
  113.  
  114. led_matrix[y][x] = key;
  115. size--;
  116. }
  117.  
  118. if (*keys++ != 255 || *sizes++ != 0)
  119. {
  120. printf("Bad line %d\n", y);
  121. return 1;
  122. }
  123. }
  124.  
  125. while(1)
  126. {
  127. for(int i = 0; i < 91; i++)
  128. {
  129. for(int x = 0; x < 91; x++)
  130. {
  131. for(int y = 0; y < 7; y++)
  132. {
  133. char red = 3.5f * ( sin( ( x / 92.0f ) * 2 * 3.14f ) + 1 );
  134. char grn = 3.5f * ( sin( ( ( x / 92.0f ) * 2 * 3.14f ) - ( 6.28f / 3 ) ) + 1 );
  135. char blu = 3.5f * ( sin( ( ( x / 92.0f ) * 2 * 3.14f ) + ( 6.28f / 3 ) ) + 1 );
  136.  
  137. set_led( ( x + i ) % 92, y, red, grn, blu );
  138. }
  139. }
  140.  
  141. update_keyboard();
  142. usleep(12500);
  143. }
  144. }
  145. return 0;
  146. }
  147.  
  148. static void set_led( int x, int y, int r, int g, int b )
  149. {
  150. int led = led_matrix[y][x];
  151.  
  152. if(led >= 144)
  153. {
  154. return;
  155. }
  156.  
  157. if( r > 7 ) r = 7;
  158. if( g > 7 ) g = 7;
  159. if( b > 7 ) b = 7;
  160.  
  161. r = 7 - r;
  162. g = 7 - g;
  163. b = 7 - b;
  164.  
  165. red_val[led] = r;
  166. grn_val[led] = g;
  167. blu_val[led] = b;
  168. }
  169.  
  170.  
  171. static void update_keyboard()
  172. {
  173. // Perform USB control message to keyboard
  174. //
  175. // Request Type: 0x21
  176. // Request: 0x09
  177. // Value 0x0300
  178. // Index: 0x03
  179. // Size: 64
  180.  
  181. data_pkt[0][0] = 0x7F;
  182. data_pkt[0][1] = 0x01;
  183. data_pkt[0][2] = 0x3C;
  184.  
  185. data_pkt[1][0] = 0x7F;
  186. data_pkt[1][1] = 0x02;
  187. data_pkt[1][2] = 0x3C;
  188.  
  189. data_pkt[2][0] = 0x7F;
  190. data_pkt[2][1] = 0x03;
  191. data_pkt[2][2] = 0x3C;
  192.  
  193. data_pkt[3][0] = 0x7F;
  194. data_pkt[3][1] = 0x04;
  195. data_pkt[3][2] = 0x24;
  196.  
  197. data_pkt[4][0] = 0x07;
  198. data_pkt[4][1] = 0x27;
  199. data_pkt[4][4] = 0xD8;
  200.  
  201. for(int i = 0; i < 60; i++)
  202. {
  203. data_pkt[0][i+4] = red_val[i*2+1] << 4 | red_val[i*2];
  204. }
  205.  
  206. for(int i = 0; i < 12; i++)
  207. {
  208. data_pkt[1][i+4] = red_val[i*2+121] << 4 | red_val[i*2+120];
  209. }
  210.  
  211. for(int i = 0; i < 48; i++)
  212. {
  213. data_pkt[1][i+16] = grn_val[i*2+1] << 4 | grn_val[i*2];
  214. }
  215.  
  216. for(int i = 0; i < 24; i++)
  217. {
  218. data_pkt[2][i+4] = grn_val[i*2+97] << 4 | grn_val[i*2+96];
  219. }
  220.  
  221. for(int i = 0; i < 36; i++)
  222. {
  223. data_pkt[2][i+28] = blu_val[i*2+1] << 4 | blu_val[i*2];
  224. }
  225.  
  226. for(int i = 0; i < 36; i++)
  227. {
  228. data_pkt[3][i+4] = blu_val[i*2+73] << 4 | blu_val[i*2+72];
  229. }
  230.  
  231. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[0], 64, 1000);
  232. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[1], 64, 1000);
  233. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[2], 64, 1000);
  234. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[3], 64, 1000);
  235. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[4], 64, 1000);
  236. }
  237.  
  238.  
  239. static struct usb_device *find_device(uint16_t vendor, uint16_t product)
  240. {
  241. struct usb_bus *bus;
  242. struct usb_device *dev;
  243. struct usb_bus *busses;
  244.  
  245. usb_init();
  246. usb_find_busses();
  247. usb_find_devices();
  248.  
  249. busses = usb_get_busses();
  250.  
  251. for(bus = busses; bus; bus = bus->next)
  252. {
  253. for(dev = bus->devices; dev; dev = dev->next)
  254. {
  255. if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
  256. {
  257. return dev;
  258. }
  259. }
  260. }
  261.  
  262. return NULL;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment