2018-09-10 18:57:16 +00:00
|
|
|
/**
|
|
|
|
* @file Arduboy2Beep.cpp
|
|
|
|
* \brief
|
|
|
|
* Classes to generate simple square wave tones on the Arduboy speaker pins.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include "Arduboy2Beep.h"
|
|
|
|
|
|
|
|
#ifndef AB_DEVKIT
|
|
|
|
|
|
|
|
// Speaker pin 1, Timer 3A, Port C bit 6, Arduino pin 5
|
|
|
|
|
|
|
|
uint8_t BeepPin1::duration = 0;
|
|
|
|
|
|
|
|
void BeepPin1::begin()
|
|
|
|
{
|
|
|
|
TCCR3A = 0;
|
|
|
|
TCCR3B = (bit(WGM32) | bit(CS31)); // CTC mode. Divide by 8 clock prescale
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::tone(uint16_t count)
|
|
|
|
{
|
|
|
|
tone(count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::tone(uint16_t count, uint8_t dur)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::timer()
|
|
|
|
{
|
|
|
|
if (duration && (--duration == 0)) {
|
|
|
|
TCCR3A = 0; // set normal mode (which disconnects the pin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::noTone()
|
|
|
|
{
|
|
|
|
duration = 0;
|
|
|
|
TCCR3A = 0; // set normal mode (which disconnects the pin)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-05 20:43:40 +00:00
|
|
|
// Speaker pin 2, Timer 4A, Port C bit 7, Arduino pin 13 or Port D bit 7, Arduino pin 6 for alternate wiring
|
2018-09-10 18:57:16 +00:00
|
|
|
|
|
|
|
uint8_t BeepPin2::duration = 0;
|
|
|
|
|
|
|
|
void BeepPin2::begin()
|
|
|
|
{
|
|
|
|
TCCR4A = 0; // normal mode. Disable PWM
|
|
|
|
TCCR4B = bit(CS43); // divide by 128 clock prescale
|
2021-12-05 20:43:40 +00:00
|
|
|
#ifdef AB_ALTERNATE_WIRING
|
|
|
|
TCCR4C = 0; // normal mode
|
|
|
|
#endif
|
2018-09-10 18:57:16 +00:00
|
|
|
TCCR4D = 0; // normal mode
|
|
|
|
TC4H = 0; // toggle pin at count = 0
|
|
|
|
OCR4A = 0; // "
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::tone(uint16_t count)
|
|
|
|
{
|
|
|
|
tone(count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::tone(uint16_t count, uint8_t dur)
|
|
|
|
{
|
|
|
|
duration = dur;
|
2021-12-05 20:43:40 +00:00
|
|
|
#ifdef AB_ALTERNATE_WIRING
|
|
|
|
TCCR4C = bit(COM4D0); // set toggle on compare mode (which connects pin 6)
|
|
|
|
#else
|
|
|
|
TCCR4A = bit(COM4A0); // set toggle on compare mode (which connects pin 13)
|
|
|
|
#endif
|
|
|
|
TC4H = highByte(count); // load the count (10 bits),
|
2018-09-10 18:57:16 +00:00
|
|
|
OCR4C = lowByte(count); // which determines the frequency
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::timer()
|
|
|
|
{
|
|
|
|
if (duration && (--duration == 0)) {
|
2021-12-05 20:43:40 +00:00
|
|
|
noTone();
|
2018-09-10 18:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::noTone()
|
|
|
|
{
|
|
|
|
duration = 0;
|
2021-12-05 20:43:40 +00:00
|
|
|
#ifdef AB_ALTERNATE_WIRING
|
|
|
|
TCCR4C = 0; // set normal mode (which disconnects the pin)
|
|
|
|
#else
|
2018-09-10 18:57:16 +00:00
|
|
|
TCCR4A = 0; // set normal mode (which disconnects the pin)
|
2021-12-05 20:43:40 +00:00
|
|
|
#endif
|
2018-09-10 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else /* AB_DEVKIT */
|
|
|
|
|
|
|
|
// *** The pins used for the speaker on the DevKit cannot be directly
|
|
|
|
// controlled by a timer/counter. The following "dummy" functions will
|
|
|
|
// compile and operate properly but no sound will be produced
|
|
|
|
|
|
|
|
uint8_t BeepPin1::duration = 0;
|
|
|
|
|
|
|
|
void BeepPin1::begin()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::tone(uint16_t count)
|
|
|
|
{
|
|
|
|
tone(count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::tone(uint16_t count, uint8_t dur)
|
|
|
|
{
|
|
|
|
(void) count; // parameter not used
|
|
|
|
|
|
|
|
duration = dur;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::timer()
|
|
|
|
{
|
|
|
|
if (duration) {
|
|
|
|
--duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin1::noTone()
|
|
|
|
{
|
|
|
|
duration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t BeepPin2::duration = 0;
|
|
|
|
|
|
|
|
void BeepPin2::begin()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::tone(uint16_t count)
|
|
|
|
{
|
|
|
|
tone(count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::tone(uint16_t count, uint8_t dur)
|
|
|
|
{
|
|
|
|
(void) count; // parameter not used
|
|
|
|
|
|
|
|
duration = dur;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::timer()
|
|
|
|
{
|
|
|
|
if (duration) {
|
|
|
|
--duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeepPin2::noTone()
|
|
|
|
{
|
|
|
|
duration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|