-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino.cpp
More file actions
69 lines (57 loc) · 1.85 KB
/
Copy patharduino.cpp
File metadata and controls
69 lines (57 loc) · 1.85 KB
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
#include "arduino.h"
//Initializes object
Arduino::Arduino(int a){
devAddr = a;
}
//Initializes the i2c connection
int Arduino::init(){
//----- OPEN THE I2C BUS -----
char *filename = (char*)"/dev/i2c-1";
if ((devFile = open(filename, O_RDWR)) < 0)
{
//ERROR HANDLING: you can check errno to see what went wrong
printf("Failed to open the i2c bus");
return -1;
}
if (ioctl(devFile, I2C_SLAVE, devAddr) < 0)
{
printf("Failed to acquire bus access and/or talk to slave.\n");
//ERROR HANDLING; you can check errno to see what went wrong
return -1;
}
return 0;
}
void Arduino::deInit(){
}
void Arduino::setServo(int angle){
if (angle >= 0 and angle <= 90){
buffer[0] = 'G';
buffer[1] = angle & 0xFF;
buffer[2] = '\r';
//write() returns the number of bytes actually written,
//if it doesn't match then an error occurred (e.g. no response from the device)
if (write(devFile, buffer, 3) != 3) {
// ERROR HANDLING: i2c transaction failed
printf("Failed to write to the i2c bus.\n");
}
}
}
float Arduino::getSonarDist(){
buffer[0] = 'S';
buffer[2] = '\r';
//write() returns the number of bytes actually written,
//if it doesn't match then an error occurred (e.g. no response from the device)
if (write(devFile, buffer, 3) != 3) {
// ERROR HANDLING: i2c transaction failed
printf("Failed to write to the i2c bus.\n");
}
if (read(devFile, buffer, 4) != 4) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device)
{
//ERROR HANDLING: i2c transaction failed
printf("Failed to read from the i2c bus.\n");
}
else
{
//handle input data
}
}