-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.c
More file actions
97 lines (83 loc) · 2.13 KB
/
i2c.c
File metadata and controls
97 lines (83 loc) · 2.13 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
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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <ncurses.h> /* sudo apt-get install libncurses5-dev */
int main(int argc, char **argv) {
int i;
int r;
int fd;
int aout;
unsigned char command[2];
unsigned char value[4];
unsigned char str[8];
useconds_t delay = 10000;
char *dev = "/dev/i2c-1";
int addr = 0x48;
int j;
int key;
initscr();
noecho();
cbreak();
nodelay(stdscr, true);
curs_set(0);
printw("PCF8591");
mvaddstr(10, 0, "Brightness");
mvaddstr(12, 0, "Temperature");
mvaddstr(14, 0, "?");
mvaddstr(16, 0, "Resistor");
mvaddstr(18, 0, "Output");
refresh();
fd = open(dev, O_RDWR);
if (fd < 0) {
perror("Opening i2c device node\n");
return 1;
}
r = ioctl(fd, I2C_SLAVE, addr);
if (r < 0) {
perror("Selecting i2c device\n");
}
command[1] = 0;
aout = 0;
while (1) {
for (i = 0; i < 4; i++) {
// command[1] = 0;
command[0] = 0x40 | ((i + 1) & 0x03); // output enable | read input i
r = write(fd, &command, 2);
usleep(delay);
// the read is always one step behind the selected input
r = read(fd, &value[i], 1);
if (r != 1) {
perror("reading i2c device\n");
}
usleep(delay);
sprintf(str, "%3d", value[i]);
mvaddstr(10 + i + i, 12, str);
sprintf(str, "%3d", command[1]);
mvaddstr(18, 12, str);
value[i] = value[i] / 4;
move(10 + i + i, 16);
for (j = 0; j < 64; j++) {
if (j < value[i]) {
addch('*');
} else {
addch(' ');
}
}
}
refresh();
key = getch();
if (key == 43) {
command[1]+=10;
} else if (key == 45) {
command[1]--;
} else if (key > -1) {
break;
}
}
endwin();
close(fd);
printf("%d\n", key);
return (0);
}