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