-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi2cControl.cpp
83 lines (73 loc) · 2.16 KB
/
i2cControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2018 Chris Hamer
/* This file is part of RaspLCDDriver.
RaspLCDDriver is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RaspLCDDriver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RaspLCDDriver. If not, see <http://www.gnu.org/licenses/>.
*/
#include "i2cControl.hpp"
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
I2cControl::I2cControl(int i2cBusNumber)
{
char i2cBus[10];
sprintf(i2cBus, "/dev/i2c-%i", i2cBusNumber);
//----- OPEN THE I2C BUS -----
char *filename = (char*)i2cBus;
if ((this->file_i2c = open(filename, O_RDWR)) < 0)
{
error("Failed to open the i2c bus");
exit(1);
}
}
void I2cControl::writeByte(unsigned char address, unsigned char byte)
{
if(address != this->I2C_ADDR)
{
this->I2C_ADDR = address;
if (ioctl(this->file_i2c, I2C_SLAVE, this->I2C_ADDR) < 0)
{
error("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
}
else
{
//----- WRITE BYTES -----
unsigned char buffer[60] = {0};
buffer[0] = byte;
int length = 1; //<<< Number of bytes to write
if (write(this->file_i2c, buffer, length) != length) //write() returns the number of bytes actually written
{
error("write_i2c Failed to write to the i2c bus.\n");
}
}
}
void I2cControl::readI2c()
{
//----- READ BYTES -----
unsigned char buffer[60] = {0};
int length = 4; //<<< Number of bytes to read
if (read(this->file_i2c, buffer, length) != length) //read() returns the number of bytes actually read
{
error("readI2c Failed to read from the i2c bus.\n");
}
else
{
printf("Data read: %s\n", buffer);
}
}
void I2cControl::error(const char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}