-
Notifications
You must be signed in to change notification settings - Fork 1
/
speaker.c
161 lines (144 loc) · 4.13 KB
/
speaker.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright (c) 2011 Steve Chamberlin
Permission is hereby granted, free of charge, to any person obtaining a copy of this hardware, software, and associated documentation
files (the "Product"), to deal in the Product without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Product, and to permit persons to whom the Product is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Product.
THE PRODUCT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE PRODUCT OR THE USE OR OTHER DEALINGS IN THE PRODUCT.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "speaker.h"
#define SPEAKER_PIN PD6
#define SPEAKER_GROUND_PIN PD0
uint8_t speaker_beepType;
volatile uint8_t speaker_toneCount;
volatile uint8_t speaker_in_use = 0;
volatile uint8_t soundEnable = 1;
void SpeakerInit()
{
// set the speaker as output
DDRD |= (1<<SPEAKER_PIN);
DDRD |= (1<<SPEAKER_GROUND_PIN);
SpeakerOff();
}
void SpeakerOff()
{
speaker_in_use = 0;
PORTD &= ~(1<<SPEAKER_PIN);
PORTD &= ~(1<<SPEAKER_GROUND_PIN);
TCCR0A = 0; // disable OCR0A pin
TIMSK1 = 0; // disable timer 1 interrupts
TIFR1 = 0; // clear timer 1 interrupt flags
PRR |= (1<<PRTIM1) | (1<<PRTIM0);
}
#ifdef LOGGER_CLASSIC
#define BEEP_DURATION 100
#define BEEP_DURATION_LONG 300
#endif
#ifdef LOGGER_MINI
#define BEEP_DURATION 800
#define BEEP_DURATION_LONG 2400
#endif
void SpeakerNextTone()
{
uint8_t tone0;
if (speaker_toneCount == 0)
{
if (speaker_beepType == BEEP_NEXT || speaker_beepType == BEEP_ENTER
#ifdef SHAKE_SENSOR
|| speaker_beepType == BEEP_TIMER_END
#endif
)
{
tone0 = 64;
}
else
{
tone0 = 96;
}
}
else if (speaker_toneCount == 1)
{
if (speaker_beepType == BEEP_NEXT)
{
SpeakerOff();
return;
}
else if (speaker_beepType == BEEP_EXIT
#ifdef SHAKE_SENSOR
|| speaker_beepType == BEEP_TIMER_START
#endif
)
{
tone0 = 64;
}
else
{
tone0 = 96;
}
}
#ifdef SHAKE_SENSOR
else if (speaker_toneCount == 2 && speaker_beepType == BEEP_TIMER_START)
{
tone0 = 64;
}
else if (speaker_toneCount == 2 && speaker_beepType == BEEP_TIMER_END)
{
tone0 = 96;
}
#endif
else
{
SpeakerOff();
return;
}
// setup timer 0 for tone PWM
PRR &= ~(1<<PRTIM0);
TCCR0A = (1<<COM0A0) | (1<<WGM01); // Toggle OC0A on Compare Match, CTC OCR0A mode (WGM02 in TCCR0B)
#ifdef LOGGER_CLASSIC
TCCR0B = (1<<CS01) ; // clk/8 prescale
#endif
#ifdef LOGGER_MINI
TCCR0B = (1<<CS01) | (1<<CS00); // clk/64 prescale
#endif
OCR0A = tone0;
TCNT0 = 0;
// setup timer 1 for note length of approximately 100 ms
PRR &= ~(1<<PRTIM1);
TCCR1A = 0; // CTC OCR0A mode, (WGM13 and WGM12 in TCCR1B)
TCCR1B = (1<<CS12) | (1<<CS10) | (1<<WGM12) ; // prescale clock/1024
// datasheet section 16.3: To do a 16-bit write, the high byte must be written before the low byte.
#ifdef SHAKE_SENSOR
if (speaker_beepType == BEEP_TIMER_START || speaker_beepType == BEEP_TIMER_END)
{
OCR1AH = (BEEP_DURATION_LONG >> 8);
OCR1AL = (BEEP_DURATION_LONG & 0xFF);
}
else
#endif
{
OCR1AH = (BEEP_DURATION >> 8);
OCR1AL = (BEEP_DURATION & 0xFF);
}
TIFR1 = (1 << OCF1A); // clear the timer 1 compare A match interrupt flag
TIMSK1 = (1 << OCIE1A); // enable timer 1 compare A match interrupt
}
void SpeakerBeep(uint8_t beepType)
{
if (!soundEnable)
return;
speaker_beepType = beepType;
speaker_toneCount = 0;
speaker_in_use = 1;
SpeakerNextTone();
}
ISR(TIMER1_COMPA_vect)
{
speaker_toneCount++;
SpeakerNextTone();
}