Skip to content

Commit a6b133b

Browse files
thinkyheadLCh-77
authored andcommitted
♻️ Small sound / buzz refactor (MarlinFirmware#24520)
1 parent 0ba6011 commit a6b133b

File tree

16 files changed

+49
-55
lines changed

16 files changed

+49
-55
lines changed

Marlin/src/feature/pause.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
#include "../lcd/marlinui.h"
6767

68-
#if HAS_BUZZER
68+
#if HAS_SOUND
6969
#include "../libs/buzzer.h"
7070
#endif
7171

@@ -100,7 +100,7 @@ fil_change_settings_t fc_settings[EXTRUDERS];
100100
#define _PMSG(L) L##_LCD
101101
#endif
102102

103-
#if HAS_BUZZER
103+
#if HAS_SOUND
104104
static void impatient_beep(const int8_t max_beep_count, const bool restart=false) {
105105

106106
if (TERN0(HAS_MARLINUI_MENU, pause_mode == PAUSE_MODE_PAUSE_PRINT)) return;

Marlin/src/feature/spindle_laser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class SpindleLaser {
294294
* If not set defaults to 80% power
295295
*/
296296
static void test_fire_pulse() {
297-
TERN_(HAS_BEEPER, buzzer.tone(30, 3000));
297+
BUZZ(30, 3000);
298298
cutter_mode = CUTTER_MODE_STANDARD;// Menu needs standard mode.
299299
laser_menu_toggle(true); // Laser On
300300
delay(testPulse); // Delay for time set by user in pulse ms menu screen.

Marlin/src/gcode/gcode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
769769
case 290: M290(); break; // M290: Babystepping
770770
#endif
771771

772-
#if HAS_BUZZER
772+
#if HAS_SOUND
773773
case 300: M300(); break; // M300: Play beep tone
774774
#endif
775775

Marlin/src/gcode/gcode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ class GcodeSuite {
914914
static void M290();
915915
#endif
916916

917-
#if HAS_BUZZER
917+
#if HAS_SOUND
918918
static void M300();
919919
#endif
920920

Marlin/src/gcode/lcd/M300.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "../../inc/MarlinConfig.h"
2424

25-
#if HAS_BUZZER
25+
#if HAS_SOUND
2626

2727
#include "../gcode.h"
2828

@@ -42,4 +42,4 @@ void GcodeSuite::M300() {
4242
BUZZ(duration, frequency);
4343
}
4444

45-
#endif // HAS_BUZZER
45+
#endif // HAS_SOUND

Marlin/src/inc/Conditionals_post.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -3564,8 +3564,11 @@
35643564
#if PIN_EXISTS(BEEPER)
35653565
#define HAS_BEEPER 1
35663566
#endif
3567-
#if ANY(HAS_BEEPER, LCD_USE_I2C_BUZZER, PCA9632_BUZZER)
3568-
#define HAS_BUZZER 1
3567+
#if ANY(IS_TFTGLCD_PANEL, PCA9632_BUZZER, LCD_USE_I2C_BUZZER)
3568+
#define USE_MARLINUI_BUZZER 1
3569+
#endif
3570+
#if EITHER(HAS_BEEPER, USE_MARLINUI_BUZZER)
3571+
#define HAS_SOUND 1
35693572
#endif
35703573

35713574
#if ENABLED(LCD_USE_I2C_BUZZER)
@@ -3575,7 +3578,7 @@
35753578
#ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS
35763579
#define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100
35773580
#endif
3578-
#elif HAS_BUZZER
3581+
#elif HAS_SOUND
35793582
#ifndef LCD_FEEDBACK_FREQUENCY_HZ
35803583
#define LCD_FEEDBACK_FREQUENCY_HZ 5000
35813584
#endif
@@ -3584,12 +3587,13 @@
35843587
#endif
35853588
#endif
35863589

3587-
#if HAS_BUZZER
3590+
#if HAS_SOUND
35883591
#if LCD_FEEDBACK_FREQUENCY_DURATION_MS && LCD_FEEDBACK_FREQUENCY_HZ
35893592
#define HAS_CHIRP 1
35903593
#endif
35913594
#else
35923595
#undef SOUND_MENU_ITEM // No buzzer menu item without a buzzer
3596+
#undef SOUND_ON_DEFAULT
35933597
#endif
35943598

35953599
/**

Marlin/src/lcd/HD44780/marlinui_HD44780.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,11 @@ static void createChar_P(const char c, const byte * const ptr) {
120120
#endif
121121

122122
#if ENABLED(LCD_USE_I2C_BUZZER)
123+
123124
void MarlinUI::buzz(const long duration, const uint16_t freq) {
124-
if (!sound_on) return;
125-
lcd.buzz(duration, freq);
125+
if (sound_on) lcd.buzz(duration, freq);
126126
}
127+
127128
#endif
128129

129130
void MarlinUI::set_custom_characters(const HD44780CharSet screen_charset/*=CHARSET_INFO*/) {

Marlin/src/lcd/e3v2/common/encoder.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "../../marlinui.h"
3737
#include "../../../HAL/shared/Delay.h"
3838

39-
#if HAS_BUZZER
39+
#if HAS_SOUND
4040
#include "../../../libs/buzzer.h"
4141
#endif
4242

@@ -50,9 +50,7 @@ ENCODER_Rate EncoderRate;
5050

5151
// TODO: Replace with ui.quick_feedback
5252
void Encoder_tick() {
53-
#if PIN_EXISTS(BEEPER)
54-
if (ui.sound_on) buzzer.click(10);
55-
#endif
53+
TERN_(HAS_BEEPER, if (ui.sound_on) buzzer.click(10));
5654
}
5755

5856
// Encoder initialization
@@ -66,7 +64,7 @@ void Encoder_Configuration() {
6664
#if BUTTON_EXISTS(ENC)
6765
SET_INPUT_PULLUP(BTN_ENC);
6866
#endif
69-
#if PIN_EXISTS(BEEPER)
67+
#if HAS_BEEPER
7068
SET_OUTPUT(BEEPER_PIN); // TODO: Use buzzer.h which already inits this
7169
#endif
7270
}

Marlin/src/lcd/e3v2/creality/dwin.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -2625,15 +2625,13 @@ void Draw_HomeOff_Menu() {
26252625
#include "../../../libs/buzzer.h"
26262626

26272627
void HMI_AudioFeedback(const bool success=true) {
2628-
#if HAS_BUZZER
2629-
if (success) {
2630-
buzzer.tone(100, 659);
2631-
buzzer.tone(10, 0);
2632-
buzzer.tone(100, 698);
2633-
}
2634-
else
2635-
buzzer.tone(40, 440);
2636-
#endif
2628+
if (success) {
2629+
BUZZ(100, 659);
2630+
BUZZ(10, 0);
2631+
BUZZ(100, 698);
2632+
}
2633+
else
2634+
BUZZ(40, 440);
26372635
}
26382636

26392637
// Prepare

Marlin/src/lcd/marlinui.cpp

+8-15
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,9 @@ constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
121121
bool MarlinUI::sound_on = ENABLED(SOUND_ON_DEFAULT);
122122
#endif
123123

124-
#if EITHER(PCA9632_BUZZER, HAS_BEEPER)
125-
#if ENABLED(PCA9632_BUZZER)
126-
#include "../feature/leds/pca9632.h"
127-
#endif
124+
#if ENABLED(PCA9632_BUZZER)
128125
void MarlinUI::buzz(const long duration, const uint16_t freq) {
129-
if (!sound_on) return;
130-
#if ENABLED(PCA9632_BUZZER)
131-
PCA9632_buzz(duration, freq);
132-
#elif HAS_BEEPER
133-
buzzer.tone(duration, freq);
134-
#endif
126+
if (sound_on) PCA9632_buzz(duration, freq);
135127
}
136128
#endif
137129

@@ -685,7 +677,7 @@ void MarlinUI::init() {
685677
if (old_frm != new_frm) {
686678
feedrate_percentage = new_frm;
687679
encoderPosition = 0;
688-
#if BOTH(HAS_BUZZER, BEEP_ON_FEEDRATE_CHANGE)
680+
#if BOTH(HAS_SOUND, BEEP_ON_FEEDRATE_CHANGE)
689681
static millis_t next_beep;
690682
#ifndef GOT_MS
691683
const millis_t ms = millis();
@@ -743,11 +735,12 @@ void MarlinUI::init() {
743735
UNUSED(clear_buttons);
744736
#endif
745737

746-
#if HAS_CHIRP
747-
chirp(); // Buzz and wait. Is the delay needed for buttons to settle?
748-
#if BOTH(HAS_MARLINUI_MENU, HAS_BEEPER)
738+
chirp(); // Buzz and wait. Is the delay needed for buttons to settle?
739+
740+
#if HAS_CHIRP && HAS_MARLINUI_MENU
741+
#if HAS_BEEPER
749742
for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
750-
#elif HAS_MARLINUI_MENU
743+
#else
751744
delay(10);
752745
#endif
753746
#endif

Marlin/src/lcd/marlinui.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ class MarlinUI {
227227
static constexpr bool sound_on = true;
228228
#endif
229229

230-
#if HAS_BUZZER
230+
#if USE_MARLINUI_BUZZER
231231
static void buzz(const long duration, const uint16_t freq);
232232
#endif
233233

234-
FORCE_INLINE static void chirp() {
235-
TERN_(HAS_CHIRP, TERN(HAS_BUZZER, buzz, BUZZ)(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ));
234+
static void chirp() {
235+
TERN_(HAS_CHIRP, TERN(USE_MARLINUI_BUZZER, buzz, BUZZ)(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ));
236236
}
237237

238238
#if ENABLED(LCD_HAS_STATUS_INDICATORS)
@@ -455,7 +455,7 @@ class MarlinUI {
455455
#endif
456456

457457
static void quick_feedback(const bool clear_buttons=true);
458-
#if HAS_BUZZER
458+
#if HAS_SOUND
459459
static void completion_feedback(const bool good=true);
460460
#else
461461
static void completion_feedback(const bool=true) { TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); }

Marlin/src/lcd/menu/game/game.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
//#define MUTE_GAMES
3030

31-
#if ENABLED(MUTE_GAMES) || !HAS_BUZZER
31+
#if ENABLED(MUTE_GAMES) || !HAS_SOUND
3232
#define _BUZZ(D,F) NOOP
3333
#else
3434
#define _BUZZ(D,F) BUZZ(D,F)

Marlin/src/lcd/menu/menu.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "../../module/temperature.h"
3232
#include "../../gcode/queue.h"
3333

34-
#if HAS_BUZZER
34+
#if HAS_SOUND
3535
#include "../../libs/buzzer.h"
3636
#endif
3737

@@ -272,7 +272,7 @@ void scroll_screen(const uint8_t limit, const bool is_menu) {
272272
encoderTopLine = encoderLine;
273273
}
274274

275-
#if HAS_BUZZER
275+
#if HAS_SOUND
276276
void MarlinUI::completion_feedback(const bool good/*=true*/) {
277277
TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); // Wake up on rotary encoder click...
278278
if (good) OKAY_BUZZ(); else ERR_BUZZ();

Marlin/src/libs/buzzer.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@
117117
// Buzz directly via the BEEPER pin tone queue
118118
#define BUZZ(d,f) buzzer.tone(d, f)
119119

120-
#elif HAS_BUZZER
120+
#elif USE_MARLINUI_BUZZER
121121

122-
// Buzz indirectly via the MarlinUI instance
123-
#define BUZZ(d,f) ui.buzz(d,f)
122+
// Use MarlinUI for a buzzer on the LCD
124123
#include "../lcd/marlinui.h"
124+
#define BUZZ(d,f) ui.buzz(d,f)
125125

126126
#else
127127

Marlin/src/module/printcounter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Stopwatch print_job_timer; // Global Print Job Timer instance
3737
#include "../MarlinCore.h"
3838
#include "../HAL/shared/eeprom_api.h"
3939

40-
#if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0
40+
#if HAS_SOUND && SERVICE_WARNING_BUZZES > 0
4141
#include "../libs/buzzer.h"
4242
#endif
4343

@@ -156,7 +156,7 @@ void PrintCounter::loadStats() {
156156
#if SERVICE_INTERVAL_3 > 0
157157
if (data.nextService3 == 0) doBuzz = _service_warn(PSTR(" " SERVICE_NAME_3));
158158
#endif
159-
#if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0
159+
#if HAS_SOUND && SERVICE_WARNING_BUZZES > 0
160160
if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) { BUZZ(200, 404); BUZZ(10, 0); }
161161
#else
162162
UNUSED(doBuzz);

ini/features.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ HAS_STATUS_MESSAGE = src_filter=+<src/gcode/lcd/M117.cpp>
203203
HAS_LCD_CONTRAST = src_filter=+<src/gcode/lcd/M250.cpp>
204204
HAS_GCODE_M255 = src_filter=+<src/gcode/lcd/M255.cpp>
205205
HAS_LCD_BRIGHTNESS = src_filter=+<src/gcode/lcd/M256.cpp>
206-
HAS_BUZZER = src_filter=+<src/gcode/lcd/M300.cpp>
206+
HAS_SOUND = src_filter=+<src/gcode/lcd/M300.cpp>
207207
TOUCH_SCREEN_CALIBRATION = src_filter=+<src/gcode/lcd/M995.cpp>
208208
ARC_SUPPORT = src_filter=+<src/gcode/motion/G2_G3.cpp>
209209
GCODE_MOTION_MODES = src_filter=+<src/gcode/motion/G80.cpp>

0 commit comments

Comments
 (0)