SparkFun SSD168x Library  v1.0.2
Library for SSD1680 / SSD1681 e-paper displays on SPI
Loading...
Searching...
No Matches
sfe_platform.h
Go to the documentation of this file.
1
37#pragma once
38
39#if defined(ARDUINO)
40
41// ---------------------------------------------------------------------------------------------
42// Arduino
43// ---------------------------------------------------------------------------------------------
44
45#define SFE_SSD168X_ARDUINO 1
46
47#include <Arduino.h>
48#include <SPI.h>
49
50namespace SparkFun_SSD168x
51{
52typedef String sfe_string_t;
53
54static inline unsigned long sfe_millis(void) { return millis(); }
55static inline void sfe_delay(unsigned long ms) { delay(ms); }
56static inline void sfe_pin_output(int pin) { pinMode(pin, OUTPUT); }
57static inline void sfe_pin_input(int pin) { pinMode(pin, INPUT); }
58static inline void sfe_pin_write(int pin, bool high) { digitalWrite(pin, high ? HIGH : LOW); }
59static inline bool sfe_pin_read(int pin) { return digitalRead(pin) == HIGH; }
60
61} // namespace SparkFun_SSD168x
62
63#elif defined(ESP_PLATFORM)
64
65// ---------------------------------------------------------------------------------------------
66// Native ESP-IDF
67// ---------------------------------------------------------------------------------------------
68
69#define SFE_SSD168X_ESP_IDF 1
70
71#include <stdint.h>
72#include <stddef.h>
73#include <stdio.h>
74#include <string.h>
75#include <string>
76
77#include "freertos/FreeRTOS.h"
78#include "freertos/task.h"
79#include "esp_timer.h"
80#include "esp_rom_sys.h"
81#include "driver/gpio.h"
82
83#ifndef DEC
84#define DEC 10
85#endif
86#ifndef HEX
87#define HEX 16
88#endif
89
90namespace SparkFun_SSD168x
91{
92typedef std::string sfe_string_t;
93
94static inline unsigned long sfe_millis(void) { return (unsigned long)(esp_timer_get_time() / 1000ULL); }
95
96// sfe_delay: with the ESP-IDF default FreeRTOS tick of 100Hz, pdMS_TO_TICKS(1) is zero - and
97// vTaskDelay(0) only yields to tasks of equal priority. So: delays shorter than one tick are done
98// as a short busy-wait followed by a yield; longer delays use vTaskDelay. Setting
99// CONFIG_FREERTOS_HZ=1000 (recommended - see the examples' sdkconfig.defaults) makes every delay
100// a true sleep.
101static inline void sfe_delay(unsigned long ms)
102{
103 if (ms == 0)
104 {
105 taskYIELD();
106 return;
107 }
108 TickType_t ticks = pdMS_TO_TICKS(ms);
109 if (ticks == 0)
110 {
111 esp_rom_delay_us((uint32_t)(ms * 1000UL));
112 taskYIELD();
113 }
114 else
115 {
116 vTaskDelay(ticks);
117 }
118}
119
120static inline void sfe_pin_output(int pin)
121{
122 gpio_reset_pin((gpio_num_t)pin);
123 gpio_set_direction((gpio_num_t)pin, GPIO_MODE_OUTPUT);
124}
125static inline void sfe_pin_input(int pin)
126{
127 gpio_reset_pin((gpio_num_t)pin); // Note: gpio_reset_pin enables the internal pull-up
128 gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT);
129 gpio_set_pull_mode((gpio_num_t)pin, GPIO_FLOATING); // BUSY is driven by the SSD168x - no pull needed
130}
131static inline void sfe_pin_write(int pin, bool high) { gpio_set_level((gpio_num_t)pin, high ? 1 : 0); }
132static inline bool sfe_pin_read(int pin) { return gpio_get_level((gpio_num_t)pin) != 0; }
133
134} // namespace SparkFun_SSD168x
135
136#else
137
138#error "SparkFun SSD168x Library: unsupported platform. Arduino (ARDUINO) or ESP-IDF (ESP_PLATFORM) is required."
139
140#endif
Definition: sfe_bus.h:34