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_linux.c
1 /*
2  MIT License
3 
4  Copyright (c) 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_linux.h"
26 #include "intf/ssd1306_interface.h"
27 #include "ssd1306_i2c_conf.h"
28 #include "ssd1306_i2c.h"
29 
30 #ifdef SSD1306_LINUX_SUPPORTED
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <linux/i2c-dev.h>
37 
38 static uint8_t s_sa = SSD1306_SA;
39 static int s_fd = -1;
40 static uint8_t s_buffer[128];
41 static uint8_t s_dataSize = 0;
42 
43 static void ssd1306_i2cStart_Linux(void)
44 {
45  s_dataSize = 0;
46 }
47 
48 static void ssd1306_i2cStop_Linux(void)
49 {
50  if (write(s_fd, s_buffer, s_dataSize) != s_dataSize)
51  {
52  fprintf(stderr, "Failed to write to the i2c bus.\n");
53  }
54  s_dataSize = 0;
55 }
56 
57 static void ssd1306_i2cSendByte_Linux(uint8_t data)
58 {
59  s_buffer[s_dataSize] = data;
60  s_dataSize++;
61  if (s_dataSize == sizeof(s_buffer))
62  {
63  /* Send function puts all data to internal buffer. *
64  * Restart transmission if internal buffer is full. */
65  ssd1306_i2cStop_Linux();
67  }
68 }
69 
70 static void ssd1306_i2cClose_Linux()
71 {
72  if (s_fd >= 0)
73  {
74  close(s_fd);
75  s_fd = -1;
76  }
77 }
78 
79 void ssd1306_i2cInit_Linux(int8_t busId, uint8_t sa)
80 {
81  char filename[20];
82  if (busId < 0)
83  {
84  busId = 1;
85  }
86  snprintf(filename, 19, "/dev/i2c-%d", busId);
87  if ((s_fd = open(filename, O_RDWR)) < 0)
88  {
89  fprintf(stderr, "Failed to open the i2c bus\n");
90  return;
91  }
92  if (sa)
93  {
94  s_sa = sa;
95  }
96  if (ioctl(s_fd, I2C_SLAVE, s_sa) < 0)
97  {
98  fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
99  return;
100  }
101  ssd1306_startTransmission = ssd1306_i2cStart_Linux;
102  ssd1306_endTransmission = ssd1306_i2cStop_Linux;
103  ssd1306_sendByte = ssd1306_i2cSendByte_Linux;
104  ssd1306_closeInterface = ssd1306_i2cClose_Linux;
107 }
108 
109 #endif
110 
111 
void(* ssd1306_sendByte)(uint8_t data)
void(* ssd1306_dataStart)()
void(* ssd1306_endTransmission)()
void ssd1306_i2cCommandStart()
void ssd1306_i2cDataStart()
void(* ssd1306_startTransmission)()
void(* ssd1306_closeInterface)()
deinitializes internal resources, allocated for interface.
void(* ssd1306_commandStart)()
#define SSD1306_SA