From 05995d1fd6c3b85717d428ff5c0c269f7872857b Mon Sep 17 00:00:00 2001 From: Ludy Date: Tue, 20 Aug 2019 09:01:37 +0200 Subject: [PATCH] Unify buzz methods as MarlinUI::buzz (#14803) --- Marlin/src/Marlin.cpp | 4 +- Marlin/src/feature/leds/pca9632.cpp | 8 +-- Marlin/src/feature/leds/pca9632.h | 3 +- Marlin/src/inc/Conditionals_post.h | 5 +- Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp | 29 +++++++++- Marlin/src/lcd/dogm/ultralcd_DOGM.cpp | 2 + Marlin/src/lcd/ultralcd.cpp | 59 +++++++-------------- Marlin/src/lcd/ultralcd.h | 14 ++--- Marlin/src/libs/buzzer.cpp | 4 +- Marlin/src/libs/buzzer.h | 21 ++++---- Marlin/src/module/printcounter.cpp | 2 + Marlin/src/module/temperature.cpp | 4 +- 12 files changed, 83 insertions(+), 72 deletions(-) diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp index ebcb0dda06..726f67139e 100644 --- a/Marlin/src/Marlin.cpp +++ b/Marlin/src/Marlin.cpp @@ -65,7 +65,7 @@ #include "feature/host_actions.h" #endif -#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER) +#if USE_BEEPER #include "libs/buzzer.h" #endif @@ -702,7 +702,7 @@ void idle( print_job_timer.tick(); #endif - #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER) && DISABLED(PCA9632_BUZZER) + #if USE_BEEPER buzzer.tick(); #endif diff --git a/Marlin/src/feature/leds/pca9632.cpp b/Marlin/src/feature/leds/pca9632.cpp index 6f69bd31ef..87589a9bcd 100644 --- a/Marlin/src/feature/leds/pca9632.cpp +++ b/Marlin/src/feature/leds/pca9632.cpp @@ -137,13 +137,15 @@ void pca9632_set_led_color(const LEDColor &color) { } #if ENABLED(PCA9632_BUZZER) - void pca9632_buzz(uint16_t const f, uint16_t d) { - UNUSED(f); UNUSED(d); + + void pca9632_buzz(const long duration, const uint16_t freq) { + UNUSED(duration); UNUSED(freq); uint8_t data[] = PCA9632_BUZZER_DATA; Wire.beginTransmission(I2C_ADDRESS(PCA9632_ADDRESS)); Wire.write(data, sizeof(data)); Wire.endTransmission(); } -#endif + +#endif // PCA9632_BUZZER #endif // PCA9632 diff --git a/Marlin/src/feature/leds/pca9632.h b/Marlin/src/feature/leds/pca9632.h index 023e451979..2abdf7c7af 100644 --- a/Marlin/src/feature/leds/pca9632.h +++ b/Marlin/src/feature/leds/pca9632.h @@ -32,5 +32,6 @@ typedef LEDColor LEDColor; void pca9632_set_led_color(const LEDColor &color); #if ENABLED(PCA9632_BUZZER) - void pca9632_buzz(uint16_t const, uint16_t); + #include + void pca9632_buzz(const long, const uint16_t); #endif diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index f3d850aea1..937db9ee6a 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -1048,7 +1048,8 @@ #define HAS_KILL (PIN_EXISTS(KILL)) #define HAS_SUICIDE (PIN_EXISTS(SUICIDE)) #define HAS_PHOTOGRAPH (PIN_EXISTS(PHOTOGRAPH)) -#define HAS_BUZZER (PIN_EXISTS(BEEPER) || ENABLED(LCD_USE_I2C_BUZZER) || ENABLED(PCA9632_BUZZER)) +#define HAS_BUZZER (PIN_EXISTS(BEEPER) || EITHER(LCD_USE_I2C_BUZZER, PCA9632_BUZZER)) +#define USE_BEEPER (HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER, PCA9632_BUZZER)) #define HAS_CASE_LIGHT (PIN_EXISTS(CASE_LIGHT) && ENABLED(CASE_LIGHT_ENABLE)) // Digital control @@ -1570,7 +1571,7 @@ #ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS #define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100 #endif -#else +#elif HAS_BUZZER #ifndef LCD_FEEDBACK_FREQUENCY_HZ #define LCD_FEEDBACK_FREQUENCY_HZ 5000 #endif diff --git a/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp b/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp index 9e1a614cd6..54eb614cb4 100644 --- a/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp +++ b/Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp @@ -360,6 +360,33 @@ void MarlinUI::init_lcd() { lcd.clear(); } +bool MarlinUI::detected() { + return true + #if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE) + && lcd.LcdDetected() == 1 + #endif + ; +} + +#if HAS_SLOW_BUTTONS + uint8_t MarlinUI::read_slow_buttons() { + #if ENABLED(LCD_I2C_TYPE_MCP23017) + // Reading these buttons this is likely to be too slow to call inside interrupt context + // so they are called during normal lcd_update + uint8_t slow_bits = lcd.readButtons() + #if !BUTTON_EXISTS(ENC) + << B_I2C_BTN_OFFSET + #endif + ; + #if ENABLED(LCD_I2C_VIKI) + if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked + slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated + #endif // LCD_I2C_VIKI + return slow_bits; + #endif // LCD_I2C_TYPE_MCP23017 + } +#endif + void MarlinUI::clear_lcd() { lcd.clear(); } #if ENABLED(SHOW_BOOTSCREEN) @@ -1063,7 +1090,7 @@ void MarlinUI::draw_status_screen() { #if ENABLED(LCD_HAS_STATUS_INDICATORS) - static void MarlinUI::update_indicators() { + void MarlinUI::update_indicators() { // Set the LEDS - referred to as backlights by the LiquidTWI2 library static uint8_t ledsprev = 0; uint8_t leds = 0; diff --git a/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp b/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp index eb5f69fe94..5d627de473 100644 --- a/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp +++ b/Marlin/src/lcd/dogm/ultralcd_DOGM.cpp @@ -102,6 +102,8 @@ void MarlinUI::set_font(const MarlinFont font_nr) { } } +bool MarlinUI::detected() { return true; } + #if ENABLED(SHOW_BOOTSCREEN) #if ENABLED(SHOW_CUSTOM_BOOTSCREEN) diff --git a/Marlin/src/lcd/ultralcd.cpp b/Marlin/src/lcd/ultralcd.cpp index 8fc66dddf9..f201c1b2a0 100644 --- a/Marlin/src/lcd/ultralcd.cpp +++ b/Marlin/src/lcd/ultralcd.cpp @@ -64,6 +64,19 @@ uint8_t MarlinUI::progress_bar_percent; // = 0 #endif +#if HAS_BUZZER + #include "../libs/buzzer.h" + void MarlinUI::buzz(const long duration, const uint16_t freq) { + #if ENABLED(LCD_USE_I2C_BUZZER) + lcd.buzz(duration, freq); + #elif ENABLED(PCA9632_BUZZER) + pca9632_buzz(const long duration, const uint16_t freq) { + #elif USE_BEEPER + buzzer.tone(duration, freq); + #endif + } +#endif + #if HAS_SPI_LCD #if HAS_GRAPHICAL_LCD @@ -89,10 +102,6 @@ #include "../feature/bedlevel/bedlevel.h" #endif -#if HAS_BUZZER - #include "../libs/buzzer.h" -#endif - #if HAS_TRINAMIC #include "../feature/tmc_util.h" #endif @@ -568,7 +577,7 @@ void MarlinUI::status_screen() { const millis_t ms = millis(); #endif if (ELAPSED(ms, next_beep)) { - BUZZ(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY); + buzz(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY); next_beep = ms + 500UL; } #endif @@ -611,13 +620,12 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) { #if HAS_BUZZER // Buzz and wait. Is the delay needed for buttons to settle? buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ); - #endif - - #if HAS_LCD_MENU - #if ENABLED(LCD_USE_I2C_BUZZER) - delay(10); - #elif PIN_EXISTS(BEEPER) - for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); } + #if HAS_LCD_MENU + #if USE_BEEPER + for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); } + #else + delay(10); + #endif #endif #endif } @@ -729,16 +737,6 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) { LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; -bool MarlinUI::detected() { - return - #if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE) - lcd.LcdDetected() == 1 - #else - true - #endif - ; -} - void MarlinUI::update() { static uint16_t max_display_update_time = 0; @@ -1295,23 +1293,6 @@ void MarlinUI::update() { #endif // HAS_ENCODER_WHEEL } - #if HAS_SLOW_BUTTONS - - uint8_t MarlinUI::read_slow_buttons() { - #if ENABLED(LCD_I2C_TYPE_MCP23017) - // Reading these buttons this is likely to be too slow to call inside interrupt context - // so they are called during normal lcd_update - uint8_t slow_bits = lcd.readButtons() << B_I2C_BTN_OFFSET; - #if ENABLED(LCD_I2C_VIKI) - if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked - slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated - #endif // LCD_I2C_VIKI - return slow_bits; - #endif // LCD_I2C_TYPE_MCP23017 - } - - #endif - #endif // HAS_ENCODER_ACTION #endif // HAS_SPI_LCD diff --git a/Marlin/src/lcd/ultralcd.h b/Marlin/src/lcd/ultralcd.h index 3d893ed0f9..4d0a541e19 100644 --- a/Marlin/src/lcd/ultralcd.h +++ b/Marlin/src/lcd/ultralcd.h @@ -259,15 +259,11 @@ public: } #if HAS_BUZZER - static inline void buzz(const long duration, const uint16_t freq) { - #if ENABLED(LCD_USE_I2C_BUZZER) - lcd.buzz(duration, freq); - #elif PIN_EXISTS(BEEPER) - buzzer.tone(duration, freq); - #elif ENABLED(PCA9632_BUZZER) - pca9632_buzz(duration, freq); - #endif - } + static void buzz(const long duration, const uint16_t freq); + #endif + + #if ENABLED(LCD_HAS_STATUS_INDICATORS) + static void update_indicators(); #endif // LCD implementations diff --git a/Marlin/src/libs/buzzer.cpp b/Marlin/src/libs/buzzer.cpp index aa20127fa0..549c76008e 100644 --- a/Marlin/src/libs/buzzer.cpp +++ b/Marlin/src/libs/buzzer.cpp @@ -22,7 +22,7 @@ #include "../inc/MarlinConfig.h" -#if DISABLED(LCD_USE_I2C_BUZZER) && PIN_EXISTS(BEEPER) +#if USE_BEEPER #include "buzzer.h" #include "../module/temperature.h" @@ -78,4 +78,4 @@ void Buzzer::tick() { else if (ELAPSED(now, state.endtime)) reset(); } -#endif // !LCD_USE_I2C_BUZZER && BEEPER +#endif // USE_BEEPER diff --git a/Marlin/src/libs/buzzer.h b/Marlin/src/libs/buzzer.h index 1908b6aeaf..026b9330f9 100644 --- a/Marlin/src/libs/buzzer.h +++ b/Marlin/src/libs/buzzer.h @@ -23,16 +23,7 @@ #include "../inc/MarlinConfig.h" -#if ENABLED(LCD_USE_I2C_BUZZER) - - #define BUZZ(d,f) ui.buzz(d,f) - -#elif ENABLED(PCA9632_BUZZER) - - #include "../feature/leds/pca9632.h" - #define BUZZ(d, f) pca9632_buzz(d,f) - -#elif PIN_EXISTS(BEEPER) +#if USE_BEEPER #include "circularqueue.h" @@ -120,10 +111,18 @@ // Provide a buzzer instance extern Buzzer buzzer; + + // Buzz directly via the BEEPER pin tone queue #define BUZZ(d,f) buzzer.tone(d, f) -#else // No buzz capability +#elif HAS_BUZZER + // Buzz indirectly via the MarlinUI instance + #define BUZZ(d,f) ui.buzz(d,f) + +#else + + // No buzz capability #define BUZZ(d,f) NOOP #endif diff --git a/Marlin/src/module/printcounter.cpp b/Marlin/src/module/printcounter.cpp index 25e8a767ab..ba308142e4 100644 --- a/Marlin/src/module/printcounter.cpp +++ b/Marlin/src/module/printcounter.cpp @@ -157,6 +157,8 @@ void PrintCounter::loadStats() { #endif #if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0 if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) BUZZ(200, 404); + #else + UNUSED(doBuzz); #endif #endif // HAS_SERVICE_INTERVALS } diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 79f63b5315..04492987b7 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -68,7 +68,7 @@ #include "tool_change.h" #endif -#if HAS_BUZZER && PIN_EXISTS(BEEPER) +#if USE_BEEPER #include "../libs/buzzer.h" #endif @@ -749,7 +749,7 @@ int16_t Temperature::getHeaterPower(const heater_ind_t heater_id) { inline void loud_kill(PGM_P const lcd_msg) { Running = false; - #if HAS_BUZZER && PIN_EXISTS(BEEPER) + #if USE_BEEPER for (uint8_t i = 20; i--;) { WRITE(BEEPER_PIN, HIGH); delay(25); WRITE(BEEPER_PIN, LOW); delay(80);