Skip to content

Instantly share code, notes, and snippets.

@mickeyl
Created May 13, 2023 12:31
Show Gist options
  • Save mickeyl/0b38ea67db747823bb5610aeddb4e7e6 to your computer and use it in GitHub Desktop.
Save mickeyl/0b38ea67db747823bb5610aeddb4e7e6 to your computer and use it in GitHub Desktop.
Arduino => ESP-IDF Compatibility Header
#ifndef ESPENLAUB_ARDUINO_COMPAT_H
#define ESPENLAUB_ARDUINO_COMPAT_H
#pragma once
#include "driver/gpio.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
using byte = uint8_t;
using word = uint16_t;
using boolean = bool;
#define NOP() asm volatile("nop")
static inline unsigned long micros() {
return (unsigned long)(esp_timer_get_time());
}
static inline void delayMicroseconds(uint32_t us) {
uint32_t m = micros();
if (us) {
uint32_t e = (m + us);
if (m > e) { // overflow
while (micros() > e) {
NOP();
}
}
while (micros() < e) {
NOP();
}
}
}
// Enums for LOW and HIGH
enum ArduinoLevel {
LOW = 0,
HIGH = 1,
};
// Enums for INPUT and OUTPUT
enum ArduinoMode {
INPUT = 0,
OUTPUT = 1,
UNINITIALIZED = 0xFF,
};
static uint8_t gpios[] = {
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED,
};
// Set the mode of a pin (INPUT or OUTPUT)
static inline void pinMode(int pin, ArduinoMode mode) {
switch (mode) {
case INPUT:
if (gpios[pin] == UNINITIALIZED) {
gpio_reset_pin((gpio_num_t)pin);
}
if (gpios[pin] != GPIO_MODE_INPUT) {
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT);
gpios[pin] = GPIO_MODE_INPUT;
}
break;
case OUTPUT:
if (gpios[pin] == UNINITIALIZED) {
gpio_reset_pin((gpio_num_t)pin);
}
if (gpios[pin]!= GPIO_MODE_OUTPUT) {
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_OUTPUT);
gpios[pin] = GPIO_MODE_OUTPUT;
}
break;
default:
assert(false);
}
}
// Write a value (HIGH or LOW) to a pin
static inline void digitalWrite(int pin, ArduinoLevel value) {
switch (value) {
case LOW:
gpio_set_level((gpio_num_t)pin, 0);
break;
case HIGH:
gpio_set_level((gpio_num_t)pin, 1);
break;
default:
assert(false);
}
}
// Read the value (HIGH or LOW) from a pin
static inline ArduinoLevel digitalRead(int pin) {
// Read pin level
int level = gpio_get_level((gpio_num_t)pin);
// Convert ESP-IDF level to ArduinoLevel enum
return (level == 0) ? LOW : HIGH;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment