forked from hartmann1301/Arduboy2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Arduboy2Beep.cpp
114 lines (99 loc) · 2.15 KB
/
Arduboy2Beep.cpp
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
/**
* @file Arduboy2Beep.cpp
* \brief
* Classes to generate simple square wave tones on the Arduboy speaker pins.
*/
#include <Arduino.h>
#include "Arduboy2Beep.h"
#include "Arduboy2Core.h"
uint8_t BeepPin1::duration = 0;
void BeepPin1::begin()
{
#ifndef ESP8266
TCCR3A = 0;
TCCR3B = (bit(WGM32) | bit(CS31)); // CTC mode. Divide by 8 clock prescale
#endif
}
void BeepPin1::tone(uint16_t count)
{
#ifdef ESP8266
::tone(PIN_SPEAKER_1, count);
#else
tone(count, 0);
#endif
}
void BeepPin1::tone(uint16_t count, uint8_t dur)
{
#ifdef ESP8266
::tone(PIN_SPEAKER_1, count, dur);
#else
duration = dur;
TCCR3A = bit(COM3A0); // set toggle on compare mode (which connects the pin)
OCR3A = count; // load the count (16 bits), which determines the frequency
#endif
}
void BeepPin1::timer()
{
#ifndef ESP8266
if (duration && (--duration == 0)) {
TCCR3A = 0; // set normal mode (which disconnects the pin)
}
#endif
}
void BeepPin1::noTone()
{
#ifdef ESP8266
::noTone(PIN_SPEAKER_1);
#else
duration = 0;
TCCR3A = 0; // set normal mode (which disconnects the pin)
#endif
}
// Speaker pin 2, Timer 4A, Port C bit 7, Arduino pin 13
uint8_t BeepPin2::duration = 0;
void BeepPin2::begin()
{
#ifndef ESP8266
TCCR4A = 0; // normal mode. Disable PWM
TCCR4B = bit(CS43); // divide by 128 clock prescale
TCCR4D = 0; // normal mode
TC4H = 0; // toggle pin at count = 0
OCR4A = 0; // "
#endif
}
void BeepPin2::tone(uint16_t count)
{
#ifdef ESP8266
::tone(PIN_SPEAKER_2, count);
#else
tone(count, 0);
#endif
}
void BeepPin2::tone(uint16_t count, uint8_t dur)
{
#ifdef ESP8266
::tone(PIN_SPEAKER_2, count, dur);
#else
duration = dur;
TCCR4A = bit(COM4A0); // set toggle on compare mode (which connects the pin)
TC4H = highByte(count); // load the count (10 bits),
OCR4C = lowByte(count); // which determines the frequency
#endif
}
void BeepPin2::timer()
{
#ifndef ESP8266
if (duration && (--duration == 0)) {
TCCR4A = 0; // set normal mode (which disconnects the pin)
}
#endif
}
void BeepPin2::noTone()
{
#ifdef ESP8266
::noTone(PIN_SPEAKER_2);
#else
duration = 0;
TCCR4A = 0; // set normal mode (which disconnects the pin)
#endif
}