From 72826cb90cb08c3948a21ce250d2a249734c6d89 Mon Sep 17 00:00:00 2001 From: jp112sdl Date: Sun, 20 Jan 2019 21:16:24 +0100 Subject: [PATCH] moved buzzer section from Led.h to Buzzer.h --- AskSinPP.h | 1 + Buzzer.h | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Device.h | 1 + Led.h | 94 --------------------------------------------- 4 files changed, 112 insertions(+), 94 deletions(-) create mode 100644 Buzzer.h diff --git a/AskSinPP.h b/AskSinPP.h index 55b4dd90..2c593ffd 100644 --- a/AskSinPP.h +++ b/AskSinPP.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/Buzzer.h b/Buzzer.h new file mode 100644 index 00000000..276418e1 --- /dev/null +++ b/Buzzer.h @@ -0,0 +1,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 +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 diff --git a/Device.h b/Device.h index a56747a0..d8b7ff5e 100644 --- a/Device.h +++ b/Device.h @@ -14,6 +14,7 @@ #include "Message.h" #include "Radio.h" #include "Led.h" +#include "Buzzer.h" #include "Activity.h" #ifdef USE_HW_SERIAL diff --git a/Led.h b/Led.h index 9e95a43a..fa715f18 100644 --- a/Led.h +++ b/Led.h @@ -176,100 +176,6 @@ class NoLed { void invert (__attribute__((unused)) bool value) {} }; - -template -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