SSD1306 OLED display driver  1.4.4
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
ssd1306_i2c_wire.cpp
1 /*
2  MIT License
3 
4  Copyright (c) 2016-2018, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
24 
25 #include "ssd1306_i2c_wire.h"
26 #include "intf/ssd1306_interface.h"
27 #include "ssd1306_i2c_conf.h"
28 #include "ssd1306_i2c.h"
29 
30 #ifdef SSD1306_WIRE_SUPPORTED
31 
32 #include <Wire.h>
33 
34 static uint8_t s_bytesWritten = 0;
35 static uint8_t s_sa = SSD1306_SA;
36 
37 static void ssd1306_i2cStart_Wire(void)
38 {
39  Wire.beginTransmission(s_sa);
40  s_bytesWritten = 0;
41 }
42 
43 static void ssd1306_i2cStop_Wire(void)
44 {
45  Wire.endTransmission();
46 }
47 
48 void ssd1306_i2cConfigure_Wire(int8_t scl, int8_t sda)
49 {
50 #if defined(ESP8266) || defined(ESP32) || defined(ESP31B)
51  if ((scl>=0) && (sda >=0))
52  {
53  Wire.begin(sda, scl);
54  }
55  else
56 #endif
57  {
58  Wire.begin();
59  }
60  #ifdef SSD1306_WIRE_CLOCK_CONFIGURABLE
61  Wire.setClock(400000);
62  #endif
63 }
64 
69 static void ssd1306_i2cSendByte_Wire(uint8_t data)
70 {
71  // Do not write too many bytes for standard Wire.h. It may become broken
72 #if defined(ESP32) || defined(ESP31B)
73  if (s_bytesWritten >= (I2C_BUFFER_LENGTH >> 4))
74 #elif defined(ARDUINO_ARCH_SAMD)
75  if (s_bytesWritten >= 64)
76 #elif defined(BUFFER_LENGTH)
77  if (s_bytesWritten >= (BUFFER_LENGTH >> 1))
78 #else
79  if (s_bytesWritten >= (USI_BUF_SIZE -2))
80 #endif
81  {
82  ssd1306_i2cStop_Wire();
83  ssd1306_i2cStart_Wire();
84  /* Commands never require many bytes. Thus assume that user tries to send data */
85  Wire.write(0x40);
86  s_bytesWritten++;
87  }
88  Wire.write(data);
89  s_bytesWritten++;
90 }
91 
92 static void ssd1306_i2cClose_Wire()
93 {
94 }
95 
96 void ssd1306_i2cInit_Wire(uint8_t sa)
97 {
98  if (sa) s_sa = sa;
99  ssd1306_startTransmission = ssd1306_i2cStart_Wire;
100  ssd1306_endTransmission = ssd1306_i2cStop_Wire;
101  ssd1306_sendByte = ssd1306_i2cSendByte_Wire;
102  ssd1306_closeInterface = ssd1306_i2cClose_Wire;
105 }
106 
107 #endif
108 
109 
void(* ssd1306_sendByte)(uint8_t data)
void(* ssd1306_dataStart)()
void(* ssd1306_endTransmission)()
void ssd1306_i2cCommandStart()
void ssd1306_i2cDataStart()
void ssd1306_i2cConfigure_Wire(int8_t scl, int8_t sda)
void(* ssd1306_startTransmission)()
void(* ssd1306_closeInterface)()
deinitializes internal resources, allocated for interface.
void ssd1306_i2cInit_Wire(uint8_t sa)
void(* ssd1306_commandStart)()
#define SSD1306_SA