Mercurial > hg > beaglert
annotate include/I2c.h @ 510:85ba865d3845 prerelease
moved hvresources/heavy_render.cpp to hvresources/render.cpp
| author | Giulio Moro <giuliomoro@yahoo.it> |
|---|---|
| date | Wed, 22 Jun 2016 03:28:26 +0100 |
| parents | 8a575ba3ab52 |
| children |
| rev | line source |
|---|---|
| andrewm@0 | 1 /* |
| andrewm@0 | 2 * I2c.h |
| andrewm@0 | 3 * |
| andrewm@0 | 4 * Created on: Oct 14, 2013 |
| andrewm@0 | 5 * Author: Victor Zappi |
| andrewm@0 | 6 */ |
| andrewm@0 | 7 |
| andrewm@0 | 8 #ifndef I2C_H_ |
| andrewm@0 | 9 #define I2C_H_ |
| andrewm@0 | 10 |
| andrewm@0 | 11 #include <iostream> |
| andrewm@0 | 12 #include <iomanip> |
| andrewm@0 | 13 #include <string> |
| andrewm@0 | 14 #include <stdio.h> |
| andrewm@0 | 15 #include <stdlib.h> |
| andrewm@0 | 16 #include <unistd.h> |
| andrewm@0 | 17 #include <fcntl.h> |
| andrewm@0 | 18 #include <linux/i2c.h> |
| andrewm@0 | 19 #include <linux/i2c-dev.h> |
| andrewm@0 | 20 #include <sys/ioctl.h> |
| andrewm@0 | 21 #include <stropts.h> |
| andrewm@0 | 22 |
| andrewm@0 | 23 #define MAX_BUF_NAME 64 |
| andrewm@0 | 24 |
| andrewm@0 | 25 using namespace std; |
| andrewm@0 | 26 |
| andrewm@0 | 27 |
| andrewm@0 | 28 class I2c |
| andrewm@0 | 29 { |
| andrewm@0 | 30 |
| andrewm@0 | 31 protected: |
| andrewm@0 | 32 int i2C_bus; |
| andrewm@0 | 33 int i2C_address; |
| andrewm@0 | 34 int i2C_file; |
| andrewm@0 | 35 |
| andrewm@0 | 36 public: |
| andrewm@0 | 37 int initI2C_RW(int bus, int address, int file); |
| andrewm@0 | 38 virtual int readI2C() = 0; |
| andrewm@0 | 39 int closeI2C(); |
| andrewm@0 | 40 |
| andrewm@0 | 41 virtual ~I2c(); |
| andrewm@0 | 42 |
| andrewm@0 | 43 }; |
| andrewm@0 | 44 |
| andrewm@0 | 45 |
| andrewm@0 | 46 inline int I2c::initI2C_RW(int bus, int address, int fileHnd) |
| andrewm@0 | 47 { |
| andrewm@0 | 48 i2C_bus = bus; |
| andrewm@0 | 49 i2C_address = address; |
| andrewm@0 | 50 i2C_file = fileHnd; |
| andrewm@0 | 51 |
| andrewm@0 | 52 // open I2C device as a file |
| andrewm@0 | 53 char namebuf[MAX_BUF_NAME]; |
| andrewm@0 | 54 snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2C_bus); |
| andrewm@0 | 55 |
| andrewm@0 | 56 if ((i2C_file = open(namebuf, O_RDWR)) < 0) |
| andrewm@0 | 57 { |
| andrewm@0 | 58 cout << "Failed to open " << namebuf << " I2C Bus" << endl; |
| andrewm@0 | 59 return(1); |
| andrewm@0 | 60 } |
| andrewm@0 | 61 |
| andrewm@0 | 62 // target device as slave |
| andrewm@0 | 63 if (ioctl(i2C_file, I2C_SLAVE, i2C_address) < 0){ |
| andrewm@0 | 64 cout << "I2C_SLAVE address " << i2C_address << " failed..." << endl; |
| andrewm@0 | 65 return(2); |
| andrewm@0 | 66 } |
| andrewm@0 | 67 |
| andrewm@0 | 68 return 0; |
| andrewm@0 | 69 } |
| andrewm@0 | 70 |
| andrewm@0 | 71 |
| andrewm@0 | 72 |
| andrewm@0 | 73 inline int I2c::closeI2C() |
| andrewm@0 | 74 { |
| andrewm@0 | 75 if(close(i2C_file)>0) |
| andrewm@0 | 76 { |
| andrewm@0 | 77 cout << "Failed to close file "<< i2C_file << endl; |
| andrewm@0 | 78 return 1; |
| andrewm@0 | 79 } |
| andrewm@0 | 80 return 0; |
| andrewm@0 | 81 } |
| andrewm@0 | 82 |
| andrewm@0 | 83 |
| andrewm@0 | 84 inline I2c::~I2c(){} |
| andrewm@0 | 85 |
| andrewm@0 | 86 |
| andrewm@0 | 87 #endif /* I2C_H_ */ |
