buzzer.cpp 1.85 KB
Newer Older
MagoKimbra's avatar
MagoKimbra committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * MK & MK4due 3D Printer Firmware
 *
 * Based on Marlin, Sprinter and grbl
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
 * Copyright (C) 2013 - 2016 Alberto Cotronei @MagoKimbra
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
MagoKimbra's avatar
MagoKimbra committed
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MagoKimbra's avatar
MagoKimbra committed
16 17 18
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
MagoKimbra's avatar
MagoKimbra committed
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
MagoKimbra's avatar
MagoKimbra committed
20 21 22
 *
 */

MagoKimbra's avatar
MagoKimbra committed
23
#include "../../base.h"
MagoKimbra's avatar
MagoKimbra committed
24

25
#if HAS(BUZZER)
MagoKimbra's avatar
MagoKimbra committed
26
  #include "buzzer.h"
MagoKimbra's avatar
MagoKimbra committed
27

MagoKimbra's avatar
MagoKimbra committed
28
  void buzz(long duration, uint16_t freq) {
MagoKimbra's avatar
MagoKimbra committed
29 30 31 32 33 34 35 36 37 38 39
    if (freq > 0) {
      #if ENABLED(LCD_USE_I2C_BUZZER)
        lcd_buzz(duration, freq);
      #elif PIN_EXISTS(BEEPER) // on-board buzzers have no further condition
        SET_OUTPUT(BEEPER_PIN);
        #if ENABLED(SPEAKER) // a speaker needs a AC ore a pulsed DC
          //tone(BEEPER_PIN, freq, duration); // needs a PWMable pin
          unsigned int delay = 1000000 / freq / 2;
          int i = duration * freq / 1000;
          while (i--) {
            WRITE(BEEPER_PIN, HIGH);
MagoKimbra's avatar
MagoKimbra committed
40
            HAL::delayMicroseconds(delay);
MagoKimbra's avatar
MagoKimbra committed
41
            WRITE(BEEPER_PIN, LOW);
MagoKimbra's avatar
MagoKimbra committed
42
            HAL::delayMicroseconds(delay);
MagoKimbra's avatar
MagoKimbra committed
43 44
          }
        #else // buzzer has its own resonator - needs a DC
MagoKimbra's avatar
MagoKimbra committed
45
          tone(BEEPER_PIN, freq, duration);
MagoKimbra's avatar
MagoKimbra committed
46
          HAL::delayMilliseconds(1 + duration);
MagoKimbra's avatar
MagoKimbra committed
47 48
        #endif
      #else
MagoKimbra's avatar
MagoKimbra committed
49
        HAL::delayMilliseconds(duration);
MagoKimbra's avatar
MagoKimbra committed
50
      #endif
MagoKimbra's avatar
MagoKimbra committed
51
    }
MagoKimbra's avatar
MagoKimbra committed
52
    else {
MagoKimbra's avatar
MagoKimbra committed
53
      HAL::delayMilliseconds(duration);
MagoKimbra's avatar
MagoKimbra committed
54 55
    }
  }
MagoKimbra's avatar
MagoKimbra committed
56
#endif