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(); } }
FRESULT PFFS::opendir ( DIRECT *dj, /* Pointer to directory object to create */ const char *path /* Pointer to the directory path */ ) { return pf_opendir(dj, path); }
FRESULT PFFS::begin ( unsigned char cs_pin, /* Pin to connect to CS */ uint8_t clkdivider // parameter for spi clock divider ) { _clkdivider = clkdivider; FRESULT res; CS = cs_pin; pinMode(CS, OUTPUT); #if _SoftSPI pinMode(_SCLK, OUTPUT); pinMode(_MOSI, OUTPUT); pinMode(_MISO, INPUT); #else SPI.begin(); #endif attach_cs_pin(CS_LOW, CS_HIGH, CS_READ); attach_dly100u(DLY100U); attach_SPIdriver(SPI_RECEIVE, SPI_SEND); disk_init(); res = mount(&fatfs_obj); SPI_SET_DIVIDER(_clkdivider); #if _USE_DIR res = pf_opendir(&dir_obj, (const CHAR *)"/"); #endif return res; }
FRESULT scan_files (char* path) { FRESULT res; FILINFO fno; DIR dir; char bufpath[12]; res = pf_opendir(&dir, path); if (res == FR_OK) { for (;;) { res = pf_readdir(&dir, &fno); if (res != FR_OK || fno.fname[0] == 0) break; if (fno.fattrib & AM_DIR) { psprintf(bufpath, "/%s", fno.fname); res = scan_files(bufpath); if (res != FR_OK) break; } else { #ifdef SERIAL_PRINT serial_printf("%s/", path); serial_printf("%s\r\n", fno.fname); #endif #ifdef CDC_PRINT CDCprintf("%s/", path); CDCprintf("%s\r\n", fno.fname); #endif } } } return res; }
void PrintNextImage(volatile void *uselessParameter) { _receivingImage = 0; if (previousFileIsValid) { uint16_t stringLength = strlen(fno.fname) + 1; MemCopy(fno.fname, previousFileName, 0, 0, stringLength); } FRESULT res; res = pf_readdir(&dir, &fno); if (res == FR_NO_FILE) { char *rootDir = " "; pf_opendir(&dir, rootDir); res = pf_readdir(&dir, &fno); if (res == FR_NO_FILE) { return; } } if (!CheckBMPImage(fno.fname)) { previousFileIsValid = true; PrintImage(fno.fname); } }
int FS_OpenDir(const char *path) { if (strncmp(path, "media", 5) == 0) { pf_switchfile(&fat[2]); _drive_num = 1; } else { pf_switchfile(&fat[0]); _drive_num = 0; } FRESULT res = pf_opendir(&dir, path); dbgprintf("Opendir: %d\n", res); return (res == FR_OK); }
static uint8_t mas_pff_get_nth_file(const char *path, uint16_t n, char *buf, uint8_t *is_dir) { FRESULT fr; uint16_t c = 0; if ( mas_last_readdir_n >= n ) { fr = pf_opendir(&mas_pff_dir, path); if ( fr != FR_OK ) return 0; } else { c = mas_last_readdir_n; c++; } while( c < 0x0ffff) { fr = pf_readdir(&mas_pff_dir, &mas_pff_fi); if ( fr != FR_OK ) return 0; if ( mas_pff_fi.fname[0] == '\0' ) return 0; if ( c == n ) { strcpy(buf, mas_pff_fi.fname); if ( (mas_pff_fi.fattrib & AM_DIR) != 0 ) { *is_dir = 1; } else { *is_dir = 0; } mas_last_readdir_n = n; return 1; } c++; } return 0; }
/** List a given directory. \param path The path to list. Toplevel path is "/". A slash is added to directory names, to make it easier for users to recognize them. */ void sd_list(const char* path) { FILINFO fno; DIR dir; result = pf_opendir(&dir, path); if (result == FR_OK) { for (;;) { result = pf_readdir(&dir, &fno); if (result != FR_OK || fno.fname[0] == 0) break; serial_writestr((uint8_t *)fno.fname); if (fno.fattrib & AM_DIR) serial_writechar('/'); serial_writechar('\n'); delay_ms(2); // Time for sending the characters. } } else { sersendf_P(PSTR("E: failed to open dir. (%su)\n"), result); } }
static uint16_t mas_pff_get_cnt(const char *path) { FRESULT fr; uint16_t c = 0; fr = pf_opendir(&mas_pff_dir, path); if ( fr != FR_OK ) return 0; while( c < 0x0ffff) { fr = pf_readdir(&mas_pff_dir, &mas_pff_fi); if ( fr != FR_OK ) break; if ( mas_pff_fi.fname[0] == '\0' ) break; c++; } mas_last_readdir_n = 0x0ffff; return c; }
FRESULT PFFS::opendir ( const char *path /* Pointer to the directory path */ ) { return pf_opendir(&dir_obj, path); }
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 } } } } }
/* Main */ int main (void) { // setare pin 5 (difuzor) si pin 6 (CS_SD) al portului A ca pin de iesire DDRD |= (1<<PD5) | (1<<PD6); // setam toti pinii portului C ca pini de iesire DDRC = 0xFF; DDRB &= ~(1<<PB0); DDRB &= ~(1<<PB1); PORTB |= (1<<PB1) | (1<<PB0); PORTD &= ~((1<<PD5) | (1<<PD6)); PORTD &= ~((1<<PD5) | (1<<PD6)); LCD_init(); sei(); for (;;) { _delay_ms(1000); /* Delay 1000ms for things to settle down */ if (pf_mount(&Fs)) { continue; /* Initialize FS */ } if (pf_opendir(&Dir, "wav")) break; /* Open /wav folder */ int state = STAHPED; char* filename, first_wav, last_wav_played; if (!pf_readdir(&Dir, &Fno) && Fno.fname[0]) /* go to next file*/ { if (!(Fno.fattrib & (AM_DIR|AM_HID)) && strstr(Fno.fname, "WAV")) /* filter out all the folders, hidden files and the files that are not "WAV" */ { /* display the filename on the LCD */ LCD_print(Fno.fname); strcpy(first_wav, Fno.fname); } } while(1) { /* check if button on PA3 (start/stahp) is pressed */ if (!(PINA & (1 << PA3))) { if (state == STAHPED) { state = STARTED; LCD_print(Fno.fname); play(strcat("wav", Fno.fname)); // play song } else { state = STAHPED; LCD_print(Fno.fname); FifoCt = 0; // stop melody } } /* check if button on PA2 (next) is pressed */ if (!(PINA & (1 << PA2))) { sprintf(last_wav_played, "%s", Fno.fname); if (state == STARTED) { if (!pf_readdir(&Dir, &Fno) && Fno.fname[0]) /* go to next file*/ { if (!(Fno.fattrib & (AM_DIR|AM_HID)) && strstr(Fno.fname, "WAV")) /* filter out all the folders, hidden files and the files that are not "WAV" */ { /* display the filename on the LCD */ LCD_print(Fno.fname); /* play the WAV file */ play(strcat("wav", Fno.fname)); } } } else { if (!pf_readdir(&Dir, &Fno) && Fno.fname[0]) /* go to next file*/ { if (!(Fno.fattrib & (AM_DIR|AM_HID)) && strstr(Fno.fname, "WAV")) /* filter out all the folders, hidden files and the files that are not "WAV" */ { /* display the filename on the LCD */ LCD_print(Fno.fname); } } } } /* check if button on PA1 (previous) is pressed */ if (!(PINA & (1 << PA1))) { if (state == STARTED) { if (strcmp(last_wav_played, first_wav) != 0 ) { /* display the filename on the LCD */ LCD_print(last_wav_played); /* play the WAV file */ play(strcat("wav", last_wav_played)); } else /* display the filename on the LCD */ LCD_print(first_wav); } else if (strcmp(last_wav_played, first_wav) != 0 ) /* display the filename on the LCD */ LCD_print(last_wav_played); } } } return 1; }
void InitializeFileSystem() { pf_mount(&fs); char *rootDir = " "; pf_opendir(&dir, rootDir); }
// **************** main() *********************************************** int main(void) { DDRD |= (1<<PD5)|(1<<PD4)|(1<<PD0)|(1<<PD1); // ustaw piny PWM1 (OC1A) oraz PWM2 (OC1B) jako wyjœcia WA¯NE !!! DDRB |= (1<<PB0); // wyjœcie do sterowania zasilaniem karty SD PORTB = 0b00000001; // podci¹gniêcie PORTB do VCC ?? set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Wybranie trybu uœpienia mikrokontrolera // init SPI DDRB |= (1<<CS)|(1<<MOSI)|(1<<SCK)|(1<<CS); PORTB |= (1<<CS); SPCR |= (1<<SPE)|(1<<MSTR); SPSR |= (1<<SPI2X); // masymalny zegar SCK // konfiguracja PWM (Timer1) TCCR1A = (1<<WGM10)|(1<<COM1A1)|(0<<COM1A0)|(1<<COM1B1);//|(1<<COM1B0); TCCR1B = (1<<CS10); // konfiguracja Timer0 (samplowanie) TCCR0 = (1<<WGM01); // tryb CTC TIMSK = (1<<OCIE0); // zezwolenie na przerwanie CompareMatch sei(); // globalne zezwolenie na przerwania OCR0 = (uint8_t)44; //bo F_CPU/8/samplerate // **************** akcelerometr ********************************** ADCSRA |= 1<<ADPS2 | 1<<ADPS1 | 1<<ADPS0; //ustawienie preskalera ADMUX |= 1<<REFS0 | 1<<REFS1; //ustawienie napiêcia referencyjnego ADCSRA |= 1<<ADIE; //w³¹czenie przerwania ADCSRA |= 1<<ADEN; //w³¹czenie modu³u konwersji // **************** pêtla g³ówna ********************************** while(1) { if (pf_mount(&Fs)) continue; //inicjalizacja FS for (;;) { if (pf_opendir(&Dir, "")) break; //otwarcie g³ównego folderu karty if (startup==1) //odtworzenie dŸwiêku w³¹czania { if (play("startup.wav")) break; startup=0; PORTD |= (1<<PIND0); } // ********************* zdarzenie SWING **************** if (hitpriority==1) { swingnumber=swing[swingcounter]; if (swingnumber==1) { play("swing.wav"); } if (swingnumber==2) { play("swing.wav"); } if (swingnumber==3) { play("swing.wav"); } hitpriority=0; breakdisablehigh=0; breakdisablelow=0; swingcounter++; if (swingcounter==9) { swingcounter=0; } } // ******************** zdarzenie HIT *********** if (hitpriority==2) { hitnumber=hit[hitcounter]; PORTD |= (1<<PIND1); //W³¹czenie dodatkowej diody ledoff=1; if (hitnumber==1) { play("clash.wav"); } if (hitnumber==2) { play("clash.wav"); } if (hitnumber==3) { play("clash.wav"); } hitpriority=0; breakdisablehigh=0; hitcounter++; if (hitcounter==9) { hitcounter=0; } } // ********************** sekwencja POWER DOWN ****************** if (shutdown==1) { play("startup.wav"); GENERAL_MAJOR_SHUTDOWN_PROCEDURE; sleep_mode(); } // ********************** tryb ja³owy ****************** else { if (play("hum.wav")) break; } } } }
FRESULT PFFS::opendir ( void /* Working copies of directory object & path */ ) { return pf_opendir(&dir_obj, (const char *)"/"); //ok }
uint16_t testSDCard(void) { FATFS fatfs; /* File system object */ DIR dir; /* Directory object */ FILINFO fno; /* File information object */ WORD bw, br, i; BYTE buff[64]; print("Mount a volume."); RET(); FRESULT rc = pf_mount(&fatfs); if (rc) die(rc); print("Open a test file (message.txt)."); RET(); rc = pf_open("MESSAGE.TXT"); if (rc) die(rc); print("Type the file content."); RET(); for (;;) { rc = pf_read(buff, sizeof(buff), &br); /* Read a chunk of file */ if (rc || !br) break; /* Error or end of file */ for (i = 0; i < br; i++) /* Type the data */ putchar(buff[i]); } if (rc) die(rc); #if _USE_WRITE sprintf(string,"\nOpen a file to write (write.txt).\n"); print(string); rc = pf_open("WRITE.TXT"); if (rc) die(rc); sprintf(string,"\nWrite a text data. (Hello world!)\n"); print(string); for (;;) { rc = pf_write("Hello world!\r\n", 14, &bw); if (rc || !bw) break; } if (rc) die(rc); sprintf(string,"\nTerminate the file write process.\n"); print(string); rc = pf_write(0, 0, &bw); if (rc) die(rc); #endif #if _USE_DIR print("Open root directory."); RET(); rc = pf_opendir(&dir, ""); if (rc) die(rc); print("Directory listing..."); RET(); for (;;) { rc = pf_readdir(&dir, &fno); /* Read a directory item */ if (rc || !fno.fname[0]) break; /* Error or end of dir */ if (fno.fattrib & AM_DIR) { print(" <dir> "); print(fno.fname); RET(); } else { printInt((uint8_t*)&fno.fsize, sizeof(fno.fsize)); print(fno.fname); RET(); } } if (rc) die(rc); #endif print("\nTest completed.\n"); while(1) { delay_ten_us(50000); delay_ten_us(50000); P1OUT ^= (1 << RED_LED); } return 0xaa55; }
uint8_t genMenu() { uint8_t *p; uint16_t filecount = 0; // shut off TI cart bug disableBus(); // open SD card rc = pf_mount(&sd_fs); if (rc) flash_error(1); rc = pf_opendir(&sd_dp, ""); if (rc) flash_error(2); // add menu and browser code writeMenuCode(); while (images_written < MAX_ENTRIES) { // get next item in directory rc = pf_readdir(&sd_dp, &sd_fno); if (rc) flash_error(3); if (sd_fno.fname[0] == 0) // end of dir break; ++filecount; if (sd_fno.fattrib & AM_DIR) continue; // skip directories // check if file ends in ".BIN" p = (uint8_t *)sd_fno.fname; while (*++p); if (p - (uint8_t *)sd_fno.fname < 5 || *--p != 'N' || *--p != 'I' || *--p != 'B' || *--p != '.') continue; --p; // p at last char of filename w/o extension // check image size if (sd_fno.fsize > 32768) continue; #ifdef MULTI_FILE // check for multi-file image // - if file ends in C -> check how many banks // - if file ends in D, E, F -> check if C exists, then ignore uint8_t c; if (sd_fno.fsize == 8192 && *p == 'C') { for (c = 'D'; c <= 'F'; ++c) { *p = c; rc = pf_open(sd_fno.fname); if (rc) break; } *p = 'C'; } else if (sd_fno.fsize == 8192 && (*p == 'D' || *p == 'E' || *p == 'F')) { c = *p; *p = 'C'; rc = pf_open(sd_fno.fname); if (!rc) continue; // current file covered by C file *p = c; // actually not a multi-file image } #endif // create entry if (addEntries(sd_fno.fname)) ++files_written; } // if only one image found, load file directly if (files_written == 1) return 1; // close menu and browser items closeEntries(); // bring cartridge online enableBus(); return 0; }