diff --git a/docs/Commands.txt b/docs/Commands.txt index 0f5736a9..d627175e 100644 --- a/docs/Commands.txt +++ b/docs/Commands.txt @@ -76,6 +76,10 @@ Flash pins (6~11) cannot be used * Touch Calibration [ESP215][pwd=] +* Play sound +No parameter just play beep +[ESP250]F= D= [pwd=] + * Get full EEPROM settings content but do not give any passwords [ESP400] pwd= @@ -95,7 +99,7 @@ ESP_BAUD_RATE 112 //4 bytes = int ESP_NOTIFICATION_TYPE 116 //1 byte = flag ESP_CALIBRATION 117 //1 byte = flag ESP_AP_CHANNEL 118 //1 byte = flag -FREE 119 //1 byte = flag +ESP_BUZZER 119 //1 byte = flag FREE 120 //1 byte = flag ESP_HTTP_PORT 121 //4 bytes = int ESP_TELNET_PORT 125 //4 bytes = int @@ -168,3 +172,9 @@ Get will give type and settings only, not the protected T1/T2 * FW Informations [ESP800] pwd= + +* Get state / Set Enable / Disable Serial Communication +[ESP900][pwd=] + +* Get state / Set Enable / Disable buzzer +[ESP910][pwd=] diff --git a/esp3d/configuration.h b/esp3d/configuration.h index 7facf4d7..cd923502 100644 --- a/esp3d/configuration.h +++ b/esp3d/configuration.h @@ -51,6 +51,10 @@ //TFT_SPI_ILI9341_320X240 3 #define DISPLAY_DEVICE OLED_I2C_SSD1306 +//BUZZER_DEVICE: allow to connect passive buzzer +#define BUZZER_DEVICE + + #if defined (DISPLAY_DEVICE) //for ILI9143 edit User_Setup.h of TFT_eSPI library #if (DISPLAY_DEVICE == OLED_I2C_SSD1306) || (DISPLAY_DEVICE == OLED_I2C_SSDSH1106) @@ -72,6 +76,10 @@ //DHT_DEVICE: send update of temperature / humidity based on DHT 11/22 //#define DHT_DEVICE +#ifdef BUZZER_DEVICE +#define ESP3D_BUZZER_PIN 5 +#endif //BUZZER_DEVICE + #ifdef DHT_DEVICE #define ESP3D_DHT_PIN 22 //USE_CELSIUS diff --git a/esp3d/src/core/commands.cpp b/esp3d/src/core/commands.cpp index 6d23c52a..e6aeb937 100644 --- a/esp3d/src/core/commands.cpp +++ b/esp3d/src/core/commands.cpp @@ -366,6 +366,13 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ response = ESP215(cmd_params, auth_type, output); break; #endif //DISPLAY_TOUCH_DRIVER +#ifdef BUZZER_DEVICE + //Play sound + //[ESP250]F= D= [pwd=] + case 250: + response = ESP250(cmd_params, auth_type, output); + break; +#endif //BUZZER_DEVICE #endif //DISPLAY_DEVICE //Get full ESP3D settings //[ESP400] @@ -448,7 +455,13 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ case 900: response = ESP900(cmd_params, auth_type, output); break; - +#ifdef BUZZER_DEVICE + //Get state / Set Enable / Disable buzzer + //[ESP910] + case 910: + response = ESP910(cmd_params, auth_type, output); + break; +#endif //BUZZER_DEVICE default: output->printERROR ("Invalid Command"); response = false; diff --git a/esp3d/src/core/commands.h b/esp3d/src/core/commands.h index a8dd4a71..944e61ec 100644 --- a/esp3d/src/core/commands.h +++ b/esp3d/src/core/commands.h @@ -108,6 +108,10 @@ public: #endif //FILESYSTEM_FEATURE bool ESP800(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); bool ESP900(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); +#ifdef BUZZER_DEVICE + bool ESP910(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); + bool ESP250(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); +#endif //BUZZER_DEVICE }; extern Commands esp3d_commands; diff --git a/esp3d/src/core/espcmd/ESP250.cpp b/esp3d/src/core/espcmd/ESP250.cpp new file mode 100644 index 00000000..3539df8e --- /dev/null +++ b/esp3d/src/core/espcmd/ESP250.cpp @@ -0,0 +1,70 @@ +/* + ESP250.cpp - ESP3D command 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" +#if defined (BUZZER_DEVICE) +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/buzzer/buzzer.h" +//Play sound +//[ESP250]F= D= [pwd=] +bool Commands::ESP250(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool response = true; + String parameter; +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + output->printERROR("Wrong authentication!", 401); + return false; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + if (!esp3d_buzzer.started()) { + output->printERROR ("Buzzer disabled"); + return false; + } + parameter = get_param (cmd_params, ""); + //get + if (parameter.length() == 0) { + esp3d_buzzer.beep(); + } else { + int f,d; + //frequency + parameter = get_param (cmd_params, "F="); + if (parameter.length() == 0) { + output->printERROR ("No frequency"); + return false; + } + f = parameter.toInt(); + parameter = get_param (cmd_params, "D="); + if (parameter.length() == 0) { + output->printERROR ("No duration"); + return false; + } + d = parameter.toInt(); + esp3d_buzzer.playsound(f,d); + } + output->printMSG ("ok"); + return response; +} + +#endif //BUZZER_DEVICE diff --git a/esp3d/src/core/espcmd/ESP400.cpp b/esp3d/src/core/espcmd/ESP400.cpp index a15e7857..df040b4e 100644 --- a/esp3d/src/core/espcmd/ESP400.cpp +++ b/esp3d/src/core/espcmd/ESP400.cpp @@ -385,6 +385,14 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, output->print (Settings_ESP3D::get_min_string_size(ESP_NOTIFICATION_SETTINGS)); output->printLN ("\"}"); #endif //NOTIFICATION_FEATURE +#ifdef BUZZER_DEVICE + //Buzzer state + output->print (",{\"F\":\"printer\",\"P\":\""); + output->print (ESP_BUZZER); + output->print ("\",\"T\":\"B\",\"V\":\""); + output->print (Settings_ESP3D::read_byte(ESP_BUZZER)); + output->printLN ("\",\"H\":\"Buzzer\",\"O\":[{\"No\":\"0\"},{\"Yes\":\"1\"}]}"); +#endif //BUZZER_DEVICE //Target FW output->print (",{\"F\":\"printer\",\"P\":\""); output->print (ESP_TARGET_FW); diff --git a/esp3d/src/core/espcmd/ESP401.cpp b/esp3d/src/core/espcmd/ESP401.cpp index 29537fb6..59c2dc1d 100644 --- a/esp3d/src/core/espcmd/ESP401.cpp +++ b/esp3d/src/core/espcmd/ESP401.cpp @@ -25,6 +25,9 @@ #ifdef DHT_DEVICE #include "../../modules/dht/dht.h" #endif //DHT_DEVICE +#ifdef BUZZER_DEVICE +#include "../../modules/buzzer/buzzer.h" +#endif //BUZZER_DEVICE //Set EEPROM setting //[ESP401]P= T= V= pwd= bool Commands::ESP401(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) @@ -66,6 +69,15 @@ bool Commands::ESP401(const char* cmd_params, level_authenticate_type auth_type, esp3d_DHT.begin(); break; #endif //DHT_DEVICE +#ifdef BUZZER_DEVICE + case ESP_BUZZER: + if (sval.toInt() == 1) { + esp3d_buzzer.begin(); + } else if (sval.toInt() == 0) { + esp3d_buzzer.end(); + } + break; +#endif //BUZZER_DEVICE default: break; } diff --git a/esp3d/src/core/espcmd/ESP420.cpp b/esp3d/src/core/espcmd/ESP420.cpp index 8db1099d..70ae4fed 100644 --- a/esp3d/src/core/espcmd/ESP420.cpp +++ b/esp3d/src/core/espcmd/ESP420.cpp @@ -56,6 +56,10 @@ #ifdef NOTIFICATION_FEATURE #include "../../modules/notifications/notifications_service.h" #endif //NOTIFICATION_FEATURE +#ifdef BUZZER_DEVICE +#include "../../modules/buzzer/buzzer.h" +#endif //BUZZER_DEVICE + //Get ESP current status //output is JSON or plain text according parameter //[ESP420] @@ -974,6 +978,21 @@ bool Commands::ESP420(const char* cmd_params, level_authenticate_type auth_type, output->printLN(""); } #endif //TIMESTAMP_FEATURE + if (!plain) { + output->print (",{\"id\":\""); + } + output->print ("Serial communication"); + if (!plain) { + output->print ("\",\"value\":\""); + } else { + output->print (": "); + } + output->print (serial_service.started()?"Enabled":"Disabled"); + if (!plain) { + output->print ("\"}"); + } else { + output->printLN(""); + } #if defined (NOTIFICATION_FEATURE) if (!plain) { output->print (",{\"id\":\""); @@ -1016,6 +1035,23 @@ bool Commands::ESP420(const char* cmd_params, level_authenticate_type auth_type, output->printLN(""); } #endif //DHT_DEVICE +#if defined (BUZZER_DEVICE) + if (!plain) { + output->print (",{\"id\":\""); + } + output->print ("Buzzer"); + if (!plain) { + output->print ("\",\"value\":\""); + } else { + output->print (": "); + } + output->print (esp3d_buzzer.started()?"Enabled":"Disabled"); + if (!plain) { + output->print ("\"}"); + } else { + output->printLN(""); + } +#endif //BUZZER_DEVICE #if defined (ESP_DEBUG_FEATURE) //debug if (!plain) { diff --git a/esp3d/src/core/espcmd/ESP900.cpp b/esp3d/src/core/espcmd/ESP900.cpp index 48b70028..9c75b800 100644 --- a/esp3d/src/core/espcmd/ESP900.cpp +++ b/esp3d/src/core/espcmd/ESP900.cpp @@ -57,7 +57,7 @@ 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); + output->printERROR("Incorrect command!"); response = false; } } diff --git a/esp3d/src/core/espcmd/ESP910.cpp b/esp3d/src/core/espcmd/ESP910.cpp new file mode 100644 index 00000000..9badea04 --- /dev/null +++ b/esp3d/src/core/espcmd/ESP910.cpp @@ -0,0 +1,70 @@ +/* + ESP910.cpp - ESP3D command 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" +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/buzzer/buzzer.h" +//Get state / Set Enable / Disable buzzer +//[ESP910][pwd=] +bool Commands::ESP910(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool response = true; + String parameter; +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + output->printERROR("Wrong authentication!", 401); + return false; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + parameter = get_param (cmd_params, ""); + //get + if (parameter.length() == 0) { + if (esp3d_buzzer.started()) { + output->printMSG("ENABLED"); + } else { + output->printMSG("DISABLED"); + } + } else { //set + if (!Settings_ESP3D::write_byte (ESP_BUZZER, (parameter == "ENABLE")?1:0)) { + output->printERROR ("Set failed!"); + response = false; + } + if (parameter == "ENABLE" ) { + + if (esp3d_buzzer.begin()) { + output->printMSG ("Buzzer enabled"); + } else { + output->printERROR("Cannot enable buzzer!", 500); + response = false; + } + } else if (parameter == "DISABLE" ) { + output->printMSG ("Buzzer disabled"); + esp3d_buzzer.end(); + } else { + output->printERROR("Incorrect command!"); + response = false; + } + } + return response; +} diff --git a/esp3d/src/core/settings_esp3d.cpp b/esp3d/src/core/settings_esp3d.cpp index db882ff9..4f7da506 100644 --- a/esp3d/src/core/settings_esp3d.cpp +++ b/esp3d/src/core/settings_esp3d.cpp @@ -75,7 +75,9 @@ #endif //ETH_FEATURE #endif //BLUETOOTH_FEATURE #endif //WIFI_FEATURE - +#ifdef BUZZER_DEVICE +#define DEFAULT_BUZZER_STATE 1 +#endif //BUZZER_DEVICE #define DEFAULT_ESP_BYTE 0 #define DEFAULT_ESP_STRING_SIZE 0 @@ -219,6 +221,11 @@ uint8_t Settings_ESP3D::get_default_byte_value(int pos) case ESP_RADIO_MODE: res = DEFAULT_ESP_RADIO_MODE; break; +#ifdef BUZZER_DEVICE + case ESP_BUZZER: + res = DEFAULT_BUZZER_STATE; + break; +#endif //BUZZER_DEVICE #ifdef NOTIFICATION_FEATURE case ESP_NOTIFICATION_TYPE: res = DEFAULT_NOTIFICATION_TYPE; @@ -975,7 +982,10 @@ bool Settings_ESP3D::reset() Settings_ESP3D::write_uint32 (ESP_CALIBRATION_4, Settings_ESP3D::get_default_int32_value(ESP_CALIBRATION_4)); Settings_ESP3D::write_uint32 (ESP_CALIBRATION_5, Settings_ESP3D::get_default_int32_value(ESP_CALIBRATION_5)); #endif // DISPLAY_DEVICE && DISPLAY_TOUCH_DRIVER - +#ifdef BUZZER_DEVICE + //Buzzer state + Settings_ESP3D::write_byte(ESP_BUZZER,Settings_ESP3D::get_default_byte_value(ESP_BUZZER)); +#endif //BUZZER_DEVICE #if defined (WIFI_FEATURE) || defined (BLUETOOTH_FEATURE) || defined (ETH_FEATURE) //Hostname Settings_ESP3D::write_string(ESP_HOSTNAME,Settings_ESP3D::get_default_string_value(ESP_HOSTNAME).c_str()); diff --git a/esp3d/src/core/settings_esp3d.h b/esp3d/src/core/settings_esp3d.h index 641668f7..c093b2ec 100644 --- a/esp3d/src/core/settings_esp3d.h +++ b/esp3d/src/core/settings_esp3d.h @@ -47,7 +47,7 @@ #define ESP_NOTIFICATION_TYPE 116 //1 byte = flag #define ESP_CALIBRATION 117 //1 byte = flag #define ESP_AP_CHANNEL 118 //1 byte = flag -//#define ESP_AP_AUTH_TYPE 119 //1 byte = flag +#define ESP_BUZZER 119 //1 byte = flag //#define ESP_SSID_VISIBLE 120 //1 byte = flag #define ESP_HTTP_PORT 121 //4 bytes = int #define ESP_TELNET_PORT 125 //4 bytes = int diff --git a/esp3d/src/include/esp3d_config.h b/esp3d/src/include/esp3d_config.h index 0394b585..5018e32d 100644 --- a/esp3d/src/include/esp3d_config.h +++ b/esp3d/src/include/esp3d_config.h @@ -39,7 +39,7 @@ #define RECOVERY_FEATURE #endif //PIN_RESET_FEATURE || SD_RECOVERY_FEATURE -#if defined(DISPLAY_DEVICE) || defined(DHT_DEVICE) || defined(RECOVERY_FEATURE) +#if defined(DISPLAY_DEVICE) || defined(DHT_DEVICE) || defined(RECOVERY_FEATURE) || defined(BUZZER_DEVICE) #define CONNECTED_DEVICES_FEATURE #endif //DISPLAY_DEVICE || DHT_DEVICE diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index 0286acab..59ef5f3e 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a9" +#define FW_VERSION "3.0.0.a10" #define REPOSITORY "https://github.com/luc-github/ESP3D" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/buzzer/buzzer.cpp b/esp3d/src/modules/buzzer/buzzer.cpp index eb860c0f..2b827b51 100644 --- a/esp3d/src/modules/buzzer/buzzer.cpp +++ b/esp3d/src/modules/buzzer/buzzer.cpp @@ -20,10 +20,11 @@ #include "../../include/esp3d_config.h" -#ifdef BUZZER_FEATURE +#ifdef BUZZER_DEVICE #include #include "buzzer.h" -BuzzerDevice buzzer; +#include "../../core/settings_esp3d.h" +BuzzerDevice esp3d_buzzer; Ticker buzzer_tick; #define BUZZER_CHANNEL 1 #define BEEP_DURATION 200 @@ -34,11 +35,11 @@ extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration); void process() { - if (buzzer.started()) { - tone_data * current = buzzer.getNextTone(); + if (esp3d_buzzer.started()) { + tone_data * current = esp3d_buzzer.getNextTone(); if (current) { #if defined(ARDUINO_ARCH_ESP8266) - tone(BUZZER_PIN, (unsigned int)current->frequency, (unsigned long) current->duration); + 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); @@ -52,25 +53,41 @@ BuzzerDevice::BuzzerDevice() { _head = nullptr; _tail = nullptr; + _started = false; } -void BuzzerDevice::begin() +bool BuzzerDevice::begin() { - _started = true; + if(_started) { + end(); + } + if (Settings_ESP3D::read_byte(ESP_BUZZER) == 1) { + _started = true; #if defined(ARDUINO_ARCH_ESP32) - ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL); + ledcAttachPin(ESP3D_BUZZER_PIN, BUZZER_CHANNEL); #endif //ARDUINO_ARCH_ESP32 - + } + return _started; } void BuzzerDevice::end() { + if(!_started) { + return; + } purgeData(); - no_tone(); + //no_tone(); + _started = false; #if defined(ARDUINO_ARCH_ESP32) - ledcDetachPin(BUZZER_PIN); + ledcDetachPin(ESP3D_BUZZER_PIN); #endif //ARDUINO_ARCH_ESP32 } + +void BuzzerDevice::handle() +{ + //Nothing to do as handled by ticker +} + void BuzzerDevice::beep(int count, int delay, int frequency) { while (count > 0) { @@ -86,7 +103,7 @@ void BuzzerDevice::beep(int count, int delay, int frequency) void BuzzerDevice::no_tone() { #if defined(ARDUINO_ARCH_ESP8266) - tone(BUZZER_PIN, 0, 0); + tone(ESP3D_BUZZER_PIN, 0, 0); #endif //ARDUINO_ARCH_ESP8266 #if defined(ARDUINO_ARCH_ESP32) ledcWrite(BUZZER_CHANNEL, 0); @@ -169,4 +186,4 @@ void BuzzerDevice::playsound(int frequency, int duration) } } -#endif //BUZZER_FEATURE +#endif //BUZZER_DEVICE diff --git a/esp3d/src/modules/buzzer/buzzer.h b/esp3d/src/modules/buzzer/buzzer.h index 2c260e01..1e8d1df5 100644 --- a/esp3d/src/modules/buzzer/buzzer.h +++ b/esp3d/src/modules/buzzer/buzzer.h @@ -38,8 +38,9 @@ public: { return _started; } - void begin(); + bool begin(); void end(); + void handle(); tone_data * getNextTone(); bool isPlaying(); void waitWhilePlaying(); @@ -53,5 +54,5 @@ private: void no_tone(); }; -extern BuzzerDevice buzzer; +extern BuzzerDevice esp3d_buzzer; #endif //_BUZZER_H diff --git a/esp3d/src/modules/devices/devices_services.cpp b/esp3d/src/modules/devices/devices_services.cpp index d61eb394..d29066f5 100644 --- a/esp3d/src/modules/devices/devices_services.cpp +++ b/esp3d/src/modules/devices/devices_services.cpp @@ -30,6 +30,9 @@ #ifdef DHT_DEVICE #include "../dht/dht.h" #endif //DHT_DEVICE +#ifdef BUZZER_DEVICE +#include "../buzzer/buzzer.h" +#endif //BUZZER_DEVICE #ifdef RECOVERY_FEATURE #include "../recovery/recovery_service.h" #endif //RECOVERY_FEATURE @@ -60,6 +63,12 @@ bool DevicesServices::begin() res = false; } #endif //DHT_DEVICE +#ifdef BUZZER_DEVICE + if (!esp3d_buzzer.begin()) { + log_esp3d("Error starting buzzer device"); + res = false; + } +#endif //BUZZER_DEVICE #ifdef RECOVERY_FEATURE if (!recovery_service.begin()) { log_esp3d("Error starting recorery service"); @@ -81,6 +90,9 @@ void DevicesServices::end() #ifdef RECOVERY_FEATURE recovery_service.end(); #endif //RECOVERY_FEATURE +#ifdef BUZZER_DEVICE + esp3d_buzzer.end(); +#endif //BUZZER_DEVICE #ifdef DISPLAY_DEVICE esp3d_display.end(); #endif //DISPLAY_DEVICE @@ -98,6 +110,9 @@ void DevicesServices::handle() #ifdef DHT_DEVICE esp3d_DHT.handle(); #endif //DHT_DEVICE +#ifdef BUZZER_DEVICE + esp3d_buzzer.handle(); +#endif //BUZZER_DEVICE #ifdef RECOVERY_FEATURE recovery_service.handle(); #endif //RECOVERY_FEATURE