Revision 32c4f513
Added some error checking to paintboard i2c code
scout/painter/src/paint-i2c.cpp | ||
---|---|---|
33 | 33 |
* |
34 | 34 |
*/ |
35 | 35 |
|
36 |
#include <errno.h> |
|
37 |
#include <stdio.h> |
|
38 |
#include <stdlib.h> |
|
36 | 39 |
#include <unistd.h> |
37 | 40 |
#include <sys/types.h> |
38 | 41 |
#include <sys/stat.h> |
... | ... | |
67 | 70 |
* This function opens communication to I2C. |
68 | 71 |
*/ |
69 | 72 |
void i2c_start(void) { |
70 |
/* @todo error check */
|
|
73 |
/* TODO error check */
|
|
71 | 74 |
fd = open("/dev/i2c-3", O_RDWR); |
72 |
ioctl(fd, I2C_SLAVE, TRACKING_ID); |
|
75 |
if (fd < 0) { |
|
76 |
perror("/dev/i2c-3"); |
|
77 |
exit(1); |
|
78 |
} |
|
79 |
if (ioctl(fd, I2C_SLAVE, TRACKING_ID)) { |
|
80 |
perror("ioctl"); |
|
81 |
exit(1); |
|
82 |
} |
|
73 | 83 |
} |
74 | 84 |
|
75 | 85 |
/* |
... | ... | |
89 | 99 |
buf[0] = (char) dest; |
90 | 100 |
buf[1] = (char) val; |
91 | 101 |
|
92 |
write(fd, buf, 2); |
|
102 |
if (write(fd, buf, 2) < 2) { |
|
103 |
fprintf(stderr, "Warning: i2c_write failed\n"); |
|
104 |
} |
|
93 | 105 |
} |
94 | 106 |
|
95 | 107 |
/* |
... | ... | |
99 | 111 |
char buf; |
100 | 112 |
|
101 | 113 |
buf = src; |
102 |
write(fd, &buf, 1); |
|
103 |
read(fd, &buf, 1); |
|
114 |
if (write(fd, &buf, 1) < 1) { |
|
115 |
fprintf(stderr, "Warning: write in i2c_read failed\n"); |
|
116 |
} else if (read(fd, &buf, 1) < 1) { |
|
117 |
fprintf(stderr, "Warning: read in i2c_read failed\n"); |
|
118 |
} |
|
104 | 119 |
return buf; |
105 | 120 |
} |
106 | 121 |
|
Also available in: Unified diff