Skip to content

Commit 05995d1

Browse files
Ludy87thinkyhead
authored andcommitted
Unify buzz methods as MarlinUI::buzz (MarlinFirmware#14803)
1 parent 29c1290 commit 05995d1

12 files changed

+83
-72
lines changed

Marlin/src/Marlin.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
#include "feature/host_actions.h"
6666
#endif
6767

68-
#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
68+
#if USE_BEEPER
6969
#include "libs/buzzer.h"
7070
#endif
7171

@@ -702,7 +702,7 @@ void idle(
702702
print_job_timer.tick();
703703
#endif
704704

705-
#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER) && DISABLED(PCA9632_BUZZER)
705+
#if USE_BEEPER
706706
buzzer.tick();
707707
#endif
708708

Marlin/src/feature/leds/pca9632.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,15 @@ void pca9632_set_led_color(const LEDColor &color) {
137137
}
138138

139139
#if ENABLED(PCA9632_BUZZER)
140-
void pca9632_buzz(uint16_t const f, uint16_t d) {
141-
UNUSED(f); UNUSED(d);
140+
141+
void pca9632_buzz(const long duration, const uint16_t freq) {
142+
UNUSED(duration); UNUSED(freq);
142143
uint8_t data[] = PCA9632_BUZZER_DATA;
143144
Wire.beginTransmission(I2C_ADDRESS(PCA9632_ADDRESS));
144145
Wire.write(data, sizeof(data));
145146
Wire.endTransmission();
146147
}
147-
#endif
148+
149+
#endif // PCA9632_BUZZER
148150

149151
#endif // PCA9632

Marlin/src/feature/leds/pca9632.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ typedef LEDColor LEDColor;
3232
void pca9632_set_led_color(const LEDColor &color);
3333

3434
#if ENABLED(PCA9632_BUZZER)
35-
void pca9632_buzz(uint16_t const, uint16_t);
35+
#include <stdint.h>
36+
void pca9632_buzz(const long, const uint16_t);
3637
#endif

Marlin/src/inc/Conditionals_post.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,8 @@
10481048
#define HAS_KILL (PIN_EXISTS(KILL))
10491049
#define HAS_SUICIDE (PIN_EXISTS(SUICIDE))
10501050
#define HAS_PHOTOGRAPH (PIN_EXISTS(PHOTOGRAPH))
1051-
#define HAS_BUZZER (PIN_EXISTS(BEEPER) || ENABLED(LCD_USE_I2C_BUZZER) || ENABLED(PCA9632_BUZZER))
1051+
#define HAS_BUZZER (PIN_EXISTS(BEEPER) || EITHER(LCD_USE_I2C_BUZZER, PCA9632_BUZZER))
1052+
#define USE_BEEPER (HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER, PCA9632_BUZZER))
10521053
#define HAS_CASE_LIGHT (PIN_EXISTS(CASE_LIGHT) && ENABLED(CASE_LIGHT_ENABLE))
10531054

10541055
// Digital control
@@ -1570,7 +1571,7 @@
15701571
#ifndef LCD_FEEDBACK_FREQUENCY_DURATION_MS
15711572
#define LCD_FEEDBACK_FREQUENCY_DURATION_MS 100
15721573
#endif
1573-
#else
1574+
#elif HAS_BUZZER
15741575
#ifndef LCD_FEEDBACK_FREQUENCY_HZ
15751576
#define LCD_FEEDBACK_FREQUENCY_HZ 5000
15761577
#endif

Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,33 @@ void MarlinUI::init_lcd() {
360360
lcd.clear();
361361
}
362362

363+
bool MarlinUI::detected() {
364+
return true
365+
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
366+
&& lcd.LcdDetected() == 1
367+
#endif
368+
;
369+
}
370+
371+
#if HAS_SLOW_BUTTONS
372+
uint8_t MarlinUI::read_slow_buttons() {
373+
#if ENABLED(LCD_I2C_TYPE_MCP23017)
374+
// Reading these buttons this is likely to be too slow to call inside interrupt context
375+
// so they are called during normal lcd_update
376+
uint8_t slow_bits = lcd.readButtons()
377+
#if !BUTTON_EXISTS(ENC)
378+
<< B_I2C_BTN_OFFSET
379+
#endif
380+
;
381+
#if ENABLED(LCD_I2C_VIKI)
382+
if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked
383+
slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated
384+
#endif // LCD_I2C_VIKI
385+
return slow_bits;
386+
#endif // LCD_I2C_TYPE_MCP23017
387+
}
388+
#endif
389+
363390
void MarlinUI::clear_lcd() { lcd.clear(); }
364391

365392
#if ENABLED(SHOW_BOOTSCREEN)
@@ -1063,7 +1090,7 @@ void MarlinUI::draw_status_screen() {
10631090

10641091
#if ENABLED(LCD_HAS_STATUS_INDICATORS)
10651092

1066-
static void MarlinUI::update_indicators() {
1093+
void MarlinUI::update_indicators() {
10671094
// Set the LEDS - referred to as backlights by the LiquidTWI2 library
10681095
static uint8_t ledsprev = 0;
10691096
uint8_t leds = 0;

Marlin/src/lcd/dogm/ultralcd_DOGM.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void MarlinUI::set_font(const MarlinFont font_nr) {
102102
}
103103
}
104104

105+
bool MarlinUI::detected() { return true; }
106+
105107
#if ENABLED(SHOW_BOOTSCREEN)
106108

107109
#if ENABLED(SHOW_CUSTOM_BOOTSCREEN)

Marlin/src/lcd/ultralcd.cpp

+20-39
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@
6464
uint8_t MarlinUI::progress_bar_percent; // = 0
6565
#endif
6666

67+
#if HAS_BUZZER
68+
#include "../libs/buzzer.h"
69+
void MarlinUI::buzz(const long duration, const uint16_t freq) {
70+
#if ENABLED(LCD_USE_I2C_BUZZER)
71+
lcd.buzz(duration, freq);
72+
#elif ENABLED(PCA9632_BUZZER)
73+
pca9632_buzz(const long duration, const uint16_t freq) {
74+
#elif USE_BEEPER
75+
buzzer.tone(duration, freq);
76+
#endif
77+
}
78+
#endif
79+
6780
#if HAS_SPI_LCD
6881

6982
#if HAS_GRAPHICAL_LCD
@@ -89,10 +102,6 @@
89102
#include "../feature/bedlevel/bedlevel.h"
90103
#endif
91104

92-
#if HAS_BUZZER
93-
#include "../libs/buzzer.h"
94-
#endif
95-
96105
#if HAS_TRINAMIC
97106
#include "../feature/tmc_util.h"
98107
#endif
@@ -568,7 +577,7 @@ void MarlinUI::status_screen() {
568577
const millis_t ms = millis();
569578
#endif
570579
if (ELAPSED(ms, next_beep)) {
571-
BUZZ(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
580+
buzz(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
572581
next_beep = ms + 500UL;
573582
}
574583
#endif
@@ -611,13 +620,12 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
611620
#if HAS_BUZZER
612621
// Buzz and wait. Is the delay needed for buttons to settle?
613622
buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
614-
#endif
615-
616-
#if HAS_LCD_MENU
617-
#if ENABLED(LCD_USE_I2C_BUZZER)
618-
delay(10);
619-
#elif PIN_EXISTS(BEEPER)
620-
for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
623+
#if HAS_LCD_MENU
624+
#if USE_BEEPER
625+
for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
626+
#else
627+
delay(10);
628+
#endif
621629
#endif
622630
#endif
623631
}
@@ -729,16 +737,6 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
729737

730738
LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
731739

732-
bool MarlinUI::detected() {
733-
return
734-
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
735-
lcd.LcdDetected() == 1
736-
#else
737-
true
738-
#endif
739-
;
740-
}
741-
742740
void MarlinUI::update() {
743741

744742
static uint16_t max_display_update_time = 0;
@@ -1295,23 +1293,6 @@ void MarlinUI::update() {
12951293
#endif // HAS_ENCODER_WHEEL
12961294
}
12971295

1298-
#if HAS_SLOW_BUTTONS
1299-
1300-
uint8_t MarlinUI::read_slow_buttons() {
1301-
#if ENABLED(LCD_I2C_TYPE_MCP23017)
1302-
// Reading these buttons this is likely to be too slow to call inside interrupt context
1303-
// so they are called during normal lcd_update
1304-
uint8_t slow_bits = lcd.readButtons() << B_I2C_BTN_OFFSET;
1305-
#if ENABLED(LCD_I2C_VIKI)
1306-
if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked
1307-
slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated
1308-
#endif // LCD_I2C_VIKI
1309-
return slow_bits;
1310-
#endif // LCD_I2C_TYPE_MCP23017
1311-
}
1312-
1313-
#endif
1314-
13151296
#endif // HAS_ENCODER_ACTION
13161297

13171298
#endif // HAS_SPI_LCD

Marlin/src/lcd/ultralcd.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,11 @@ class MarlinUI {
259259
}
260260

261261
#if HAS_BUZZER
262-
static inline void buzz(const long duration, const uint16_t freq) {
263-
#if ENABLED(LCD_USE_I2C_BUZZER)
264-
lcd.buzz(duration, freq);
265-
#elif PIN_EXISTS(BEEPER)
266-
buzzer.tone(duration, freq);
267-
#elif ENABLED(PCA9632_BUZZER)
268-
pca9632_buzz(duration, freq);
269-
#endif
270-
}
262+
static void buzz(const long duration, const uint16_t freq);
263+
#endif
264+
265+
#if ENABLED(LCD_HAS_STATUS_INDICATORS)
266+
static void update_indicators();
271267
#endif
272268

273269
// LCD implementations

Marlin/src/libs/buzzer.cpp

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

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

25-
#if DISABLED(LCD_USE_I2C_BUZZER) && PIN_EXISTS(BEEPER)
25+
#if USE_BEEPER
2626

2727
#include "buzzer.h"
2828
#include "../module/temperature.h"
@@ -78,4 +78,4 @@ void Buzzer::tick() {
7878
else if (ELAPSED(now, state.endtime)) reset();
7979
}
8080

81-
#endif // !LCD_USE_I2C_BUZZER && BEEPER
81+
#endif // USE_BEEPER

Marlin/src/libs/buzzer.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@
2323

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

26-
#if ENABLED(LCD_USE_I2C_BUZZER)
27-
28-
#define BUZZ(d,f) ui.buzz(d,f)
29-
30-
#elif ENABLED(PCA9632_BUZZER)
31-
32-
#include "../feature/leds/pca9632.h"
33-
#define BUZZ(d, f) pca9632_buzz(d,f)
34-
35-
#elif PIN_EXISTS(BEEPER)
26+
#if USE_BEEPER
3627

3728
#include "circularqueue.h"
3829

@@ -120,10 +111,18 @@
120111

121112
// Provide a buzzer instance
122113
extern Buzzer buzzer;
114+
115+
// Buzz directly via the BEEPER pin tone queue
123116
#define BUZZ(d,f) buzzer.tone(d, f)
124117

125-
#else // No buzz capability
118+
#elif HAS_BUZZER
119+
120+
// Buzz indirectly via the MarlinUI instance
121+
#define BUZZ(d,f) ui.buzz(d,f)
122+
123+
#else
126124

125+
// No buzz capability
127126
#define BUZZ(d,f) NOOP
128127

129128
#endif

Marlin/src/module/printcounter.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ void PrintCounter::loadStats() {
157157
#endif
158158
#if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0
159159
if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) BUZZ(200, 404);
160+
#else
161+
UNUSED(doBuzz);
160162
#endif
161163
#endif // HAS_SERVICE_INTERVALS
162164
}

Marlin/src/module/temperature.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
#include "tool_change.h"
6969
#endif
7070

71-
#if HAS_BUZZER && PIN_EXISTS(BEEPER)
71+
#if USE_BEEPER
7272
#include "../libs/buzzer.h"
7373
#endif
7474

@@ -749,7 +749,7 @@ int16_t Temperature::getHeaterPower(const heater_ind_t heater_id) {
749749

750750
inline void loud_kill(PGM_P const lcd_msg) {
751751
Running = false;
752-
#if HAS_BUZZER && PIN_EXISTS(BEEPER)
752+
#if USE_BEEPER
753753
for (uint8_t i = 20; i--;) {
754754
WRITE(BEEPER_PIN, HIGH); delay(25);
755755
WRITE(BEEPER_PIN, LOW); delay(80);

0 commit comments

Comments
 (0)