-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialize.cpp
121 lines (90 loc) · 2.75 KB
/
initialize.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright (c) 2014 Daniel D. Bruce
This file is part of i2cledcontrol.
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
The developer can be found at:
If that doesn't work, contact the Rensselaer Union
Administration office and ask. Chances are they'll have a
good idea as to where the developer went. Note that no
association with Rensselaer Polytechnic Institute shall
construe any copyright obligations to Rensselaer.
*/
#include <iostream>
#include <cstdlib>
#include <stdint.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <iomanip>
#include <unistd.h>
using namespace std;
int initializechip(int file, int chip) {
int address = (chip) | 0x40;
if (ioctl(file, I2C_SLAVE, address) < 0) {
cerr << "Failed to acquire bus access and/or talk to slave..." << endl;
return 1;
}
uint8_t bytestowrite[2];
bytestowrite[0] = 0x00;
bytestowrite[1] = 0xa1;
if (write(file,bytestowrite,sizeof(bytestowrite)) != sizeof(bytestowrite)) {
return 1;
}
usleep(500);
bytestowrite[0] = 0x00;
bytestowrite[1] = 0x30;
if (write(file,bytestowrite,sizeof(bytestowrite)) != sizeof(bytestowrite)) {
return 1;
}
bytestowrite[0] = 0xfe;
bytestowrite[1] = 0x03;
if (write(file,bytestowrite,sizeof(bytestowrite)) != sizeof(bytestowrite)) {
return 1;
}
bytestowrite[0] = 0x00;
bytestowrite[1] = 0x20;
if (write(file,bytestowrite,sizeof(bytestowrite)) != sizeof(bytestowrite)) {
return 1;
}
usleep(500);
bytestowrite[0] = 0x01;
bytestowrite[1] = 0x04;
if (write(file,bytestowrite,sizeof(bytestowrite)) != sizeof(bytestowrite)) {
return 1;
}
return 0;
}
int main() {
int chips_found = 0;
cout << "Initializing PWM chips..." << endl;
int file;
const char *filename = "/dev/i2c-1";
if ((file = open(filename, O_RDWR)) < 0) {
cerr << "Failed to open the i2c bus..." << endl;
return 1;
}
for (int x = 0; x < 64; x++) {
if (x==48)
continue;
if (initializechip(file,x) == 0) {
cout << "Found chip at address " << hex << (x | 0x40) << endl;
chips_found++;
}
}
if (chips_found == 0) {
cout << "Error: no chips found!" << endl;
}
close(file);
return 0;
}