mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-08-02 23:50:40 +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
@ -38,7 +38,7 @@ void Commands::process(uint8_t * sbuf, size_t len, ESP3DOutput * output, level_a
|
|||||||
if(is_esp_command(sbuf,len)) {
|
if(is_esp_command(sbuf,len)) {
|
||||||
size_t slen = len;
|
size_t slen = len;
|
||||||
String tmpbuf = (const char*)sbuf;
|
String tmpbuf = (const char*)sbuf;
|
||||||
if (tmpbuf.startsWith("echo: ")){
|
if (tmpbuf.startsWith("echo: ")) {
|
||||||
tmpbuf.replace("echo: ", "");
|
tmpbuf.replace("echo: ", "");
|
||||||
slen = tmpbuf.length();
|
slen = tmpbuf.length();
|
||||||
}
|
}
|
||||||
@ -81,8 +81,8 @@ bool Commands::is_esp_command(uint8_t * sbuf, size_t len)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if((char(sbuf[0]) == 'e') && (char(sbuf[1]) == 'c') && (char(sbuf[2]) == 'h') && (char(sbuf[3]) == 'o') && (char(sbuf[4]) == ':') && (char(sbuf[5]) == ' ') && (char(sbuf[6]) == '[') && (char(sbuf[7]) == 'E')) {
|
if((char(sbuf[0]) == 'e') && (char(sbuf[1]) == 'c') && (char(sbuf[2]) == 'h') && (char(sbuf[3]) == 'o') && (char(sbuf[4]) == ':') && (char(sbuf[5]) == ' ') && (char(sbuf[6]) == '[') && (char(sbuf[7]) == 'E')) {
|
||||||
if (len >= 14){
|
if (len >= 14) {
|
||||||
if ((char(sbuf[8]) == 'S') && (char(sbuf[9]) == 'P') && (char(sbuf[13]) == ']')){
|
if ((char(sbuf[8]) == 'S') && (char(sbuf[9]) == 'P') && (char(sbuf[13]) == ']')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ bool Commands::ESP900(const char* cmd_params, level_authenticate_type auth_type,
|
|||||||
parameter = get_param (cmd_params, "");
|
parameter = get_param (cmd_params, "");
|
||||||
//get
|
//get
|
||||||
if (parameter.length() == 0) {
|
if (parameter.length() == 0) {
|
||||||
if (serial_service.started()){
|
if (serial_service.started()) {
|
||||||
output->printMSG("ENABLED");
|
output->printMSG("ENABLED");
|
||||||
} else {
|
} else {
|
||||||
output->printMSG("DISABLED");
|
output->printMSG("DISABLED");
|
||||||
|
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
|
@ -127,7 +127,7 @@ bool Display::showStatus(bool force)
|
|||||||
refresh_status = true;
|
refresh_status = true;
|
||||||
status+=" ";
|
status+=" ";
|
||||||
//log_esp3d("current %s", status.c_str());
|
//log_esp3d("current %s", status.c_str());
|
||||||
if (status_shift != -1){
|
if (status_shift != -1) {
|
||||||
if( (uint16_t)(status_shift)> status.length()) {
|
if( (uint16_t)(status_shift)> status.length()) {
|
||||||
status_shift = -1;
|
status_shift = -1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user