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
io.h
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 
29 #ifndef _SSD1306_LINUX_IO_H_
30 #define _SSD1306_LINUX_IO_H_
31 
32 #if (defined(__linux__) || defined(__MINGW32__)) && !defined(ARDUINO)
33 
34 #if defined(__KERNEL__)
35 #include <linux/types.h>
36 #elif defined(__MINGW32__)
37 #include <windows.h>
38 #include <stdint.h>
39 #include <unistd.h>
40 #include "sdl_core.h"
41 #else
42 #include <stdint.h>
43 #include <unistd.h>
44 #endif
45 #include <time.h>
46 
47 #define LOW 0
48 #define HIGH 1
49 #define INPUT 1
50 #define OUTPUT 0
51 #define PROGMEM
52 
53 #define SSD1306_LINUX_SUPPORTED
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 static inline int digitalRead(int pin) { return LOW; };
60 static inline void pinMode(int pin, int mode) {};
61 #if defined(__KERNEL__)
62 static inline void delay(unsigned long ms) { };
63 #elif defined(__MINGW32__)
64 static inline void delay(unsigned long ms) { Sleep(ms); };
65 static inline void delayMicroseconds(unsigned long us) { Sleep((us+500)/1000); };
66 #else
67 static inline void delay(unsigned long ms) { usleep(ms*1000); };
68 static inline void delayMicroseconds(unsigned long us) { usleep(us); };
69 #endif
70 
71 #if defined(__KERNEL__)
72 static inline int analogRead(int pin) { return 0; };
73 static inline void digitalWrite(int pin, int level) {};
74 #elif defined(__MINGW32__) || defined(SDL_EMULATION)
75 static inline int analogRead(int pin) { return sdl_read_analog(pin); };
76 static inline void digitalWrite(int pin, int level) { sdl_write_digital(pin, level); };
77 #else
78 static inline int analogRead(int pin) { return 0; };
79 static inline void digitalWrite(int pin, int level) {};
80 #endif
81 
82 #if defined(__KERNEL__)
83 static inline uint32_t millis(void) { return 0; };
84 static inline uint32_t micros(void) { return 0; };
85 
86 #elif defined(__MINGW32__)
87 static inline uint32_t millis(void)
88 {
89  return GetTickCount();
90 };
91 
92 static inline uint32_t micros(void)
93 {
94  return GetTickCount()*1000;
95 };
96 
97 #else
98 static inline uint32_t millis(void)
99 {
100  struct timespec ts;
101  clock_gettime(CLOCK_MONOTONIC, &ts);
102  return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
103 };
104 
105 static inline uint32_t micros(void)
106 {
107  struct timespec ts;
108  clock_gettime(CLOCK_MONOTONIC, &ts);
109  return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
110 };
111 #endif
112 
113 
114 static inline void randomSeed(int seed) { };
115 static inline void attachInterrupt(int pin, void (*interrupt)(void), int level) { };
116 static inline uint8_t pgm_read_byte(const void *ptr) { return *((const uint8_t *)ptr); };
117 static inline uint16_t eeprom_read_word(const void *ptr) { return 0; };
118 static inline void eeprom_write_word(const void *ptr, uint16_t val) { };
119 
120 #if !defined(__MINGW32__)
121 /* For some reason defines do not work accross the libraries *
122  * Didn't yet figure out, what is the reason fo this issue */
123 //#define min(a,b) (((a)<(b))?(a):(b))
124 //#define max(a,b) (((a)>(b))?(a):(b))
125 static inline int min(int a, int b) { return a<b?a:b; };
126 static inline int max(int a, int b) { return a>b?a:b; };
127 #endif
128 
129 static inline char *utoa(unsigned int num, char *str, int radix) {
130  char temp[17]; //an int can only be 16 bits long
131  //at radix 2 (binary) the string
132  //is at most 16 + 1 null long.
133  int temp_loc = 0;
134  int digit;
135  int str_loc = 0;
136 
137  //construct a backward string of the number.
138  do {
139  digit = (unsigned int)num % radix;
140  if (digit < 10)
141  temp[temp_loc++] = digit + '0';
142  else
143  temp[temp_loc++] = digit - 10 + 'A';
144  num = ((unsigned int)num) / radix;
145  } while ((unsigned int)num > 0);
146 
147  temp_loc--;
148 
149 
150  //now reverse the string.
151  while ( temp_loc >=0 ) {// while there are still chars
152  str[str_loc++] = temp[temp_loc--];
153  }
154  str[str_loc] = 0; // add null termination.
155 
156  return str;
157 }
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #ifdef __cplusplus
164 static inline int random(int max) { return 0; };
165 static inline int random(int min, int max) { return 0; };
166 #endif
167 
168 #endif
169 
170 #endif
171 
#define max(a, b)
#define min(a, b)