mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-06-06 02:36:49 +08:00
Use hal for buzzer instead of set channel in buzzer
This commit is contained in:
parent
918f09cdfa
commit
0449893385
@ -49,7 +49,7 @@
|
|||||||
//OLED_I2C_SSD1306 1
|
//OLED_I2C_SSD1306 1
|
||||||
//OLED_I2C_SSDSH1106 2
|
//OLED_I2C_SSDSH1106 2
|
||||||
//TFT_SPI_ILI9341_320X240 3
|
//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
|
//BUZZER_DEVICE: allow to connect passive buzzer
|
||||||
#define BUZZER_DEVICE
|
#define BUZZER_DEVICE
|
||||||
@ -77,7 +77,7 @@
|
|||||||
//#define DHT_DEVICE
|
//#define DHT_DEVICE
|
||||||
|
|
||||||
#ifdef BUZZER_DEVICE
|
#ifdef BUZZER_DEVICE
|
||||||
#define ESP3D_BUZZER_PIN 5
|
#define ESP3D_BUZZER_PIN 15
|
||||||
#endif //BUZZER_DEVICE
|
#endif //BUZZER_DEVICE
|
||||||
|
|
||||||
#ifdef DHT_DEVICE
|
#ifdef DHT_DEVICE
|
||||||
|
@ -77,6 +77,38 @@ void Hal::pinMode(uint8_t pin, uint8_t mode)
|
|||||||
pinMode(pin, 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)
|
int Hal::analogRead(uint8_t pin)
|
||||||
{
|
{
|
||||||
#ifdef ARDUINO_ARCH_ESP8266 //only one ADC on ESP8266 A0
|
#ifdef ARDUINO_ARCH_ESP8266 //only one ADC on ESP8266 A0
|
||||||
@ -87,6 +119,25 @@ int Hal::analogRead(uint8_t pin)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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)
|
bool Hal::analogWrite(uint8_t pin, uint value)
|
||||||
{
|
{
|
||||||
if (value > (_analogWriteRange-1)) {
|
if (value > (_analogWriteRange-1)) {
|
||||||
@ -96,21 +147,7 @@ bool Hal::analogWrite(uint8_t pin, uint value)
|
|||||||
analogWrite(pin, value);
|
analogWrite(pin, value);
|
||||||
#endif //ARDUINO_ARCH_ESP8266
|
#endif //ARDUINO_ARCH_ESP8266
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
int channel = -1;
|
int channel = getAnalogWriteChannel(pin);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (channel==-1) {
|
if (channel==-1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -133,7 +170,6 @@ bool Hal::analogWrite(uint8_t pin, uint value)
|
|||||||
_analogWriteRange = 255;
|
_analogWriteRange = 255;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ledcSetup(channel, _analogWriteFreq, resolution);
|
ledcSetup(channel, _analogWriteFreq, resolution);
|
||||||
ledcAttachPin(pin, channel);
|
ledcAttachPin(pin, channel);
|
||||||
ledcWrite(channel, value);
|
ledcWrite(channel, value);
|
||||||
@ -170,6 +206,9 @@ bool Hal::begin()
|
|||||||
//End ESP3D
|
//End ESP3D
|
||||||
void Hal::end()
|
void Hal::end()
|
||||||
{
|
{
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
|
clearAnalogChannels();
|
||||||
|
#endif //ARDUINO_ARCH_ESP32
|
||||||
}
|
}
|
||||||
|
|
||||||
//Watchdog feeder
|
//Watchdog feeder
|
||||||
|
@ -50,9 +50,14 @@ public:
|
|||||||
static bool analogWrite(uint8_t pin, uint value);
|
static bool analogWrite(uint8_t pin, uint value);
|
||||||
static void analogWriteFreq(uint32_t freq);
|
static void analogWriteFreq(uint32_t freq);
|
||||||
static void analogWriteRange(uint32_t range);
|
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:
|
private:
|
||||||
static void wdtFeed();
|
static void wdtFeed();
|
||||||
static uint32_t _analogWriteRange;
|
static uint32_t _analogWriteRange;
|
||||||
static uint32_t _analogWriteFreq;
|
static uint32_t _analogWriteFreq;
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
|
static int getAnalogWriteChannel(uint8_t pin);
|
||||||
|
#endif //ARDUINO_ARCH_ESP32
|
||||||
};
|
};
|
||||||
#endif //_ESP3D_HAL_H
|
#endif //_ESP3D_HAL_H
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
#define _VERSION_ESP3D_H
|
#define _VERSION_ESP3D_H
|
||||||
|
|
||||||
//version and sources location
|
//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"
|
#define REPOSITORY "https://github.com/luc-github/ESP3D"
|
||||||
|
|
||||||
#endif //_VERSION_ESP3D_H
|
#endif //_VERSION_ESP3D_H
|
||||||
|
@ -24,9 +24,9 @@
|
|||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
#include "buzzer.h"
|
#include "buzzer.h"
|
||||||
#include "../../core/settings_esp3d.h"
|
#include "../../core/settings_esp3d.h"
|
||||||
|
#include "../../core/hal.h"
|
||||||
BuzzerDevice esp3d_buzzer;
|
BuzzerDevice esp3d_buzzer;
|
||||||
Ticker buzzer_tick;
|
Ticker buzzer_tick;
|
||||||
#define BUZZER_CHANNEL 1
|
|
||||||
#define BEEP_DURATION 200
|
#define BEEP_DURATION 200
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration);
|
extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration);
|
||||||
@ -38,12 +38,7 @@ void process()
|
|||||||
if (esp3d_buzzer.started()) {
|
if (esp3d_buzzer.started()) {
|
||||||
tone_data * current = esp3d_buzzer.getNextTone();
|
tone_data * current = esp3d_buzzer.getNextTone();
|
||||||
if (current) {
|
if (current) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
Hal::toneESP(ESP3D_BUZZER_PIN,(unsigned int)current->frequency, (unsigned long) current->duration, false);
|
||||||
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
|
|
||||||
buzzer_tick.once_ms(current->duration, process);
|
buzzer_tick.once_ms(current->duration, process);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,9 +58,8 @@ bool BuzzerDevice::begin()
|
|||||||
}
|
}
|
||||||
if (Settings_ESP3D::read_byte(ESP_BUZZER) == 1) {
|
if (Settings_ESP3D::read_byte(ESP_BUZZER) == 1) {
|
||||||
_started = true;
|
_started = true;
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
playsound(5000, 240);
|
||||||
ledcAttachPin(ESP3D_BUZZER_PIN, BUZZER_CHANNEL);
|
playsound(3000, 120);
|
||||||
#endif //ARDUINO_ARCH_ESP32
|
|
||||||
}
|
}
|
||||||
return _started;
|
return _started;
|
||||||
}
|
}
|
||||||
@ -75,11 +69,7 @@ void BuzzerDevice::end()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
purgeData();
|
purgeData();
|
||||||
//no_tone();
|
|
||||||
_started = false;
|
_started = false;
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
ledcDetachPin(ESP3D_BUZZER_PIN);
|
|
||||||
#endif //ARDUINO_ARCH_ESP32
|
|
||||||
no_tone();
|
no_tone();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,13 +95,7 @@ void BuzzerDevice::beep(int count, int delay, int frequency)
|
|||||||
|
|
||||||
void BuzzerDevice::no_tone()
|
void BuzzerDevice::no_tone()
|
||||||
{
|
{
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
Hal::no_tone(ESP3D_BUZZER_PIN);
|
||||||
tone(ESP3D_BUZZER_PIN, 0, 0);
|
|
||||||
#endif //ARDUINO_ARCH_ESP8266
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
ledcWrite(BUZZER_CHANNEL, 0);
|
|
||||||
#endif //ARDUINO_ARCH_ESP32
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BuzzerDevice::isPlaying()
|
bool BuzzerDevice::isPlaying()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user