Initial driver for Ducky One 2 keyboard

This commit is contained in:
Adam Honse 2020-07-05 03:37:34 -05:00
parent 6492852627
commit 343c285321
7 changed files with 451 additions and 0 deletions

View file

@ -0,0 +1,187 @@
/*-----------------------------------------*\
| DuckyKeyboardController.cpp |
| |
| Driver for Ducky RGB keyboardlighting |
| controller |
| |
| Adam Honse (CalcProgrammer1) 7/4/2020 |
\*-----------------------------------------*/
#include <cstring>
#include "DuckyKeyboardController.h"
DuckyKeyboardController::DuckyKeyboardController(hid_device* dev_handle)
{
dev = dev_handle;
SendInitialize();
}
DuckyKeyboardController::~DuckyKeyboardController()
{
}
void DuckyKeyboardController::SendColors
(
unsigned char* color_data,
unsigned int color_data_size
)
{
unsigned int bytes_sent;
unsigned char* color_data_ptr = color_data;
SendInitializeColorPacket();
for(int i = 0; i < 8; i++)
{
bytes_sent = SendColorDataPacket(i, color_data_ptr, color_data_size);
color_data_ptr += bytes_sent;
color_data_size -= bytes_sent;
}
SendTerminateColorPacket();
}
void DuckyKeyboardController::SendInitialize()
{
char usb_buf[65];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Initialize Direct Mode packet |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x00;
usb_buf[0x01] = 0x41;
usb_buf[0x02] = 0x01;
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 65);
}
void DuckyKeyboardController::SendInitializeColorPacket()
{
char usb_buf[65];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Initialize Color packet |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x00;
usb_buf[0x01] = 0x56;
usb_buf[0x02] = 0x81;
usb_buf[0x05] = 0x01;
usb_buf[0x09] = 0x08;
usb_buf[0x0D] = 0xAA;
usb_buf[0x0E] = 0xAA;
usb_buf[0x0F] = 0xAA;
usb_buf[0x10] = 0xAA;
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 65);
}
unsigned int DuckyKeyboardController::SendColorDataPacket
(
unsigned char packet_id,
unsigned char* color_data,
unsigned int color_size
)
{
unsigned int bytes_sent;
char usb_buf[65];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Color Data packet |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x00;
usb_buf[0x01] = 0x56;
usb_buf[0x02] = 0x83;
usb_buf[0x03] = packet_id;
if(packet_id == 0x00)
{
usb_buf[0x05] = 0x01;
usb_buf[0x09] = 0x80;
usb_buf[0x0A] = 0x01;
usb_buf[0x0C] = 0xC1;
usb_buf[0x11] = 0xFF;
usb_buf[0x12] = 0xFF;
usb_buf[0x13] = 0xFF;
usb_buf[0x14] = 0xFF;
}
/*-----------------------------------------------------*\
| Copy in color data |
\*-----------------------------------------------------*/
if(packet_id == 0x00)
{
bytes_sent = 65 - 0x19;
if(color_size < bytes_sent)
{
bytes_sent = color_size;
}
memcpy(&usb_buf[0x19], color_data, bytes_sent);
}
else
{
bytes_sent = 65 - 0x05;
if(color_size < bytes_sent)
{
bytes_sent = color_size;
}
memcpy(&usb_buf[0x05], color_data, bytes_sent);
}
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 65);
return(bytes_sent);
}
void DuckyKeyboardController::SendTerminateColorPacket()
{
char usb_buf[65];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up Terminate Color packet |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x00;
usb_buf[0x01] = 0x51;
usb_buf[0x02] = 0x28;
usb_buf[0x05] = 0xFF;
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev, (unsigned char *)usb_buf, 65);
}

View file

@ -0,0 +1,41 @@
/*-----------------------------------------*\
| DuckyKeyboardController.h |
| |
| Definitions and types for Ducky RGB |
| keyboard lighting controller |
| |
| Adam Honse (CalcProgrammer1) 7/4/2020 |
\*-----------------------------------------*/
#include "RGBController.h"
#include <string>
#include <hidapi/hidapi.h>
#pragma once
class DuckyKeyboardController
{
public:
DuckyKeyboardController(hid_device* dev_handle);
~DuckyKeyboardController();
void SendColors
(
unsigned char* color_data,
unsigned int color_data_size
);
private:
hid_device* dev;
void SendInitialize();
void SendInitializeColorPacket();
unsigned int SendColorDataPacket
(
unsigned char packet_id,
unsigned char* color_data,
unsigned int color_size
);
void SendTerminateColorPacket();
};

View file

@ -0,0 +1,81 @@
#include "DuckyKeyboardController.h"
#include "RGBController.h"
#include "RGBController_DuckyKeyboard.h"
#include <vector>
#include <hidapi/hidapi.h>
/*-----------------------------------------------------*\
| Ducky vendor ID |
\*-----------------------------------------------------*/
#define DUCKY_VID 0x04D9
/*-----------------------------------------------------*\
| Keyboard product IDs |
\*-----------------------------------------------------*/
#define DUCKY_SHINE_7_ONE_2_RGB_PID 0x0348
#define DUCKY_ONE_2_RGB_TKL_PID 0x0356
typedef struct
{
unsigned short usb_vid;
unsigned short usb_pid;
unsigned char usb_interface;
const char * name;
} ducky_device;
#define DUCKY_NUM_DEVICES (sizeof(device_list) / sizeof(device_list[ 0 ]))
static const ducky_device device_list[] =
{
/*-----------------------------------------------------------------------------------------------------*\
| Keyboards |
\*-----------------------------------------------------------------------------------------------------*/
{ DUCKY_VID, DUCKY_SHINE_7_ONE_2_RGB_PID, 1, "Ducky Shine 7/Ducky One 2 RGB" },
{ DUCKY_VID, DUCKY_ONE_2_RGB_TKL_PID, 1, "Ducky One 2 RGB TKL" },
};
/******************************************************************************************\
* *
* DetectDuckyKeyboardControllers *
* *
* Tests the USB address to see if a Ducky RGB Keyboard controller exists there. *
* *
\******************************************************************************************/
void DetectDuckyKeyboardControllers(std::vector<RGBController*>& rgb_controllers)
{
hid_device_info* info;
hid_device* dev;
hid_init();
for(std::size_t device_idx = 0; device_idx < DUCKY_NUM_DEVICES; device_idx++)
{
dev = NULL;
info = hid_enumerate(device_list[device_idx].usb_vid, device_list[device_idx].usb_pid);
//Look for Ducky RGB Peripheral
while(info)
{
if((info->vendor_id == device_list[device_idx].usb_vid)
&&(info->product_id == device_list[device_idx].usb_pid)
&&(info->interface_number == device_list[device_idx].usb_interface))
{
dev = hid_open_path(info->path);
if( dev )
{
DuckyKeyboardController* controller = new DuckyKeyboardController(dev);
RGBController_DuckyKeyboard* rgb_controller = new RGBController_DuckyKeyboard(controller);
rgb_controller->name = device_list[device_idx].name;
rgb_controllers.push_back(rgb_controller);
}
}
info = info->next;
}
}
}