Skip to content

Commit

Permalink
moved buzzer section from Led.h to Buzzer.h
Browse files Browse the repository at this point in the history
  • Loading branch information
jp112sdl committed Jan 20, 2019
1 parent ef07be9 commit 72826cb
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 94 deletions.
1 change: 1 addition & 0 deletions AskSinPP.h
Expand Up @@ -37,6 +37,7 @@
#include <Debug.h>
#include <Activity.h>
#include <Led.h>
#include <Buzzer.h>
#include <AlarmClock.h>
#include <Message.h>
#include <Button.h>
Expand Down
110 changes: 110 additions & 0 deletions 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<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
1 change: 1 addition & 0 deletions Device.h
Expand Up @@ -14,6 +14,7 @@
#include "Message.h"
#include "Radio.h"
#include "Led.h"
#include "Buzzer.h"
#include "Activity.h"

#ifdef USE_HW_SERIAL
Expand Down
94 changes: 0 additions & 94 deletions Led.h
Expand Up @@ -176,100 +176,6 @@ class NoLed {
void invert (__attribute__((unused)) bool value) {}
};


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

0 comments on commit 72826cb

Please sign in to comment.