From 89a9c3a391101e4d2d8dbfbf0cdb261ad2d9592b Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Sat, 12 Mar 2022 16:34:58 -0600
Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20=20Add=20st?=
 =?UTF-8?q?andard=20BUZZ=20types?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/feature/mmu/mmu2.cpp         | 45 ++++++++++++-------------
 Marlin/src/gcode/bedlevel/mbl/G29.cpp   |  3 +-
 Marlin/src/gcode/geometry/M206_M428.cpp |  5 ++-
 Marlin/src/lcd/e3v2/jyersui/dwin.cpp    | 14 ++------
 Marlin/src/lcd/e3v2/proui/dwin.cpp      | 12 +------
 Marlin/src/lcd/menu/menu.cpp            |  6 +---
 Marlin/src/libs/buzzer.h                |  5 +++
 Marlin/src/module/printcounter.cpp      |  2 +-
 Marlin/src/module/probe.cpp             |  3 +-
 9 files changed, 36 insertions(+), 59 deletions(-)

diff --git a/Marlin/src/feature/mmu/mmu2.cpp b/Marlin/src/feature/mmu/mmu2.cpp
index 2813337c635..28dea681ef4 100644
--- a/Marlin/src/feature/mmu/mmu2.cpp
+++ b/Marlin/src/feature/mmu/mmu2.cpp
@@ -143,6 +143,8 @@ uint8_t MMU2::get_current_tool() {
   #define FILAMENT_PRESENT() (READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE)
 #endif
 
+inline void ATTN_BUZZ(const bool two=false) { BUZZ(200, 404); if (two) { BUZZ(10, 0); BUZZ(200, 404); } }
+
 void MMU2::mmu_loop() {
 
   switch (state) {
@@ -525,7 +527,7 @@ static void mmu2_not_responding() {
             while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
             load_filament_to_nozzle(index);
           #else
-            BUZZ(400, 40);
+            ERR_BUZZ();
           #endif
         } break;
 
@@ -544,7 +546,7 @@ static void mmu2_not_responding() {
               active_extruder = 0;
             }
           #else
-            BUZZ(400, 40);
+            ERR_BUZZ();
           #endif
         } break;
 
@@ -613,7 +615,7 @@ static void mmu2_not_responding() {
           while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
           load_filament_to_nozzle(index);
         #else
-          BUZZ(400, 40);
+          ERR_BUZZ();
         #endif
       } break;
 
@@ -633,7 +635,7 @@ static void mmu2_not_responding() {
           extruder = index;
           active_extruder = 0;
         #else
-          BUZZ(400, 40);
+          ERR_BUZZ();
         #endif
       } break;
 
@@ -707,7 +709,7 @@ static void mmu2_not_responding() {
           while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
           load_filament_to_nozzle(index);
         #else
-          BUZZ(400, 40);
+          ERR_BUZZ();
         #endif
       } break;
 
@@ -726,7 +728,7 @@ static void mmu2_not_responding() {
           extruder = index;
           active_extruder = 0;
         #else
-          BUZZ(400, 40);
+          ERR_BUZZ();
         #endif
       } break;
 
@@ -811,25 +813,21 @@ void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
       if (turn_off_nozzle && resume_hotend_temp) {
         thermalManager.setTargetHotend(resume_hotend_temp, active_extruder);
         LCD_MESSAGE(MSG_HEATING);
-        BUZZ(200, 40);
+        ERR_BUZZ();
 
         while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(1000);
       }
 
-      if (move_axes && all_axes_homed()) {
-        LCD_MESSAGE(MSG_MMU2_RESUMING);
-        BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404);
+      LCD_MESSAGE(MSG_MMU2_RESUMING);
+      ATTN_BUZZ(true);
 
+      if (move_axes && all_axes_homed()) {
         // Move XY to starting position, then Z
         do_blocking_move_to_xy(resume_position, feedRate_t(NOZZLE_PARK_XY_FEEDRATE));
 
         // Move Z_AXIS to saved position
         do_blocking_move_to_z(resume_position.z, feedRate_t(NOZZLE_PARK_Z_FEEDRATE));
       }
-      else {
-        BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404);
-        LCD_MESSAGE(MSG_MMU2_RESUMING);
-      }
     }
   }
 }
@@ -898,7 +896,7 @@ void MMU2::load_filament(const uint8_t index) {
 
   command(MMU_CMD_L0 + index);
   manage_response(false, false);
-  BUZZ(200, 404);
+  ATTN_BUZZ();
 }
 
 /**
@@ -909,7 +907,7 @@ bool MMU2::load_filament_to_nozzle(const uint8_t index) {
   if (!_enabled) return false;
 
   if (thermalManager.tooColdToExtrude(active_extruder)) {
-    BUZZ(200, 404);
+    ATTN_BUZZ();
     LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
     return false;
   }
@@ -924,7 +922,7 @@ bool MMU2::load_filament_to_nozzle(const uint8_t index) {
     extruder = index;
     active_extruder = 0;
     load_to_nozzle();
-    BUZZ(200, 404);
+    ATTN_BUZZ();
   }
   return success;
 }
@@ -945,7 +943,7 @@ bool MMU2::eject_filament(const uint8_t index, const bool recover) {
   if (!_enabled) return false;
 
   if (thermalManager.tooColdToExtrude(active_extruder)) {
-    BUZZ(200, 404);
+    ATTN_BUZZ();
     LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
     return false;
   }
@@ -961,12 +959,11 @@ bool MMU2::eject_filament(const uint8_t index, const bool recover) {
 
   if (recover)  {
     LCD_MESSAGE(MSG_MMU2_EJECT_RECOVER);
-    BUZZ(200, 404);
+    ATTN_BUZZ();
     TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_do(PROMPT_USER_CONTINUE, F("MMU2 Eject Recover"), FPSTR(CONTINUE_STR)));
     TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired(F("MMU2 Eject Recover")));
     TERN_(HAS_RESUME_CONTINUE, wait_for_user_response());
-    BUZZ(200, 404);
-    BUZZ(200, 404);
+    ATTN_BUZZ(true);
 
     command(MMU_CMD_R0);
     manage_response(false, false);
@@ -979,7 +976,7 @@ bool MMU2::eject_filament(const uint8_t index, const bool recover) {
 
   set_runout_valid(false);
 
-  BUZZ(200, 404);
+  ATTN_BUZZ();
 
   stepper.disable_extruder();
 
@@ -994,7 +991,7 @@ bool MMU2::unload() {
   if (!_enabled) return false;
 
   if (thermalManager.tooColdToExtrude(active_extruder)) {
-    BUZZ(200, 404);
+    ATTN_BUZZ();
     LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
     return false;
   }
@@ -1005,7 +1002,7 @@ bool MMU2::unload() {
   command(MMU_CMD_U0);
   manage_response(false, true);
 
-  BUZZ(200, 404);
+  ATTN_BUZZ();
 
   // no active tool
   extruder = MMU2_NO_TOOL;
diff --git a/Marlin/src/gcode/bedlevel/mbl/G29.cpp b/Marlin/src/gcode/bedlevel/mbl/G29.cpp
index f262546d619..f06bfc0fdce 100644
--- a/Marlin/src/gcode/bedlevel/mbl/G29.cpp
+++ b/Marlin/src/gcode/bedlevel/mbl/G29.cpp
@@ -155,8 +155,7 @@ void GcodeSuite::G29() {
         mbl_probe_index = -1;
         SERIAL_ECHOLNPGM("Mesh probing done.");
         TERN_(HAS_STATUS_MESSAGE, LCD_MESSAGE(MSG_MESH_DONE));
-        BUZZ(100, 659);
-        BUZZ(100, 698);
+        OKAY_BUZZ();
 
         home_all_axes();
         set_bed_leveling_enabled(true);
diff --git a/Marlin/src/gcode/geometry/M206_M428.cpp b/Marlin/src/gcode/geometry/M206_M428.cpp
index 131dbecf334..b32d7299694 100644
--- a/Marlin/src/gcode/geometry/M206_M428.cpp
+++ b/Marlin/src/gcode/geometry/M206_M428.cpp
@@ -92,7 +92,7 @@ void GcodeSuite::M428() {
     if (!WITHIN(diff[i], -20, 20)) {
       SERIAL_ERROR_MSG(STR_ERR_M428_TOO_FAR);
       LCD_ALERTMESSAGE_F("Err: Too far!");
-      BUZZ(200, 40);
+      ERR_BUZZ();
       return;
     }
   }
@@ -100,8 +100,7 @@ void GcodeSuite::M428() {
   LOOP_LINEAR_AXES(i) set_home_offset((AxisEnum)i, diff[i]);
   report_current_position();
   LCD_MESSAGE(MSG_HOME_OFFSETS_APPLIED);
-  BUZZ(100, 659);
-  BUZZ(100, 698);
+  OKAY_BUZZ();
 }
 
 #endif // HAS_M206_COMMAND
diff --git a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp
index d384bae3e33..51df15da739 100644
--- a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp
+++ b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp
@@ -4920,18 +4920,10 @@ void CrealityDWINClass::Screen_Update() {
 }
 
 void CrealityDWINClass::AudioFeedback(const bool success/*=true*/) {
-  if (success) {
-    if (ui.buzzer_enabled) {
-      BUZZ(100, 659);
-      BUZZ( 10,   0);
-      BUZZ(100, 698);
-    }
-    else Update_Status("Success");
-  }
-  else if (ui.buzzer_enabled)
-    BUZZ(40, 440);
+  if (ui.buzzer_enabled)
+    DONE_BUZZ(success);
   else
-    Update_Status("Failed");
+    Update_Status(success ? "Success" : "Failed");
 }
 
 void CrealityDWINClass::Save_Settings(char *buff) {
diff --git a/Marlin/src/lcd/e3v2/proui/dwin.cpp b/Marlin/src/lcd/e3v2/proui/dwin.cpp
index 95cb0b28b92..66e90740fdf 100644
--- a/Marlin/src/lcd/e3v2/proui/dwin.cpp
+++ b/Marlin/src/lcd/e3v2/proui/dwin.cpp
@@ -1285,17 +1285,7 @@ void HMI_Printing() {
 
 #include "../../../libs/buzzer.h"
 
-void HMI_AudioFeedback(const bool success/*=true*/) {
-  #if HAS_BUZZER
-    if (success) {
-      BUZZ(100, 659);
-      BUZZ(10, 0);
-      BUZZ(100, 698);
-    }
-    else
-      BUZZ(40, 440);
-  #endif
-}
+void HMI_AudioFeedback(const bool success/*=true*/) { DONE_BUZZ(success); }
 
 void Draw_Main_Area() {
   switch (checkkey) {
diff --git a/Marlin/src/lcd/menu/menu.cpp b/Marlin/src/lcd/menu/menu.cpp
index 2dca6c1b826..52c43ec5e90 100644
--- a/Marlin/src/lcd/menu/menu.cpp
+++ b/Marlin/src/lcd/menu/menu.cpp
@@ -276,11 +276,7 @@ void scroll_screen(const uint8_t limit, const bool is_menu) {
 #if HAS_BUZZER
   void MarlinUI::completion_feedback(const bool good/*=true*/) {
     TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); // Wake up on rotary encoder click...
-    if (good) {
-      BUZZ(100, 659);
-      BUZZ(100, 698);
-    }
-    else BUZZ(20, 440);
+    if (good) OKAY_BUZZ(); else ERR_BUZZ();
   }
 #endif
 
diff --git a/Marlin/src/libs/buzzer.h b/Marlin/src/libs/buzzer.h
index db5e3ee4ca7..9fc46ff6296 100644
--- a/Marlin/src/libs/buzzer.h
+++ b/Marlin/src/libs/buzzer.h
@@ -127,3 +127,8 @@
   #define BUZZ(d,f) NOOP
 
 #endif
+
+#define ERR_BUZZ() BUZZ(400, 40);
+#define ATTN_BUZZ() do{ BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404); }while(0)
+#define OKAY_BUZZ() do{ BUZZ(100, 659); BUZZ(10, 0); BUZZ(100, 698); }while(0)
+#define DONE_BUZZ(OK) do{ if (OK) OKAY_BUZZ(); else ERR_BUZZ(); }while(0)
diff --git a/Marlin/src/module/printcounter.cpp b/Marlin/src/module/printcounter.cpp
index affb6087802..c35b722cf4c 100644
--- a/Marlin/src/module/printcounter.cpp
+++ b/Marlin/src/module/printcounter.cpp
@@ -151,7 +151,7 @@ void PrintCounter::loadStats() {
       if (data.nextService3 == 0) doBuzz = _service_warn(PSTR(" " SERVICE_NAME_3));
     #endif
     #if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0
-      if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) BUZZ(200, 404);
+      if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) { BUZZ(200, 404); BUZZ(10, 0); }
     #else
       UNUSED(doBuzz);
     #endif
diff --git a/Marlin/src/module/probe.cpp b/Marlin/src/module/probe.cpp
index 8793ea49dcf..ee6323518a2 100644
--- a/Marlin/src/module/probe.cpp
+++ b/Marlin/src/module/probe.cpp
@@ -306,8 +306,7 @@ FORCE_INLINE void probe_specific_action(const bool deploy) {
         if (deploy != PROBE_TRIGGERED()) break;
       #endif
 
-      BUZZ(100, 659);
-      BUZZ(100, 698);
+      OKAY_BUZZ();
 
       FSTR_P const ds_str = deploy ? GET_TEXT_F(MSG_MANUAL_DEPLOY) : GET_TEXT_F(MSG_MANUAL_STOW);
       ui.return_to_status();       // To display the new status message