LCDGFX LCD display driver  1.1.5
This library is developed to control SSD1306/SSD1325/SSD1327/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
io.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2022, 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 _LCD_HAL_IO_H_
30 #define _LCD_HAL_IO_H_
31 
41 #include "UserSettings.h"
42 #include "interface.h"
43 
44 #if defined(ARDUINO) || defined(PARTICLE_USER_MODULE)
45 #include "arduino/io.h"
46 
47 #ifdef __cplusplus
48 #include "arduino/arduino_spi.h"
49 #include "arduino/arduino_wire.h"
50 #include "avr/spi_avr.h"
51 #include "avr/i2c_twi.h"
52 #include "avr/spi_usi.h"
53 #include "avr/i2c_embedded.h"
54 #include "esp/esp32_i2c.h"
55 #include "esp/esp32_spi.h"
56 #include "esp/esp8266_i2c.h"
57 #include "esp/esp8266_spi.h"
58 #endif
59 
60 #elif defined(__AVR__) && !defined(ARDUINO)
61 #include "avr/io.h"
62 #ifdef __cplusplus
63 #include "avr/spi_avr.h"
64 #include "avr/i2c_twi.h"
65 #include "avr/spi_usi.h"
66 #include "avr/i2c_embedded.h"
67 #endif
68 #elif defined(__XTENSA__) && !defined(ARDUINO)
69 #include "esp/io.h"
70 #ifdef __cplusplus
71 #include "esp/esp32_i2c.h"
72 #include "esp/esp32_spi.h"
73 #include "esp/esp8266_i2c.h"
74 #include "esp/esp8266_spi.h"
75 #endif
76 #elif defined(STM32F1) || defined(STM32F2) || defined(STM32F4)
77 #include "stm32/io.h"
78 #elif defined(__linux__) || defined(__APPLE__)
79 #include "linux/io.h"
80 #ifdef __cplusplus
81 #include "linux/linux_i2c.h"
82 #include "linux/linux_spi.h"
83 #include "linux/sdl_i2c.h"
84 #include "linux/sdl_spi.h"
85 #endif
86 #elif defined(__MINGW32__)
87 #include "mingw/io.h"
88 #ifdef __cplusplus
89 #include "linux/sdl_i2c.h"
90 #include "linux/sdl_spi.h"
91 #endif
92 #elif defined(PICO_BOARD)
93 #include "pico/io.h"
94 #ifdef __cplusplus
95 #include "pico/pico_spi.h"
96 #include "pico/pico_i2c.h"
97 #endif
98 #else
99 #warning "Platform is not supported. Use template to add support"
100 #endif
101 
102 #include <stdint.h>
103 
104 #ifndef LCDINT_TYPES_DEFINED
105 
106 #define LCDINT_TYPES_DEFINED
107 
108 typedef int lcdint_t;
110 typedef unsigned int lcduint_t;
111 #endif
112 
114 #define ssd1306_swap_data(a, b, type) \
115  { \
116  type t = a; \
117  a = b; \
118  b = t; \
119  }
120 
122 // HAL Layer functions
124 
125 #ifndef DOXYGEN_SHOULD_SKIP_THIS
126 #define ARDUINO_COMPAT
127 #endif
128 
129 #if defined(ARDUINO)
130 
131 #define LCD_LOW LOW
132 #define LCD_HIGH HIGH
133 #define LCD_GPIO_INPUT INPUT
134 #define LCD_GPIO_OUTPUT OUTPUT
135 #define LCD_GPIO_INPUT_PULLUP INPUT_PULLUP
136 #define LCD_GPIO_INPUT_PULLDOWN INPUT_PULLDOWN
137 #define LCD_PROGMEM PROGMEM
138 
139 #define lcd_gpioRead digitalRead
140 #define lcd_gpioMode pinMode
141 #define lcd_adcRead analogRead
142 
143 #ifdef __cplusplus
144 extern "C"
145 {
146 #endif
147 
148 #ifndef DOXYGEN_SHOULD_SKIP_THIS
149  void lcd_registerGpioEvent(int pin, void (*on_pin_change)(void *), void *arg);
150  void lcd_unregisterGpioEvent(int pin);
151 #endif
152 
153  void lcd_gpioWrite(int pin, int level);
154 
155 #ifdef __cplusplus
156 }
157 #endif
158 
159 #define lcd_pgmReadByte pgm_read_byte
160 #define lcd_eepromReadWord eeprom_read_word
161 #define lcd_eepromWriteWord eeprom_write_word
162 
163 #define lcd_millis millis
164 #define lcd_micros micros
165 #define lcd_delay delay
166 #define lcd_delayUs delayMicroseconds
167 #define lcd_attachInterrupt attachInterrupt
168 
169 #define lcd_random random
170 #define lcd_randomSeed randomSeed
171 
172 #else
173 
175 #define LCD_LOW 0
176 
177 #define LCD_HIGH 1
178 
179 #define LCD_GPIO_INPUT 0
180 
181 #define LCD_GPIO_OUTPUT 1
182 
183 #define LCD_GPIO_INPUT_PULLUP 2
184 
185 #define LCD_GPIO_INPUT_PULLDOWN 3
186 // LCD_PROMEM is platform specific definition
187 #ifndef LCD_PROGMEM
188 
189 #define LCD_PROGMEM PROGMEM
190 #endif
191 
192 #ifdef __cplusplus
193 extern "C"
194 {
195 #endif
196 
202  void lcd_gpioMode(int pin, int mode);
203 
209  int lcd_gpioRead(int pin);
210 
216  void lcd_gpioWrite(int pin, int level);
217 
218 #ifndef DOXYGEN_SHOULD_SKIP_THIS
219  void lcd_registerGpioEvent(int pin, void (*on_pin_change)(void *), void *arg);
220  void lcd_unregisterGpioEvent(int pin);
221 #endif
222 
229  int lcd_adcRead(int pin);
230 
234  uint32_t lcd_millis(void);
235 
239  uint32_t lcd_micros(void);
240 
245  void lcd_randomSeed(int seed);
246 
253  void lcd_attachInterrupt(int pin, void (*interrupt)(), int level);
254 
259  void lcd_delay(unsigned long ms);
260 
265  void lcd_delayUs(unsigned long us);
266 
273  uint8_t lcd_pgmReadByte(const void *ptr);
274 
280  uint16_t lcd_eepromReadWord(const void *ptr);
281 
287  void lcd_eepromWriteWord(const void *ptr, uint16_t val);
288 
289 #ifdef __cplusplus
290 }
291 #endif
292 
293 #ifdef __cplusplus
294 
299 int lcd_random(int max);
300 
306 int lcd_random(int min, int max);
307 #endif
308 
309 #ifdef ARDUINO_COMPAT
310 
311 #define LOW LCD_LOW
312 
313 #define HIGH LCD_HIGH
314 
315 #define INPUT LCD_GPIO_INPUT
316 
317 #define OUTPUT LCD_GPIO_OUTPUT
318 
319 #define INPUT_PULLUP LCD_GPIO_INPUT_PULLUP
320 
321 #define INPUT_PULLDOWN LCD_GPIO_INPUT_PULLDOWN
322 #ifndef PROGMEM
323 
324 #define PROGMEM LCD_PROGMEM
325 #endif
326 
328 #define digitalRead lcd_gpioRead
329 
330 #define digitalWrite lcd_gpioWrite
331 
332 #define pinMode lcd_gpioMode
333 
334 #define analogRead lcd_adcRead
335 
336 #if !defined(__AVR__)
337 
338 #define pgm_read_byte lcd_pgmReadByte
339 
340 #define eeprom_read_word lcd_eepromReadWord
341 
342 #define eeprom_write_word lcd_eepromWriteWord
343 #endif
344 
346 #define millis lcd_millis
347 
348 #define micros lcd_micros
349 
350 #define delay lcd_delay
351 
352 #define delayMicroseconds lcd_delayUs
353 
355 #define random lcd_random
356 
357 #define randomSeed lcd_randomSeed
358 
359 #endif
360 
361 #endif
362 
363 #ifdef __cplusplus
364 
366 // I2C PLATFORM
368 
369 #if defined(CONFIG_ARDUINO_I2C_AVAILABLE) && defined(CONFIG_ARDUINO_I2C_ENABLE)
370 
373 class PlatformI2c: public ArduinoI2c
374 {
375 public:
380  explicit PlatformI2c(const SPlatformI2cConfig &config)
381  : ArduinoI2c(config.scl, config.sda, config.addr)
382  {
383  }
384 };
385 
386 #elif defined(CONFIG_TWI_I2C_AVAILABLE) && defined(CONFIG_TWI_I2C_ENABLE)
387 
391 class PlatformI2c: public TwiI2c
392 {
393 public:
398  explicit PlatformI2c(const SPlatformI2cConfig &config)
399  : TwiI2c(config.addr)
400  {
401  }
402 };
403 
404 #elif defined(CONFIG_LINUX_I2C_AVAILABLE) && defined(CONFIG_LINUX_I2C_ENABLE)
405 
406 #if defined(SDL_EMULATION)
407 
410 class PlatformI2c: public SdlI2c
411 {
412 public:
417  explicit PlatformI2c(const SPlatformI2cConfig &config)
418  : SdlI2c(config.scl, config.sda, config.addr)
419  {
420  }
421 };
422 #else
423 
426 class PlatformI2c: public LinuxI2c
427 {
428 public:
433  explicit PlatformI2c(const SPlatformI2cConfig &config)
434  : LinuxI2c(config.busId, config.addr)
435  {
436  }
437 };
438 #endif
439 
440 #elif defined(CONFIG_ESP32_I2C_AVAILABLE) && defined(CONFIG_ESP32_I2C_ENABLE)
441 
445 class PlatformI2c: public EspI2c
446 {
447 public:
452  explicit PlatformI2c(const SPlatformI2cConfig &config)
453  : EspI2c(config.busId, config.addr, config.scl, config.sda, 400000)
454  {
455  }
456 };
457 
458 #elif defined(CONFIG_ESP8266_I2C_AVAILABLE) && defined(CONFIG_ESP8266_I2C_ENABLE)
459 
463 class PlatformI2c: public EspI2c
464 {
465 public:
470  explicit PlatformI2c(const SPlatformI2cConfig &config)
471  : EspI2c(config.busId, config.addr, config.scl, config.sda, 400000)
472  {
473  }
474 };
475 
476 #elif defined(CONFIG_SOFTWARE_I2C_AVAILABLE) && defined(CONFIG_SOFTWARE_I2C_ENABLE)
477 
481 class PlatformI2c: public SoftwareI2c
482 {
483 public:
488  explicit PlatformI2c(const SPlatformI2cConfig &config)
489  : SoftwareI2c(config.scl, config.sda, config.addr)
490  {
491  }
492 };
493 
494 #elif defined(PICO_BOARD)
495 
499 class PlatformI2c: public PicoI2c
500 {
501 public:
506  explicit PlatformI2c(const SPlatformI2cConfig &config)
507  : PicoI2c(config.scl, config.sda, config.addr)
508  {
509  }
510 };
511 
512 #else
513 
514 #error "Platform not supported"
515 
516 #endif
517 
519 // SPI PLATFORM
521 
522 #if defined(CONFIG_AVR_SPI_AVAILABLE) && defined(CONFIG_AVR_SPI_ENABLE)
523 
527 class PlatformSpi: public AvrSpi
528 {
529 public:
534  explicit PlatformSpi(const SPlatformSpiConfig &config)
535  : AvrSpi(config.cs, config.dc, config.frequency)
536  {
537  }
538 };
539 
540 #elif defined(CONFIG_ARDUINO_SPI_AVAILABLE) && defined(CONFIG_ARDUINO_SPI_ENABLE)
541 
545 class PlatformSpi: public ArduinoSpi
546 {
547 public:
552  explicit PlatformSpi(const SPlatformSpiConfig &config)
553  : ArduinoSpi(config.cs, config.dc, config.scl, config.sda, config.frequency)
554  {
555  }
556 };
557 
558 #elif defined(CONFIG_LINUX_SPI_AVAILABLE) && defined(CONFIG_LINUX_SPI_ENABLE)
559 
560 #if defined(SDL_EMULATION)
561 
564 class PlatformSpi: public SdlSpi
565 {
566 public:
571  explicit PlatformSpi(const SPlatformSpiConfig &config)
572  : SdlSpi(config.dc)
573  {
574  }
575 };
576 #else
577 
580 class PlatformSpi: public LinuxSpi
581 {
582 public:
587  explicit PlatformSpi(const SPlatformSpiConfig &config)
588  : LinuxSpi(config.busId, config.devId, config.dc, config.frequency)
589  {
590  }
591 };
592 #endif
593 
594 #elif defined(CONFIG_ESP32_SPI_AVAILABLE) && defined(CONFIG_ESP32_SPI_ENABLE)
595 
598 class PlatformSpi: public EspSpi
599 {
600 public:
605  explicit PlatformSpi(const SPlatformSpiConfig &config)
606  : EspSpi(config.busId, config.cs, config.dc, config.scl, config.sda, config.frequency)
607  {
608  }
609 };
610 
611 #elif defined(CONFIG_ESP8266_SPI_AVAILABLE) && defined(CONFIG_ESP8266_SPI_ENABLE)
612 
615 class PlatformSpi: public EspSpi
616 {
617 public:
622  explicit PlatformSpi(const SPlatformSpiConfig &config)
623  : EspSpi(config.busId, config.cs, config.dc, config.scl, config.sda, config.frequency)
624  {
625  }
626 };
627 
628 #elif defined(CONFIG_USI_SPI_AVAILABLE) && defined(CONFIG_USI_SPI_ENABLE)
629 
633 class PlatformSpi: public UsiSpi
634 {
635 public:
640  explicit PlatformSpi(const SPlatformSpiConfig &config)
641  : UsiSpi(config.cs, config.dc)
642  {
643  }
644 };
645 
646 #elif defined(PICO_BOARD)
647 
651 class PlatformSpi: public PicoSpi
652 {
653 public:
658  explicit PlatformSpi(const SPlatformSpiConfig &config)
659  : PicoSpi(config.cs, config.dc, config.scl, config.sda, config.frequency)
660  {
661  }
662 };
663 
664 #else
665 
666 #error "Platform not supported"
667 
668 #endif
669 
670 #include "custom_interface.h"
671 
672 #endif
673 
678 #endif
void lcd_randomSeed(int seed)
int lcd_gpioRead(int pin)
void lcd_attachInterrupt(int pin, void(*interrupt)(), int level)
Definition: i2c_twi.h:36
void lcd_gpioWrite(int pin, int level)
uint32_t lcd_micros(void)
int lcdint_t
Definition: io.h:108
PlatformSpi(const SPlatformSpiConfig &config)
Definition: io.h:534
uint8_t lcd_pgmReadByte(const void *ptr)
unsigned int lcduint_t
Definition: io.h:110
void lcd_eepromWriteWord(const void *ptr, uint16_t val)
void lcd_gpioMode(int pin, int mode)
Definition: spi_usi.h:41
void lcd_delayUs(unsigned long us)
uint32_t frequency
Definition: interface.h:108
uint32_t lcd_millis(void)
int lcd_random(int max)
PlatformI2c(const SPlatformI2cConfig &config)
Definition: io.h:380
Definition: spi_avr.h:36
uint16_t lcd_eepromReadWord(const void *ptr)
int lcd_adcRead(int pin)
void lcd_delay(unsigned long ms)