Advertisement
CalcProgrammer1

Corsair K70 RGB LED position test demo

Oct 8th, 2014
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <usb.h>
  4.  
  5. static struct usb_device *find_device(uint16_t vendor, uint16_t product);
  6.  
  7. static void update_keyboard();
  8.  
  9. static char data_pkt_0[] =
  10.   { 0x07,   0x27,   0x00,   0x00,   0xD8,   0x00,   0x00,   0x00,
  11.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  12.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  13.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  14.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  15.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  16.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
  17.     0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00 };
  18.  
  19. char red_val[144];
  20. char grn_val[144];
  21. char blu_val[144];
  22.  
  23. char data_pkt[4][64] = { 0 };
  24. struct usb_device *dev;
  25. struct usb_dev_handle *handle;
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.  
  30.     int bytes_written = 0;
  31.     int status = 0;
  32.  
  33.     printf("Searching for Corsair K70 RGB keyboard...\n");
  34.    
  35.     dev = find_device(0x1B1C, 0x1B13);
  36.  
  37.     if(dev == NULL)
  38.     {
  39.         printf("Corsair K70 RGB keyboard not detected :(\n");
  40.         return 1;
  41.     }
  42.     else
  43.     {
  44.         printf("Corsair K70 RGB keyboard detected successfully :)\n");
  45.     }    
  46.  
  47.     handle = usb_open(dev);
  48.  
  49.     if(handle == NULL)
  50.     {
  51.         printf("USB device handle did not open :(\n");
  52.         return 1;
  53.     }
  54.     else
  55.     {
  56.         printf("USB device handle opened successfully :)\n");
  57.     }
  58.  
  59.     status = usb_claim_interface(handle, 3);
  60.    
  61.     status = usb_detach_kernel_driver_np(handle, 3);
  62.    
  63.     if(status == 0)
  64.     {
  65.         printf("USB interface claimed successfully :)\n");
  66.     }
  67.     else
  68.     {
  69.         printf("USB interface claim failed with error %d :(\n", status);
  70.         return 1;
  71.     }
  72.  
  73.     for(int j = 0; j < 144; j++)
  74.     {
  75.         printf("LED %d\n", j);
  76.  
  77.         for(int k = 0; k < 3; k++)
  78.         {
  79.             for(int i = 0; i < 144; i++)
  80.             {
  81.                 if( j == i )
  82.                 {
  83.                     red_val[i] = 0x07 * ( k != 0 );
  84.                     grn_val[i] = 0x07 * ( k != 1 );
  85.                     blu_val[i] = 0x07 * ( k != 2 );
  86.                 }
  87.                 else
  88.                 {
  89.                     red_val[i] = 0x07;
  90.                     grn_val[i] = 0x07;
  91.                     blu_val[i] = 0x07;
  92.                 }
  93.             }
  94.  
  95.             update_keyboard();
  96.             usleep(333333);
  97.         }
  98.     }
  99.     return 0;
  100. }
  101.  
  102.  
  103. static void update_keyboard()
  104. {
  105.     // Perform USB control message to keyboard
  106.     //
  107.     // Request Type:  0x21
  108.     // Request:       0x09
  109.     // Value          0x0300
  110.     // Index:         0x03
  111.     // Size:          64
  112.  
  113.     data_pkt[0][0] = 0x7F;
  114.     data_pkt[0][1] = 0x01;
  115.     data_pkt[0][2] = 0x3C;
  116.  
  117.     data_pkt[1][0] = 0x7F;
  118.     data_pkt[1][1] = 0x02;
  119.     data_pkt[1][2] = 0x3C;
  120.  
  121.     data_pkt[2][0] = 0x7F;
  122.     data_pkt[2][1] = 0x03;
  123.     data_pkt[2][2] = 0x3C;
  124.  
  125.     data_pkt[3][0] = 0x7F;
  126.     data_pkt[3][1] = 0x04;
  127.     data_pkt[3][2] = 0x24;
  128.  
  129.  
  130.     for(int i = 0; i < 60; i++)
  131.     {
  132.         data_pkt[0][i+4] = red_val[i*2+1] << 4 | red_val[i*2];
  133.     }
  134.  
  135.     for(int i = 0; i < 12; i++)
  136.     {
  137.         data_pkt[1][i+4] = red_val[i*2+121] << 4 | red_val[i*2+120];
  138.     }
  139.  
  140.     for(int i = 0; i < 48; i++)
  141.     {
  142.         data_pkt[1][i+16] = grn_val[i*2+1] << 4 | grn_val[i*2];
  143.     }
  144.  
  145.     for(int i = 0; i < 24; i++)
  146.     {
  147.         data_pkt[2][i+4] = grn_val[i*2+97] << 4 | grn_val[i*2+96];
  148.     }
  149.  
  150.     for(int i = 0; i < 36; i++)
  151.     {
  152.         data_pkt[2][i+28] = blu_val[i*2+1] << 4 | blu_val[i*2];
  153.     }
  154.  
  155.     for(int i = 0; i < 36; i++)
  156.     {
  157.         data_pkt[3][i+4] = blu_val[i*2+73] << 4 | blu_val[i*2+72];
  158.     }
  159.  
  160.     usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[0], 64, 1000);
  161.     usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[1], 64, 1000);
  162.     usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[2], 64, 1000);
  163.     usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[3], 64, 1000);
  164.     usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt_0, 64, 1000);
  165. }
  166.  
  167.  
  168. static struct usb_device *find_device(uint16_t vendor, uint16_t product)
  169. {
  170.     struct usb_bus *bus;
  171.     struct usb_device *dev;
  172.     struct usb_bus *busses;
  173.  
  174.     usb_init();
  175.     usb_find_busses();
  176.     usb_find_devices();
  177.  
  178.     busses = usb_get_busses();
  179.  
  180.     for(bus = busses; bus; bus = bus->next)
  181.     {
  182.         for(dev = bus->devices; dev; dev = dev->next)
  183.         {
  184.             if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
  185.             {
  186.                 return dev;
  187.             }
  188.         }
  189.     }
  190.  
  191.     return NULL;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement