Code cleanup

This commit is contained in:
Adam Honse 2019-02-17 20:47:30 -06:00
parent d1a5fed46c
commit 47468e8c3f
6 changed files with 197 additions and 227 deletions

View file

@ -35,6 +35,6 @@ void AuraController::AuraRegisterWriteBlock(aura_register reg, unsigned char * d
bus->i2c_smbus_write_word_data(dev, 0x00, ((reg << 8) & 0xFF00) | ((reg >> 8) & 0x00FF));
//Write Aura block data
bus->i2c_smbus_write_block_data(dev, 0x03, 15, data);
bus->i2c_smbus_write_block_data(dev, 0x03, sz, data);
}
}

View file

@ -1,31 +1,46 @@
// OpenAuraSDK.cpp : Defines the exported functions for the DLL application.
//
/******************************************************************************************\
* *
* OpenAuraSDK.cpp *
* *
* Functions for communicating with Asus Aura devices on Windows and Linux *
* *
\******************************************************************************************/
#include "i2c_smbus.h"
//#include "i2c_smbus_piix4.h"
//#include "i2c_smbus_i801.h"
#include "i2c_smbus_linux.h"
#include "AuraController.h"
#include "OpenAuraSDK.h"
#include "i2c_smbus.h"
#include <vector>
#include <regex>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef WIN32
#include "i2c_smbus_piix4.h"
#include "i2c_smbus_i801.h"
#include "wmi.h"
#else /* WIN32 */
#include "i2c_smbus_linux.h"
#include <regex>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
//#include "wmi.h"
#endif /* WIN32 */
std::vector<AuraController *> controllers;
std::vector<i2c_smbus_interface *> busses;
#if 0
// DetectI2CBusses
//
// Detects available AMD and Intel SMBUS adapters and enumerates i2c_smbus_interface objects for them
//
#ifdef WIN32
/******************************************************************************************\
* *
* DetectI2CBusses (Windows) *
* *
* Detects available AMD and Intel SMBUS adapters and enumerates i2c_smbus_interface *
* objects for them *
* *
\******************************************************************************************/
void DetectI2CBusses()
{
i2c_smbus_interface * bus;
@ -89,16 +104,101 @@ void DetectI2CBusses()
}
}
}
}
#endif
// CreateAuraDevice
//
// Enumerates an ASUS Aura compatibler I2C device with the given address on the given bus
//
// bus - pointer to i2c_smbus_interface where Aura device is connected
// dev - I2C address of Aura device
//
} /* DetectI2CBusses() */
#else /* WIN32 */
/******************************************************************************************\
* *
* DetectI2CBusses (Linux) *
* *
* Detects available SMBUS adapters and enumerates i2c_smbus_interface objects for *
* them *
* *
\******************************************************************************************/
void DetectI2CBusses()
{
i2c_smbus_linux * bus;
char device_string[1024];
DIR * dir;
char driver_path[512];
struct dirent * ent;
int test_fd;
// Start looking for I2C adapters in /sys/class/i2c-adapter/
strcpy(driver_path, "/sys/class/i2c-adapter/");
dir = opendir(driver_path);
if(dir == NULL)
{
return;
}
// Loop through all entries in i2c-adapter list
ent = readdir(dir);
while(ent != NULL)
{
if(ent->d_type == DT_DIR || ent->d_type == DT_LNK)
{
if(strncmp(ent->d_name, "i2c-", 4) == 0)
{
strcpy(device_string, driver_path);
strcat(device_string, ent->d_name);
strcat(device_string, "/name");
test_fd = open(device_string, O_RDONLY);
if(test_fd)
{
read(test_fd, device_string, sizeof(device_string));
close(test_fd);
//ignore nvidia adapters for now
if(strncmp(device_string, "NVIDIA", 6) == 0)
{
ent = readdir(dir);
continue;
}
bus = new i2c_smbus_linux();
strcpy(bus->device_name, device_string);
strcpy(device_string, "/dev/");
strcat(device_string, ent->d_name);
test_fd = open(device_string, O_RDWR);
if (test_fd < 0)
{
delete bus;
ent = readdir(dir);
continue;
}
bus->handle = test_fd;
busses.push_back(bus);
}
}
}
ent = readdir(dir);
}
} /* DetectI2CBusses() */
#endif /* WIN32 */
/******************************************************************************************\
* *
* CreateAuraDevice *
* *
* Enumerates an ASUS Aura compatibler I2C device with the given address on the given *
* bus *
* *
* bus - pointer to i2c_smbus_interface where Aura device is connected *
* dev - I2C address of Aura device *
* *
\******************************************************************************************/
AuraController * CreateAuraDevice(i2c_smbus_interface * bus, aura_dev_id dev)
{
AuraController * aura;
@ -108,22 +208,23 @@ AuraController * CreateAuraDevice(i2c_smbus_interface * bus, aura_dev_id dev)
aura->dev = dev;
return(aura);
}
void DeleteAuraDevice(AuraController * controller)
{
delete controller;
}
} /* CreateAuraDevice() */
/******************************************************************************************\
* *
* DetectI2C *
* *
* Prints a list of all detected I2C addresses on the given bus *
* *
* bus - pointer to i2c_smbus_interface to scan *
* mode - one of AUTO, QUICK, READ, FUNC - method of access *
* *
* Code adapted from i2cdetect.c from i2c-tools Linux package *
* *
\******************************************************************************************/
// DetectI2C
//
// Prints a list of all detected I2C addresses on the given bus
//
// bus - pointer to i2c_smbus_interface to scan
// mode - one of AUTO, QUICK, READ, FUNC - method of access
//
// Code adapted from i2cdetect.c from i2c-tools Linux package
//
#define MODE_AUTO 0
#define MODE_QUICK 1
#define MODE_READ 2
@ -148,10 +249,6 @@ void DetectI2C(i2c_smbus_interface * bus, int mode)
/* Set slave address */
slave_addr = i + j;
if (slave_addr == 0x08)
{
int a = 0;
}
/* Probe this address */
switch (mode)
{
@ -181,88 +278,17 @@ void DetectI2C(i2c_smbus_interface * bus, int mode)
}
printf("\r\n");
}
}
// DumpAuraDevices
//
// Dumps known register values from all enumerated Aura devices
//
void DumpAuraDevices()
{
freopen("auradump.txt", "w", stdout);
} /* DetectI2C() */
for (unsigned int i = 0; i < controllers.size(); i++)
{
unsigned char dcolor0 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x00 );
unsigned char dcolor1 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x01 );
unsigned char dcolor2 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x02 );
unsigned char dcolor3 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x03 );
unsigned char dcolor4 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x04 );
unsigned char dcolor5 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x05 );
unsigned char dcolor6 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x06 );
unsigned char dcolor7 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x07 );
unsigned char dcolor8 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x08 );
unsigned char dcolor9 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x09 );
unsigned char dcolorA = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x0A );
unsigned char dcolorB = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x0B );
unsigned char dcolorC = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x0C );
unsigned char dcolorD = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x0D );
unsigned char dcolorE = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_DIRECT + 0x0E );
unsigned char ecolor0 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x00 );
unsigned char ecolor1 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x01 );
unsigned char ecolor2 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x02 );
unsigned char ecolor3 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x03 );
unsigned char ecolor4 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x04 );
unsigned char ecolor5 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x05 );
unsigned char ecolor6 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x06 );
unsigned char ecolor7 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x07 );
unsigned char ecolor8 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x08 );
unsigned char ecolor9 = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x09 );
unsigned char ecolorA = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x0A );
unsigned char ecolorB = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x0B );
unsigned char ecolorC = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x0C );
unsigned char ecolorD = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x0D );
unsigned char ecolorE = controllers[i]->AuraRegisterRead( AURA_REG_COLORS_EFFECT + 0x0E );
unsigned char direct = controllers[i]->AuraRegisterRead( AURA_REG_DIRECT );
unsigned char mode = controllers[i]->AuraRegisterRead( AURA_REG_MODE );
printf("Controller %d\r\n", i);
printf("Direct Mode: %d \r\n", direct );
printf("Mode Value: %d \r\n", mode );
printf("Direct Color 0: %d \r\n", dcolor0);
printf("Direct Color 1: %d \r\n", dcolor1);
printf("Direct Color 2: %d \r\n", dcolor2);
printf("Direct Color 3: %d \r\n", dcolor3);
printf("Direct Color 4: %d \r\n", dcolor4);
printf("Direct Color 5: %d \r\n", dcolor5);
printf("Direct Color 6: %d \r\n", dcolor6);
printf("Direct Color 7: %d \r\n", dcolor7);
printf("Direct Color 8: %d \r\n", dcolor8);
printf("Direct Color 9: %d \r\n", dcolor9);
printf("Direct Color A: %d \r\n", dcolorA);
printf("Dirsmbus_adapter_devect Color B: %d \r\n", dcolorB);
printf("Direct Color C: %d \r\n", dcolorC);
printf("Direct Color D: %d \r\n", dcolorD);
printf("Direct Color E: %d \r\n", dcolorE);
printf("Effect Color 0: %d \r\n", ecolor0);
printf("Effect Color 1: %d \r\n", ecolor1);
printf("Effect Color 2: %d \r\n", ecolor2);
printf("Effect Color 3: %d \r\n", ecolor3);
printf("Effect Color 4: %d \r\n", ecolor4);
printf("Effect Color 5: %d \r\n", ecolor5);
printf("Effect Color 6: %d \r\n", ecolor6);
printf("Effect Color 7: %d \r\n", ecolor7);
printf("Effect Color 8: %d \r\n", ecolor8);
printf("Effect Color 9: %d \r\n", ecolor9);
printf("Effect Color A: %d \r\n", ecolorA);
printf("Effect Color B: %d \r\n", ecolorB);
printf("Effect Color C: %d \r\n", ecolorC);
printf("Effect Color D: %d \r\n", ecolorD);
printf("Effect Color E: %d \r\n", ecolorE);
}
}
/******************************************************************************************\
* *
* DumpAuraRegisters *
* *
* Dumps register values from an Aura device *
* *
\******************************************************************************************/
void DumpAuraRegisters(AuraController * controller)
{
@ -285,91 +311,44 @@ void DumpAuraRegisters(AuraController * controller)
printf("\r\n");
}
}
// WinMain
//
// Main function. Has no defined purpose yet, but is where you can call the other functions
// in this project to test them out
//
// Currently, this program will enumerate all detected SMBus adapters and then detect
// all attached devices, similar to i2cdetect on Linux
//
//INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
} /* DumpAuraRegisters() */
/******************************************************************************************\
* *
* main/WinMain *
* *
* Main function. Has no defined purpose yet, but is where you can call the other *
* functions in this project to test them out *
* *
* Currently, this program will enumerate all detected SMBus adapters and then detect *
* all attached devices, similar to i2cdetect on Linux *
* *
\******************************************************************************************/
#ifdef WIN32
INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
#else /* WIN32 */
int main()
#endif /* WIN32 */
{
//DetectI2CBusses();
// Colors Array R B G
unsigned char colors[15] = { 255, 0, 0,
0, 255, 0,
0, 0, 255,
255, 0, 255,
0, 255, 255 };
DetectI2CBusses();
char driver_path[512];
DIR *dir;
struct dirent *ent;
strcpy(driver_path, "/sys/class/i2c-adapter/");
dir = opendir(driver_path);
if(dir == NULL)
{
}
ent = readdir(dir);
while(ent != NULL)
{
if(ent->d_type == DT_DIR || ent->d_type == DT_LNK)
{
if(strncmp(ent->d_name, "i2c-", 4) == 0)
{
int test_fd;
char device_string[1024];
strcpy(device_string, driver_path);
strcat(device_string, ent->d_name);
strcat(device_string, "/name");
test_fd = open(device_string, O_RDONLY);
if(test_fd)
{
read(test_fd, device_string, sizeof(device_string));
close(test_fd);
i2c_smbus_interface *bus;
bus = new i2c_smbus_linux();
strcpy(bus->device_name, device_string);
//ignore nvidia adapters for now
if(strncmp(device_string, "NVIDIA", 6) == 0)
{
ent = readdir(dir);
continue;
}
strcpy(device_string, "/dev/");
strcat(device_string, ent->d_name);
test_fd = open(device_string, O_RDWR);
if (test_fd < 0)
{
ent = readdir(dir);
continue;
}
((i2c_smbus_linux *)bus)->handle = test_fd;
busses.push_back(bus);
}
}
}
ent = readdir(dir);
}
for( int bus = 0; bus < busses.size(); bus++ )
for (unsigned int bus = 0; bus < busses.size(); bus++)
{
// Remap Aura-enabled RAM modules on 0x77
for(int slot = 0; slot < 8; slot++)
for (unsigned int slot = 0; slot < 8; slot++)
{
int res = busses[bus]->i2c_smbus_write_quick(0x77, I2C_SMBUS_WRITE);
if(res < 0)
if (res < 0)
{
break;
}
@ -383,11 +362,11 @@ int main()
}
// Add Aura-enabled controllers at their remapped addresses
for(int slot = 0; slot < 8; slot++)
for (unsigned int slot = 0; slot < 8; slot++)
{
int res = busses[bus]->i2c_smbus_write_quick(0x70 + slot, I2C_SMBUS_WRITE);
if(res >= 0)
if (res >= 0)
{
AuraController * new_controller = CreateAuraDevice(busses[bus], 0x70 + slot);
controllers.push_back(new_controller);
@ -397,27 +376,22 @@ int main()
// Check for Aura controller at 0x4E
int res = busses[bus]->i2c_smbus_write_quick(0x4E, I2C_SMBUS_WRITE);
if(res >= 0)
if (res >= 0)
{
AuraController * new_controller = CreateAuraDevice(busses[bus], 0x4E);
controllers.push_back(new_controller);
}
}
for (int i = 0; i < controllers.size(); i++)
for (unsigned int i = 0; i < controllers.size(); i++)
{
controllers[i]->AuraRegisterWrite(AURA_REG_DIRECT, 1);
controllers[i]->AuraRegisterWrite(AURA_REG_APPLY, AURA_APPLY_VAL);
}
for (int i = 0; i < controllers.size(); i++)
for (unsigned int i = 0; i < controllers.size(); i++)
{
for(int j = 0; j < 5; j++)
{
controllers[i]->AuraRegisterWrite(AURA_REG_COLORS_DIRECT + ( j * 3 ) + 0, 0);
controllers[i]->AuraRegisterWrite(AURA_REG_COLORS_DIRECT + ( j * 3 ) + 1, 255 );
controllers[i]->AuraRegisterWrite(AURA_REG_COLORS_DIRECT + ( j * 3 ) + 2, 0 );
}
controllers[i]->AuraRegisterWriteBlock(AURA_REG_COLORS_DIRECT, colors, 15);
}
return 1;

View file

@ -1,10 +0,0 @@
#pragma once
typedef unsigned int AuraBusDriverType;
enum
{
I2C_DRIVER_SMBUS_PIIX4,
I2C_DRIVER_SMBUS_I801,
NUM_I2C_DRIVERS
};

View file

@ -12,7 +12,6 @@ SOURCES += \
HEADERS += \
i2c_smbus.h \
i2c_smbus_linux.h \
AuraController.h \
OpenAuraSDK.h
AuraController.h
FORMS +=

View file

@ -1,3 +1,11 @@
/******************************************************************************************\
* *
* i2c_smbus.cpp *
* *
* Device-independent i2c/SMBus communication functions *
* *
\******************************************************************************************/
#include "i2c_smbus.h"
#include <string.h>

View file

@ -11,15 +11,13 @@
#ifndef I2C_SMBUS_H
#define I2C_SMBUS_H
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef int s32;
#ifdef WIN32
//Data for SMBus Messages
#define I2C_SMBUS_BLOCK_MAX 32
@ -29,7 +27,8 @@ union i2c_smbus_data
u16 word;
u8 block[I2C_SMBUS_BLOCK_MAX + 2];
};
#else
#else /* WIN32 */
#include <linux/i2c.h>