mirror of
https://github.com/miskcoo/ugreen_dx4600_leds_controller.git
synced 2025-07-22 20:10:36 +02:00
Squashed commit of the following:
commit 48ad47c2cb990c7550325e798064cfab612c3e7e
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Fri Jun 7 01:51:01 2024 +0800
update README.md
commit 576cc1e21d629e83ac589cd7f806698814da4858
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Fri Jun 7 01:23:31 2024 +0800
update makefiles
commit 577e2fc326df4d8edc8c303a89bde89ceca90949
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Fri Jun 7 00:29:26 2024 +0800
change the i2c device name
commit 303a05e1f4695dcc0bc117c412b0e668dd7bb2a5
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 22:46:18 2024 +0800
add the status attribute
commit ca1a51522b9ad28b04dc12d6012994bc6dce022c
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 22:34:33 2024 +0800
fix including filename
commit 4684bd1aaaa0de089537686d65caee4352df3bcf
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 22:18:11 2024 +0800
add dkms.conf
commit 4925eec10259c5c626c9459a65a7abeb115c4050
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 19:35:37 2024 +0800
Update the manual of the kernel module
commit e07f1570efab507221092d9f2d09d6b98e32f2eb
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 16:25:30 2024 +0800
add a netdev monitoring script
commit 813884f0bd928424b3e5be9053a7cc9289ab2263
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 15:33:28 2024 +0800
support blink and breath
commit 991db7c11521a2b7e2f841bf5916ff58003b951a
Merge: a4e540c c3e6324
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 13:35:16 2024 +0800
Merge branch 'master' into kmod
commit a4e540cbbde41d6a8570881a85bbfebab9def1ad
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Thu Jun 6 13:20:28 2024 +0800
add a script of monitoring diskio
commit 5b65173ec68d2a9ebc952d120935e0f533f6760f
Author: Yuhao Zhou <miskcoo@gmail.com>
Date: Wed Jun 5 23:26:09 2024 +0800
add a kernel driver of leds
101 lines
2.2 KiB
C++
101 lines
2.2 KiB
C++
|
|
#include <linux/i2c-dev.h>
|
|
#include <linux/i2c.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
#include "i2c.h"
|
|
|
|
|
|
i2c_device_t::~i2c_device_t() {
|
|
if (_fd) close(_fd);
|
|
}
|
|
|
|
int i2c_device_t::start(const char *filename, uint16_t addr) {
|
|
_fd = open(filename, O_RDWR);
|
|
|
|
if (_fd < 0) {
|
|
int rc = _fd;
|
|
_fd = 0;
|
|
return rc;
|
|
}
|
|
|
|
int rc = ioctl(_fd, I2C_SLAVE, addr);
|
|
if (rc < 0) {
|
|
close(_fd);
|
|
_fd = 0;
|
|
return rc;
|
|
}
|
|
|
|
return 0;
|
|
};
|
|
|
|
std::vector<uint8_t> i2c_device_t::read_block_data(uint8_t command, uint32_t size) {
|
|
if (!_fd) return { };
|
|
|
|
if (size > I2C_SMBUS_BLOCK_MAX)
|
|
return { };
|
|
|
|
i2c_smbus_data smbus_data;
|
|
smbus_data.block[0] = size;
|
|
|
|
i2c_smbus_ioctl_data ioctl_data;
|
|
ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA;
|
|
ioctl_data.read_write = I2C_SMBUS_READ;
|
|
ioctl_data.command = command;
|
|
ioctl_data.data = &smbus_data;
|
|
|
|
int rc = ioctl(_fd, I2C_SMBUS, &ioctl_data);
|
|
|
|
if (rc < 0) return { };
|
|
|
|
std::vector<uint8_t> data;
|
|
for (uint32_t i = 0; i < size; ++i)
|
|
data.push_back(smbus_data.block[i + 1]);
|
|
|
|
return data;
|
|
}
|
|
|
|
int i2c_device_t::write_block_data(uint8_t command, std::vector<uint8_t> data) {
|
|
if (!_fd) return -1;
|
|
|
|
uint32_t size = data.size();
|
|
if (size > I2C_SMBUS_BLOCK_MAX)
|
|
size = I2C_SMBUS_BLOCK_MAX;
|
|
|
|
i2c_smbus_data smbus_data;
|
|
smbus_data.block[0] = size;
|
|
for (uint32_t i = 0; i < size; ++i)
|
|
smbus_data.block[i + 1] = data[i];
|
|
|
|
i2c_smbus_ioctl_data ioctl_data;
|
|
ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA;
|
|
ioctl_data.read_write = I2C_SMBUS_WRITE;
|
|
ioctl_data.command = command;
|
|
ioctl_data.data = &smbus_data;
|
|
|
|
int rc = ioctl(_fd, I2C_SMBUS, &ioctl_data);
|
|
|
|
return rc;
|
|
}
|
|
|
|
uint8_t i2c_device_t::read_byte_data(uint8_t command) {
|
|
if (!_fd) return { };
|
|
|
|
i2c_smbus_data smbus_data;
|
|
|
|
i2c_smbus_ioctl_data ioctl_data;
|
|
ioctl_data.size = I2C_SMBUS_BYTE_DATA;
|
|
ioctl_data.read_write = I2C_SMBUS_READ;
|
|
ioctl_data.command = command;
|
|
ioctl_data.data = &smbus_data;
|
|
|
|
int rc = ioctl(_fd, I2C_SMBUS, &ioctl_data);
|
|
|
|
if (rc < 0) return { };
|
|
|
|
return smbus_data.byte & 0xff;
|
|
}
|