Skip to content

Instantly share code, notes, and snippets.

@blindman2k
Last active December 21, 2015 20:09
Show Gist options
  • Save blindman2k/6359427 to your computer and use it in GitHub Desktop.
Save blindman2k/6359427 to your computer and use it in GitHub Desktop.
Squirrel code for outputting a three second tone from an Imp "Lala" audio board.
/* ----------------[ PIN CONFIGURATION ]----------------------------------------
Pinout:
1 = Wake / SPI CLK
2 = Sampler (Audio In)
5 = DAC (Audio Out)
6 = Button 1
7 = SPI CS_L
8 = SPI MOSI
9 = SPI MISO
A = Battery Check (ADC) (Enabled on Mic Enable)
B = Speaker Enable
C = Mic Enable
D = User LED
E = Button 2
*/
// =============================================================================
class audio {
static DAC_SAMPLE_RATE = 880; // Twice the frequency of the output (middle A)
static DAC_BUFFER_SIZE = 1024;
pin_speaker = null;
pin_speaker_enable = null;
noise = null;
// -------------------------------------------------------------------------
constructor(_pin_speaker, _pin_speaker_enable) {
pin_speaker = _pin_speaker;
pin_speaker_enable = _pin_speaker_enable;
pin_speaker_enable.configure(DIGITAL_OUT);
pin_speaker_enable.write(0);
noise = blob(DAC_BUFFER_SIZE);
for (local i = 0; i < noise.len(); i++) {
noise[i] = (i%2==0) ? 0x05 : 0x00;
}
}
// -------------------------------------------------------------------------
function play()
{
stop_play();
hardware.fixedfrequencydac.configure(pin_speaker,
DAC_SAMPLE_RATE,
[ noise, noise ],
_buffer_empty.bindenv(this),
A_LAW_DECOMPRESS);
pin_speaker_enable.write(1);
hardware.fixedfrequencydac.start();
}
// -------------------------------------------------------------------------
function _buffer_empty(buffer)
{
if (!buffer) {
server.error("Audio buffer underrun");
return stop_play();
}
hardware.fixedfrequencydac.addbuffer(noise);
}
// -------------------------------------------------------------------------
function stop_play()
{
pin_speaker_enable.write(0);
hardware.fixedfrequencydac.stop();
}
}
sound_system <- audio(hardware.pin5, hardware.pinB);
sound_system.play();
imp.wakeup(3, sound_system.stop_play.bindenv(sound_system))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment