aiie/teensy/teensy-speaker.cpp

51 lines
922 B
C++
Raw Normal View History

2017-02-20 18:55:16 +00:00
#include <Arduino.h>
#include "teensy-speaker.h"
2017-02-27 01:34:38 +00:00
#include "globals.h"
2017-02-20 18:55:16 +00:00
TeensySpeaker::TeensySpeaker(uint8_t pinNum) : PhysicalSpeaker()
{
toggleState = false;
needsToggle = false;
2017-02-20 18:55:16 +00:00
speakerPin = pinNum;
pinMode(speakerPin, OUTPUT); // analog speaker output, used as digital volume control
mixerValue = numMixed = 0;
2017-02-20 18:55:16 +00:00
}
TeensySpeaker::~TeensySpeaker()
{
}
void TeensySpeaker::toggle()
2017-02-20 18:55:16 +00:00
{
needsToggle = true;
2017-02-20 18:55:16 +00:00
}
void TeensySpeaker::maintainSpeaker(uint32_t c)
{
if (needsToggle) {
toggleState = !toggleState;
needsToggle = false;
2017-02-20 18:55:16 +00:00
}
2017-02-28 22:51:07 +00:00
mixerValue += (toggleState ? 0x1FF : 0x00);
2017-02-27 01:34:38 +00:00
mixerValue >>= (16-g_volume);
2017-02-28 22:51:07 +00:00
// FIXME: glad it's DAC0 and all, but... how does that relate to the pin passed in the constructor?
2017-02-27 01:34:38 +00:00
analogWriteDAC0(mixerValue);
2017-02-20 18:55:16 +00:00
}
2017-02-24 15:15:17 +00:00
void TeensySpeaker::beginMixing()
2017-02-24 15:15:17 +00:00
{
mixerValue = 0;
numMixed = 0;
2017-02-24 15:15:17 +00:00
}
void TeensySpeaker::mixOutput(uint8_t v)
{
mixerValue += v;
numMixed++;
}