diff --git a/esp3d/configuration.h b/esp3d/configuration.h index ac763979..0ea69215 100644 --- a/esp3d/configuration.h +++ b/esp3d/configuration.h @@ -49,7 +49,7 @@ //OLED_I2C_SSD1306 1 //OLED_I2C_SSDSH1106 2 //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 #define BUZZER_DEVICE @@ -77,7 +77,7 @@ //#define DHT_DEVICE #ifdef BUZZER_DEVICE -#define ESP3D_BUZZER_PIN 5 +#define ESP3D_BUZZER_PIN 15 #endif //BUZZER_DEVICE #ifdef DHT_DEVICE diff --git a/esp3d/src/core/hal.cpp b/esp3d/src/core/hal.cpp index 80d08f47..2b214503 100644 --- a/esp3d/src/core/hal.cpp +++ b/esp3d/src/core/hal.cpp @@ -77,6 +77,38 @@ void Hal::pinMode(uint8_t pin, uint8_t 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) { #ifdef ARDUINO_ARCH_ESP8266 //only one ADC on ESP8266 A0 @@ -87,7 +119,26 @@ int Hal::analogRead(uint8_t pin) #endif } -bool Hal::analogWrite(uint8_t pin, uint value) +#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) { if (value > (_analogWriteRange-1)) { return false; @@ -96,21 +147,7 @@ bool Hal::analogWrite(uint8_t pin, uint value) analogWrite(pin, value); #endif //ARDUINO_ARCH_ESP8266 #ifdef ARDUINO_ARCH_ESP32 - int channel = -1; - 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; - } - } - } + int channel = getAnalogWriteChannel(pin); if (channel==-1) { return false; } @@ -133,7 +170,6 @@ bool Hal::analogWrite(uint8_t pin, uint value) _analogWriteRange = 255; break; } - ledcSetup(channel, _analogWriteFreq, resolution); ledcAttachPin(pin, channel); ledcWrite(channel, value); @@ -170,6 +206,9 @@ bool Hal::begin() //End ESP3D void Hal::end() { +#if defined(ARDUINO_ARCH_ESP32) + clearAnalogChannels(); +#endif //ARDUINO_ARCH_ESP32 } //Watchdog feeder diff --git a/esp3d/src/core/hal.h b/esp3d/src/core/hal.h index ad158105..a4e243ec 100644 --- a/esp3d/src/core/hal.h +++ b/esp3d/src/core/hal.h @@ -50,9 +50,14 @@ public: static bool analogWrite(uint8_t pin, uint value); static void analogWriteFreq(uint32_t freq); 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: static void wdtFeed(); static uint32_t _analogWriteRange; static uint32_t _analogWriteFreq; +#if defined(ARDUINO_ARCH_ESP32) + static int getAnalogWriteChannel(uint8_t pin); +#endif //ARDUINO_ARCH_ESP32 }; #endif //_ESP3D_HAL_H diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index 3d1773c0..3163d6cd 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.a11" +#define FW_VERSION "3.0.0.a12" #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 4a726f96..e2da2462 100644 --- a/esp3d/src/modules/buzzer/buzzer.cpp +++ b/esp3d/src/modules/buzzer/buzzer.cpp @@ -24,9 +24,9 @@ #include #include "buzzer.h" #include "../../core/settings_esp3d.h" +#include "../../core/hal.h" BuzzerDevice esp3d_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); @@ -38,12 +38,7 @@ void process() if (esp3d_buzzer.started()) { tone_data * current = esp3d_buzzer.getNextTone(); if (current) { -#if defined(ARDUINO_ARCH_ESP8266) - 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 + Hal::toneESP(ESP3D_BUZZER_PIN,(unsigned int)current->frequency, (unsigned long) current->duration, false); buzzer_tick.once_ms(current->duration, process); } } @@ -63,9 +58,8 @@ bool BuzzerDevice::begin() } if (Settings_ESP3D::read_byte(ESP_BUZZER) == 1) { _started = true; -#if defined(ARDUINO_ARCH_ESP32) - ledcAttachPin(ESP3D_BUZZER_PIN, BUZZER_CHANNEL); -#endif //ARDUINO_ARCH_ESP32 + playsound(5000, 240); + playsound(3000, 120); } return _started; } @@ -75,11 +69,7 @@ void BuzzerDevice::end() return; } purgeData(); - //no_tone(); _started = false; -#if defined(ARDUINO_ARCH_ESP32) - ledcDetachPin(ESP3D_BUZZER_PIN); -#endif //ARDUINO_ARCH_ESP32 no_tone(); } @@ -105,13 +95,7 @@ void BuzzerDevice::beep(int count, int delay, int frequency) void BuzzerDevice::no_tone() { -#if defined(ARDUINO_ARCH_ESP8266) - tone(ESP3D_BUZZER_PIN, 0, 0); -#endif //ARDUINO_ARCH_ESP8266 -#if defined(ARDUINO_ARCH_ESP32) - ledcWrite(BUZZER_CHANNEL, 0); -#endif //ARDUINO_ARCH_ESP32 - + Hal::no_tone(ESP3D_BUZZER_PIN); } bool BuzzerDevice::isPlaying()