定时闹钟

This commit is contained in:
Stone 2016-08-15 09:01:38 +00:00
parent 5b2d4bce64
commit 4b0e9246bb
2 changed files with 154 additions and 0 deletions

111
App/Alarm.cpp Normal file
View File

@ -0,0 +1,111 @@
#include "Alarm.h"
Alarm::Alarm(Timer* timer,OutputPort* pin)
{
Init();
if(timer!= nullptr)
_timer = timer;
if(pin!= nullptr)
_phonatePin = pin;
}
void Alarm::Init()
{
_tuneSet = nullptr;
_tuneNum = 0;
_timer = nullptr;
_phonatePin = nullptr;
Sounding = false;
sound_cnt = 0;
music_beat = 0;
music_freq = 0;
}
void Alarm::Disposable()
{
if(_timer)
{
_timer->Close();
delete(_timer);
}
if(_phonatePin)delete(_phonatePin);
_tuneSet = nullptr;
}
Alarm::~Alarm()
{
_tuneSet = nullptr;
_tuneNum = 0;
_timer->Close();
Sounding = false;
_timer = nullptr;
}
void Alarm::Sound()
{
if(_timer!= nullptr && _phonatePin != nullptr && _tuneSet != nullptr && _tuneNum != 0)
{
_timer->SetFrequency(100000);
//_timer->Register(TimerHander, this);
_timer->Register(Delegate<Timer&>(&Alarm::TimerHander, this));
_timer->Open();
Sounding = true;
}
}
void Alarm::Sound(const Tune* tune,int num)
{
SetTuneSet(tune,num);
Sound();
}
void Alarm::Unsound()
{
_timer->Close();
Sounding = false;
}
void Alarm::SetTuneSet(const Tune* tune, int num)
{
if(Sounding) Unsound();
if(tune != nullptr && num != 0)
{
_tuneSet = tune;
_tuneNum = num;
}
}
bool Alarm::getStat()
{
return Sounding;
}
void Alarm::TimerHander(Timer& timer)
{
/*if(param == nullptr)return;
Alarm * music = (Alarm * )param;
music->phonate();*/
phonate();
}
void Alarm::phonate()
{
if(music_freq == 0)
{
music_freq = _tuneSet[sound_cnt].freq;
*_phonatePin = !*_phonatePin;
}
if(music_beat == 0)
{
sound_cnt++;
if(sound_cnt >= _tuneNum)
sound_cnt = 0 ;
music_beat = _tuneSet[sound_cnt].timer * 700;
music_freq = _tuneSet[sound_cnt].freq;
}
music_freq --;
music_beat --;
}

43
App/Alarm.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef __Alarm_H__
#define __Alarm_H__
#include "Sys.h"
#include "Timer.h"
#include "Port.h"
typedef struct // 单个音节
{
byte freq;
byte timer;
}Tune;
class Alarm
{
private:
const Tune* _tuneSet; // 调子集合 就是曲子
int _tuneNum; // 有多少个调子
Timer* _timer; // 定时器
OutputPort* _phonatePin; // 输出引脚
bool Sounding; // 作为判断的依据 最好只有Get 没有Set
public:
bool getStat();
Alarm(Timer*,OutputPort*);
void Init();
~Alarm();
void Sound();
void Unsound();
void Sound(const Tune*,int num = 0);
void SetTuneSet(const Tune*,int num = 0);
void Disposable();
private:
volatile int sound_cnt;
volatile int music_freq;
volatile int music_beat;
void TimerHander(Timer& timer);
void phonate();
};
#endif