-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi2cledcontrol.cpp
103 lines (78 loc) · 2.58 KB
/
i2cledcontrol.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
/*
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 <unistd.h>
using namespace std;
#define LED0_ON_L 0x6
#define LED0_ON_H 0x7
#define LED0_OFF_L 0x8
#define LED0_OFF_H 0x9
//there are 16 "channels" on each "board"
//there are 3 "channels" per "strip"
//each "board" has one PWM chip
int setPWM(int channel, int value, int file) {
int address = (channel/16) | 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[5];
bytestowrite[0] = LED0_ON_L+4*(channel%16);
bytestowrite[1] = 0;
bytestowrite[2] = 0;
bytestowrite[3] = value*16;
bytestowrite[4] = (value*16) >> 8;
if (write(file,bytestowrite,5) != 5) {
cerr << "Failed to write to the i2c bus..." << endl;
return 1;
}
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
cerr << "Incorrect number of arguments..." << endl;
return 1;
}
int strip = atoi(argv[1]);
long colors = strtol(argv[2], NULL, 16);
uint8_t red = colors >> 16;
uint8_t green = (colors >> 8) & 0xff;
uint8_t blue = colors & 0xff;
//cout << "R=" << int(red) << " G=" << int(green) << " B=" << int(blue) << 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;
}
setPWM(strip*3,red,file);
setPWM(strip*3+1,green,file);
setPWM(strip*3+2,blue,file);
close(file);
return 0;
}