mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-06-06 02:36:49 +08:00
Add backbone for passive buzzer device
need to implement EEPROM and ESP commands related to buzzer apply astyle
This commit is contained in:
parent
676df60b91
commit
4330d8eb30
172
esp3d/src/modules/buzzer/buzzer.cpp
Normal file
172
esp3d/src/modules/buzzer/buzzer.cpp
Normal file
@ -0,0 +1,172 @@
|
||||
/*
|
||||
buzzer.cpp - ESP3D buzzer class
|
||||
|
||||
Copyright (c) 2014 Luc Lebosse. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "../../include/esp3d_config.h"
|
||||
|
||||
#ifdef BUZZER_FEATURE
|
||||
#include <Ticker.h>
|
||||
#include "buzzer.h"
|
||||
BuzzerDevice 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);
|
||||
#endif //ARDUINO_ARCH_ESP8266
|
||||
|
||||
|
||||
void process()
|
||||
{
|
||||
if (buzzer.started()) {
|
||||
tone_data * current = buzzer.getNextTone();
|
||||
if (current) {
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
tone(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BuzzerDevice::BuzzerDevice()
|
||||
{
|
||||
_head = nullptr;
|
||||
_tail = nullptr;
|
||||
}
|
||||
|
||||
void BuzzerDevice::begin()
|
||||
{
|
||||
_started = true;
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL);
|
||||
#endif //ARDUINO_ARCH_ESP32
|
||||
|
||||
}
|
||||
void BuzzerDevice::end()
|
||||
{
|
||||
purgeData();
|
||||
no_tone();
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
ledcDetachPin(BUZZER_PIN);
|
||||
#endif //ARDUINO_ARCH_ESP32
|
||||
}
|
||||
|
||||
void BuzzerDevice::beep(int count, int delay, int frequency)
|
||||
{
|
||||
while (count > 0) {
|
||||
playsound(frequency,BEEP_DURATION);
|
||||
if (delay > 0 ) {
|
||||
playsound(0,delay);
|
||||
}
|
||||
waitWhilePlaying();
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
void BuzzerDevice::no_tone()
|
||||
{
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
tone(BUZZER_PIN, 0, 0);
|
||||
#endif //ARDUINO_ARCH_ESP8266
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
ledcWrite(BUZZER_CHANNEL, 0);
|
||||
#endif //ARDUINO_ARCH_ESP32
|
||||
|
||||
}
|
||||
|
||||
bool BuzzerDevice::isPlaying()
|
||||
{
|
||||
return !(_head == nullptr);
|
||||
}
|
||||
void BuzzerDevice::waitWhilePlaying()
|
||||
{
|
||||
while (_head != nullptr) {
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
bool BuzzerDevice::addToneToList(int frequency, int duration)
|
||||
{
|
||||
tone_data * tmp = (tone_data*)malloc(sizeof(tone_data));
|
||||
bool startprocess = false;
|
||||
if (tmp) {
|
||||
tmp->_next = nullptr;
|
||||
tmp->frequency = frequency;
|
||||
tmp->duration = duration;
|
||||
tmp->processing = false;
|
||||
if (_tail) {
|
||||
_tail->_next=tmp;
|
||||
} else { //no tail means also no head
|
||||
_head = tmp;
|
||||
startprocess = true;//no ongoing list, so lets start it
|
||||
}
|
||||
_tail = tmp;
|
||||
if(startprocess) {
|
||||
process();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
tone_data * BuzzerDevice::getNextTone()
|
||||
{
|
||||
if (_head) {
|
||||
if (_head->processing == false) {
|
||||
_head->processing = true;
|
||||
} else {
|
||||
tone_data * tmp = _head->_next;
|
||||
free(_head);
|
||||
_head = tmp;
|
||||
if (!_head) {
|
||||
_tail=_head;
|
||||
no_tone();
|
||||
}
|
||||
}
|
||||
}
|
||||
return _head;
|
||||
}
|
||||
|
||||
void BuzzerDevice::purgeData()
|
||||
{
|
||||
while (_head) {
|
||||
tone_data * tmp = _head->_next;
|
||||
free(_head);
|
||||
_head = tmp;
|
||||
}
|
||||
_tail = nullptr;
|
||||
}
|
||||
|
||||
BuzzerDevice::~BuzzerDevice()
|
||||
{
|
||||
end();
|
||||
}
|
||||
|
||||
void BuzzerDevice::playsound(int frequency, int duration)
|
||||
{
|
||||
if (_started) {
|
||||
addToneToList(frequency, duration);
|
||||
}
|
||||
}
|
||||
|
||||
#endif //BUZZER_FEATURE
|
57
esp3d/src/modules/buzzer/buzzer.h
Normal file
57
esp3d/src/modules/buzzer/buzzer.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
buzzer.h - ESP3D buzzer class
|
||||
|
||||
Copyright (c) 2014 Luc Lebosse. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef _BUZZER_H
|
||||
#define _BUZZER_H
|
||||
#define BEEP_FREQUENCY 3000
|
||||
|
||||
struct tone_data {
|
||||
int frequency;
|
||||
int duration;
|
||||
bool processing;
|
||||
tone_data * _next;
|
||||
};
|
||||
|
||||
class BuzzerDevice
|
||||
{
|
||||
public:
|
||||
BuzzerDevice();
|
||||
~BuzzerDevice();
|
||||
void playsound(int frequency, int duration);
|
||||
bool started()
|
||||
{
|
||||
return _started;
|
||||
}
|
||||
void begin();
|
||||
void end();
|
||||
tone_data * getNextTone();
|
||||
bool isPlaying();
|
||||
void waitWhilePlaying();
|
||||
void beep(int count=1, int delay = 0, int frequency = BEEP_FREQUENCY);
|
||||
private:
|
||||
tone_data * _head;
|
||||
tone_data * _tail;
|
||||
bool _started;
|
||||
void purgeData();
|
||||
bool addToneToList(int frequency, int duration);
|
||||
void no_tone();
|
||||
|
||||
};
|
||||
extern BuzzerDevice buzzer;
|
||||
#endif //_BUZZER_H
|
Loading…
x
Reference in New Issue
Block a user