SSD1306 OLED display driver  1.6.3
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
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 #if defined(SSD1306_LINUX_SUPPORTED) && !defined(__KERNEL__)
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 #if !defined(SDL_EMULATION)
36 
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <sys/ioctl.h>
40 #include <linux/i2c-dev.h>
41 #include <stdlib.h>
42 
43 static uint8_t s_sa = SSD1306_SA;
44 static int s_fd = -1;
45 static uint8_t s_buffer[128];
46 static uint8_t s_dataSize = 0;
47 
48 static void ssd1306_i2cStart_Linux(void)
49 {
50  s_dataSize = 0;
51 }
52 
53 static void ssd1306_i2cStop_Linux(void)
54 {
55  if (write(s_fd, s_buffer, s_dataSize) != s_dataSize)
56  {
57  fprintf(stderr, "Failed to write to the i2c bus: %s.\n", strerror(errno));
58  }
59  s_dataSize = 0;
60 }
61 
62 static void ssd1306_i2cSendByte_Linux(uint8_t data)
63 {
64  s_buffer[s_dataSize] = data;
65  s_dataSize++;
66  if (s_dataSize == sizeof(s_buffer))
67  {
68  /* Send function puts all data to internal buffer. *
69  * Restart transmission if internal buffer is full. */
72  ssd1306_intf.send(0x40);
73  }
74 }
75 
76 static void ssd1306_i2cSendBytes_Linux(const uint8_t *buffer, uint16_t size)
77 {
78  while (size--)
79  {
80  ssd1306_i2cSendByte_Linux(*buffer);
81  buffer++;
82  }
83 }
84 
85 static void ssd1306_i2cClose_Linux()
86 {
87  if (s_fd >= 0)
88  {
89  close(s_fd);
90  s_fd = -1;
91  }
92 }
93 
94 static void empty_function()
95 {
96 }
97 
98 static void empty_function_single_arg(uint8_t arg)
99 {
100 }
101 
102 static void empty_function_two_args(const uint8_t *arg1, uint16_t arg2)
103 {
104 }
105 
106 void ssd1306_i2cInit_Linux(int8_t busId, uint8_t sa)
107 {
108  char filename[20];
109  if (busId < 0)
110  {
111  busId = 1;
112  }
113  snprintf(filename, 19, "/dev/i2c-%d", busId);
114  ssd1306_intf.start = empty_function;
115  ssd1306_intf.stop = empty_function;
116  ssd1306_intf.close = empty_function;
117  ssd1306_intf.send = empty_function_single_arg;
118  ssd1306_intf.send_buffer = empty_function_two_args;
119  if ((s_fd = open(filename, O_RDWR)) < 0)
120  {
121  fprintf(stderr, "Failed to open the i2c bus\n");
122  return;
123  }
124  if (sa)
125  {
126  s_sa = sa;
127  }
128  if (ioctl(s_fd, I2C_SLAVE, s_sa) < 0)
129  {
130  fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
131  return;
132  }
133  ssd1306_intf.start = ssd1306_i2cStart_Linux;
134  ssd1306_intf.stop = ssd1306_i2cStop_Linux;
135  ssd1306_intf.send = ssd1306_i2cSendByte_Linux;
136  ssd1306_intf.send_buffer = ssd1306_i2cSendBytes_Linux;
137  ssd1306_intf.close = ssd1306_i2cClose_Linux;
138 }
139 
140 #else /* SDL_EMULATION */
141 
142 #include "sdl_core.h"
143 
144 static void sdl_send_bytes(const uint8_t *buffer, uint16_t size)
145 {
146  while (size--)
147  {
148  sdl_send_byte(*buffer);
149  buffer++;
150  };
151 }
152 
153 void ssd1306_i2cInit_Linux(int8_t busId, uint8_t sa)
154 {
155  sdl_core_init();
156  ssd1306_intf.spi = 0;
157  ssd1306_intf.start = sdl_send_init;
158  ssd1306_intf.stop = sdl_send_stop;
159  ssd1306_intf.send = sdl_send_byte;
160  ssd1306_intf.send_buffer = sdl_send_bytes;
161  ssd1306_intf.close = sdl_core_close;
162 }
163 
164 #endif /* SDL_EMULATION */
165 
166 #endif
167 
168 
void(* send)(uint8_t data)
void(* close)(void)
deinitializes internal resources, allocated for interface.
ssd1306_interface_t ssd1306_intf
#define SSD1306_SA
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.