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-2020 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 #ifdef ARDUINO
27 
28 
29 #include <stdint.h>
30 #include <stddef.h>
31 
32 #include "freertos/FreeRTOS.h"
33 #include "freertos/queue.h"
34 #include "freertos/semphr.h"
35 
36 #include "esp32-hal.h"
37 
38 #include "fabglconf.h"
39 
40 
41 
49 namespace fabgl {
50 
51 
52 struct I2CJobInfo {
53  int32_t frequency;
54  uint8_t * buffer;
55  uint8_t address;
56  uint16_t size;
57  uint16_t timeout;
58  uint32_t readCount;
59  i2c_err_t lastError;
60 };
61 
62 
76 class I2C {
77 
78 public:
79 
85  I2C(int bus = 0);
86 
87  ~I2C();
88 
100  bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO);
101 
102  void end();
103 
117  bool write(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
118 
132  int read(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
133 
139  int getMaxBufferLength() { return 128; }
140 
141 
142 private:
143 
144  static void commTaskFunc(void * pvParameters);
145 
146 
147  i2c_t * m_i2c;
148 
149  uint8_t m_bus;
150  gpio_num_t m_SDAGPIO;
151  gpio_num_t m_SCLGPIO;
152 
153  TaskHandle_t m_commTaskHandle;
154 
155  EventGroupHandle_t m_eventGroup;
156 
157  I2CJobInfo m_jobInfo;
158 };
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 } // end of namespace
170 
171 
172 
173 #endif // #ifdef ARDUINO
174 
int getMaxBufferLength()
Returns maximum size of read and write buffers.
Definition: tsi2c.h:139
bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO)
Initializes I2C instance associating GPIOs to I2C signals.
Definition: tsi2c.cpp:62
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:76
I2C(int bus=0)
I2C class constructor.
Definition: tsi2c.cpp:47
int read(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Receives a buffer from I2C bus.
Definition: tsi2c.cpp:124
Definition: canvas.cpp:31
This file contains FabGL library configuration settings, like number of supported colors...
bool write(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Sends a buffer to I2C bus.
Definition: tsi2c.cpp:99