Skip to content

Instantly share code, notes, and snippets.

@aditya-r-m
Last active January 11, 2025 23:45

Revisions

  1. aditya-r-m revised this gist Jul 13, 2024. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions crgb.cc
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    // pieces extracted & trimmed from https://gitlab.com/CalcProgrammer1/OpenRGB
    // g++ crgb.cc -o ledoff && ./ledoff
    // Pieces extracted & trimmed from https://gitlab.com/CalcProgrammer1/OpenRGB
    // Basic usage : g++ crgb.cc -o ledoff && ./ledoff
    // For turning off RAM LEDs at startup : compile in root home directory & add "@reboot /root/ledoff" to "sudo crontab -e"

    #include <cstring>
    #include <iostream>
  2. aditya-r-m revised this gist Jul 13, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion crgb.cc
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // pieces extracted & trimmed from https://gitlab.com/CalcProgrammer1/OpenRGB
    // g++ crgb.cc -o ledoff && ./ledoff

    #include <cstring>
    #include <iostream>
    @@ -8,7 +9,7 @@

    #include <dirent.h>
    #include <fcntl.h>
    #include <string.h>
    #include <string>
    #include <unistd.h>

    typedef unsigned char u8;
  3. aditya-r-m created this gist Jul 24, 2022.
    187 changes: 187 additions & 0 deletions crgb.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,187 @@
    // pieces extracted & trimmed from https://gitlab.com/CalcProgrammer1/OpenRGB

    #include <cstring>
    #include <iostream>
    #include <linux/i2c-dev.h>
    #include <linux/i2c.h>
    #include <sys/ioctl.h>

    #include <dirent.h>
    #include <fcntl.h>
    #include <string.h>
    #include <unistd.h>

    typedef unsigned char u8;
    typedef unsigned short u16;
    typedef unsigned int u32;
    typedef int s32;

    typedef unsigned short ene_register;
    typedef unsigned char ene_dev_id;

    #define ENE_APPLY_VAL 0x01 /* Value for Apply Changes Register */
    #define ENE_SAVE_VAL 0xAA /* Value for Save Changes */

    #define I2C_SMBUS_WRITE 0
    #define I2C_SMBUS_BYTE_DATA 2
    #define I2C_SMBUS_WORD_DATA 3

    enum {
    ENE_REG_MODE = 0x8021, /* Mode Selection Register */
    ENE_REG_APPLY = 0x80A0, /* Apply Changes Register */
    };

    s32 i2c_smbus_xfer(int handle, u8 addr, char read_write, u8 command, int size,
    union i2c_smbus_data *data) {

    struct i2c_smbus_ioctl_data args;

    // Tell I2C host which slave address to transfer to
    ioctl(handle, I2C_SLAVE, addr);

    args.read_write = read_write;
    args.command = command;
    args.size = size;
    args.data = data;

    return ioctl(handle, I2C_SMBUS, &args);
    }

    s32 i2c_smbus_read_byte(int handle, u8 addr) {
    i2c_smbus_data data;
    if (i2c_smbus_xfer(handle, addr, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data)) {
    return -1;
    } else {
    return data.byte;
    }
    }

    s32 i2c_smbus_write_byte_data(int handle, u8 addr, u8 command, u8 value) {
    i2c_smbus_data data;
    data.byte = value;
    return i2c_smbus_xfer(handle, addr, I2C_SMBUS_WRITE, command,
    I2C_SMBUS_BYTE_DATA, &data);
    }

    s32 i2c_smbus_write_word_data(int handle, u8 addr, u8 command, u16 value) {
    i2c_smbus_data data;
    data.word = value;
    return i2c_smbus_xfer(handle, addr, I2C_SMBUS_WRITE, command,
    I2C_SMBUS_WORD_DATA, &data);
    }

    void ENERegisterWrite(int handle, ene_dev_id dev, ene_register reg,
    unsigned char val) {
    // Write ENE register
    i2c_smbus_write_word_data(handle, dev, 0x00,
    ((reg << 8) & 0xFF00) | ((reg >> 8) & 0x00FF));

    // Write ENE value
    i2c_smbus_write_byte_data(handle, dev, 0x01, val);
    }

    int main() {
    char device_string[1024];
    DIR *dir;
    char driver_path[512];
    struct dirent *ent;
    int test_fd;
    char path[1024];
    char buff[100];
    unsigned short pci_device, pci_vendor;
    char *ptr;

    // Start looking for I2C adapters in /sys/bus/i2c/devices/
    strcpy(driver_path, "/sys/bus/i2c/devices/");
    dir = opendir(driver_path);

    if (dir == NULL) {
    return 0;
    }

    // Loop through all entries in i2c-adapter list
    ent = readdir(dir);

    if (ent == NULL) {
    return 0;
    }

    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) {
    memset(device_string, 0x00, sizeof(device_string));
    read(test_fd, device_string, sizeof(device_string));
    device_string[strlen(device_string) - 1] = 0x00;

    close(test_fd);

    // Clear PCI Information
    pci_vendor = 0;
    pci_device = 0;
    // Get device path
    strcpy(path, driver_path);
    strcat(path, ent->d_name);
    if (ent->d_type == DT_LNK) {
    ptr = realpath(path, NULL);
    if (ptr == NULL)
    continue;

    strcpy(path, ptr);
    strcat(path, "/..");
    free(ptr);
    } else {
    strcat(path, "/device");
    }
    ptr = path + strlen(path);

    // Get PCI Vendor
    strcpy(ptr, "/vendor");
    test_fd = open(path, O_RDONLY);
    if (test_fd >= 0) {
    memset(buff, 0x00, sizeof(buff));
    read(test_fd, buff, sizeof(buff));
    buff[strlen(buff) - 1] = 0x00;
    pci_vendor = strtoul(buff, NULL, 16);
    close(test_fd);
    }

    // Get PCI Device
    strcpy(ptr, "/device");
    test_fd = open(path, O_RDONLY);
    if (test_fd >= 0) {
    memset(buff, 0x00, sizeof(buff));
    read(test_fd, buff, sizeof(buff));
    buff[strlen(buff) - 1] = 0x00;
    pci_device = strtoul(buff, NULL, 16);
    close(test_fd);
    }

    strcpy(device_string, "/dev/");
    strcat(device_string, ent->d_name);
    test_fd = open(device_string, O_RDWR);

    if (test_fd < 0) {
    ent = readdir(dir);
    } else {
    if (pci_vendor == 0x1022 && pci_device == 0x790B) {
    if (i2c_smbus_read_byte(test_fd, 0x77) > -1) {
    ENERegisterWrite(test_fd, 0x77, ENE_REG_MODE, 0);
    ENERegisterWrite(test_fd, 0x77, ENE_REG_APPLY, ENE_APPLY_VAL);
    }
    }
    }
    }
    }
    }
    ent = readdir(dir);
    }
    closedir(dir);

    return 0;
    }