/* * init_sd : open connection to sd card port */ void init_sd() { alt_up_sd_card_dev *device_reference = NULL; char *interface = "/dev/Altera_UP_SD_Card_Avalon_Interface_0"; if ( ( device_reference = alt_up_sd_card_open_dev( interface ) ) == NULL ) { printf( "ERROR! SD card path is bad: %s\n", interface ); } else { if ( device_reference != NULL ) { if ( ( SD_CONNECTED == 0 ) && ( alt_up_sd_card_is_Present() ) ) { if ( alt_up_sd_card_is_FAT16() ) { printf( "< SD Initialized >\n" ); SD_CONNECTED = 1; } else { printf( "ERROR! Unknown file system on SD card\n" ); } } else if ( ( SD_CONNECTED == 1) && ( alt_up_sd_card_is_Present() == 0 ) ) { printf( "ERROR! card disconnected.\n" ); SD_CONNECTED = 0; } } else { printf( "ERROR! can't open sd card device\n" ); } } }
/* * Overview: exercise 3 reads sd_card and list its file. * */ void sd_card(void){ char file_name[10]; alt_up_sd_card_dev *device_reference = NULL; int connected = 0; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { //printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { //printf("FAT16 file system detected.\n"); if(alt_up_sd_card_find_first(".", (char*) file_name)==0){ //printf("%s\n",file_name); while(alt_up_sd_card_find_next((char*)file_name)==0) { //printf("%s\n", file_name); } } } else { //printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { //printf("Card disconnected.\n"); connected = 0; } break; } } else { //printf("Card could not be opened\n"); } //printf("sd_card function finished\n"); }
int main(void) { char buffer_name[10]; short int handler; alt_up_sd_card_dev *device_reference = NULL; int connected = 0; device_reference = alt_up_sd_card_open_dev("/dev/SD_Card"); if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); //Print out the list in the sd card handler = alt_up_sd_card_find_first("", buffer_name); printf("%s \n", buffer_name); while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1) printf("%s \n", buffer_name); }else{ printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } return 0; }
//Initializes SD card, outputs filenames on sd card int init_sd(char * filenames[]){ int j; int i = 0; char thepath[] = AUD_DIRECT; //Directory for filenames we want to read short int check_file_name; for (j = 0; j < MAXFILES; j++) { filenames[j] = (char *)malloc(sizeof(char) * MAX_NAME_LEN); if (filenames[j] == NULL) printf("Error, out of memory (2)\n"); } alt_up_sd_card_dev * sd_card = NULL; int connected = 0; sd_card = alt_up_sd_card_open_dev("/dev/Interface"); if (sd_card != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); check_file_name = alt_up_sd_card_find_first(thepath,filenames[i]); if (check_file_name == 1) printf("Invalid Directory.\n"); else if (check_file_name == 2) printf("Invalid SD Card.\n"); else if (check_file_name == -1) printf("No files found.\n"); else { i++; } while(alt_up_sd_card_find_next(filenames[i]) != -1){ i++; } arr = (unsigned int ***)malloc(sizeof(unsigned int) * i); if (arr == NULL){ printf("Error, arr null\n"); } return i; //If card connected, returns amount of files } else { printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } else { printf("Error.\n"); return -1; } }
/* * Overview: Helper function to print out the contents of the given file to console. * */ void print_file( alt_up_sd_card_dev * sd, char * filename ){ char buf_read [SD_CHAR_BUF_LENGTH_LONG]; int file; int i; if(sd != NULL){ if(alt_up_sd_card_is_Present()) { file = alt_up_sd_card_fopen(filename, false); if(file < 0) printf("Text File open failed\n"); else { //printf("Reading from file...\n"); for(i=0; i<SD_CHAR_BUF_LENGTH_LONG; i++) { buf_read[i]=alt_up_sd_card_read(file); } //printf("Done reading\n"); alt_up_sd_card_fclose(file); //printf("Closed the file\n"); printf("\nPrinting out file contents\n"); for (i = 0; i <SD_CHAR_BUF_LENGTH_LONG; i++ ) { printf("%c", buf_read[i] ); } printf("\n"); } } } }
int sdcard_write(const short int * filehandle, const char * data, const int data_length) { int i; if (device_reference == NULL) { printf("Error: SDCard not open\n"); return 1; } if (!alt_up_sd_card_is_Present()) { printf("Error: SDCard not present\n"); device_reference = NULL; return 1; } if (*filehandle == -1) { printf("Error: File handle not open\n"); return 1; } /* Write data to the SDCard */ printf("Writing data <%s> length <%d>\n", data, data_length); for (i = 0; i < data_length; i++) { if (alt_up_sd_card_write(*filehandle, data[i]) == false) { printf("Error: Could not write to file. Aborting write\n"); break; } } return 0; }
void load_file(char* filename, void (*func)(short)){ bool found_file = false; if (get_device_reference() == NULL || !alt_up_sd_card_is_Present() || !alt_up_sd_card_is_FAT16()){ printf("Can't find device, or device not configured properly\n"); return; } char filename_all_caps[strlen(filename)]; to_caps(filename, filename_all_caps); char found_file_name[13]; if (alt_up_sd_card_find_first(".", found_file_name) != 0){ printf("Couldn't find root dir\n"); return; } do { if (strcmp(found_file_name, filename_all_caps)== 0){ short int file = alt_up_sd_card_fopen(found_file_name, false); if (file >= 0){ printf("found file %s in SD\n", filename_all_caps); (*func)(file); found_file = true; //want to close file, so use this rather than returning } alt_up_sd_card_fclose(file); } }while(!found_file && alt_up_sd_card_find_next(found_file_name) == 0); }
void save_level(char level) { short int handle; alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (device_reference != NULL && alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { handle = alt_up_sd_card_fopen("saves.txt",0); if (handle==-1) { printf("ITS BAD\n"); } /* char temp[4]; if (handle==-1){ printf("ITS BAD\n"); } sprintf(temp,"%d",level); if(!alt_up_sd_card_write(handle, temp[0])){ printf("NOT WORKING\n"); } alt_up_sd_card_write(handle, temp[2]); alt_up_sd_card_write(handle, temp[3]); alt_up_sd_card_write(handle, temp[4]);*/ alt_up_sd_card_write(handle,level); alt_up_sd_card_fclose(handle); } }
char openFileinSD(char* fileName, short int* file_handle_ptr){ short int file_handle; char status = -1; sd_card = alt_up_sd_card_open_dev("/dev/SD_Card"); if (sd_card != NULL){ while(1){ if (alt_up_sd_card_is_Present()){ printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()){ printf("FAT16 confirmed.\n"); file_handle = alt_up_sd_card_fopen( fileName, false ); if ( file_handle == -1 ) printf( "*** File cannot be opened.\n"); else if ( file_handle == -2 ) printf( "*** File already opeend.\n"); else { *file_handle_ptr = file_handle; status = 0; printf( "File obtained.\n"); return status; } printf("*** Error reading File.\n"); } else { printf("*** Not FAT16.\n"); } } } } printf("*** Storage Device may not found.\n"); return status; }
/* * Overview: Stores score on SD card. * */ void store_high_score_sd_card( alt_up_sd_card_dev * sd, char * filename, struct tank * t ){ struct tank* tank = (struct tank *) t; int file_handle; // write to this file int i = 0; // index char buffer[SD_CHAR_BUF_LENGTH]; // buffer sprintf( buffer, "%d", tank->score); // put integer to a char buffer if(sd != NULL) { if(alt_up_sd_card_is_Present()) { file_handle =alt_up_sd_card_fopen(filename, false); if(file_handle < 0) printf("Text file open failed\n"); else { //printf("\nStoring data...\n"); for(i =0; i < SD_CHAR_BUF_LENGTH; i++) { alt_up_sd_card_write(file_handle, buffer[i]); } //printf("Done storing and Closing File...\n"); alt_up_sd_card_fclose(file_handle); //printf("Closed the file\n\n"); } } } }
int init_sd(char * filenames[]){ int i = 0; char thepath[] = "/"; short int check_file_name; alt_up_sd_card_dev * sd_card = NULL; int connected = 0; sd_card = alt_up_sd_card_open_dev("/dev/Interface"); if (sd_card != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); check_file_name = alt_up_sd_card_find_first(thepath,filenames[i]); if (check_file_name == 1) printf("Invalid Directory.\n"); else if (check_file_name == 2) printf("Invalid SD Card.\n"); else if (check_file_name == -1) printf("No files found.\n"); else { // printf("First file name = %s.\n", filenames[i]); i++; } while(alt_up_sd_card_find_next(filenames[i]) != -1){ // printf("Next file name = %s.\n", filenames[i]); i++; } return i; } else { printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } else { printf("Error.\n"); return -1; } }
//returns a file handle that can be used using readFile short int openRead(char* filename){ if ((alt_up_sd_card_is_Present())) { if (alt_up_sd_card_is_FAT16()) { return alt_up_sd_card_fopen(filename, false); } } }
unsigned int* read_image(){ unsigned char* temp; unsigned int* image; short int handle; int k = 0; int i = 0; temp = calloc (FILE_SIZE , sizeof(char)); image = calloc (ARRAY_SIZE , sizeof(short int)); alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (device_reference != NULL && alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { handle = alt_up_sd_card_fopen("text2.txt",0); while(alt_up_sd_card_read(handle)!=-1){ *(temp+k)=(char)alt_up_sd_card_read(handle); k++; } k=0; i=0; while(*(temp+k)!='!'){ if(*(temp+k)=='0'){ k++; if(*(temp+k)=='x'){ k++; *(image+i) += (*(temp+k)); *(image+i) = *(image+i) << 4; k++; *(image+i) += (*(temp+k)); *(image+i) = *(image+i) << 4; k++; *(image+i) += (*(temp+k)); *(image+i) = *(image+i) << 4; k++; *(image+i) += (*(temp+k)); i++; k+=2; } } printf("%i\n",*(image+i)); k++; } if(614300>k){ printf("Something is wrong.\n"); } } free(temp); return image; }
/* * Overview: Helper function to test SD card. * Check connectivity, file system, and any present files. * */ void testsdcard(alt_up_sd_card_dev *device_reference) { char file_name[10]; int connected = 0; if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); if(alt_up_sd_card_find_first(".", (char*) file_name)==0) { printf("%s\n",file_name); while(alt_up_sd_card_find_next((char*)file_name)==0) { printf("%s\n", file_name); alt_up_sd_card_read(file_name); } } break; } else { printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } else { printf("Card could not be opened\n"); } }
void initSD() { int connected = 0; alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(SD_CARD_NAME); if (device_reference != NULL) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected. \n"); printf("fat16: %d",alt_up_sd_card_is_FAT16()); if (alt_up_sd_card_is_FAT16()) printf("FAT16 file system detected. \n"); else printf("Unknown file system. \n"); connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected. \n"); connected = 0; } } }
int writeToSDEncrypted(char* buffer, char* fileName) { alt_up_sd_card_dev *device_reference = NULL; short int myFileHandle; int i; char *encryptedBuffer = encryptData(buffer); printf("\nebuffer %s\n", encryptedBuffer); printf("Opening SDCard\n"); if ((device_reference = alt_up_sd_card_open_dev( "/dev/Altera_UP_SD_Card_Avalon_Interface_0")) == NULL) { printf("SDCard Open FAILED\n"); return -1; } else { printf("SDCard Open PASSED\n"); } if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { myFileHandle = alt_up_sd_card_fopen(fileName, false); if (myFileHandle == -1) { myFileHandle = alt_up_sd_card_fopen(fileName, true); } if (myFileHandle != -1) { printf("File Opened\n"); for (i = 0; i < strlen(encryptedBuffer); i++) { if (alt_up_sd_card_write(myFileHandle, encryptedBuffer[i]) == false) { printf("Error writing to file...\n"); return -1; } } printf("Done!!!\n"); alt_up_sd_card_fclose(myFileHandle); } else { printf("File NOT Opened\n"); } } } return 0; }
void initSD(){ int connected = 0; alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (device_reference != NULL) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { //printf("Card connected. \n"); //printf("fat16: %d",alt_up_sd_card_is_FAT16()); if (alt_up_sd_card_is_FAT16()); //printf("FAT16 file system detected. \n"); else printf("Unknown file system. \n"); connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected. \n"); connected = 0; } } findDatabaseVersion(); setDatabase(0); }
/* * Detect and connect to SD Card */ void connectToSDCard() { if (sd_card_reference != NULL) { // repeated check the connectivity of the SD CARD while (alt_up_sd_card_is_Present() == false); while (alt_up_sd_card_is_Present()) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); break; } else { printf("Unknown file system.\n"); } } } }
void printSD() { char* fileName; if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { if (alt_up_sd_card_find_first("", fileName) == 0) { printf("Files detected: \n"); do { printf("%s \n", fileName); } while (alt_up_sd_card_find_next(fileName) == 0); } else if (alt_up_sd_card_find_first("", fileName) == -1) printf("No files. \n"); } }
bool loadSDCard(alt_up_sd_card_dev* device) { if((device = alt_up_sd_card_open_dev("/dev/SD_Card")) != NULL) { if (alt_up_sd_card_is_Present()) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); return true; } else { printf("Unknown file system.\n"); } } } return false; }
/* * sd_write : writes to a file from a loaded sd card * * @args sd_file : file name and content to be written */ void sd_write( struct sd_file *sd ) { if ( SD_CONNECTED == 0 ) { init_sd(); } // file handle for file to be written short int fh; if ( alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16() ) { fh = alt_up_sd_card_fopen( sd->name, false ); if (fh == -1) { fh = alt_up_sd_card_fopen( sd->name, true ); } if ( fh != -1 ) { SD_OPEN = 1; // write sd_file to sd card int i = 0; int length = strlen( sd->data ); for ( i = 0; i < length; i ++ ) { if ( alt_up_sd_card_write( fh, sd->data[i] ) == false ) { printf( "ERROR! failure during write to %s. Failure @ char #%d of %d\n", sd->name, i, length ); SD_ERROR = 1; return; } } // close file handle alt_up_sd_card_fclose( fh ); SD_ERROR = 0; SD_OPEN = 1; printf( "SUCCESS! File written: %s\n", sd->name ); } else { printf( "ERROR! could not write the file: %s\n", sd->name ); SD_ERROR = 1; } } else { printf( "ERROR! SD card is either not present or is not FAT 16\n" ); SD_ERROR = 1; } }
int readFromSDEncrypted(char* buffer, char* fileName, int bufferSize) { alt_up_sd_card_dev *device_reference = NULL; short int myFileHandle; int i; char tempChar[2]; tempChar[1] = '\0'; //printf("Opening SDCard\n"); if ((device_reference = alt_up_sd_card_open_dev( "/dev/Altera_UP_SD_Card_Avalon_Interface_0")) == NULL) { //printf("SDCard Open FAILED\n"); return -1; } else { //printf("SDCard Open PASSED\n"); } if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { myFileHandle = alt_up_sd_card_fopen(fileName, false); if (myFileHandle == -1) { myFileHandle = alt_up_sd_card_fopen(fileName, true); } if (myFileHandle != -1) { //printf("File Opened\n"); for (i = 0; i < bufferSize; i++) { tempChar[0] = alt_up_sd_card_read(myFileHandle); if (tempChar[0] < 0) { break; } strcat(buffer, tempChar); } //printf("Done!!!\n"); alt_up_sd_card_fclose(myFileHandle); printf("buffer %s", buffer); char *decryptedData = decryptData(buffer); printf("Decrypted after read: %s", decryptedData); strcpy(buffer, decryptedData); return 0; } else { //printf("File NOT Opened\n"); } } return -1; }
/* * sd_read : reads in a file from a loaded sd card * * @args file_name : file name relative to sd root folder */ char * sd_read( char *file_name ) { if ( SD_CONNECTED == 0 ) { init_sd(); } // file handle for file being read short int fh; if ( alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16() ) { fh = alt_up_sd_card_fopen( file_name, false ); if (fh != -1) { SD_OPEN = 1; unsigned char *reading; // data read char current; // current char read int index = 0; // current position in file // read in all chars while ( ( current = alt_up_sd_card_read( fh ) ) >= 0 ) { reading[ index ] = malloc(1); reading[ index ] = current; ++index; } // close file handle alt_up_sd_card_fclose( fh ); // print out file contents return reading; } else { printf( "ERROR! can't open file: %s\n", file_name ); SD_ERROR = 1; return ""; } } else { printf( "ERROR! SD card is either not present or is not FAT 16\n" ); SD_ERROR = 1; return ""; } }
/* * This function is used for reading data to a buffer from the SD card. It takes in three arguments, a * pointer to a character buffer where the data will be stored, the name of a file i.e. "Readme.txt" * and the size of the character buffer. It will return the number of * characters read into the buffer. If the SD card is inaccessible, returns -1 * * Example of use * * char charbuffer[1024]; int charbuffersize = 1024; char* charptr; charptr = &charbuffer[0]; int charcount=0; * charcount = file_read(charptr, "README.TXT", charbuffersize); */ int file_read(char* charbuffer, char* filename, int charmax){ if ((alt_up_sd_card_is_Present())) { if (alt_up_sd_card_is_FAT16()) { short int file0; int k; int charcount; //Open the file in the argument for reading. False means that the file will not be created //if it is not found. A negative return value means that the file was not opened successfully file0 = alt_up_sd_card_fopen(filename, false); if (file0 == -1) return -1; if (file0 == -2) return -2; //Read as many characters as possible from the file, up to charmax or until the file has been read //to completion. else { for(k=0; k<charmax; k++){ *(charbuffer+k) = (char)alt_up_sd_card_read(file0); //If the end of file has been reached, stop reading. EOF is designated with a negative value if ((*(charbuffer+k) == -1) && (*(charbuffer+k-1) == -1) && (*(charbuffer+k-2) == -1) && (*(charbuffer+k-3)== -1)){ break; } charcount = k; } } //Close the file and return the amount of characters read into the buffer alt_up_sd_card_fclose(file0); return charcount; } } return -1; }
int sdcard_open(short int * filehandle, char * filename) { if (device_reference == NULL) { printf("Error: SDCard not open\n"); return 1; } if (!alt_up_sd_card_is_Present()) { printf("Error: SDCard not present\n"); device_reference = NULL; return 1; } if (!alt_up_sd_card_is_FAT16()) { printf("Error: SDCard is not FAT16 format\n"); return 1; } /* Try to create the file */ *filehandle = alt_up_sd_card_fopen(filename, true); if (*filehandle == -1) { printf("Error: Could not create file <%s>, trying to open file instead...\n", filename); /* Maybe the file exists, try opening it instead */ *filehandle = alt_up_sd_card_fopen(filename, false); if (*filehandle == -1) { printf("Error: Could not open file <%s>\n", filename); return 1; } else { printf("Opened file <%s>\n", filename); } } else { printf("Created file <%s>\n", filename); } return 0; }
/** * Looks for an SD Card * Return: True if SD Card present. False otherwise. */ int sdcard_isPresent(void) { int is_present = alt_up_sd_card_is_Present(); return is_present; }
int main(void) { device_reference = alt_up_sd_card_open_dev("/dev/SD_Card"); if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); //Print out the list in the sd card handler = alt_up_sd_card_find_first("", buffer_name); printf("%s \n", buffer_name); //Read a file in SD card -needs to speed up(10 sec?) - needs array of int sd_fileh = alt_up_sd_card_fopen("TEST.WAV", false); if (sd_fileh < 0) printf("Problem reading file. Error %i\n", sd_fileh); else { printf("Reading file...\n"); audio_dev = alt_up_audio_open_dev (AUDIO_NAME); if ( audio_dev == NULL){ return; } av_config = alt_up_av_config_open_dev(AUDIO_AND_VIDEO_CONFIG_NAME); while(!alt_up_av_config_read_ready(av_config)); alt_up_audio_disable_read_interrupt(audio_dev); alt_up_audio_enable_write_interrupt(audio_dev); /* // Get information of number of channels //count = 0; char a, b; int d; short c; int index = 0; while (buffer_name[index] != "\0"){ a = alt_up_sd_card_read(sd_fileh); b = alt_up_sd_card_read(sd_fileh); c = ((unsigned char) a <<8) | (unsigned char) b; d = c; d = alt_up_audio_write_fifo(device_reference,d,) index++; } */ /* // printf("NumChannels "); byte1 = 0;byte2 = 0;total = 0; byte1 = alt_up_sd_card_read(sd_fileh); byte2 = alt_up_sd_card_read(sd_fileh); total = (byte1 & 0x00ff) | (byte2 << 8); NumChannels = total; // printf("%d\n",total); // printf("SampleRate "); byte1 = 0;byte2 = 0;byte3 = 0;byte4 = 0;long_total = 0; byte1 = alt_up_sd_card_read(sd_fileh); byte2 = alt_up_sd_card_read(sd_fileh); byte3 = alt_up_sd_card_read(sd_fileh); byte4 = alt_up_sd_card_read(sd_fileh); long_total = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | ( byte1 & 0x000000ff); sampleRate = long_total; // printf("%ld\n",long_total); */ /* count = 0; while (count < 16) // 44-byte header { alt_up_sd_card_read(sd_fileh); count++; } */ } //find next file while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1) printf("%s \n", buffer_name); }else{ printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } return 0; }
int main(void) { /* Related object data */ system_t* system; player_t* player1; player_t* player2; alt_timestamp_start(); int start_time = alt_timestamp(); int wind; int restart = 0; /* initialize all hardware dev */ system = system_init(VIDEO_PIXEL_BUFFER_DMA_NAME, "/dev/video_character_buffer_with_dma_0", ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME, PS2_0_NAME); if (!alt_up_sd_card_is_Present()) { printf("SD card not present\n"); return -2; } else { printf("SD card detected.\n"); } if (!alt_up_sd_card_is_FAT16()) { printf("SD card is not FAT16.\n"); return -3; } else { printf("SD card is FAT 16.\n"); } fireSound = initSoundbank("shotgun.wav"); explosionSound = initSoundbank("big_bomb.wav"); printf("Done Initializing\n"); int end_time = alt_timestamp(); srand(end_time - start_time); clearScreen(system); printf("Press enter to start game"); restartGame(system); while (restart == 1) { /* initialize required objects */ player1 = makePlayer(1, "Lomash"); player2 = makePlayer(2, "TJ"); /* * Draw the screen for the first time. */ store_background_data(); draw_background(system); drawPlayers(system); draw_ground(system); draw_windbox(system); draw_player1GUI(system); draw_player2GUI(system); update_wind(system, wind); draw_health(player1, system); draw_health(player2, system); update_power(player1, system); update_power(player2, system); usleep(2000000); // sleep to wait for video buffer to load while (TRUE) { printf( "\n\n\n\n===========================================================\n"); printf("Player 1 Health = %d \t\t Player 2 Health = %d\n", player1->health, player2->health); wind = rand() % 11 - 5; draw_windbox(system); update_wind(system, wind); // Player 1's turn player_1_jump(system); player_1_jump(system); printf("Getting Player 1 Power.\n"); getKeyboardInput(1, player1, system); // Power skipOneEnter(system); printf("Getting Player 1 Angle.\n"); getKeyboardInput(2, player1, system); // Angle // Player 1 Animation printf("Starting animation\n"); clear_angle_drawer(system); animate_cannon1(system); playSound(fireSound, system->audio); switch (animateShooting(system, player1, wind)) { // different value for result case 1: { update_health(player2, system, -DAMAGE); printf( "Player 1 hit player 2.\n Remaining Health for Player 2: %d\n", player2->health); break; } case 2: { update_health(player1, system, -DAMAGE); printf( "Player 1 hit player 1.\n Remaining Health for Player 1: %d\n", player1->health); break; } default: { break; } } printf("Ended animation\n"); // Post-animation Calculation if (player1->health <= 0 || player2->health <= 0) { break; } // Player 2's turn wind = rand() % 11 - 5; draw_windbox(system); update_wind(system, wind); player_2_jump(system); player_2_jump(system); printf("Getting Player 2 Velocity.\n"); getKeyboardInput(1, player2, system); // Player 2 Angle-Selection skipOneEnter(system); printf("Getting Player 2 Angle.\n"); getKeyboardInput(2, player2, system); // Player 2 Animation printf("Starting animation\n"); clear_angle_drawer(system); animate_cannon2(system); playSound(fireSound, system->audio); // Post-animation Calculation switch (animateShooting(system, player2, wind)) { // different value for result case 1: { update_health(player1, system, -DAMAGE); printf( "Player 2 hit player 1.\n Remaining Health for Player 1: %d\n", player1->health); break; } case 2: { update_health(player2, system, -DAMAGE); printf( "Player 2 hit player 2.\n Remaining Health for Player 2: %d\n", player2->health); break; } default: { break; } } if (player1->health <= 0 || player2->health <= 0) { break; } }; /* * Find out who won. */ if (player1->health <= 0) { printf("Player 2 Wins!!! \n"); draw_P2WIN(system); } else if (player2->health <= 0) { printf("Player 1 Wins!!!\n"); draw_P1WIN(system); } else { printf("we shouldn't be here.\n"); } restart = restartGame(system); } return 0; // FIN }
int filenames_read(char* filename, int charmax){ //Test if the SD card is inserted and is in a valid file format for reading if ((alt_up_sd_card_is_Present())) { if (alt_up_sd_card_is_FAT16()) { int offset = 0; int filecount = 0; int returnval = alt_up_sd_card_find_first(0x0, filename); if (returnval < 0){ printf("the read failed"); return -2; } /* * Read the first filename within the SD card as long as there is sufficient space within the character buffer */ if((returnval == 0)) { filecount++; while(*(filename+offset) != '.') offset++; offset+=4; *(filename+offset) = ';'; offset++; } /* As long as there is sufficient space within the character buffer, will read every name within the * SD card to a buffer. */ while (1){ returnval = alt_up_sd_card_find_next((filename+offset)); if (returnval == -1 ){ break; } filecount++; if((charmax-offset) <= 12){ break; } while(*(filename+offset) != '.'){ offset++; } offset+=4; *(filename+offset) = ';'; offset++; } return filecount; } } return -1; }
void bmp_read(char* filename, char* pixeldata, char* palptr) { char image[2048] = {0}; char* imageptr; imageptr = &image[0]; //char* palptr; //palptr = &palette[0]; int i; int offset = 0;//0x36; //file_read (imageptr, filename, 2048); if ((alt_up_sd_card_is_Present())) { if (alt_up_sd_card_is_FAT16()) { short int file0; int k; //int charcount; //Open the file in the argument for reading. False means that the file will not be created //if it is not found. A negative return value means that the file was not opened successfully file0 = alt_up_sd_card_fopen(filename, false); if (file0 == -1) return -1; if (file0 == -2) return -2; //Read as many characters as possible from the file, up to charmax or until the file has been read //to completion. else { for(k=0; k<2048; k++){ *(imageptr+k) = (char)alt_up_sd_card_read(file0); //If the end of file has been reached, stop reading. EOF is designated with a negative value //if ((*(charbuffer+k) == -1) && (*(charbuffer+k-1) == -1) && (*(charbuffer+k-2) == -1) && (*(charbuffer+k-3)== -1)){ //break; //} //charcount = k; } } //Close the file and return the amount of characters read into the buffer alt_up_sd_card_fclose(file0); //return charcount; } } //return -1; offset += *(imageptr+0xA); offset += *(imageptr+0xB)*256; for(i=0; i<480;i++){ //if(i%16 == 0) *(pixeldata+i)= *(imageptr+offset+i); //printf( "%02hhX ", *(pixeldata+i) ); //if(i%20 == 0) printf("\n"); } for(i=0; i<256; i++){ *(palptr + 3*i) = *(imageptr + 4*i + 0 + 0x36); *(palptr + 3*i + 1) = *(imageptr + 4*i + 1 + 0x36); *(palptr + 3*i + 2) = *(imageptr + 4*i + 2 + 0x36); } //printf(""); //printf( "\n %02hhX , %02hhX , %02hhX, %02hhX \n", *(imageptr+4*0xFC+0x36),*(imageptr+4*0xFc+1+0x36),*(imageptr+4*0xFC+2+0x36),*(imageptr+4*0xFC+3+0x36)); //b --1 //printf( "\n %02hhX , %02hhX , %02hhX \n", palette[750], palette[751], palette[752]); return; }