Use hal for buzzer instead of set channel in buzzer

This commit is contained in:
Luc 2019-08-11 14:19:00 +02:00
parent 918f09cdfa
commit 0449893385
5 changed files with 69 additions and 41 deletions

View File

@ -49,7 +49,7 @@
//OLED_I2C_SSD1306 1
//OLED_I2C_SSDSH1106 2
//TFT_SPI_ILI9341_320X240 3
#define DISPLAY_DEVICE OLED_I2C_SSD1306
#define DISPLAY_DEVICE TFT_SPI_ILI9341_320X240
//BUZZER_DEVICE: allow to connect passive buzzer
#define BUZZER_DEVICE
@ -77,7 +77,7 @@
//#define DHT_DEVICE
#ifdef BUZZER_DEVICE
#define ESP3D_BUZZER_PIN 5
#define ESP3D_BUZZER_PIN 15
#endif //BUZZER_DEVICE
#ifdef DHT_DEVICE

View File

@ -77,6 +77,38 @@ void Hal::pinMode(uint8_t pin, uint8_t mode)
pinMode(pin, mode);
}
void Hal::toneESP(uint8_t pin, unsigned int frequency, unsigned int duration, bool sync)
{
#if defined(ARDUINO_ARCH_ESP8266)
(void) sync; //useless for esp8266
tone(pin, frequency, duration);
#endif //ARDUINO_ARCH_ESP8266
#if defined(ARDUINO_ARCH_ESP32)
int channel = getAnalogWriteChannel(pin);
if (channel != -1){
ledcAttachPin(pin, channel);
ledcWriteTone(channel,frequency);
if (sync) {
wait(duration);
ledcWriteTone(pin,0);
}
}
#endif //ARDUINO_ARCH_ESP32
}
void Hal::no_tone(uint8_t pin)
{
#if defined(ARDUINO_ARCH_ESP8266)
tone(pin, 0, 0);
#endif //ARDUINO_ARCH_ESP8266
#if defined(ARDUINO_ARCH_ESP32)
int channel = getAnalogWriteChannel(pin);
if (channel != -1){
ledcWrite(channel, 0);
}
#endif //ARDUINO_ARCH_ESP32
}
int Hal::analogRead(uint8_t pin)
{
#ifdef ARDUINO_ARCH_ESP8266 //only one ADC on ESP8266 A0
@ -87,7 +119,26 @@ int Hal::analogRead(uint8_t pin)
#endif
}
bool Hal::analogWrite(uint8_t pin, uint value)
#if defined(ARDUINO_ARCH_ESP32)
int Hal::getAnalogWriteChannel(uint8_t pin)
{
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] == pin) {
return p;
}
}
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] == -1) {
ChannelAttached2Pin[p] = pin;
return p;
}
}
return -1;
}
#endif //ARDUINO_ARCH_ESP32
bool Hal::analogWrite(uint8_t pin, uint value)
{
if (value > (_analogWriteRange-1)) {
return false;
@ -96,21 +147,7 @@ bool Hal::analogWrite(uint8_t pin, uint value)
analogWrite(pin, value);
#endif //ARDUINO_ARCH_ESP8266
#ifdef ARDUINO_ARCH_ESP32
int channel = -1;
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] == pin) {
channel = p;
}
}
if (channel==-1) {
for (uint8_t p = 0; p < 16; p++) {
if(ChannelAttached2Pin[p] == -1) {
channel = p;
ChannelAttached2Pin[p] = pin;
p = 16;
}
}
}
int channel = getAnalogWriteChannel(pin);
if (channel==-1) {
return false;
}
@ -133,7 +170,6 @@ bool Hal::analogWrite(uint8_t pin, uint value)
_analogWriteRange = 255;
break;
}
ledcSetup(channel, _analogWriteFreq, resolution);
ledcAttachPin(pin, channel);
ledcWrite(channel, value);
@ -170,6 +206,9 @@ bool Hal::begin()
//End ESP3D
void Hal::end()
{
#if defined(ARDUINO_ARCH_ESP32)
clearAnalogChannels();
#endif //ARDUINO_ARCH_ESP32
}
//Watchdog feeder

View File

@ -50,9 +50,14 @@ public:
static bool analogWrite(uint8_t pin, uint value);
static void analogWriteFreq(uint32_t freq);
static void analogWriteRange(uint32_t range);
static void toneESP(uint8_t pin, unsigned int frequency, unsigned int duration, bool sync = true);
static void no_tone(uint8_t pin);
private:
static void wdtFeed();
static uint32_t _analogWriteRange;
static uint32_t _analogWriteFreq;
#if defined(ARDUINO_ARCH_ESP32)
static int getAnalogWriteChannel(uint8_t pin);
#endif //ARDUINO_ARCH_ESP32
};
#endif //_ESP3D_HAL_H

View File

@ -22,7 +22,7 @@
#define _VERSION_ESP3D_H
//version and sources location
#define FW_VERSION "3.0.0.a11"
#define FW_VERSION "3.0.0.a12"
#define REPOSITORY "https://github.com/luc-github/ESP3D"
#endif //_VERSION_ESP3D_H

View File

@ -24,9 +24,9 @@
#include <Ticker.h>
#include "buzzer.h"
#include "../../core/settings_esp3d.h"
#include "../../core/hal.h"
BuzzerDevice esp3d_buzzer;
Ticker buzzer_tick;
#define BUZZER_CHANNEL 1
#define BEEP_DURATION 200
#if defined(ARDUINO_ARCH_ESP8266)
extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration);
@ -38,12 +38,7 @@ void process()
if (esp3d_buzzer.started()) {
tone_data * current = esp3d_buzzer.getNextTone();
if (current) {
#if defined(ARDUINO_ARCH_ESP8266)
tone(ESP3D_BUZZER_PIN, (unsigned int)current->frequency, (unsigned long) current->duration);
#endif //ARDUINO_ARCH_ESP8266
#if defined(ARDUINO_ARCH_ESP32)
ledcWriteTone(BUZZER_CHANNEL, current->frequency);
#endif //ARDUINO_ARCH_ESP32
Hal::toneESP(ESP3D_BUZZER_PIN,(unsigned int)current->frequency, (unsigned long) current->duration, false);
buzzer_tick.once_ms(current->duration, process);
}
}
@ -63,9 +58,8 @@ bool BuzzerDevice::begin()
}
if (Settings_ESP3D::read_byte(ESP_BUZZER) == 1) {
_started = true;
#if defined(ARDUINO_ARCH_ESP32)
ledcAttachPin(ESP3D_BUZZER_PIN, BUZZER_CHANNEL);
#endif //ARDUINO_ARCH_ESP32
playsound(5000, 240);
playsound(3000, 120);
}
return _started;
}
@ -75,11 +69,7 @@ void BuzzerDevice::end()
return;
}
purgeData();
//no_tone();
_started = false;
#if defined(ARDUINO_ARCH_ESP32)
ledcDetachPin(ESP3D_BUZZER_PIN);
#endif //ARDUINO_ARCH_ESP32
no_tone();
}
@ -105,13 +95,7 @@ void BuzzerDevice::beep(int count, int delay, int frequency)
void BuzzerDevice::no_tone()
{
#if defined(ARDUINO_ARCH_ESP8266)
tone(ESP3D_BUZZER_PIN, 0, 0);
#endif //ARDUINO_ARCH_ESP8266
#if defined(ARDUINO_ARCH_ESP32)
ledcWrite(BUZZER_CHANNEL, 0);
#endif //ARDUINO_ARCH_ESP32
Hal::no_tone(ESP3D_BUZZER_PIN);
}
bool BuzzerDevice::isPlaying()