Marlin_Firmware/Marlin/buzzer.cpp
AnHardt fd0e81b0c7 Distinguish between BUZZER and SPEAKER (PR#2513)
A speaker needs a AC or a pulsed DC to make a sound, a buzzer only needs a DC.
A buzzer has it's own resonator. It works in most cases to feed the buzzer with a pulsed DC, but the sound will not be as loud as with pure DC.

There seem to be boards where the BEEPER-pin is not able to handle a PWM. Obviously intended for a buzzer.
To make these board able to handle a speaker

* replace the PWM based tone()-function again with a on-delay-off-delay loop.

Hopefully the last time I touch the beeper code.
2015-07-24 22:10:04 -05:00

36 lines
991 B
C++

#include "Marlin.h"
#include "buzzer.h"
#include "ultralcd.h"
#if HAS_BUZZER
void buzz(long duration, uint16_t freq) {
if (freq > 0) {
#ifdef LCD_USE_I2C_BUZZER
lcd_buzz(duration, freq);
#elif defined(BEEPER) && BEEPER >= 0 // on-board buzzers have no further condition
SET_OUTPUT(BEEPER);
#ifdef SPEAKER // a speaker needs a AC ore a pulsed DC
//tone(BEEPER, freq, duration); // needs a PWMable pin
unsigned int delay = 1000000 / freq / 2;
int i = duration * freq / 1000;
while (i--) {
WRITE(BEEPER,HIGH);
delayMicroseconds(delay);
WRITE(BEEPER,LOW);
delayMicroseconds(delay);
}
#else // buzzer has its own resonator - needs a DC
WRITE(BEEPER, HIGH);
delay(duration);
WRITE(BEEPER, LOW);
#endif
#else
delay(duration);
#endif
}
else {
delay(duration);
}
}
#endif