Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //By Steef
- //I2C.h
- #ifndef I2C_H
- #define I2C_H
- #include <fcntl.h>
- #include <iostream>
- #include <linux/i2c-dev.h>
- #include <string>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <vector>
- using namespace std;
- class I2C {
- private:
- const string DEFAULTDEVICE = "/dev/i2c-1";
- int slave_address;
- string devicefile;
- int fd;
- void close_fd();
- int open_fd();
- void init(string DeviceFile, int Address);
- public:
- I2C();
- I2C(string DeviceFile);
- ~I2C();
- vector<int> detect();
- int setAddress(int Address);
- int getAddress() const;
- int setDevicefile(string filename);
- int receive(vector <unsigned char> &RxBuf, int length);
- int receive(unsigned char RegisterAddress, vector <unsigned char> &RxBuf, int length);
- int send(vector <unsigned char> &TxBuf);
- int send(unsigned char Slave_address, vector<unsigned char> &TxBuf);
- int send(unsigned char value);
- };
- #endif
- // I2C.cpp
- #include "I2C.h"
- I2C::I2C()
- {
- init(DEFAULTDEVICE, -1);
- }
- I2C::I2C(string Devicefile)
- {
- init(Devicefile, -1);
- }
- I2C::~I2C()
- {
- close_fd();
- }
- void I2C::init(string Devicefile, int Address)
- {
- devicefile=Devicefile;
- slave_address = Address;
- fd = 0;
- }
- vector<int> I2C::detect()
- {
- vector<int> allDevices;
- char buf[5];
- for(int allAddress = 0x03;allAddress < 0x78 ;allAddress ++ )
- {
- setAddress(allAddress);
- if(read(fd, buf, 1) == 1)
- {
- allDevices.push_back(allAddress);
- }
- }
- return allDevices;
- }
- void I2C::close_fd()
- {
- if (fd) {
- close(fd);
- fd = 0;
- }
- }
- int I2C::open_fd()
- {
- if (fd) {
- close_fd();
- fd = 0;
- }
- if (slave_address == -1) {
- cout<<"Erreur l'adresse de l'esclave non déclaré"<<endl;
- return -1;
- }
- if ((fd = open(devicefile.c_str(), O_RDWR)) < 0) {
- cout<<"Erreur d'ouverture: " + devicefile <<endl;
- return -1;
- }
- if (ioctl(fd, I2C_SLAVE, slave_address) < 0)
- {
- cout<<"Erreur d'adresse: " + slave_address <<endl;
- return -1;
- }
- return 0;
- }
- int I2C::setAddress(int Address){
- slave_address = Address;
- return open_fd();
- }
- int I2C::getAddress() const{
- return slave_address;
- }
- int I2C::setDevicefile(string filename){
- devicefile = filename;
- return open_fd();
- }
- int I2C::receive(vector <unsigned char> &RxBuf, int length){
- RxBuf.clear();
- if (length < 1)
- cout<<"Receive method received an invalid buffer length";
- for(int i=0 ; i<length ; i++)
- RxBuf.push_back(0);
- if (!fd)
- if (open_fd() == -1)
- return -1;
- if (read(fd, RxBuf.data(), RxBuf.size()) != length)
- cout<<"Erreur de lecture I2C ! Address: " + slave_address << " dev file: " + devicefile <<endl;
- return 1;
- }
- int I2C::receive(unsigned char RegisterAddress, vector <unsigned char> &RxBuf, int length){
- RxBuf.clear();
- if (length < 1)
- cout<<"Receive method received an invalid buffer length";
- for(int i=0 ; i<length ; i++)
- RxBuf.push_back(0);
- if (!fd)
- if (open_fd() == -1)
- return -1;
- if (write(fd, &RegisterAddress, 1) != 1)
- cout<<"Erreur d'ecriture I2C !";
- if (read(fd, RxBuf.data(), RxBuf.size()) != length)
- cout<<"Erreur de lecture I2C ! Address: " + slave_address << " dev file: " + devicefile <<endl;
- return 1;
- }
- int I2C::send(vector<unsigned char> &TxBuf){
- if (TxBuf.size() == 0)
- cout<<"Erreur TxBuf est vide"<<endl;
- if (!fd)
- if (open_fd() == -1)
- return -1;
- if(write(fd, TxBuf.data(), TxBuf.size()) != TxBuf.size())
- cout<<"Erreur d'ecriture I2C !"<<endl;
- TxBuf.clear();
- return 1;
- }
- int I2C::send(unsigned char Slave_address, vector<unsigned char> &TxBuf){
- setAddress(static_cast<int>(Slave_address));
- if (TxBuf.size() == 0)
- cout<<"Erreur TxBuf est vide"<<endl;
- if (!fd)
- if (open_fd() == -1)
- return -1;
- if(write(fd, TxBuf.data(), TxBuf.size()) != TxBuf.size())
- cout<<"Erreur d'ecriture I2C !"<<endl;
- TxBuf.clear();
- return 1;
- }
- int I2C::send(unsigned char value){
- if (!fd)
- if (open_fd() == -1)
- return -1;
- if(write(fd, &value, 1) != 1)
- cout<<"Erreur d'ecriture I2C! "<<endl;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment