SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorCommEspIDF_HW.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "../SensorCommBase.hpp"
33
34#if !defined(ARDUINO) && defined(ESP_PLATFORM)
35
36#include "sdkconfig.h"
37#include "freertos/FreeRTOS.h"
38#include "esp_idf_version.h"
39#include "esp_err.h"
40#include "esp_timer.h"
41#include "rom/ets_sys.h"
42#include "driver/gpio.h"
43
44class HalEspIDF: public SensorHal
45{
46public:
47 void pinMode(uint8_t pin, uint8_t mode)
48 {
49 if (modeCallback) {
50 modeCallback(pin, mode);
51 } else {
52 gpio_config_t config;
53 memset(&config, 0, sizeof(config));
54 config.pin_bit_mask = 1ULL << pin;
55 switch (mode) {
56 case INPUT:
57 config.mode = GPIO_MODE_INPUT;
58 break;
59 case OUTPUT:
60 config.mode = GPIO_MODE_OUTPUT;
61 break;
62 }
63 config.pull_up_en = GPIO_PULLUP_DISABLE;
64 config.pull_down_en = GPIO_PULLDOWN_DISABLE;
65 config.intr_type = GPIO_INTR_DISABLE;
66 gpio_config(&config);
67 }
68 }
69
70 void digitalWrite(uint8_t pin, uint8_t level)
71 {
72 if (writeCallback) {
73 writeCallback(pin, level);
74 } else {
75 gpio_set_level((gpio_num_t )pin, level);
76 }
77 }
78
79 uint8_t digitalRead(uint8_t pin)
80 {
81 if (readCallback) {
82 return readCallback(pin);
83 }
84 return gpio_get_level((gpio_num_t)pin);
85 }
86
87 void delay(uint32_t ms)
88 {
89 ets_delay_us((ms % portTICK_PERIOD_MS) * 1000UL);
90 }
91
92 uint32_t millis()
93 {
94 return (uint32_t) (esp_timer_get_time() / 1000LL);
95 }
96
97 uint32_t micros()
98 {
99 return (uint32_t) esp_timer_get_time();
100 }
101
102 void delayMicroseconds(uint32_t us)
103 {
104 ets_delay_us(us);
105 }
106};
107
108
109
110#endif //*ESP_PLATFORM
uint8_t level
CustomMode modeCallback
Custom pinMode, nullptr if not set.
CustomWrite writeCallback
Custom digitalWrite, nullptr if not set.
CustomRead readCallback
Custom digitalRead, nullptr if not set.
Abstract hardware abstraction layer.
virtual uint8_t digitalRead(uint8_t pin)=0
Read the digital value from a pin.
virtual void pinMode(uint8_t pin, uint8_t mode)=0
Set the mode of a digital pin.
virtual void digitalWrite(uint8_t pin, uint8_t level)=0
Write a digital value to a pin.
virtual void delay(uint32_t ms)=0
Blocking delay for a given number of milliseconds.
virtual void delayMicroseconds(uint32_t us)=0
Blocking delay for a given number of microseconds.
virtual uint32_t millis()=0
Get the current system time in milliseconds.