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