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 #define CONFIG_LCDGFX_PLATFORM_I2C
371 
374 class PlatformI2c: public ArduinoI2c
375 {
376 public:
381  explicit PlatformI2c(const SPlatformI2cConfig &config)
382  : ArduinoI2c(config.scl, config.sda, config.addr)
383  {
384  }
385 };
386 
387 #elif defined(CONFIG_TWI_I2C_AVAILABLE) && defined(CONFIG_TWI_I2C_ENABLE)
388 #define CONFIG_LCDGFX_PLATFORM_I2C
389 
393 class PlatformI2c: public TwiI2c
394 {
395 public:
400  explicit PlatformI2c(const SPlatformI2cConfig &config)
401  : TwiI2c(config.addr)
402  {
403  }
404 };
405 
406 #elif defined(CONFIG_LINUX_I2C_AVAILABLE) && defined(CONFIG_LINUX_I2C_ENABLE)
407 #define CONFIG_LCDGFX_PLATFORM_I2C
408 
409 #if defined(SDL_EMULATION)
410 
413 class PlatformI2c: public SdlI2c
414 {
415 public:
420  explicit PlatformI2c(const SPlatformI2cConfig &config)
421  : SdlI2c(config.scl, config.sda, config.addr)
422  {
423  }
424 };
425 #else
426 
429 class PlatformI2c: public LinuxI2c
430 {
431 public:
436  explicit PlatformI2c(const SPlatformI2cConfig &config)
437  : LinuxI2c(config.busId, config.addr)
438  {
439  }
440 };
441 #endif
442 
443 #elif defined(CONFIG_ESP32_I2C_AVAILABLE) && defined(CONFIG_ESP32_I2C_ENABLE)
444 #define CONFIG_LCDGFX_PLATFORM_I2C
445 
449 class PlatformI2c: public EspI2c
450 {
451 public:
456  explicit PlatformI2c(const SPlatformI2cConfig &config)
457  : EspI2c(config.busId, config.addr, config.scl, config.sda, 400000)
458  {
459  }
460 };
461 
462 #elif defined(CONFIG_ESP8266_I2C_AVAILABLE) && defined(CONFIG_ESP8266_I2C_ENABLE)
463 #define CONFIG_LCDGFX_PLATFORM_I2C
464 
468 class PlatformI2c: public EspI2c
469 {
470 public:
475  explicit PlatformI2c(const SPlatformI2cConfig &config)
476  : EspI2c(config.busId, config.addr, config.scl, config.sda, 400000)
477  {
478  }
479 };
480 
481 #elif defined(CONFIG_SOFTWARE_I2C_AVAILABLE) && defined(CONFIG_SOFTWARE_I2C_ENABLE)
482 #define CONFIG_LCDGFX_PLATFORM_I2C
483 
487 class PlatformI2c: public SoftwareI2c
488 {
489 public:
494  explicit PlatformI2c(const SPlatformI2cConfig &config)
495  : SoftwareI2c(config.scl, config.sda, config.addr)
496  {
497  }
498 };
499 
500 #elif defined(PICO_BOARD)
501 #define CONFIG_LCDGFX_PLATFORM_I2C
502 
506 class PlatformI2c: public PicoI2c
507 {
508 public:
513  explicit PlatformI2c(const SPlatformI2cConfig &config)
514  : PicoI2c(config.scl, config.sda, config.addr)
515  {
516  }
517 };
518 
519 #endif
520 
522 // SPI PLATFORM
524 
525 #if defined(CONFIG_AVR_SPI_AVAILABLE) && defined(CONFIG_AVR_SPI_ENABLE)
526 #define CONFIG_LCDGFX_PLATFORM_SPI
527 
531 class PlatformSpi: public AvrSpi
532 {
533 public:
538  explicit PlatformSpi(const SPlatformSpiConfig &config)
539  : AvrSpi(config.cs, config.dc, config.frequency)
540  {
541  }
542 };
543 
544 #elif defined(CONFIG_ARDUINO_SPI_AVAILABLE) && defined(CONFIG_ARDUINO_SPI_ENABLE)
545 #define CONFIG_LCDGFX_PLATFORM_SPI
546 
550 class PlatformSpi: public ArduinoSpi
551 {
552 public:
557  explicit PlatformSpi(const SPlatformSpiConfig &config)
558  : ArduinoSpi(config.cs, config.dc, config.scl, config.sda, config.frequency)
559  {
560  }
561 };
562 
563 #elif defined(CONFIG_LINUX_SPI_AVAILABLE) && defined(CONFIG_LINUX_SPI_ENABLE)
564 #define CONFIG_LCDGFX_PLATFORM_SPI
565 
566 #if defined(SDL_EMULATION)
567 
570 class PlatformSpi: public SdlSpi
571 {
572 public:
577  explicit PlatformSpi(const SPlatformSpiConfig &config)
578  : SdlSpi(config.dc)
579  {
580  }
581 };
582 #else
583 
586 class PlatformSpi: public LinuxSpi
587 {
588 public:
593  explicit PlatformSpi(const SPlatformSpiConfig &config)
594  : LinuxSpi(config.busId, config.devId, config.dc, config.frequency)
595  {
596  }
597 };
598 #endif
599 
600 #elif defined(CONFIG_ESP32_SPI_AVAILABLE) && defined(CONFIG_ESP32_SPI_ENABLE)
601 #define CONFIG_LCDGFX_PLATFORM_SPI
602 
605 class PlatformSpi: public EspSpi
606 {
607 public:
612  explicit PlatformSpi(const SPlatformSpiConfig &config)
613  : EspSpi(config.busId, config.cs, config.dc, config.scl, config.sda, config.frequency)
614  {
615  }
616 };
617 
618 #elif defined(CONFIG_ESP8266_SPI_AVAILABLE) && defined(CONFIG_ESP8266_SPI_ENABLE)
619 #define CONFIG_LCDGFX_PLATFORM_SPI
620 
623 class PlatformSpi: public EspSpi
624 {
625 public:
630  explicit PlatformSpi(const SPlatformSpiConfig &config)
631  : EspSpi(config.busId, config.cs, config.dc, config.scl, config.sda, config.frequency)
632  {
633  }
634 };
635 
636 #elif defined(CONFIG_USI_SPI_AVAILABLE) && defined(CONFIG_USI_SPI_ENABLE)
637 #define CONFIG_LCDGFX_PLATFORM_SPI
638 
642 class PlatformSpi: public UsiSpi
643 {
644 public:
649  explicit PlatformSpi(const SPlatformSpiConfig &config)
650  : UsiSpi(config.cs, config.dc)
651  {
652  }
653 };
654 
655 #elif defined(PICO_BOARD)
656 #define CONFIG_LCDGFX_PLATFORM_SPI
657 
661 class PlatformSpi: public PicoSpi
662 {
663 public:
668  explicit PlatformSpi(const SPlatformSpiConfig &config)
669  : PicoSpi(config.cs, config.dc, config.scl, config.sda, config.frequency)
670  {
671  }
672 };
673 
674 #endif
675 
676 #include "custom_interface.h"
677 
678 #endif
679 
684 #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:538
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:381
Definition: spi_avr.h:36
uint16_t lcd_eepromReadWord(const void *ptr)
int lcd_adcRead(int pin)
void lcd_delay(unsigned long ms)