int main (void) { FRESULT res; char *dir; BYTE org_osc = OSCCAL; MCUSR = 0; WDTCR = _BV(WDE) | 0b110; /* Enable WDT reset in timeout of 1s */ PORTB = 0b101001; /* Initialize port: - - H L H L L P */ DDRB = 0b111110; sei(); for (;;) { if (pf_mount(&Fs) == FR_OK) { /* Initialize FS */ wdt_reset(); sendDiag(MOUNT_OK); Buff[0] = 0; if (!pf_open("osccal")) pf_read(Buff, 1, &rb); /* Adjust frequency */ OSCCAL = org_osc + Buff[0]; res = pf_opendir(&Dir, dir = "wav"); /* Open sound file directory */ if (res == FR_NO_PATH){ res = pf_opendir(&Dir, dir = ""); /* Open root directory */ sendDiag(OPEN_ROOT_DIR); } else{ sendDiag(OPEN_WAV_DIR); } while (res == FR_OK) { /* Repeat in the dir */ res = pf_readdir(&Dir, 0); /* Rewind dir */ while (res == FR_OK) { /* Play all wav files in the dir */ wdt_reset(); res = pf_readdir(&Dir, &Fno); /* Get a dir entry */ if (res || !Fno.fname[0]){ if(res) sendDiag(DIR_ERROR); else sendDiag(END_OF_DIR); break; } /* Break on error or end of dir */ if (!(Fno.fattrib & (AM_DIR|AM_HID)) && strstr(Fno.fname, ".WAV")){ sendFilename(Fno.fname); wdt_reset(); sendDiag(PLAY_SONG); res = play(dir, Fno.fname); /* Play file */ } sendDiag(FINISH); } } } else{ sendDiag(MOUNT_FAIL); } delay500(); } }
int main (void) { FRESULT res; char *dir; BYTE org_osc = OSCCAL; BYTE ndir = 0; BYTE nskip = 0; BYTE nplay = 0; // MCUSR = 0; WDTCR = _BV(WDE) | 0b110; /* Enable WDT reset in timeout of 1s */ PORTB = 0b101001; /* Initialize port: - - H L H L L P */ DDRB = 0b111110; sei(); /*-----------------------------------------------------------------------*/ /* POWER ON and RESET */ ndir = eeprom_read_byte((uint8_t*)DIR_EEADR); if(bit_is_set(MCUSR, PORF)) { // Power on - resume playback! MCUSR = 0; // clear MCUSR } if(bit_is_set(MCUSR, WDRF)) { // WatchDog reset - on error! MCUSR = 0; // clear MCUSR if (ndir !=0) { ndir = 0; // resetdirectory nskip = 0; // resetdirectory eeprom_write_byte ((uint8_t*)SKIP_EEADR, (uint8_t*) nskip); eeprom_write_byte ((uint8_t*)DIR_EEADR, (uint8_t*) ndir); // for (;;) { // PORTB ^= _BV(PB1); // delay_ms(200); // wdt_reset(); // } } } if(bit_is_set(MCUSR, EXTRF)) { // Reset button. MCUSR = 0; // clear MCUSR ndir++; // advance mode // for (;;) {wdt_reset();} wdt_reset(); if(ndir > MAX_DIR) { ndir = 0; // resetdirectory } nskip = 0; // resetdirectory eeprom_write_byte ((uint8_t*)SKIP_EEADR, (uint8_t*) nskip); eeprom_write_byte ((uint8_t*)DIR_EEADR, (uint8_t*) ndir); } /*-----------------------------------------------------------------------*/ /* How many songs need to skip */ str[0] =48+ndir/10; str[1] = 48+ndir%10; str[2] = 0; PLAYMOD=0; if (pf_mount(&Fs) == FR_OK) { /* Initialize FS */ res = pf_opendir(&Dir, dir = str); /* Open sound file directory */ res = play(dir, "i"); /* Play intro file */ res = play(dir, "n"); /* Play name file */ /* PLAY mode 0 normal, 1 repeat loop, 2 only one file */ if (play(dir, "r") == FR_OK) { PLAYMOD=1; //repeat loop } if (play(dir, "o") == FR_OK) { PLAYMOD=2; //only one file } } nskip = eeprom_read_byte((uint8_t*)SKIP_EEADR); // FR_OK = 0 for (;;) { if (pf_mount(&Fs) == FR_OK) { /* Initialize FS */ wdt_reset(); // Buff[0] = 0; // if (!pf_open("osccal")) pf_read(Buff, 1, &rb); /* Adjust frequency */ // OSCCAL = org_osc + Buff[0]; res = pf_opendir(&Dir, dir = str); /* Open sound file directory */ if (res == FR_NO_PATH) res = pf_opendir(&Dir, dir = ""); /* Open root directory */ while (res == FR_OK) { /* Repeat in the dir */ res = pf_readdir(&Dir, 0); /* Rewind dir */ nplay=0; while (res == FR_OK) { /* Play all wav files in the dir */ wdt_reset(); res = pf_readdir(&Dir, &Fno); /* Get a dir entry */ if (res || !Fno.fname[0]) break; /* Break on error or end of dir */ if (!(Fno.fattrib & (AM_DIR|AM_HID)) && strstr(Fno.fname, ".WAV")) if (nplay >= nskip) { do { res = play(dir, Fno.fname); /* Play file */ wdt_reset(); } while (((PINB & 1) == 1) && (PLAYMOD == 1)); delay500(); /* Delay 500ms in low power sleep mode */ if (res == FR_OK) { eeprom_write_byte ((uint8_t*)SKIP_EEADR, (uint8_t*) nplay+1); nskip=0; /* PLAY mode 0 normal, 1 repeat loop, 2 only one file */ // PORTB ^= _BV(PB1); if (PLAYMOD == 2) { do { delay_ms(30); wdt_reset(); } while ((PINB & 1) == 1); do { delay_ms(50); wdt_reset(); } while ((PINB & 1) == 0); delay_ms(500); } } } nplay++; // playing song nr } } } } }