SSD1306 OLED display driver  1.4.12
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
gpio.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 #if (defined(__linux__) || defined(__MINGW32__)) && !defined(ARDUINO)
26 
27 #include "io.h"
28 
29 #ifndef __KERNEL__
30 
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #ifdef IN
39 #undef IN
40 #endif
41 #define IN 0
42 
43 #ifdef OUT
44 #undef OUT
45 #endif
46 #define OUT 1
47 
48 int gpio_export(int pin)
49 {
50  char buffer[4];
51  ssize_t bytes_written;
52  int fd;
53 
54  fd = open("/sys/class/gpio/export", O_WRONLY);
55  if (-1 == fd)
56  {
57  fprintf(stderr, "Failed to allocate gpio pin resources!\n");
58  return(-1);
59  }
60 
61  bytes_written = snprintf(buffer, sizeof(buffer), "%d", pin);
62  if (write(fd, buffer, bytes_written) < 0)
63  {
64  fprintf(stderr, "Failed to allocate gpio pin resources!\n");
65  }
66  close(fd);
67  return(0);
68 }
69 
70 int gpio_unexport(int pin)
71 {
72  char buffer[4];
73  ssize_t bytes_written;
74  int fd;
75 
76  fd = open("/sys/class/gpio/unexport", O_WRONLY);
77  if (-1 == fd)
78  {
79  fprintf(stderr, "Failed to free gpio pin resources!\n");
80  return(-1);
81  }
82 
83  bytes_written = snprintf(buffer, sizeof(buffer), "%d", pin);
84  if (write(fd, buffer, bytes_written) < 0)
85  {
86  fprintf(stderr, "Failed to free gpio pin resources!\n");
87  }
88  close(fd);
89  return(0);
90 }
91 
92 int gpio_direction(int pin, int dir)
93 {
94  static const char s_directions_str[] = "in\0out";
95 
96  char path[64];
97  int fd;
98 
99  snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
100  fd = open(path, O_WRONLY);
101  if (-1 == fd)
102  {
103  fprintf(stderr, "Failed to set gpio pin direction!\n");
104  return(-1);
105  }
106 
107  if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3))
108  {
109  fprintf(stderr, "Failed to set gpio pin direction!\n");
110  return(-1);
111  }
112 
113  close(fd);
114  return(0);
115 }
116 
117 int gpio_read(int pin)
118 {
119  char path[32];
120  char value_str[3];
121  int fd;
122 
123  snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
124  fd = open(path, O_RDONLY);
125  if (-1 == fd)
126  {
127  fprintf(stderr, "Failed to read gpio pin value!\n");
128  return(-1);
129  }
130 
131  if (-1 == read(fd, value_str, 3))
132  {
133  fprintf(stderr, "Failed to read gpio pin value!\n");
134  return(-1);
135  }
136 
137  close(fd);
138 
139  return(atoi(value_str));
140 }
141 
142 int gpio_write(int pin, int value)
143 {
144  static const char s_values_str[] = "01";
145 
146  char path[64];
147  int fd;
148 
149  snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
150  fd = open(path, O_WRONLY);
151  if (-1 == fd)
152  {
153  fprintf(stderr, "Failed to set gpio pin value!\n");
154  return(-1);
155  }
156 
157  if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1))
158  {
159  fprintf(stderr, "Failed to set gpio pin value!\n");
160  return(-1);
161  }
162 
163  close(fd);
164  return(0);
165 }
166 
167 /*
168  -1 == gpio_export(N)
169  -1 == gpio_direction(N, OUT)
170  -1 == gpio_write(N, HIGH)
171  -1 == gpio_unexport(N)
172 */
173 #endif
174 
175 #endif