forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buzzer.h
110 lines (94 loc) · 2.42 KB
/
Buzzer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// 2019-01-20 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __BUZZER_H__
#define __BUZZER_H__
#include "Alarm.h"
#include "Debug.h"
namespace as {
template<uint8_t PIN, class PINTYPE=ArduinoPins>
class Buzzer : public Alarm {
bool enable;
int8_t repeat;
uint16_t ontime, offtime;
public:
Buzzer () : Alarm(0), enable(false), repeat(0), ontime(0), offtime(0) {
async(true);
}
virtual ~Buzzer () {}
void init () {
PINTYPE::setOutput(PIN);
}
void enabled(bool value) {
enable = value;
}
bool on (uint16_t onticks,uint16_t offticks,int8_t repeat) {
if( on() == true ) {
ontime=onticks;
offtime=offticks;
this->repeat=repeat;
if( ontime > 0 ) {
set(ontime);
sysclock.add(*this);
}
return true;
}
return false;
}
bool on (uint16_t ticks) {
return on(ticks,0,1) ;
}
bool on () {
if( enable == true ) {
sysclock.cancel(*this);
PINTYPE::setHigh(PIN);
return true;
}
return false;
}
bool off (bool force) {
if ( force == true ) {
repeat = 0;
ontime = 0;
}
PINTYPE::setLow(PIN);
return true;
}
bool off () {
return off(false);
}
bool active () {
return PINTYPE::getState(PIN) == HIGH;
}
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
if( active() ) {
off();
if (repeat != -1) repeat--;
if( (repeat != 0) && ontime > 0 ) {
set(offtime);
clock.add(*this);
}
}
else if( (repeat != 0) && ontime > 0 ) {
on();
set(ontime);
clock.add(*this);
}
}
};
class NoBuzzer {
public:
NoBuzzer () {}
~NoBuzzer () {}
void init () {}
void enabled(__attribute__ ((unused)) bool value) {}
bool on (__attribute__ ((unused))uint16_t onticks,__attribute__ ((unused))uint16_t offticks,__attribute__ ((unused))uint8_t repeat) { return false; }
bool on (__attribute__ ((unused)) uint16_t ticks) { return false; }
bool on () { return false; }
void off () {}
bool active () { return false; }
};
}
#endif