restructure for msCode

This commit is contained in:
2021-01-25 10:02:31 +01:00
parent 957909193c
commit e3336a113f
256 changed files with 20639 additions and 8270 deletions

View File

@@ -0,0 +1 @@
{"type": "library", "name": "ESP32 AnalogWrite", "version": "0.2.0", "spec": {"owner": "erropix", "id": 5819, "name": "ESP32 AnalogWrite", "requirements": null, "url": null}}

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 ERROPiX
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,35 @@
# About
This library provides an analogWrite function polyfill for ESP32 Arduino framework by wrapping the [ledc](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c) library.
Licensed under the MIT license.
# Usage
The library will do all the timer setup and channel selection work behind the scene so you don't have to worry about that.
## Default Value Range
Call the `analogWrite` function like in standard arduino framework:
```cpp
analogWrite(LED_BUILTIN, 255); // value range 0-255 so 255 = 100%
```
## Custom Value Range
You can also set the maximum value as third parameter:
```cpp
analogWrite(LED_BUILTIN, 255, 1023); // value range 0-1023 so 255 = 25%
```
## Timer Resolution
The default timer resolution is set to 13 bits in all the 16 channels, if you want to change that, use the `analogWriteResolution` function:
```cpp
analogWriteResolution(10); // set resolution to 10 bits for all pins
analogWriteResolution(LED_BUILTIN, 10); // set resolution to 10 bits for LED pin
```
## PWM Frequency
The default PWM frequency is set to 5000 Hz in all the 16 channels, if you want to change that, use the `analogWriteFrequency` function:
```cpp
analogWriteFrequency(10000); // set frequency to 10 KHz for all pins
analogWriteFrequency(LED_BUILTIN, 10000); // set frequency to 10 KHz for LED pin
```
Please note that both timer resolution and PWM frequency should be calculated to get the expected results, if frequency is not set correctly, the output PWM signal wont be as expected, read more about [Supported Range of Frequency and Duty Resolution](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/ledc.html#ledc-api-supported-range-frequency-duty-resolution) in the official Espressif documentation website.

View File

@@ -0,0 +1,21 @@
#include <Arduino.h>
#include <analogWrite.h>
int brightStep = 1;
int brightness = 0;
void setup() {
// Set resolution for a specific pin
analogWriteResolution(LED_BUILTIN, 12);
}
void loop() {
brightness += brightStep;
if ( brightness == 0 || brightness == 255 ) {
brightStep = -brightStep;
}
analogWrite(LED_BUILTIN, brightness);
delay(10);
}

View File

@@ -0,0 +1,19 @@
#######################################
# Datatypes
#######################################
analog_write_channel_t KEYWORD1
#######################################
# Methods and Functions
#######################################
analogWrite KEYWORD2
analogWriteChannel KEYWORD2
analogWriteFrequency KEYWORD2
analogWriteResolution KEYWORD2
#######################################
# Constants
#######################################

View File

@@ -0,0 +1,19 @@
{
"name": "ESP32 AnalogWrite",
"keywords": "esp32, analogWrite, LEDC",
"description": "Provides an analogWrite polyfill for ESP32 using the LEDC functions",
"license": "MIT",
"version": "0.2",
"frameworks": "arduino",
"platforms": "espressif32",
"repository": {
"type": "git",
"url": "https://github.com/ERROPiX/ESP32_AnalogWrite.git"
},
"authors": [
{
"name": "Abdelouahed ERROUAGUY",
"email": "errouaguy.pro@gmail.com"
}
]
}

View File

@@ -0,0 +1,10 @@
name=ESP32 AnalogWrite
version=0.1
author=ERROPiX
maintainer=Abdelouahed ERROUAGUY
sentence=ESP32 Polyfill for analogWrite functions
paragraph=Provides an analogWrite polyfill for ESP32 using the LEDC functions
category=Signal Input/Output
url=https://github.com/ERROPiX/ESP32_AnalogWrite
architectures=esp32
includes=analogWrite.h

View File

@@ -0,0 +1,106 @@
#include "analogWrite.h"
analog_write_channel_t _analog_write_channels[16] = {
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13},
{-1, 5000, 13}};
int analogWriteChannel(uint8_t pin)
{
int channel = -1;
// Check if pin already attached to a channel
for (uint8_t i = 0; i < 16; i++)
{
if (_analog_write_channels[i].pin == pin)
{
channel = i;
break;
}
}
// If not, attach it to a free channel
if (channel == -1)
{
for (uint8_t i = 0; i < 16; i++)
{
if (_analog_write_channels[i].pin == -1)
{
_analog_write_channels[i].pin = pin;
channel = i;
ledcSetup(channel, _analog_write_channels[i].frequency, _analog_write_channels[i].resolution);
ledcAttachPin(pin, channel);
break;
}
}
}
return channel;
}
void analogWriteFrequency(double frequency)
{
for (uint8_t i = 0; i < 16; i++)
{
_analog_write_channels[i].frequency = frequency;
}
}
void analogWriteFrequency(uint8_t pin, double frequency)
{
int channel = analogWriteChannel(pin);
// Make sure the pin was attached to a channel, if not do nothing
if (channel != -1 && channel < 16)
{
_analog_write_channels[channel].frequency = frequency;
}
}
void analogWriteResolution(uint8_t resolution)
{
for (uint8_t i = 0; i < 16; i++)
{
_analog_write_channels[i].resolution = resolution;
}
}
void analogWriteResolution(uint8_t pin, uint8_t resolution)
{
int channel = analogWriteChannel(pin);
// Make sure the pin was attached to a channel, if not do nothing
if (channel != -1 && channel < 16)
{
_analog_write_channels[channel].resolution = resolution;
}
}
void analogWrite(uint8_t pin, uint32_t value, uint32_t valueMax)
{
int channel = analogWriteChannel(pin);
// Make sure the pin was attached to a channel, if not do nothing
if (channel != -1 && channel < 16)
{
uint8_t resolution = _analog_write_channels[channel].resolution;
uint32_t levels = pow(2, resolution);
uint32_t duty = ((levels - 1) / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(channel, duty);
}
}

View File

@@ -0,0 +1,23 @@
#ifndef _ESP32_ANALOG_WRITE_
#define _ESP32_ANALOG_WRITE_
#include <Arduino.h>
typedef struct analog_write_channel
{
int8_t pin;
double frequency;
uint8_t resolution;
} analog_write_channel_t;
int analogWriteChannel(uint8_t pin);
void analogWriteFrequency(double frequency);
void analogWriteFrequency(uint8_t pin, double frequency);
void analogWriteResolution(uint8_t resolution);
void analogWriteResolution(uint8_t pin, uint8_t resolution);
void analogWrite(uint8_t pin, uint32_t value, uint32_t valueMax = 255);
#endif