OpenRGB/Controllers/DRGBController/DRGBController.cpp

109 lines
3.3 KiB
C++

/*---------------------------------------------------------*\
| DRGBController.cpp |
| |
| Driver for DRGBmods |
| |
| Zhi Yan 25 Jun 2024 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#include <cstring>
#include "DRGBController.h"
using namespace std::chrono_literals;
DRGBController::DRGBController(hid_device* dev_handle, const char* path, unsigned short pid)
{
dev = dev_handle;
location = path;
device_pid = pid;
}
std::string DRGBController::GetLocationString()
{
return("HID: " + location);
}
std::string DRGBController::GetSerialString()
{
wchar_t serial_string[128];
int ret = hid_get_serial_number_string(dev, serial_string, 128);
if(ret != 0)
{
return("");
}
std::wstring return_wstring = serial_string;
std::string return_string(return_wstring.begin(), return_wstring.end());
return(return_string);
}
unsigned short DRGBController::GetDevicePID()
{
return(device_pid);
}
void DRGBController::SetChannelLEDs(unsigned char /*channel*/, RGBColor* /*colors*/, unsigned int /*num_colors*/)
{
}
void DRGBController::SendPacket(unsigned char* colors, unsigned int buf_packets , unsigned int Array)
{
unsigned char usb_buf[1025];
unsigned int buf_idx = 0;
memset(usb_buf, 0x00, sizeof(usb_buf));
usb_buf[0x00] = 0x00;
unsigned int HigCount = Array / 256 >= 1 ? 1 : 0;
unsigned int LowCount = Array >= 316 ? 60 : (Array % 256) ;
Array = Array <= 316 ? 0 : (Array-316);
for(unsigned int i = 0; i < buf_packets; i++)
{
usb_buf[1] = i + 100 ;
usb_buf[2] = buf_packets + 99 ;
usb_buf[3] = HigCount;
usb_buf[4] = LowCount;
buf_idx = i*1020;
for(unsigned int k=0;k<1020;k++)
{
usb_buf[k+5] = colors[buf_idx + k];
}
hid_write(dev, usb_buf, 1025);
if(Array)
{
HigCount = Array / 256 >= 1 ? 1 : 0;
LowCount = Array >= 340 ? 84 : (Array % 256) ;
Array = Array <= 340 ? 0 : (Array-316);
}
}
}
void DRGBController::SendPacketFS(unsigned char* colors, unsigned int buf_packets , bool Array)
{
unsigned char usb_buf[65];
unsigned int buf_idx = 0;
memset(usb_buf, 0x00, sizeof(usb_buf));
usb_buf[0x00] = 0x00;
if(Array)
{
for(unsigned int i = 0; i < buf_packets; i++)
{
usb_buf[1] = i == buf_packets - 1 ? 200 + i : 100 + i;
buf_idx = i*63;
for(unsigned int k=0;k<63;k++)
{
usb_buf[k+2] = colors[buf_idx + k];
}
hid_write(dev, usb_buf, 65);
}
}
else
{
for(unsigned int e=0;e<64;e++)
{
usb_buf[e+1] = colors[e];
}
hid_write(dev, usb_buf, 65);
}
}