Several improvements to the controller
Corrected syntax errors Corrected logical errors Readability improvements Build no longer segfaults changing zone
This commit is contained in:
parent
984c6bb7f5
commit
0703bcd0a8
4 changed files with 99 additions and 64 deletions
|
|
@ -12,21 +12,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
CMMP750Controller::CMMP750Controller(libusb_device_handle* dev_handle)
|
||||
CMMP750Controller::CMMP750Controller(libusb_device_handle* dev_handle, unsigned int _inAddr, unsigned int _outAddr, int _interface)
|
||||
{
|
||||
dev = dev_handle;
|
||||
inAddr = 0x82;
|
||||
outAddr = 0x03;
|
||||
inAddr = _inAddr;
|
||||
outAddr = _outAddr;
|
||||
interface = _interface;
|
||||
|
||||
strcpy(device_name, "CoolerMaster MP750");
|
||||
libusb_device* device = libusb_get_device(dev);
|
||||
int bus = libusb_get_bus_number(device);
|
||||
int bus_addr = libusb_get_device_address(device);
|
||||
int port = libusb_get_port_number(device);
|
||||
location = "Bus: " + std::to_string(bus) + " Addr: " + std::to_string(bus_addr) + " Port: " + std::to_string(port);
|
||||
|
||||
current_mode = MP750_MODE_BREATHING;
|
||||
current_speed = 0x80;
|
||||
strcpy(device_name, "Cooler Master MP750");
|
||||
|
||||
current_mode = MP750_MODE_STATIC;
|
||||
current_speed = MP750_SPEED_NORMAL;
|
||||
}
|
||||
|
||||
CMMP750Controller::~CMMP750Controller()
|
||||
{
|
||||
|
||||
libusb_release_interface(dev, interface);
|
||||
libusb_attach_kernel_driver(dev, interface);
|
||||
}
|
||||
|
||||
char* CMMP750Controller::GetDeviceName()
|
||||
|
|
@ -34,55 +42,59 @@ char* CMMP750Controller::GetDeviceName()
|
|||
return device_name;
|
||||
}
|
||||
|
||||
char* CMMP750Controller::GetSerial()
|
||||
{
|
||||
return serial;
|
||||
}
|
||||
|
||||
std::string CMMP750Controller::GetLocation()
|
||||
{
|
||||
return location;
|
||||
}
|
||||
|
||||
void CMMP750Controller::SetMode(unsigned char mode, unsigned char speed)
|
||||
{
|
||||
int actual = 0;
|
||||
unsigned char buffer[6] = { 0x00 };
|
||||
int buffer_size = (sizeof(buffer) / sizeof(buffer[0]));
|
||||
for(int i = 0; i < buffer_size; i++)
|
||||
{
|
||||
buffer[i] = colour_mode_data[mode][i];
|
||||
}
|
||||
current_mode = mode;
|
||||
current_speed = speed;
|
||||
|
||||
if(mode > 3)
|
||||
{ //If the mode is random colours set SPEED at BYTE2
|
||||
buffer[2] = speed;
|
||||
}
|
||||
else
|
||||
{ //Otherwise SPEED is BYTE5
|
||||
buffer[2] = current_red;
|
||||
buffer[3] = current_green;
|
||||
buffer[4] = current_blue;
|
||||
buffer[5] = speed;
|
||||
}
|
||||
|
||||
libusb_interrupt_transfer(dev, outAddr, buffer, buffer_size, &actual, 0);
|
||||
libusb_interrupt_transfer(dev, inAddr, buffer, buffer_size, &actual, 0);
|
||||
SendUpdate();
|
||||
}
|
||||
|
||||
void CMMP750Controller::SetColor(unsigned char red, unsigned char green, unsigned char blue)
|
||||
{
|
||||
current_red = red;
|
||||
current_green = green;
|
||||
current_blue = blue;
|
||||
|
||||
SendUpdate();
|
||||
}
|
||||
|
||||
void CMMP750Controller::SendUpdate()
|
||||
{
|
||||
int actual = 0;
|
||||
unsigned char buffer[6] = { 0x00 };
|
||||
unsigned char buffer[0x40] = { 0x00 };
|
||||
int buffer_size = (sizeof(buffer) / sizeof(buffer[0]));
|
||||
for(int i=0; i < buffer_size; i++)
|
||||
|
||||
for(int i = 0; i < CM_COLOUR_MODE_DATA_SIZE; i++)
|
||||
{
|
||||
buffer[i] = colour_mode_data[current_mode][i];
|
||||
}
|
||||
|
||||
if(current_mode > 3)
|
||||
{ //If the mode is random colours set SPEED at BYTE2
|
||||
buffer[2] = current_speed;
|
||||
if(current_mode > MP750_MODE_BREATHING)
|
||||
{
|
||||
//If the mode is random colours set SPEED at BYTE2
|
||||
buffer[CM_RED_BYTE] = speed_mode_data[current_mode][current_speed];
|
||||
}
|
||||
else
|
||||
{ //Otherwise SPEED is BYTE5
|
||||
buffer[2] = red;
|
||||
buffer[3] = green;
|
||||
buffer[4] = blue;
|
||||
buffer[5] = current_speed;
|
||||
{
|
||||
//Otherwise SPEED is BYTE5
|
||||
buffer[CM_RED_BYTE] = current_red;
|
||||
buffer[CM_GREEN_BYTE] = current_green;
|
||||
buffer[CM_BLUE_BYTE] = current_blue;
|
||||
buffer[CM_SPEED_BYTE] = speed_mode_data[current_mode][current_speed];
|
||||
}
|
||||
|
||||
libusb_interrupt_transfer(dev, outAddr, buffer, buffer_size, &actual, 0);
|
||||
libusb_interrupt_transfer(dev, inAddr, buffer, buffer_size, &actual, 0);
|
||||
//Nothing will be done with the "actual" transfer length
|
||||
libusb_interrupt_transfer(dev, outAddr, buffer, buffer_size, nullptr, CM_INTERRUPT_TIMEOUT);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,28 +20,32 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#define CM_COLOUR_MODE_DATA_SIZE (sizeof(colour_mode_data) / sizeof(colour_mode_data[0]))
|
||||
#define CM_INTERRUPT_TIMEOUT 250
|
||||
|
||||
enum
|
||||
{
|
||||
MODE_BYTE = 0,
|
||||
RED_BYTE = 2,
|
||||
GREEN_BYTE = 3,
|
||||
BLUE_BYTE = 4,
|
||||
CM_MODE_BYTE = 0,
|
||||
CM_RED_BYTE = 2,
|
||||
CM_GREEN_BYTE = 3,
|
||||
CM_BLUE_BYTE = 4,
|
||||
CM_SPEED_BYTE = 5
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
MP750_MODE_STATIC = 0x01, //Static Mode
|
||||
MP750_MODE_BLINK = 0x02, //Blinking Mode
|
||||
MP750_MODE_BREATHING = 0x03, //Breathing Mode
|
||||
MP750_MODE_COLOR_CYCLE = 0x04, //Color Cycle Mode
|
||||
MP750_MODE_BREATH_CYCLE = 0x05 //Breathing Cycle Mode
|
||||
MP750_MODE_STATIC = 0x00, //Static Mode
|
||||
MP750_MODE_BLINK = 0x01, //Blinking Mode
|
||||
MP750_MODE_BREATHING = 0x02, //Breathing Mode
|
||||
MP750_MODE_COLOR_CYCLE = 0x03, //Color Cycle Mode
|
||||
MP750_MODE_BREATH_CYCLE = 0x04 //Breathing Cycle Mode
|
||||
};
|
||||
|
||||
static unsigned char colour_mode_data[][6] =
|
||||
{
|
||||
{ 0x01, 0x04, 0x00, 0x00, 0x00, 0x00 }, /* Static */
|
||||
{ 0x02, 0x04, 0x00, 0x00, 0x00, 0x80 }, /* Blinking */
|
||||
{ 0x03, 0x04, 0x00, 0x00, 0x00, 0x80 }, /* Breathing */
|
||||
{ 0x01, 0x04, 0xFF, 0x00, 0xFF, 0x00 }, /* Static */
|
||||
{ 0x02, 0x04, 0xFF, 0x00, 0xFF, 0x80 }, /* Blinking */
|
||||
{ 0x03, 0x04, 0xFF, 0x00, 0xFF, 0x80 }, /* Breathing */
|
||||
{ 0x04, 0x04, 0x80, 0x00, 0x00, 0x00 }, /* Colour Cycle */
|
||||
{ 0x05, 0x04, 0x80, 0x00, 0x00, 0x00 } /* Colour Breath */
|
||||
};
|
||||
|
|
@ -71,19 +75,24 @@ enum
|
|||
class CMMP750Controller
|
||||
{
|
||||
public:
|
||||
CMMP750Controller(libusb_device_handle *dev_handle);
|
||||
CMMP750Controller(libusb_device_handle *dev_handle, unsigned int _inAddr, unsigned int _outAddr, int _interface);
|
||||
~CMMP750Controller();
|
||||
|
||||
char* GetDeviceName();
|
||||
char* GetSerial();
|
||||
std::string GetLocation();
|
||||
|
||||
void SetMode(unsigned char mode, unsigned char speed);
|
||||
void SetColor(unsigned char red, unsigned char green, unsigned char blue);
|
||||
|
||||
private:
|
||||
char device_name[32];
|
||||
char serial[32];
|
||||
std::string location;
|
||||
libusb_device_handle* dev;
|
||||
unsigned char inAddr;
|
||||
unsigned char outAddr;
|
||||
int interface;
|
||||
|
||||
unsigned char current_mode;
|
||||
unsigned char current_speed;
|
||||
|
|
@ -91,4 +100,6 @@ private:
|
|||
unsigned char current_red;
|
||||
unsigned char current_green;
|
||||
unsigned char current_blue;
|
||||
|
||||
void SendUpdate();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -8,9 +8,17 @@
|
|||
|
||||
#define COOLERMASTER_NUM_DEVICES (sizeof(cm_pids) / sizeof(cm_pids[ 0 ]))
|
||||
|
||||
static const unsigned int cm_pids[] =
|
||||
enum
|
||||
{
|
||||
0x0109
|
||||
CM_PID = 0,
|
||||
CM_INADDR = 1,
|
||||
CM_OUTADDR = 2,
|
||||
CM_INTERFACE = 3
|
||||
};
|
||||
|
||||
static const unsigned int cm_pids[][4] =
|
||||
{ //PID, inAddr, outAddr, interface
|
||||
{ 0x0109, 0x82, 0x03, 0x00 } //Coolermaster MP750
|
||||
};
|
||||
|
||||
/******************************************************************************************\
|
||||
|
|
@ -29,18 +37,20 @@ void DetectCoolerMasterControllers(std::vector<RGBController*>& rgb_controllers)
|
|||
for(int cm_pid_idx = 0; cm_pid_idx < COOLERMASTER_NUM_DEVICES; cm_pid_idx++)
|
||||
{
|
||||
//Look for the passed in cm_pids
|
||||
libusb_device_handle * dev = libusb_open_device_with_vid_pid(context, COOLERMASTER_VID, cm_pids[cm_pid_idx]);
|
||||
libusb_device_handle * dev = libusb_open_device_with_vid_pid(context, COOLERMASTER_VID, cm_pids[cm_pid_idx][CM_PID]);
|
||||
|
||||
if(dev)
|
||||
{
|
||||
libusb_detach_kernel_driver(dev, 1);
|
||||
libusb_claim_interface(dev, 1);
|
||||
int status = libusb_detach_kernel_driver(dev, cm_pids[cm_pid_idx][CM_INTERFACE]);
|
||||
status = libusb_claim_interface(dev, cm_pids[cm_pid_idx][CM_INTERFACE]);
|
||||
|
||||
CMMP750Controller* controller = new CMMP750Controller(dev);
|
||||
|
||||
RGBController_CMMP750Controller* rgb_controller = new RGBController_CMMP750Controller(controller);
|
||||
|
||||
rgb_controllers.push_back(rgb_controller);
|
||||
if(status == 0)
|
||||
{
|
||||
// Success: Device has been claimed
|
||||
CMMP750Controller* controller = new CMMP750Controller(dev, cm_pids[cm_pid_idx][CM_INADDR], cm_pids[cm_pid_idx][CM_OUTADDR], cm_pids[cm_pid_idx][CM_INTERFACE]);
|
||||
RGBController_CMMP750Controller* rgb_controller = new RGBController_CMMP750Controller(controller);
|
||||
rgb_controllers.push_back(rgb_controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ RGBController_CMMP750Controller::RGBController_CMMP750Controller(CMMP750Controll
|
|||
type = DEVICE_TYPE_MOUSEMAT;
|
||||
description = "Cooler Master Mousepad 750";
|
||||
version = "1.0";
|
||||
serial = "";
|
||||
location = cmmp750->GetLocation();
|
||||
|
||||
mode Static;
|
||||
Static.name = "Static";
|
||||
|
|
@ -80,7 +82,7 @@ void RGBController_CMMP750Controller::SetupZones()
|
|||
MP_zone.type = ZONE_TYPE_SINGLE;
|
||||
MP_zone.leds_min = 1;
|
||||
MP_zone.leds_max = 1;
|
||||
MP_zone.leds_count = 2;
|
||||
MP_zone.leds_count = 1;
|
||||
zones.push_back(MP_zone);
|
||||
|
||||
led MP_led;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue