1 Star 0 Fork 0

梅花三弄再回首/smbus-cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SMBus.h 4.25 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* @file SMBus.h
* @brief smbus-cpp项目头文件,包含所有类定义
* @author 梅花
* @data 2024年10月9日22:32:29
* @version 0.1
* @copyright 梅花
*/
#ifndef __SMBus_h__
#define __SMBus_h__
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <ios>
#include <string>
extern "C"
{
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}
/*
** These are required to build this module against Linux older than 2.6.23.
*/
#ifndef I2C_SMBUS_I2C_BLOCK_BROKEN
#undef I2C_SMBUS_I2C_BLOCK_DATA
#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
#define I2C_SMBUS_I2C_BLOCK_DATA 8
#endif
#define MAXPATH 16
#define ARRAY_END 2147483647
/**
* @class SMBus
* @brief a SMBus class that is (optionally) connected to the specified I2C device interface.
*
* @see https://github.com/Gadgetoid/py-smbus/blob/master/library/smbusmodule.c
*/
class SMBus
{
public:
int fd = -1; //!< open file descriptor: /dev/i2c-?, or -1
int addr = -1; //!< current client SMBus address
/**
* @brief Disconnects the object from the bus.
*/
void close();
/**
* @brief Connects the object to the specified SMBus.
*
* @param bus
*/
void open(int bus);
/**
* @brief Construct a new SMBus object
*
* @param bus
*/
SMBus(int bus = -1);
/**
* @brief Perform SMBus Quick transaction.
*
* @param addr
*/
void write_quick(int addr);
/**
* @brief Perform SMBus Read Byte transaction.
*
* @param addr
*
* @return long
*/
long read_byte(int addr);
/**
* @brief Perform SMBus Write Byte transaction.
*
* @param addr
*/
void write_byte(int addr, int val);
/**
* @brief Perform SMBus Read Byte Data transaction.
*
* @param addr
*
* @param cmd
*
* @return long
*/
long read_byte_data(int addr, int cmd);
/**
* @brief Perform SMBus Write Byte Data transaction.
*
* @param addr
*
* @param cmd
*
* @param val
*/
void write_byte_data(int addr, int cmd, int val);
/**
* @brief Perform SMBus Read Word Data transaction.
*
* @param addr
*
* @param cmd
*
* @return long
*/
long read_word_data(int addr, int cmd);
/**
* @brief Perform SMBus Write Word Data transaction.
*
* @param addr
*
* @param cmd
*
* @param val
*/
void write_word_data(int addr, int cmd, int val);
/**
* @brief Perform SMBus Process Call transaction.
*
* @param addr
*
* @param cmd
*
* @param val
*/
void process_call(int addr, int cmd, int val);
/**
* @brief Perform SMBus Read Block Data transaction.
*
* @param addr
*
* @param cmd
*
* @return long*
*/
long* read_block_data(int addr, int cmd);
/**
* @brief Perform SMBus Write Block Data transaction.
*
* @param addr
*
* @param cmd
*
* @param vals
*
* @param len
*/
void write_block_data(int addr, int cmd, long* vals, int len);
/**
* @brief Perform SMBus Block Process Call transaction.
*
* @param addr
*
* @param cmd
*
* @param vals
*
* @param len
*
* @return long*
*/
long* block_process_call(int addr, int cmd, long* vals, int len);
/**
* @brief Perform I2C Block Read transaction.
*
* @param addr
*
* @param cmd
*
* @param len
*
* @return long*
*/
long* read_i2c_block_data(int addr, int cmd, int len = 32);
/**
* @brief Perform I2C Block Write transaction.
*
* @param addr
*
* @param cmd
*
* @param vals
*
* @param len
*/
void i2c_block_data(int addr, int cmd, long* vals, int len);
/**
* @brief Whether Packet Error Codes (PEC) are enabled.
*
* @return bool
*/
bool get_pec() { return this->pec; }
/**
* @brief Set the status of PEC
*
* @param pec
*/
void set_pec(bool pec);
private:
bool pec = false; //!< !0 => Packet Error Codes enabled
void set_addr(int addr);
long* buf_to_array(__u8 const* buf, int len);
void array_to_data(long* list, int list_len, union i2c_smbus_data* data);
};
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/PlumBlossomMaid/smbus-cpp.git
git@gitee.com:PlumBlossomMaid/smbus-cpp.git
PlumBlossomMaid
smbus-cpp
smbus-cpp
master

搜索帮助