AYAB Arduino Firmware 0.95
The goal of the AYAB project is to provide an alternative way to control the famous Brother KH-9xx range of knitting machines using a computer
beeper.h
Go to the documentation of this file.
1
24#ifndef BEEPER_H_
25#define BEEPER_H_
26
27#include <Arduino.h>
28
29enum class BeepState : unsigned char {Idle, Wait, On, Off};
30
31constexpr unsigned int BEEP_DELAY = 50U; // ms
32
33constexpr uint8_t BEEP_NUM_READY = 5U;
34constexpr uint8_t BEEP_NUM_FINISHEDLINE = 3U;
35constexpr uint8_t BEEP_NUM_ENDWORK = 10U;
36constexpr uint8_t BEEP_NUM_ERROR = 15U;
37
38constexpr uint8_t BEEP_ON_DUTY = 0U;
39constexpr uint8_t BEEP_OFF_DUTY = 20U;
40constexpr uint8_t BEEP_NO_DUTY = 255U;
41
43public:
44 virtual ~BeeperInterface() = default;
45
46 // any methods that need to be mocked should go here
47 virtual void init(bool enabled) = 0;
48 virtual bool enabled() = 0;
49 virtual BeepState getState() = 0;
50 virtual void ready() = 0;
51 virtual void finishedLine() = 0;
52 virtual void endWork() = 0;
53 virtual void error() = 0;
54 virtual void schedule() = 0;
55};
56
57// Container class for the static methods that control the beeper.
58// Dependency injection is enabled using a pointer to a global instance of
59// either `Beeper` or `BeeperMock`, both of which classes implement the
60// pure virtual methods of `BeeperInterface`.
61
62class GlobalBeeper final {
63private:
64 // singleton class so private constructor is appropriate
65 GlobalBeeper() = default;
66
67public:
68 // pointer to global instance whose methods are implemented
69 static BeeperInterface *m_instance;
70
71 static void init(bool enabled);
72 static bool enabled();
73 static BeepState getState();
74 static void ready();
75 static void finishedLine();
76 static void endWork();
77 static void error();
78 static void schedule();
79};
80
84class Beeper : public BeeperInterface {
85public:
86 void init(bool enabled) final;
87 bool enabled() final;
88 BeepState getState() final;
89 void ready() final;
90 void finishedLine() final;
91 void endWork() final;
92 void error() final;
93 void schedule() final;
94
95private:
96 void beep(uint8_t repeats);
97
98 BeepState m_currentState;
99 BeepState m_nextState;
100 unsigned long m_nextTime;
101 uint8_t m_repeat;
102 bool m_enabled;
103};
104
105#endif // BEEPER_H_
Class to actuate a beeper connected to PIEZO_PIN.
Definition beeper.h:84
void beep(uint8_t repeats)
Definition beeper.cpp:128
BeepState getState() final
Definition beeper.cpp:49
void endWork() final
Definition beeper.cpp:74
void schedule() final
Definition beeper.cpp:89
void init(bool enabled) final
Definition beeper.cpp:34
void finishedLine() final
Definition beeper.cpp:65
bool enabled() final
Definition beeper.cpp:42
void ready() final
Definition beeper.cpp:56
Definition beeper.h:42
Definition beeper.h:62