/* * Overview: This is the function for reading the bytes of the boom sound for when the bullet hits a tank * */ int readDoh(unsigned int *buffer, char* filename, short int handle) { short int data; unsigned char upper; unsigned char lower; int size; alt_up_audio_dev *audio=alt_up_audio_open_dev("/dev/audio"); //printf("Now loading: %s\n", filename); handle = alt_up_sd_card_fopen(filename, false); if (handle < 0) { printf("Failed to open the file"); return; } int i; //printf("first for loop\n"); for (i = 0; i < 46; i++) { alt_up_sd_card_read(handle); } size = 0; while (1) { //printf("inside the while loop\n"); data = alt_up_sd_card_read(handle); //printf("data is 0x%x", data); if (data == -1) { break; } else { lower = data; } data = alt_up_sd_card_read(handle); if (data == -1) { break; } else { upper = data; } buffer[size] = ((upper << 8) | lower); //printf("buffer is 0x%x\n", buffer[size]); size++; if (size + 1 > DOH_SIZE) {//7720 for cha ching sound break; } //play_doh(audio, buffer, handle); } //break; //printf("Song size: %d\n", size); alt_up_sd_card_fclose(handle); return size; }
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; }
short int readWord(short int fh) { short int byte1, byte2; byte1 = alt_up_sd_card_read(fh); byte2 = alt_up_sd_card_read(fh); if (byte1 == -1 || byte2 == -1) return -1; //Little Endian (least significant byte first) return ((unsigned short int)byte2 << 8) | ((unsigned short int)byte1 & 0x00FF); }
int readDWord(short int fh) { short int byte1, byte2, byte3, byte4; byte1 = alt_up_sd_card_read(fh); byte2 = alt_up_sd_card_read(fh); byte3 = alt_up_sd_card_read(fh); byte4 = alt_up_sd_card_read(fh); if (byte1 == -1 || byte2 == -1 || byte3 == -1 || byte4 == -1) return -1; //Little Endian (least significant byte first) return ((unsigned short int)byte4 << 24) | ((unsigned short int)byte3 << 16) | ((unsigned short int)byte2 << 8) | (unsigned short int)byte1; }
/* * 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"); } } } }
void updateGameOver(void) { static int firstUpdate = 0; if(!firstUpdate) { file_handle scores = open_file("scores.txt", false); if( scores == -1) { printf("UH OH...\n"); } byte dataByte = alt_up_sd_card_read(scores); int i = 0; char buf[50]; while(dataByte >= 0 && i < 15) { buf[i] = dataByte; i++; dataByte = alt_up_sd_card_read(scores); } char* test = "99999"; draw_string(test, FINAL_SCORE_X, FINAL_SCORE_Y); draw_string(test, HIGH_SCORE_X, HIGH_SCORE_Y); firstUpdate = 1; } draw_box( 90, 130, 110, 140, back_alpha, 1); draw_bmp(cursor_bmp, 0, 240-cursor_bmp->bmp_info_header->height, false, back_alpha, 1); if(!button_states[0] || (!prev_controller_state.B_BUTTON && controller_state.B_BUTTON )) { close_bmp(background_bmp); changeState(MAIN_MENU); } else if(!button_states[1] || (!prev_controller_state.Y_BUTTON && controller_state.Y_BUTTON)) { close_bmp(background_bmp); changeState(LOADING_SCREEN); } }
char load_save(void) { char temp = '0'; short int handle = alt_up_sd_card_fopen("saves.txt",0); if(handle < 0) { printf("No saves on file\n"); } else { temp = alt_up_sd_card_read(handle); } return temp; }
/** * Helper function to read multiple bytes and return the representative int value */ unsigned int readInt( int file_pointer, int numBytesToRead ) { unsigned int ret = 0; int i; unsigned int bytes[ numBytesToRead ]; for( i = 0; i < numBytesToRead; i++) { if((bytes[i] = alt_up_sd_card_read(file_pointer))< 0) { return -1; } ret |= ( ( unsigned int ) bytes[ i ] << ( 8 * i ) ); } return ret; }
int getAllScore(score list[]) { short int handler = alt_up_sd_card_fopen(SCORE_NAME, false); if (handler == -1) return -1; int i = 0, j = 0; // j = index of players char word = alt_up_sd_card_read(handler); while ((int) word >= 0) { //slash - save the score if ((int) word == 0x2F) { for (i = 0; i < MAX_CHARACTER_OF_SCORE; i++) { word = alt_up_sd_card_read(handler); list[j].score[i] = word; } list[j].score[i] = '\0'; word = alt_up_sd_card_read(handler); if (!(list[j].name[0]=='\0'||list[j].score[0]=='\0')) j++; // move to next player i = 0; } // otherwise, save the name else { list[j].name[i] = word; word = alt_up_sd_card_read(handler); i++; if ((int) word == 0x2F) list[j].name[i] = '\0'; } } // Indicate the end of list alt_up_sd_card_fclose(handler); list[j].name[0] = '\0'; return 0; }
/* * Load bitmap image from SD card. Fixed problem with color coding * file name is required to be upper-case and bitmap pixels has to be even size x even size */ bool loadSDImage(char* filename, int** destination) { int i, j, bytes = 0, offset = 0, size = 0, byte = 0; int a, b; int file_pointer = alt_up_sd_card_fopen(filename, false); if(file_pointer < 0) { alt_up_sd_card_fclose(file_pointer); //close the file return false; } //Start reading the bitmap header while(bytes < 10) { if(alt_up_sd_card_read(file_pointer) < 0) { alt_up_sd_card_fclose(file_pointer); return false; } bytes++; } if((offset = alt_up_sd_card_read(file_pointer))< 0) { alt_up_sd_card_fclose(file_pointer); return false; } while(bytes < offset-1){ if(bytes == 21) { if((size = alt_up_sd_card_read(file_pointer))< 0) { alt_up_sd_card_fclose(file_pointer); return false; } *destination = (int*)malloc(size*size*sizeof(int)); } else if( alt_up_sd_card_read(file_pointer) < 0) { safefree(destination); alt_up_sd_card_fclose(file_pointer); return false; } bytes++; } //Start reading the pixel data for(j = size-1; j >= 0; j--) { for(i = 0; i < size; i++) { a = alt_up_sd_card_read(file_pointer); b = alt_up_sd_card_read(file_pointer); if(a < 0 || b < 0) { free(*destination); *destination = NULL; printf("%s invalid at pixel[%d, %d]!\n", filename, i, j); alt_up_sd_card_fclose(file_pointer); return false; } byte = getColor555(b*256+a); *(*destination + j*size+i) = byte; } } alt_up_sd_card_fclose(file_pointer); return true; }
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; }
/* * 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"); } }
/* * 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 getItems(){ short int handler = alt_up_sd_card_fopen(database_name, false); if (handler == -1) return -1; int i=0 ,j=0; // j = index of items char word = alt_up_sd_card_read(handler); unsigned char tmp_string[10]; item *tmp; while (word != EOF){ //printf("j=%d\n", j); if (j == 0){ // Get rid of UTF-8 BOM if (strcmp(database_name,"data000.txt") == 0){ word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); } } // UPC for (i=0;i<UPC_NUM_CHARACTER;i++){ //printf("word: %c\n",word); list[j].upc[i] = word; word = alt_up_sd_card_read(handler); } list[j].upc[i] = '\0'; //printf("upc: %s\n",list[j].upc); word = alt_up_sd_card_read(handler); // Pass the Comma // Name - up to quotation mark '"' i=0; list[j].name[i] = word; i++; word = alt_up_sd_card_read(handler); for (;(int)word != 34;i++){ list[j].name[i] = word; word = alt_up_sd_card_read(handler); } list[j].name[i++] = word; list[j].name[i] = '\0'; word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); //printf("name: %s\n",list[j].name); /* // Description i=0; list[j].description[i] = word; i++; word = alt_up_sd_card_read(handler); for (;(int)word != 34;i++){ list[j].description[i] = word; word = alt_up_sd_card_read(handler); } list[j].description[i++] = word; list[j].description[i] = '\0'; word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); */ // Price for (i=0; (int)word != 44 ; i++){ tmp_string[i] = word; word = alt_up_sd_card_read(handler); } tmp_string[i] = '\0'; //printf("price_string: %s\n",price_string); list[j].price = atof(tmp_string); word = alt_up_sd_card_read(handler); //printf("price: %.2f\n",list[j].price); /* // Category i=0; list[j].category[i] = word; i++; word = alt_up_sd_card_read(handler); for (;(int)word != 34;i++){ list[j].category[i] = word; word = alt_up_sd_card_read(handler); } list[j].category[i++] = word; list[j].category[i] = '\0'; word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); */ // Stock for (i=0;(int)word != 10;i++){ //printf("word: %c\n",word); tmp_string[i] = word; word = alt_up_sd_card_read(handler); } tmp_string[i] = '\0'; list[j].stock = atoi(tmp_string); //printf("stock: %d\n",list[j].stock); word = alt_up_sd_card_read(handler); tmp=realloc(list,(j+2)*sizeof(item)); if ( tmp != NULL ) list=tmp; else { free(list); printf("Error allocating memory!\n"); return -1; } // Next item j++; } total_items = j; printf("Total Number of Items: %d\n",total_items); alt_up_sd_card_fclose(handler); return 0; }
unsigned char readByte(short int fh) { return (unsigned char) alt_up_sd_card_read(fh); }
/* * Overview: Read high score of the previous game * */ void load_high_score( alt_up_sd_card_dev * sd, struct high_score * highscore ) { struct high_score* scores = (struct high_score *) highscore; char buf1[SD_CHAR_BUF_LENGTH_SHORT]; // player 1 buffer char buf2[SD_CHAR_BUF_LENGTH_SHORT]; // player 2 buffer int intBuf1[SD_CHAR_BUF_LENGTH_SHORT]; int intBuf2[SD_CHAR_BUF_LENGTH_SHORT]; int file_player1, file_player2; // file handles int i = 0; //index if(sd != NULL){ if(alt_up_sd_card_is_Present()) { // Player 1 previous score file_player1 = alt_up_sd_card_fopen(FILENAME_SCORE1, false); if(file_player1 < 0) { printf("Text File open failed\n"); return; } else{ //printf("\nPlayer 1: Reading prev score\n"); for(i=0; i<SD_CHAR_BUF_LENGTH_SHORT; i++) { buf1[i]=alt_up_sd_card_read(file_player1); } alt_up_sd_card_fclose(file_player1); } // Player 2 previous score file_player2 = alt_up_sd_card_fopen(FILENAME_SCORE2, false); if(file_player2 < 0) { printf("Text File open failed\n"); return; } else{ //printf("Player 2: Reading prev score\n"); for(i=0; i<SD_CHAR_BUF_LENGTH_SHORT; i++) { buf2[i]=alt_up_sd_card_read(file_player2); } alt_up_sd_card_fclose(file_player2); } } } if(buf1[0] != NULL ){ scores->highscore_p1 = buf1[0]; // load the value to player 1 highscore struct } else printf("Null reference p1\n"); if(buf2[0] != NULL ){ scores->highscore_p2 = buf2[0]; // load the value to player 2 highscore struct } else printf("Null reference p1\n"); return; }
void setPositionToEnd(short int handler) { int position = alt_up_sd_card_read(handler); while (position >= 0) { position = alt_up_sd_card_read(handler); } }
void init_wav(unsigned int *arr[]) { printf("Initializing wav file\n"); int i,j,k; int size_of_wav = 0; int location = 0; unsigned int buffer[100] = {0}; unsigned int * soundbuf; soundbuf = (unsigned int*) malloc(sizeof(char) * size_of_wav); audio = alt_up_audio_open_dev(AUDIO_DEV); alt_up_audio_reset_audio_core(audio); //alt_up_audio_enable_write_interrupt(audio); /* sound_file = alt_up_sd_card_fopen("WAV2.WAV", false); //printf("Soundfile: %d\n", sound_file); if (sound_file != -1) { printf("File opened.\n"); while(alt_up_sd_card_read(sound_file) != -1) { size_of_wav++; } printf("Size of wav: %d\n", size_of_wav); if(alt_up_sd_card_fclose(sound_file)) printf("File closed.\n"); else printf("Error closing file.\n"); } */ size_of_wav = 875484; size_of_sound = (int) size_of_wav/200; //printf("Size of sound: %d\n", size_of_sound); //printf("Size of wav: %d\n", size_of_wav); sound_file = alt_up_sd_card_fopen("wav2.wav", false); if (sound_file != -1) { //printf("File opened.\n"); for(i = 0; i<size_of_wav; i++){ soundbuf[i] = alt_up_sd_card_read(sound_file); } /* * debugging WAV header * printf("ChunkSize: 0x%02x%02x%02x%02x\n", soundbuf[7], soundbuf[6], soundbuf[5], soundbuf[4]); printf("SubChunkSize: 0x%02x%02x%02x%02x\n", soundbuf[19], soundbuf[18], soundbuf[17], soundbuf[16]); printf("Audio Format: 0x%02x%02x\n", soundbuf[21], soundbuf[20]); printf("Channels : 0x%02x%02x\n", soundbuf[23], soundbuf[22]); printf("Samplerate: 0x%02x%02x%02x%02x\n", soundbuf[27], soundbuf[26], soundbuf[25], soundbuf[24]); printf("Bitrate: 0x%02x%02x%02x%02x\n", soundbuf[31], soundbuf[30], soundbuf[29], soundbuf[28]); printf("Blockalign: 0x%02x%02x\n", soundbuf[33], soundbuf[32]); printf("BitsPerSample: 0x%02x%02x\n", soundbuf[35], soundbuf[34]); printf("Data mark: %c%c%c%c\n", soundbuf[210], soundbuf[211], soundbuf[212], soundbuf[213]); printf("Subchunksize2: 0x%02x%02x%02x%02x\n", soundbuf[217], soundbuf[216], soundbuf[215], soundbuf[214]); datasize = soundbuf[217] * pow(16,6) + soundbuf[216] * pow(16,4) + soundbuf[215] * pow(16,2) + soundbuf[214]; printf("Datasize: %d\n", datasize); printf("Chunks:"); for (i = 218; i < 318; i++) { printf(" %02x", soundbuf[i]); } printf("\n"); */ for(i = 0; i<size_of_wav; i++){ if (soundbuf[i] == 'd') { if (soundbuf[i+1] == 'a'){ if (soundbuf[i+2] == 't'){ if (soundbuf[i+3] == 'a') { location = i; break; } } } } } i = location; for (k = 0; k<size_of_sound; k++) { for (j = 0; j<100; j++){ buffer[j] = convert(soundbuf[i+1], soundbuf[i]); i = i + 2; } arr[k] = (unsigned int*)malloc(sizeof(buffer)); memcpy((void*)arr[k], (void*)buffer, sizeof(buffer)); } } free(soundbuf); }
//reads a short int of data back from the SD card. Is in no way safe. char readFile(short int filehandle){ return alt_up_sd_card_read(filehandle); }
int getItems(item *list){ short int handler = alt_up_sd_card_fopen(database_name, false); if (handler == -1) return -1; int i=0 ,j=0; // j = index of items char word = alt_up_sd_card_read(handler); unsigned char tmp_string[10]; while (j < TOTAL_ITEMS){ //printf("j=%d\n", j); if (j == 0){ // Get rid of UTF-8 BOM if (strcmp(database_name,"data000.txt") == 0){ word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); } } // UPC for (i=0;i<UPC_NUM_CHARACTER;i++){ //printf("word: %c\n",word); list[j].upc[i] = word; word = alt_up_sd_card_read(handler); } list[j].upc[i] = '\0'; //printf("upc: %s\n",list[j].upc); word = alt_up_sd_card_read(handler); // Pass the Comma // Name - up to quotation mark '"' i=0; list[j].name[i] = word; i++; word = alt_up_sd_card_read(handler); for (;(int)word != 34;i++){ list[j].name[i] = word; word = alt_up_sd_card_read(handler); } list[j].name[i++] = word; list[j].name[i] = '\0'; word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); //printf("name: %s\n",list[j].name); /* // Description i=0; list[j].description[i] = word; i++; word = alt_up_sd_card_read(handler); for (;(int)word != 34;i++){ list[j].description[i] = word; word = alt_up_sd_card_read(handler); } list[j].description[i++] = word; list[j].description[i] = '\0'; word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); */ // Price for (i=0; (int)word != 44 ; i++){ tmp_string[i] = word; word = alt_up_sd_card_read(handler); } tmp_string[i] = '\0'; //printf("price_string: %s\n",price_string); list[j].price = atof(tmp_string); word = alt_up_sd_card_read(handler); //printf("price: %.2f\n",list[j].price); /* // Category i=0; list[j].category[i] = word; i++; word = alt_up_sd_card_read(handler); for (;(int)word != 34;i++){ list[j].category[i] = word; word = alt_up_sd_card_read(handler); } list[j].category[i++] = word; list[j].category[i] = '\0'; word = alt_up_sd_card_read(handler); word = alt_up_sd_card_read(handler); */ // Stock for (i=0;(int)word != 10;i++){ //printf("word: %c\n",word); tmp_string[i] = word; word = alt_up_sd_card_read(handler); } tmp_string[i] = '\0'; list[j].stock = atoi(tmp_string); //printf("stock: %d\n",list[j].stock); word = alt_up_sd_card_read(handler); // Next item j++; } alt_up_sd_card_fclose(handler); return 0; }
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; }
//Initializes background music, input requires file directory void init_wav(char * bgmfile, int volume, int id) { fname = (char *)malloc(sizeof(char) * (MAX_NAME_LEN + direct_len)); if (fname == NULL) { printf("Error, fname null\n"); } strcpy(fname, AUD_DIRECT); strcat(fname, bgmfile); printf("Initializing %s, Id: %d\n", fname, id); int temp1 = 0; int temp2 = 0; int i,j,k; int location = 0; int size_of_wav = 0; unsigned int buffer[96] = {0}; int t1; int t2; int t3; int t4; VOL_SET = volume; printf("Volume Setting: %d\n", VOL_SET); sound_file = alt_up_sd_card_fopen(fname, false); if (sound_file != -1) { while(alt_up_sd_card_read(sound_file) != -1) { size_of_wav++; //Get size of file } alt_up_sd_card_fclose(sound_file); } sound_file = alt_up_sd_card_fopen(fname, false); //Reopen so we start from index 0 if (sound_file != -1) { printf("%s opened.\n", fname); for (i = 0; i < size_of_wav; i++) { t1 = alt_up_sd_card_read(sound_file); if (t1 == 'd') { t1 = alt_up_sd_card_read(sound_file); if (t1 == 'a') { t1 = alt_up_sd_card_read(sound_file); if(t1 == 't') { t1 = alt_up_sd_card_read(sound_file); if(t1 == 'a') { location = i; //skip header values to data chunk break; } } } } } i = location + 4; t1 = alt_up_sd_card_read(sound_file); t2 = alt_up_sd_card_read(sound_file); t3 = alt_up_sd_card_read(sound_file); t4 = alt_up_sd_card_read(sound_file); //printf("Size: %d\n", (int)(((t4 * pow(16,6) + t3 * pow(16,4) + t2 * pow(16,2) + t1)))); size_of_sound = (int)(((t4 * pow(16,6) + t3 * pow(16,4) + t2 * pow(16,2) + t1) / 192)); audiosize[id] = size_of_sound; audiosecs[id] = (int)(size_of_sound / 333); //printf("Audiosecs: %d\n", audiosecs[id]); arr[id] = (unsigned int**)malloc(size_of_sound * sizeof(unsigned int)); if (arr[id] == NULL) { printf("Error, out of memory (3).\n"); } //Skip subchunk block i += 4; unsigned int ** temp; temp = arr[id]; for (k = 0; k<size_of_sound; k++) { for (j = 0; j<96; j++){ temp1 = alt_up_sd_card_read(sound_file); temp2 = alt_up_sd_card_read(sound_file); buffer[j] = convert(temp2, temp1); //converting values into arrays of size 96, for quick feed into buffer } temp[k] = (unsigned int*)malloc(sizeof(buffer)); if (temp[k] == NULL) { printf("Error, no more memory space (4).\n"); } memcpy((void*)temp[k], (void*)buffer, sizeof(buffer)); if (k == (int)(size_of_sound / 4)) printf("25 Percent converting Wav\n"); if (k == (int)((size_of_sound * 3)/4)) printf("75 Percent converting Wav\n"); } } if(alt_up_sd_card_fclose(sound_file)) printf("%s closed.\n", fname); else printf("Error closing %s.\n", fname); free(fname); }
void load_song(){ song_size = 0; if (openFileinSD(SONG_ONE, &music_file) == 0){ char a; char b; char *data_str = "data"; int i, j; for (i = 0; i < 45; i++) { a = (char) alt_up_sd_card_read(music_file); printf("%c\n", a); if (a == data_str[j]) { j++; if (j == 3) { printf("File size found.\n"); alt_up_sd_card_read(music_file); // letter A char byte_0 = (char) alt_up_sd_card_read(music_file); char byte_1 = (char) alt_up_sd_card_read(music_file); char byte_2 = (char) alt_up_sd_card_read(music_file); char byte_3 = (char) alt_up_sd_card_read(music_file); file_size = ((unsigned char) byte_3 << 24) | ((unsigned char) byte_2 << 16) | ((unsigned char) byte_1 << 8) | (unsigned char) byte_0; printf("Music file size: %lu\n", file_size); } } else { j = 0; printf("we are in else\n"); } } printf("Allocating memory space.\n"); unsigned int WHY[file_size]; audio_stream = WHY; if (audio_stream == NULL) { printf("*** Failed to allocate left audio stream!"); exit(1); } printf("Reading music file.\n"); short converter; while (song_size < file_size / 2) { b = (char) alt_up_sd_card_read(music_file); a = (char) alt_up_sd_card_read(music_file); converter = ((unsigned char) a << 8) | (unsigned char) b; converter = converter/2; audio_stream[song_size] = converter; song_size++; } printf("Starting music. \n"); printf("Song Size: %lu, byte: 0x%08x\n", song_size, audio_stream[song_size - 41]); alt_irq_register(AUDIO_0_IRQ, 0, audio_ISR); //alt_irq_enable(AUDIO_0_IRQ); alt_up_audio_enable_write_interrupt(audio); } printf("*** Error Loading Song.\n"); return; }
int main() { int i; //1 for receiving file from middleman, 2 for sending to middleman int mode; //Num char in filename array int numFileName; //Num bytes in file int numBytesFile; //Num characters in file array int numFile; char* listName; //File to send or receive (likely a piece of it) unsigned char fileName[MAX_FILENAME]; //variable to hold data received from uart unsigned char data; //parity bit for reading (not using parity atm, but still need the bit) unsigned char parity; short int handle; //handle for the file to create/access int connected = 0; //Variable to keep track of whether the SD CARD is connected or not. while (1) { alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev("/dev/rs232_0"); if(!alt_up_sd_card_is_FAT16()){ printf("SD CARD is not FAT16 Format\n"); } if (device_reference != NULL) { while (alt_up_sd_card_is_Present()) { printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &data, &parity); } // Now receive the instruction from the Middleman printf("Waiting for instruction to come from the Middleman\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; // First byte is the mode, 1 for receiving file from middleman, 2 for sending to middleman alt_up_rs232_read_data(uart, &data, &parity); mode = (int) data; //mode -= 48; printf("Mode:%d\n", mode); //Receive file from middleman and save to SD if (mode == 1) { printf("Waiting for num char:\n"); // The second byte is the number of characters in the file name while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); numFileName = (int) data; //numFileName -= 48; //Now receive the file name printf("About to receive %d characters:\n\n", numFileName); printf("Filename received:"); for (i = 0; i < numFileName; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart)== 0); alt_up_rs232_read_data(uart, &data, &parity); fileName[i] = data; printf("%c", data); } printf("\n"); fileName[i] = '.'; fileName[i+1] = 't'; fileName[i+2] = 'x'; fileName[i+3] = 't'; fileName[i+4]= '\0'; // // TODO: // USE THAT FILENAME TO MAKE A NEW FILE ON SD CARD HERE handle = alt_up_sd_card_fopen(fileName, 1); if(handle < 0){ //TODO: File can't be opened, do something about it printf("send had a neg handle \n\n"); } else{ // The 4 bytes after filename is the number of bytes in the file // // SHIFT BYTES LEFT AND CONCATENATE TO ACCOUNT FOR SEVERAL BYTES WORTH OF FILE // WRITE FIRST 4 BYTES OF FILE AS SIZE OF FILE IN BYTES while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile = (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; //WRITE BYTES TO SD CARD printf("About to receive %d file chars:\n\n",numBytesFile); for (i = 0; i < numBytesFile; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart)== 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); } printf("File done\n"); alt_up_sd_card_fclose(handle); } //This bracket ends receiving a file } //Send file to middleman from SD else if (mode == 2) { printf("Waiting for num char:\n"); // The second byte is the number of characters in the file name while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; alt_up_rs232_read_data(uart, &data, &parity); numFileName = (int) data; //numFileName -= 48; //Now receive the file name printf("About to receive %d characters:\n\n", numFileName); printf("Filename received:"); for (i = 0; i < numFileName; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; alt_up_rs232_read_data(uart, &data, &parity); fileName[i] = data; printf("%c", data); } printf("\n"); fileName[i] = '.'; fileName[i+1] = 't'; fileName[i+2] = 'x'; fileName[i+3] = 't'; fileName[i+4]= '\0'; handle = alt_up_sd_card_fopen(fileName, 0); if (handle == -1) { //SEND ANDROID AN ASCII 2 TO LET THEM KNOW TO RECEIVE FILE NAME alt_up_rs232_write_data(uart, 50); printf("neg handle"); if(alt_up_sd_card_find_first(".",listName) ==-1){ //SEND ANDROID AN ASCII 2 TO LET THEM KNOW THERES NO FILE NAMES alt_up_rs232_write_data(uart, 50); printf("no files"); } else{ //SEND ANDROID LIST OF FILE NAMES printf("some files"); i=0; for(i = 0; listName[i] != '.'; i++){ alt_up_rs232_write_data(uart, listName[i]); } alt_up_rs232_write_data(uart, 32); while(alt_up_sd_card_find_next(listName)!=-1){ i=0; for(i = 0; listName[i] != '.'; i++){ alt_up_rs232_write_data(uart, listName[i]); } alt_up_rs232_write_data(uart, 32); } alt_up_rs232_write_data(uart, 1); printf("done files"); } } else { //SEND ANDROID AN ASCII 1 TO LET THEM KNOW TO RECEIVE A FILE alt_up_rs232_write_data(uart, 49); // SHIFT BYTES LEFT AND CONCATENATE TO ACCOUNT FOR SEVERAL BYTES WORTH OF FILE // WRITE FIRST 4 BYTES OF FILE AS SIZE OF FILE IN BYTES data = alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile = (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; printf("About to send %d file bytes\n", numFile); while (numFile > 0) { while(alt_up_rs232_get_available_space_in_write_FIFO(uart) == 0); data = alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile--; } // WRITE A "FILE DONE" STRING OR WHATEVER WE DECIDE printf("sending end bits\n"); // alt_up_rs232_write_data(uart, 1); // // // alt_up_sd_card_fclose(handle); } //This bracket ends sending a file } //Something broke else { printf("Wrong mode, something broke, starting over\n"); } } } else { printf("Card Reader is not working\n"); } } return 0; //end main }
// opens the sound file and loads it into memory void load_sound_data() { short int file_handle; unsigned int loop_counter; printf("Reading sound file...\n"); // create large buffer to store all sound data greet_data = (unsigned int*) malloc(GREET_SIZE * sizeof(unsigned int)); lose_data = (unsigned int*) malloc(LOSE_SIZE * sizeof(unsigned int)); congrat_data = (unsigned int*) malloc(CONGRAT_SIZE * sizeof(unsigned int)); background_data = (unsigned int*) malloc(BACKGROUND_SIZE * sizeof(unsigned int)); printf("Check\n"); //greet file_handle = alt_up_sd_card_fopen("greet.wav", false); // skip header for (loop_counter = 0; loop_counter < HEADER_SIZE; loop_counter++) { alt_up_sd_card_read(file_handle); } // read and store sound data into memory for (loop_counter = 0; loop_counter < GREET_SIZE; loop_counter++) { greet_data[loop_counter] = alt_up_sd_card_read(file_handle); } alt_up_sd_card_fclose(file_handle); printf("Check\n"); //congrat file_handle = alt_up_sd_card_fopen("congrats.wav", false); // skip header for (loop_counter = 0; loop_counter < HEADER_SIZE; loop_counter++) { alt_up_sd_card_read(file_handle); } // read and store sound data into memory for (loop_counter = 0; loop_counter < CONGRAT_SIZE; loop_counter++) { congrat_data[loop_counter] = alt_up_sd_card_read(file_handle); } alt_up_sd_card_fclose(file_handle); printf("Check\n"); //background file_handle = alt_up_sd_card_fopen("back.wav", false); // skip header for (loop_counter = 0; loop_counter < HEADER_SIZE; loop_counter++) { alt_up_sd_card_read(file_handle); } // read and store sound data into memory for (loop_counter = 0; loop_counter < BACKGROUND_SIZE; loop_counter++) { background_data[loop_counter] = alt_up_sd_card_read(file_handle); } alt_up_sd_card_fclose(file_handle); printf("Check\n"); //lose file_handle = alt_up_sd_card_fopen("defeat.wav", false); // skip header for (loop_counter = 0; loop_counter < HEADER_SIZE; loop_counter++) { alt_up_sd_card_read(file_handle); } // read and store sound data into memory for (loop_counter = 0; loop_counter < LOSE_SIZE; loop_counter++) { lose_data[loop_counter] = alt_up_sd_card_read(file_handle); } alt_up_sd_card_fclose(file_handle); printf("Check\n"); // create buffer for storing samples from sound_data sound_buff = (unsigned int*) malloc(SAMPLE_SIZE * sizeof(unsigned int)); }
struct Image* loadSDImage(char* filename) { int i, j, bytes = 0, offset = 0, byte = 0; int a, b; int height = 0, width = 0; int size = 0, count = 0; char bmpId[3]; int* result = NULL; memset(bmpId, 0, 3); short int file_pointer = alt_up_sd_card_fopen(filename, false); if(file_pointer < 0) { alt_up_sd_card_fclose(file_pointer); //close the file return NULL; } //Start reading the bitmap header while(bytes < 2) { if((bmpId[bytes++] = alt_up_sd_card_read(file_pointer)) < 0) { alt_up_sd_card_fclose(file_pointer); printf("fail reading %s\n", filename); return NULL; } } if(strcmp(bmpId, "BM") != 0) { alt_up_sd_card_fclose(file_pointer); printf("fail reading %s at ID %s\n", filename, bmpId); return NULL; } short int temp; while(bytes < 10) { if((temp = alt_up_sd_card_read(file_pointer)) < 0) { alt_up_sd_card_fclose(file_pointer); printf("fail reading %s\n", filename); return NULL; } bytes++; } if((offset = alt_up_sd_card_read(file_pointer))< 0) { alt_up_sd_card_fclose(file_pointer); return NULL; } //printf("offset: %x\n", offset); bytes++; while(bytes < offset){ if(bytes == 18 || bytes == 19) { if((temp = alt_up_sd_card_read(file_pointer))< 0) { alt_up_sd_card_fclose(file_pointer); return NULL; } width += (int)temp << (bytes-18)*8 ; //if(bytes == 19) //printf("width: %d, ", width); } else if(bytes == 22 || bytes == 23) { if((temp = alt_up_sd_card_read(file_pointer))< 0) { alt_up_sd_card_fclose(file_pointer); return NULL; } height += (int)temp << (bytes-22)*8 ; //if(bytes == 22) //printf("height: %d\n", height); } else if(bytes == 34 || bytes == 35 || bytes == 36 || bytes == 37) { if((temp = alt_up_sd_card_read(file_pointer))< 0) { alt_up_sd_card_fclose(file_pointer); return NULL; } size += (int)(temp << 8*(count++)); if(bytes == 37) { //printf("data size: %d\n", size/2); result = (int*)malloc(sizeof(int)*(size/2)); } } else if((temp = alt_up_sd_card_read(file_pointer)) < 0) { alt_up_sd_card_fclose(file_pointer); return NULL; } //printf("%d %x\n", bytes, temp); bytes++; } //Start reading the pixel data for(j = height-1; j >= 0; j--) { for(i = 0; i < size/2/height; i++) { a = alt_up_sd_card_read(file_pointer); b = alt_up_sd_card_read(file_pointer); if(a < 0 || b < 0) { printf("%s invalid at pixel[%d, %d]!\n", filename, i, j); free(result); result = NULL; alt_up_sd_card_fclose(file_pointer); return NULL; } byte = getColor555(b*256+a); *(result + j*(width)+i) = byte; } } alt_up_sd_card_fclose(file_pointer); return initImage(result, 0, width, height); }
/* * Returns a byte from given file */ short int sdcard_ReadByte(short int file_handle) { short int byte = alt_up_sd_card_read(file_handle); return byte; }
void read_bytes_from_file(unsigned char* str, int len, short file){ for(int x=0 ; x<len ; x++){ str[x]=(unsigned char)(alt_up_sd_card_read(file)); } }