Arduboy2 Library  6.0.0
Arduboy2Beep.cpp
Go to the documentation of this file.
1 
7 #include <Arduino.h>
8 #include "Arduboy2Beep.h"
9 
10 #ifndef AB_DEVKIT
11 
12 // Speaker pin 1, Timer 3A, Port C bit 6, Arduino pin 5
13 
14 uint8_t BeepPin1::duration = 0;
15 
17 {
18  TCCR3A = 0;
19  TCCR3B = (bit(WGM32) | bit(CS31)); // CTC mode. Divide by 8 clock prescale
20 }
21 
22 void BeepPin1::tone(uint16_t count)
23 {
24  tone(count, 0);
25 }
26 
27 void BeepPin1::tone(uint16_t count, uint8_t dur)
28 {
29  duration = dur;
30  TCCR3A = bit(COM3A0); // set toggle on compare mode (which connects the pin)
31  OCR3A = count; // load the count (16 bits), which determines the frequency
32 }
33 
35 {
36  if (duration && (--duration == 0)) {
37  TCCR3A = 0; // set normal mode (which disconnects the pin)
38  }
39 }
40 
42 {
43  duration = 0;
44  TCCR3A = 0; // set normal mode (which disconnects the pin)
45 }
46 
47 
48 // Speaker pin 2, Timer 4A, Port C bit 7, Arduino pin 13
49 
50 uint8_t BeepPin2::duration = 0;
51 
53 {
54  TCCR4A = 0; // normal mode. Disable PWM
55  TCCR4B = bit(CS43); // divide by 128 clock prescale
56  TCCR4D = 0; // normal mode
57  TC4H = 0; // toggle pin at count = 0
58  OCR4A = 0; // "
59 }
60 
61 void BeepPin2::tone(uint16_t count)
62 {
63  tone(count, 0);
64 }
65 
66 void BeepPin2::tone(uint16_t count, uint8_t dur)
67 {
68  duration = dur;
69  TCCR4A = bit(COM4A0); // set toggle on compare mode (which connects the pin)
70  TC4H = highByte(count); // load the count (10 bits),
71  OCR4C = lowByte(count); // which determines the frequency
72 }
73 
75 {
76  if (duration && (--duration == 0)) {
77  TCCR4A = 0; // set normal mode (which disconnects the pin)
78  }
79 }
80 
82 {
83  duration = 0;
84  TCCR4A = 0; // set normal mode (which disconnects the pin)
85 }
86 
87 
88 #else /* AB_DEVKIT */
89 
90 // *** The pins used for the speaker on the DevKit cannot be directly
91 // controlled by a timer/counter. The following "dummy" functions will
92 // compile and operate properly but no sound will be produced
93 
94 uint8_t BeepPin1::duration = 0;
95 
96 void BeepPin1::begin()
97 {
98 }
99 
100 void BeepPin1::tone(uint16_t count)
101 {
102  tone(count, 0);
103 }
104 
105 void BeepPin1::tone(uint16_t count, uint8_t dur)
106 {
107  (void) count; // parameter not used
108 
109  duration = dur;
110 }
111 
112 void BeepPin1::timer()
113 {
114  if (duration) {
115  --duration;
116  }
117 }
118 
119 void BeepPin1::noTone()
120 {
121  duration = 0;
122 }
123 
124 
125 uint8_t BeepPin2::duration = 0;
126 
127 void BeepPin2::begin()
128 {
129 }
130 
131 void BeepPin2::tone(uint16_t count)
132 {
133  tone(count, 0);
134 }
135 
136 void BeepPin2::tone(uint16_t count, uint8_t dur)
137 {
138  (void) count; // parameter not used
139 
140  duration = dur;
141 }
142 
143 void BeepPin2::timer()
144 {
145  if (duration) {
146  --duration;
147  }
148 }
149 
150 void BeepPin2::noTone()
151 {
152  duration = 0;
153 }
154 
155 #endif
BeepPin2::noTone
static void noTone()
Stop a tone that is playing on speaker pin 2.
Definition: Arduboy2Beep.cpp:81
BeepPin2::tone
static void tone(uint16_t count)
Play a tone on speaker pin 2 continually, until replaced by a new tone or stopped.
Definition: Arduboy2Beep.cpp:61
BeepPin1::begin
static void begin()
Set up the hardware.
Definition: Arduboy2Beep.cpp:16
BeepPin2::duration
static uint8_t duration
The counter used by the timer() function to time the duration of a tone played on speaker pin 2.
Definition: Arduboy2Beep.h:293
BeepPin2::timer
static void timer()
Handle the duration that a tone on speaker pin 2 plays for.
Definition: Arduboy2Beep.cpp:74
BeepPin1::noTone
static void noTone()
Stop a tone that is playing.
Definition: Arduboy2Beep.cpp:41
BeepPin1::duration
static uint8_t duration
The counter used by the timer() function to time the duration of a tone.
Definition: Arduboy2Beep.h:146
BeepPin1::tone
static void tone(uint16_t count)
Play a tone continually, until replaced by a new tone or stopped.
Definition: Arduboy2Beep.cpp:22
Arduboy2Beep.h
Classes to generate simple square wave tones on the Arduboy speaker pins.
BeepPin2::begin
static void begin()
Set up the hardware for playing tones using speaker pin 2.
Definition: Arduboy2Beep.cpp:52
BeepPin1::timer
static void timer()
Handle the duration that a tone plays for.
Definition: Arduboy2Beep.cpp:34