Files
ESP8266lib/io/GPIO.h
kazu 5cb02880b3 many updates..
new sensors.. display.. led.. drawing.. stuff..
2019-01-17 23:12:01 +01:00

123 lines
3.1 KiB
C++

#ifndef ESP_LIB_GPIO_H
#define ESP_LIB_GPIO_H
#include "../Platforms.h"
#if ESP8266
#include "fastGPIO.h"
struct GPIO {
static inline bool get(const uint8_t num) {
return GPIO_INPUT_GET(num);
}
static inline void set(const uint8_t num) {
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << num);
}
static inline void clear(const uint8_t num) {
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << num);
}
static inline void setOutput(const uint8_t num) {
switch(num) {
case 0: GPIO0_OUTPUT_SET; break;
case 1: GPIO1_OUTPUT_SET; break;
case 2: GPIO2_OUTPUT_SET; break;
case 3: GPIO3_OUTPUT_SET; break;
case 4: GPIO4_OUTPUT_SET; break;
case 5: GPIO5_OUTPUT_SET; break;
//case 6: GPIO6_OUTPUT_SET; break;
//case 7: GPIO7_OUTPUT_SET; break;
//case 8: GPIO8_OUTPUT_SET; break;
case 9: GPIO9_OUTPUT_SET; break;
case 10: GPIO10_OUTPUT_SET; break;
//case 11: GPIO11_OUTPUT_SET; break;
case 12: GPIO12_OUTPUT_SET; break;
case 13: GPIO13_OUTPUT_SET; break;
case 14: GPIO14_OUTPUT_SET; break;
case 15: GPIO15_OUTPUT_SET; break;
}
}
static inline void setInput(const uint8_t num) {
switch(num) {
case 0: GPIO0_INPUT_SET; break;
case 1: GPIO1_INPUT_SET; break;
case 2: GPIO2_INPUT_SET; break;
case 3: GPIO3_INPUT_SET; break;
case 4: GPIO4_INPUT_SET; break;
case 5: GPIO5_INPUT_SET; break;
//case 6: GPIO6_INPUT_SET; break;
//case 7: GPIO7_INPUT_SET; break;
//case 8: GPIO8_INPUT_SET; break;
case 9: GPIO9_INPUT_SET; break;
case 10: GPIO10_INPUT_SET; break;
//case 11: GPIO11_INPUT_SET; break;
case 12: GPIO12_INPUT_SET; break;
case 13: GPIO13_INPUT_SET; break;
case 14: GPIO14_INPUT_SET; break;
case 15: GPIO15_INPUT_SET; break;
}
}
};
#elif ESP32
#include "driver/gpio.h"
struct MyGPIO {
static inline bool get(const uint8_t num) {
return get((gpio_num_t)num);
}
static inline bool get(const gpio_num_t num) {
return gpio_get_level(num); // TODO faster access like READ_PERI_REG??
}
template <typename T> static inline void setOrClear(const uint8_t num, const T val) {
if (val) {set(num);} else {clear(num);}
//WRITE_PERI_REG(GPIO_OUT_W1TS_REG, (val != 0) << num); // branchless but usually slower
//WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (val == 0) << num);
}
static inline void set(const uint8_t num) {
WRITE_PERI_REG(GPIO_OUT_W1TS_REG, 1 << num);
}
static inline void clear(const uint8_t num) {
WRITE_PERI_REG(GPIO_OUT_W1TC_REG, 1 << num);
}
static inline void setOutput(const uint8_t num) {
setOutput((gpio_num_t)num);
}
static inline void setOutput(const gpio_num_t num) {
gpio_set_direction(num, GPIO_MODE_OUTPUT);
}
static inline void setInput(const uint8_t num) {
setInput((gpio_num_t)num);
}
static inline void setInput(const gpio_num_t num) {
gpio_set_direction(num, GPIO_MODE_INPUT);
}
static void toggleBuiltInLED() {
static bool level = false;
setOutput(GPIO_NUM_2);
level = !level;
if (level) {set(GPIO_NUM_2);} else {clear(GPIO_NUM_2);}
}
};
#endif
#endif // ESP_LIB_GPIO_H