FabGL
ESP32 Display Controller and Graphics Library
tsi2c.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
26 #include <stdint.h>
27 #include <stddef.h>
28 
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/queue.h"
31 #include "freertos/semphr.h"
32 
33 #include "esp32-hal.h"
34 
35 #include "fabglconf.h"
36 
37 
38 
46 namespace fabgl {
47 
48 
49 struct I2CJobInfo {
50  int32_t frequency;
51  uint8_t * buffer;
52  uint8_t address;
53  uint16_t size;
54  uint16_t timeout;
55  uint32_t readCount;
56  i2c_err_t lastError;
57 };
58 
59 
73 class I2C {
74 
75 public:
76 
82  I2C(int bus = 0);
83 
84  ~I2C();
85 
97  bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO);
98 
99  void end();
100 
114  bool write(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
115 
129  int read(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
130 
136  int getMaxBufferLength() { return 128; }
137 
138 
139 private:
140 
141  static void commTaskFunc(void * pvParameters);
142 
143 
144  i2c_t * m_i2c;
145 
146  uint8_t m_bus;
147  gpio_num_t m_SDAGPIO;
148  gpio_num_t m_SCLGPIO;
149 
150  TaskHandle_t m_commTaskHandle;
151 
152  EventGroupHandle_t m_eventGroup;
153 
154  I2CJobInfo m_jobInfo;
155 };
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 } // end of namespace
167 
168 
169 
int getMaxBufferLength()
Returns maximum size of read and write buffers.
Definition: tsi2c.h:136
bool write(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Sends a buffer to I2C bus.
Definition: tsi2c.cpp:96
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:73
I2C(int bus=0)
I2C class constructor.
Definition: tsi2c.cpp:44
Definition: canvas.cpp:31
bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO)
Initializes I2C instance associating GPIOs to I2C signals.
Definition: tsi2c.cpp:59
int read(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Receives a buffer from I2C bus.
Definition: tsi2c.cpp:121
This file contains FabGL library configuration settings, like number of supported colors...