My Project
 All Classes Functions Variables Pages
I2C.h
1 /*
2  * I2C.h
3  *
4  * Created on: Feb 24, 2017
5  * Author: kolban
6  */
7 
8 #ifndef MAIN_I2C_H_
9 #define MAIN_I2C_H_
10 #include <stdint.h>
11 #include <sys/types.h>
12 #include <driver/i2c.h>
13 #include <driver/gpio.h>
14 
15 
19 class I2C {
20 public:
24  static const gpio_num_t DEFAULT_SDA_PIN = GPIO_NUM_25;
25 
29  static const gpio_num_t DEFAULT_CLK_PIN = GPIO_NUM_26;
30 
34  static const uint32_t DEFAULT_CLK_SPEED = 100000;
35 
36  I2C();
37  void beginTransaction();
38  void endTransaction();
39  uint8_t getAddress() const;
40  void init(uint8_t address, gpio_num_t sdaPin = DEFAULT_SDA_PIN, gpio_num_t sclPin = DEFAULT_CLK_PIN, uint32_t clkSpeed = DEFAULT_CLK_SPEED, i2c_port_t portNum = I2C_NUM_0, bool pullup = true);
41  void read(uint8_t* bytes, size_t length, bool ack = true);
42  void read(uint8_t* byte, bool ack = true);
43  void scan();
44  void setAddress(uint8_t address);
45  void setDebug(bool enabled);
46  bool slavePresent(uint8_t address);
47  void start();
48  void stop();
49  void write(uint8_t byte, bool ack = true);
50  void write(uint8_t* bytes, size_t length, bool ack = true);
51 
52 private:
53  uint8_t m_address;
54  i2c_cmd_handle_t m_cmd;
55  bool m_directionKnown;
56  gpio_num_t m_sdaPin;
57  gpio_num_t m_sclPin;
58  i2c_port_t m_portNum;
59 
60 };
61 
62 #endif /* MAIN_I2C_H_ */
void start()
Add an I2C start request to the command stream.
Definition: I2C.cpp:240
I2C()
Create an instance of an I2C object.
Definition: I2C.cpp:25
bool slavePresent(uint8_t address)
Determine if the slave is present and responding.
Definition: I2C.cpp:224
void scan()
Scan the I2C bus looking for devices.
Definition: I2C.cpp:181
static const gpio_num_t DEFAULT_SDA_PIN
The default SDA pin.
Definition: I2C.h:24
void read(uint8_t *bytes, size_t length, bool ack=true)
Read a sequence of bytes from the slave.
Definition: I2C.cpp:134
void write(uint8_t byte, bool ack=true)
Write a single byte to the I2C slave.
Definition: I2C.cpp:275
void stop()
Add an I2C stop request to the command stream.
Definition: I2C.cpp:256
static const gpio_num_t DEFAULT_CLK_PIN
The default Clock pin.
Definition: I2C.h:29
void beginTransaction()
Begin a new I2C transaction.
Definition: I2C.cpp:41
void init(uint8_t address, gpio_num_t sdaPin=DEFAULT_SDA_PIN, gpio_num_t sclPin=DEFAULT_CLK_PIN, uint32_t clkSpeed=DEFAULT_CLK_SPEED, i2c_port_t portNum=I2C_NUM_0, bool pullup=true)
Initialize the I2C interface.
Definition: I2C.cpp:97
void setAddress(uint8_t address)
Set the address of the I2C slave against which we will be working.
Definition: I2C.cpp:204
uint8_t getAddress() const
Get the address of the I2C slave against which we are working.
Definition: I2C.cpp:84
Interface to I2C functions.
Definition: I2C.h:19
void endTransaction()
End an I2C transaction.
Definition: I2C.cpp:61
void setDebug(bool enabled)
enable or disable debugging.
Definition: I2C.cpp:214
static const uint32_t DEFAULT_CLK_SPEED
The default Clock speed.
Definition: I2C.h:34