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
@ -38,17 +38,17 @@ void Commands::process(uint8_t * sbuf, size_t len, ESP3DOutput * output, level_a
|
||||
if(is_esp_command(sbuf,len)) {
|
||||
size_t slen = len;
|
||||
String tmpbuf = (const char*)sbuf;
|
||||
if (tmpbuf.startsWith("echo: ")){
|
||||
if (tmpbuf.startsWith("echo: ")) {
|
||||
tmpbuf.replace("echo: ", "");
|
||||
slen = tmpbuf.length();
|
||||
}
|
||||
|
||||
|
||||
uint8_t cmd[4];
|
||||
cmd[0] = tmpbuf[4];
|
||||
cmd[1] = tmpbuf[5];
|
||||
cmd[2] = tmpbuf[6];
|
||||
cmd[3] = 0x0;
|
||||
|
||||
|
||||
//log_esp3d("Authentication = %d client %d", auth, output->client());
|
||||
execute_internal_command (String((const char*)cmd).toInt(), (slen > 8)?(const char*)&tmpbuf[8]:"", auth, (outputonly == nullptr)?output:outputonly);
|
||||
} else {
|
||||
@ -81,8 +81,8 @@ bool Commands::is_esp_command(uint8_t * sbuf, size_t len)
|
||||
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 (len >= 14){
|
||||
if ((char(sbuf[8]) == 'S') && (char(sbuf[9]) == 'P') && (char(sbuf[13]) == ']')){
|
||||
if (len >= 14) {
|
||||
if ((char(sbuf[8]) == 'S') && (char(sbuf[9]) == 'P') && (char(sbuf[13]) == ']')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -442,7 +442,7 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_
|
||||
case 800:
|
||||
response = ESP800(cmd_params, auth_type, output);
|
||||
break;
|
||||
|
||||
|
||||
//Get state / Set Enable / Disable Serial Communication
|
||||
//[ESP900]<ENABLE/DISABLE>
|
||||
case 900:
|
||||
|
@ -40,7 +40,7 @@ bool Commands::ESP900(const char* cmd_params, level_authenticate_type auth_type,
|
||||
parameter = get_param (cmd_params, "");
|
||||
//get
|
||||
if (parameter.length() == 0) {
|
||||
if (serial_service.started()){
|
||||
if (serial_service.started()) {
|
||||
output->printMSG("ENABLED");
|
||||
} else {
|
||||
output->printMSG("DISABLED");
|
||||
@ -57,8 +57,8 @@ bool Commands::ESP900(const char* cmd_params, level_authenticate_type auth_type,
|
||||
output->printMSG ("Serial communication disabled");
|
||||
serial_service.end();
|
||||
} else {
|
||||
output->printERROR("Cannot enable serial communication!", 500);
|
||||
response = false;
|
||||
output->printERROR("Cannot enable serial communication!", 500);
|
||||
response = false;
|
||||
}
|
||||
}
|
||||
return response;
|
||||
|
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,11 +127,11 @@ bool Display::showStatus(bool force)
|
||||
refresh_status = true;
|
||||
status+=" ";
|
||||
//log_esp3d("current %s", status.c_str());
|
||||
if (status_shift != -1){
|
||||
if (status_shift != -1) {
|
||||
if( (uint16_t)(status_shift)> status.length()) {
|
||||
status_shift = -1;
|
||||
status_shift = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
//log_esp3d("shift %d", status_shift);
|
||||
if (status_shift > 0) {
|
||||
status.remove(0,status_shift);
|
||||
|
@ -160,7 +160,7 @@ bool NotificationsService::sendPushoverMSG(const char * title, const char * mess
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
TSecureClient Notificationclient;
|
||||
#pragma GCC diagnostic pop
|
||||
#pragma GCC diagnostic pop
|
||||
#if defined(ARDUINO_ARCH_ESP8266) && !defined(USING_AXTLS)
|
||||
Notificationclient.setInsecure();
|
||||
#endif //ARDUINO_ARCH_ESP8266 && !USING_AXTLS
|
||||
|
Loading…
x
Reference in New Issue
Block a user