Encapsulate common display code in a singleton (#12395)
* Encapsulate common LCD code in a singleton * Depend more UBL code on UBL_DEVEL_DEBUGGING - Since most users don't need the debugging on at all times, this helps reduce the default build size for UBL by over 2K, a little closer to fitting on 128K boards.
This commit is contained in:
@ -23,94 +23,95 @@
|
||||
|
||||
#include "../inc/MarlinConfig.h"
|
||||
|
||||
// Make a buzzer and macro
|
||||
#if ENABLED(LCD_USE_I2C_BUZZER)
|
||||
// BUZZ() will be defined in ultralcd.h
|
||||
|
||||
#define BUZZ(d,f) ui.buzz(d,f)
|
||||
|
||||
#elif PIN_EXISTS(BEEPER)
|
||||
|
||||
#include "circularqueue.h"
|
||||
#include "circularqueue.h"
|
||||
|
||||
#define TONE_QUEUE_LENGTH 4
|
||||
#define TONE_QUEUE_LENGTH 4
|
||||
|
||||
/**
|
||||
* @brief Tone structure
|
||||
* @details Simple abstraction of a tone based on a duration and a frequency.
|
||||
*/
|
||||
struct tone_t {
|
||||
uint16_t duration;
|
||||
uint16_t frequency;
|
||||
};
|
||||
/**
|
||||
* @brief Tone structure
|
||||
* @details Simple abstraction of a tone based on a duration and a frequency.
|
||||
*/
|
||||
struct tone_t {
|
||||
uint16_t duration;
|
||||
uint16_t frequency;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Buzzer class
|
||||
*/
|
||||
class Buzzer {
|
||||
public:
|
||||
/**
|
||||
* @brief Buzzer class
|
||||
*/
|
||||
class Buzzer {
|
||||
public:
|
||||
|
||||
typedef struct {
|
||||
tone_t tone;
|
||||
uint32_t endtime;
|
||||
} state_t;
|
||||
typedef struct {
|
||||
tone_t tone;
|
||||
uint32_t endtime;
|
||||
} state_t;
|
||||
|
||||
private:
|
||||
static state_t state;
|
||||
private:
|
||||
static state_t state;
|
||||
|
||||
protected:
|
||||
static CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
|
||||
protected:
|
||||
static CircularQueue<tone_t, TONE_QUEUE_LENGTH> buffer;
|
||||
|
||||
/**
|
||||
* @brief Inverts the sate of a digital PIN
|
||||
* @details This will invert the current state of an digital IO pin.
|
||||
*/
|
||||
FORCE_INLINE static void invert() { TOGGLE(BEEPER_PIN); }
|
||||
/**
|
||||
* @brief Inverts the sate of a digital PIN
|
||||
* @details This will invert the current state of an digital IO pin.
|
||||
*/
|
||||
FORCE_INLINE static void invert() { TOGGLE(BEEPER_PIN); }
|
||||
|
||||
/**
|
||||
* @brief Turn off a digital PIN
|
||||
* @details Alias of digitalWrite(PIN, LOW) using FastIO
|
||||
*/
|
||||
FORCE_INLINE static void off() { WRITE(BEEPER_PIN, LOW); }
|
||||
/**
|
||||
* @brief Turn off a digital PIN
|
||||
* @details Alias of digitalWrite(PIN, LOW) using FastIO
|
||||
*/
|
||||
FORCE_INLINE static void off() { WRITE(BEEPER_PIN, LOW); }
|
||||
|
||||
/**
|
||||
* @brief Turn on a digital PIN
|
||||
* @details Alias of digitalWrite(PIN, HIGH) using FastIO
|
||||
*/
|
||||
FORCE_INLINE static void on() { WRITE(BEEPER_PIN, HIGH); }
|
||||
/**
|
||||
* @brief Turn on a digital PIN
|
||||
* @details Alias of digitalWrite(PIN, HIGH) using FastIO
|
||||
*/
|
||||
FORCE_INLINE static void on() { WRITE(BEEPER_PIN, HIGH); }
|
||||
|
||||
/**
|
||||
* @brief Resets the state of the class
|
||||
* @details Brings the class state to a known one.
|
||||
*/
|
||||
static inline void reset() {
|
||||
off();
|
||||
state.endtime = 0;
|
||||
}
|
||||
/**
|
||||
* @brief Resets the state of the class
|
||||
* @details Brings the class state to a known one.
|
||||
*/
|
||||
static inline void reset() {
|
||||
off();
|
||||
state.endtime = 0;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Class constructor
|
||||
*/
|
||||
Buzzer() {
|
||||
SET_OUTPUT(BEEPER_PIN);
|
||||
reset();
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* @brief Class constructor
|
||||
*/
|
||||
Buzzer() {
|
||||
SET_OUTPUT(BEEPER_PIN);
|
||||
reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add a tone to the queue
|
||||
* @details Adds a tone_t structure to the ring buffer, will block IO if the
|
||||
* queue is full waiting for one slot to get available.
|
||||
*
|
||||
* @param duration Duration of the tone in milliseconds
|
||||
* @param frequency Frequency of the tone in hertz
|
||||
*/
|
||||
static void tone(const uint16_t duration, const uint16_t frequency=0);
|
||||
/**
|
||||
* @brief Add a tone to the queue
|
||||
* @details Adds a tone_t structure to the ring buffer, will block IO if the
|
||||
* queue is full waiting for one slot to get available.
|
||||
*
|
||||
* @param duration Duration of the tone in milliseconds
|
||||
* @param frequency Frequency of the tone in hertz
|
||||
*/
|
||||
static void tone(const uint16_t duration, const uint16_t frequency=0);
|
||||
|
||||
/**
|
||||
* @brief Tick function
|
||||
* @details This function should be called at loop, it will take care of
|
||||
* playing the tones in the queue.
|
||||
*/
|
||||
static void tick();
|
||||
};
|
||||
/**
|
||||
* @brief Tick function
|
||||
* @details This function should be called at loop, it will take care of
|
||||
* playing the tones in the queue.
|
||||
*/
|
||||
static void tick();
|
||||
};
|
||||
|
||||
// Provide a buzzer instance
|
||||
extern Buzzer buzzer;
|
||||
|
Reference in New Issue
Block a user