This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
buzz.h
137 lines (111 loc) · 3.1 KB
/
buzz.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**********************************************************************
BrewManiac
created by Vito Tai
Copyright (C) 2015 Vito Tai
This soft ware is provided as-is. Use at your own risks.
You are free to modify and distribute this software without removing
this statement.
BrewManiac by Vito Tai is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
***********************************************************************/
#ifndef BUZZ_H
#define BUZZ_H
#define BUZZER_TIME_TOLERANCE 5
typedef byte SoundId;
// for one shot sound
#define SoundIdTemperatureReached 0
#define SoundIdCountDown 1
#define SoundIdAddHop 2
#define SoundIdWaitUserInteraction 3
#define SoundIdBrewEnd 4
#define SoundIdMashInTempReached SoundIdTemperatureReached
#define SoundIdBoil SoundIdTemperatureReached
#define SoundIdDelayTimeout SoundIdTemperatureReached
#define SoundIdIodineTest SoundIdTemperatureReached
#define SoundIdUserInteractiveNeeded 0
#define SoundIdConfirmUser 0
#define SoundIdWarnning SoundIdTemperatureReached
const byte _soundIdTemperatureReached[] PROGMEM ={5,10,3,10,3,10};
const byte _soundIdCountDown[] PROGMEM ={9,20,20,20,20,20,20,20,20,40}; // 5 seconds
//HOP_ALTERTING_TIME
const byte _soundIdAddHop[] PROGMEM ={19,30,10,30,10,30,10,30,10,30,10,30,10,30,10,30,10,30,10,40}; // 10 seconds
const byte _soundIdWaitUserInteraction[] PROGMEM ={2,10,30};
const byte _soundIdBrewEnd[] PROGMEM ={2,37,7};
const byte * const _sounds [] PROGMEM ={
_soundIdTemperatureReached,
_soundIdCountDown,
_soundIdAddHop,
_soundIdWaitUserInteraction,
_soundIdBrewEnd};
// a hit & run function
// buzzer thread will handle the pattern & time
byte _numberofNtesToPlay;
boolean _repeat;
boolean _playing;
boolean _buzzing;
unsigned long _buzzingTime;
word _currentPeriod;
byte* _ptrCurrentNote;
byte* _currentSound;
void buzzStartPlay(SoundId id, boolean repeat)
{
_currentSound=(byte *)pgm_read_word(&(_sounds[id]));
_numberofNtesToPlay = pgm_read_byte(_currentSound);
_ptrCurrentNote =_currentSound;
_ptrCurrentNote ++;
_currentPeriod = (word)pgm_read_byte(_ptrCurrentNote) *25;
_repeat = repeat;
_playing=true;
_buzzingTime = millis();
digitalWrite (BuzzControlPin, HIGH);
_buzzing=true;
}
void buzzPlaySound(SoundId id)
{
buzzStartPlay(id,false);
}
// play the sound until mute() is called
void buzzPlaySoundRepeat(SoundId id)
{
buzzStartPlay(id,true);
}
void buzzMute(void)
{
digitalWrite (BuzzControlPin, LOW);
_playing=false;
}
void buzzThread(void)
{
if(!_playing) return;
if((gCurrentTimeInMS-_buzzingTime) >= (_currentPeriod + BUZZER_TIME_TOLERANCE))
{
_numberofNtesToPlay --;
if(_numberofNtesToPlay ==0)
{
if(_repeat)
{
_ptrCurrentNote =_currentSound;
_numberofNtesToPlay = pgm_read_byte(_currentSound);
}
else
{
// finished, stop
buzzMute();
return;
}
}
_buzzingTime = millis();
//else
_ptrCurrentNote ++;
_currentPeriod = (word)pgm_read_byte(_ptrCurrentNote) *25;
if(_buzzing)
{
digitalWrite (BuzzControlPin, LOW);
}
else
{
digitalWrite (BuzzControlPin, HIGH);
}
_buzzing = ! _buzzing;
}
}
#endif