This repository has been archived on 2022-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
Marlin-Artillery-M600/Marlin/src/libs/buzzer.h

124 lines
3.1 KiB
C
Raw Normal View History

/**
2016-03-24 19:01:20 +01:00
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
2017-09-14 22:33:07 +02:00
#include "../inc/MarlinConfig.h"
// Make a buzzer and macro
#if ENABLED(LCD_USE_I2C_BUZZER)
// BUZZ() will be defined in ultralcd.h
#elif PIN_EXISTS(BEEPER)
2017-09-14 22:33:07 +02:00
#include "circularqueue.h"
2016-08-02 06:10:53 +02:00
2016-06-04 21:29:01 +02:00
#define TONE_QUEUE_LENGTH 4
/**
* @brief Tone structure
* @details Simple abstraction of a tone based on a duration and a frequency.
2016-06-04 21:29:01 +02:00
*/
struct tone_t {
uint16_t duration;
uint16_t frequency;
};
/**
* @brief Buzzer class
*/
class Buzzer {
2017-09-14 22:33:07 +02:00
public:
typedef struct {
2016-06-04 21:29:01 +02:00
tone_t tone;
2016-07-11 04:36:46 +02:00
uint32_t endtime;
2017-09-14 22:33:07 +02:00
} state_t;
private:
static state_t state;
2016-06-04 21:29:01 +02:00
protected:
2017-09-14 22:33:07 +02:00
static CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
2016-06-04 21:29:01 +02:00
/**
* @brief Inverts the sate of a digital PIN
* @details This will invert the current state of an digital IO pin.
*/
2017-09-14 22:33:07 +02:00
FORCE_INLINE static void invert() { TOGGLE(BEEPER_PIN); }
2016-06-04 21:29:01 +02:00
/**
* @brief Turn off a digital PIN
* @details Alias of digitalWrite(PIN, LOW) using FastIO
*/
2017-09-14 22:33:07 +02:00
FORCE_INLINE static void off() { WRITE(BEEPER_PIN, LOW); }
2016-06-04 21:29:01 +02:00
/**
* @brief Turn on a digital PIN
* @details Alias of digitalWrite(PIN, HIGH) using FastIO
*/
2017-09-14 22:33:07 +02:00
FORCE_INLINE static void on() { WRITE(BEEPER_PIN, HIGH); }
2016-06-04 21:29:01 +02:00
/**
* @brief Resets the state of the class
* @details Brings the class state to a known one.
*/
2018-08-14 06:55:12 +02:00
static inline void reset() {
2017-09-14 22:33:07 +02:00
off();
state.endtime = 0;
2016-06-04 21:29:01 +02:00
}
public:
/**
* @brief Class constructor
*/
Buzzer() {
SET_OUTPUT(BEEPER_PIN);
2017-09-14 22:33:07 +02:00
reset();
2016-06-04 21:29:01 +02:00
}
/**
* @brief Add a tone to the queue
* @details Adds a tone_t structure to the ring buffer, will block IO if the
2016-07-11 04:36:46 +02:00
* queue is full waiting for one slot to get available.
2016-06-04 21:29:01 +02:00
*
* @param duration Duration of the tone in milliseconds
* @param frequency Frequency of the tone in hertz
*/
2017-09-14 22:33:07 +02:00
static void tone(const uint16_t duration, const uint16_t frequency=0);
2016-06-04 21:29:01 +02:00
/**
2017-09-14 22:33:07 +02:00
* @brief Tick function
2016-06-04 21:29:01 +02:00
* @details This function should be called at loop, it will take care of
2016-07-11 04:36:46 +02:00
* playing the tones in the queue.
2016-06-04 21:29:01 +02:00
*/
2017-09-14 22:33:07 +02:00
static void tick();
2016-06-04 21:29:01 +02:00
};
2017-09-14 22:33:07 +02:00
// Provide a buzzer instance
extern Buzzer buzzer;
#define BUZZ(d,f) buzzer.tone(d, f)
#else // No buzz capability
#define BUZZ(d,f) NOOP
#endif