Yeah thanks! That is exactly the one I found as well (linked in the original post). That's one reason I chose to go with the Digispark. I figured if the ATTiny85 could do that then surely I could get a Digispark to simply play a single sound. I think his project is much more complicated than I need to get, as I just want it to have a single sound and have no need for the SD card reading. I've gotten that part taken care of with that other library which converts a sound file to a header file of bytes.
In that guy's play() function, some of the stuff is unfamiliar to me:
FRESULT play (
const char *dir, /* Directory */
const char *fn /* File */
)
{
DWORD sz;
FRESULT res;
BYTE sw;
WORD btr;
wdt_reset();
xsprintf((char*)Buff, PSTR("%s/%s"), dir, fn);
res = pf_open((char*)Buff); /* Open sound file */
if (res == FR_OK) {
sz = load_header(); /* Check file format and ready to play */
if (sz < 1024) return 255; /* Cannot play this file */
FifoCt = 0; FifoRi = 0; FifoWi = 0; /* Reset audio FIFO */
if (!TCCR1) { /* Enable audio out if not enabled */
PLLCSR = 0b00000110; /* Select PLL clock for TC1.ck */
GTCCR = 0b01100000; /* Enable OC1B as PWM */
TCCR1 = MODE ? 0b01100001 : 0b00000001; /* Start TC1 and enable OC1A as PWM if needed */
TCCR0A = 0b00000010; /* Statr TC0 as interval timer at 2MHz */
TCCR0B = 0b00000010;
TIMSK = _BV(OCIE0A);
ramp(1);
}
pf_read(0, 512 - (Fs.fptr % 512), &rb); /* Snip sector unaligned part */
sz -= rb;
sw = 1; /* Button status flag */
do { /* Data transfer loop */
wdt_reset();
btr = (sz > 1024) ? 1024 : (WORD)sz;/* A chunk of audio data */
res = pf_read(0, btr, &rb); /* Forward the data into audio FIFO */
if (rb != 1024) break; /* Break on error or end of data */
sz -= rb; /* Decrease data counter */
sw <<= 1; /* Break on button down */
} while ((PINB & 1) || ++sw != 1);
}
while (FifoCt) ; /* Wait for audio FIFO empty */
OCR1A = 128; OCR1B = 128; /* Return output to center level */
return res;
}So what I don't follow is all the caps keywords which obviously correspond to some pins or settings in the chip. He's got the code commented well but I don't know how those would correspond to the Digispark.
I fiddled around with the included TinySoftPwm library and managed to get an annoying buzzing out of the speaker with this code (it's a start I guess!)
#include <TinySoftPwm.h>
void setup()
{
TinySoftPwm_begin(500, 0);
}
void loop()
{
static uint32_t startUs = micros();
static uint32_t startMs = millis();
if ((micros() - startUs) >= 500)
{
startUs = micros();
TinySoftPwm_process();
}
if ((millis() - startMs) >= 10)
{
startMs = millis();
TinySoftPwm_analogWrite(1, 100);
}
}