void clean_audio(unsigned int *arr[]) { int k; for (k = 0; k < size_of_sound; k++) { free(arr[k]); } alt_up_sd_card_fclose(sound_file); }
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); } }
// Note: The user is responsible for freeing buffer allocated in loadSound. // filename - The name of the sound file in the SD card. // buf - A pointer to the newly allocated buffer. // len - Returns the length of the newly allocated memory (in words) int loadSound(char* audioFile, int** buf, float audioVolume) { int i; file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Allocate a buffer that is the same byte size as the file *buf = (unsigned int*) malloc((fileLength-32) * 2); int bufSize = (fileLength-32)/2; if (audioVolume <= 0) audioVolume = 1.0; for (i = 0; i < bufSize; i++) { // Extract data and store in the buf. char firstByte = read_file(fileHandle); char secondByte = read_file(fileHandle); short val = ( (unsigned char) secondByte << 8) | (unsigned char) firstByte; val = val * audioVolume; (*buf)[i] = val; } // Close the file on the sd card. alt_up_sd_card_fclose(fileHandle); return bufSize; }
void writeSD(char name[], int score) { short int handler = alt_up_sd_card_fopen(SCORE_NAME, true); int index = 0; char score_str[4]; if (handler == -1) { handler = alt_up_sd_card_fopen(SCORE_NAME, false); setPositionToEnd(handler); } if (handler >= 0) { while (name[index] != '\0') { alt_up_sd_card_write(handler, name[index]); index++; } alt_up_sd_card_write(handler, 0x2F); // slash index = 0; convertInt(score_str, score); while (score_str[index] != '\0') { alt_up_sd_card_write(handler, score_str[index]); index++; } alt_up_sd_card_fclose(handler); } else { printf("Handler:%d\n", handler); printf("Cannot write the score. \n"); } }
/* * 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"); } } } }
/* * 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"); } } } }
/* * Overview: This plays the boom sound when the tanks are hit * */ void play_doh(alt_up_audio_dev *audio, unsigned int *buffer, short int handle) { int lbytes; int rbytes; int lindex=22; int rindex=22; alt_up_audio_dev* aud=audio; int i=0; while(i<DOH_LENGTH) //200 for cha ching { if (alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_LEFT) > 22) { lbytes = alt_up_audio_write_fifo(aud, &buffer[lindex], 96, ALT_UP_AUDIO_LEFT); rbytes = alt_up_audio_write_fifo(aud, &buffer[rindex], 96, ALT_UP_AUDIO_RIGHT); lindex += lbytes; rindex += rbytes; i++; } } //printf("all good things come to an end\n"); alt_up_sd_card_fclose(handle); }
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); }
/* * 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; }
// Requires: // Effects: Plays an audio .wav file, blocking the CPU until the song has completed. int playBlockingMusic(char* audioFile) { int i = 0; int currentSample = 0; unsigned int *sample; file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Allocate a buffer that is the same byte size as the file unsigned int *buf = (unsigned int*) malloc(fileLength * 2); int bufSize = fileLength/2; for (i = 0; i < bufSize; i++) { // Extract data and store in the buf. unsigned char firstByte = read_file(fileHandle); unsigned char secondByte = read_file(fileHandle); unsigned short val = (secondByte << 8) | firstByte; buf[i] = val; } // Close the file on the sd card. alt_up_sd_card_fclose(fileHandle); while (currentSample < bufSize) { unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); if (space > bufSize - currentSample) { // Don't need to fully fill the rest of the buffer. space = bufSize - currentSample; } if (space > 0) { sample = &(buf[currentSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); currentSample += space; } } free(buf); 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; }
/* * 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 ""; } }
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; }
/* * 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; }
*/void sound() { short int handle; //printf("SDCARD CONNECTION CHECK\n"); alt_up_sd_card_dev *device_reference=alt_up_sd_card_open_dev("/dev/Altera_UP_SD_Card_Avalon_Interface_0"); alt_up_audio_dev *audio; sdcardcheck(device_reference); //printf("AV CONFIG SETUP\n"); av_config_setup(); //printf("AUDIO OPEN\n"); audio=alt_up_audio_open_dev("/dev/audio"); unsigned int buffer[COIN_SIZE]; char *filename="coin.wav"; readCoin(buffer, filename, handle); play_audio(audio, buffer, handle); alt_up_sd_card_fclose(filename); }
int sdcard_close(short int * filehandle) { if (device_reference == NULL) { printf("Error: SDCard not open\n"); return 1; } if (*filehandle == -1) { printf("Error: File handle not open\n"); return 1; } if (!alt_up_sd_card_fclose(*filehandle)) { printf("Error: Could not close filehandle\n"); return 1; } *filehandle = -1; printf("Closed SDCard file\n"); return 0; }
void updateDatabase(){ setDatabase(1); short int handler = alt_up_sd_card_fopen(database_name, true); unsigned char tmp[5] = {'\0'}; int i,j; printf("Updating to %s...\n",database_name); if (handler == -1){ printf("Error occurred when updating database."); } // Write data into the file for (i=0;i<total_items;i++){ for (j=0;j<UPC_NUM_CHARACTER;j++){ alt_up_sd_card_write(handler, list[i].upc[j]); } alt_up_sd_card_write(handler, ','); for (j=0;list[i].name[j] != '\0';j++){ alt_up_sd_card_write(handler, list[i].name[j]); } alt_up_sd_card_write(handler, ','); sprintf( tmp, "%.2f", list[i].price); for (j=0;tmp[j] != '\0';j++){ alt_up_sd_card_write(handler, tmp[j]); } alt_up_sd_card_write(handler, ','); sprintf( tmp, "%d", list[i].stock); for (j=0;tmp[j] != '\0';j++){ alt_up_sd_card_write(handler, tmp[j]); } alt_up_sd_card_write(handler, 13); // CR alt_up_sd_card_write(handler, 10); // LF } alt_up_sd_card_fclose(handler); printf("Database updated.\n"); }
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; }
/* * Closes the file */ void sdcard_fclose(int short file_handle) { alt_up_sd_card_fclose(file_handle); }
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; }
int loadMusic(char* audioFile, unsigned short loop, float volumeFactor) { int i = 0; unsigned int *sample; alt_up_audio_disable_write_interrupt(audio); printf("Opening file\n"); file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Discard header-- we are making an assumption about // how the data is stored to make it easier to // add sound to the music. for (i = 0; i < 32; i++) read_file(fileHandle); // Allocate the main music buffer to be the size of the file. if (interruptMusicBuffer != 0) free(interruptMusicBuffer); musicLoop = loop; musicDone = 0; interruptMusicBuffer = (int*) malloc((fileLength-32) * 2); interruptBufSize = (fileLength-32)/2; interruptSample = 0; if (volumeFactor <= 0) volumeFactor = 1; for (i = 0; i < interruptBufSize; i++) { // Extract data and store in the buf. char firstByte = read_file(fileHandle); char secondByte = read_file(fileHandle); short val = ( (unsigned char) secondByte << 8) | (unsigned char) firstByte; val = val * volumeFactor; interruptMusicBuffer[i] = val; } alt_up_sd_card_fclose(fileHandle); unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; alt_up_audio_enable_write_interrupt(audio); } } else { // Enable the write interrupt alt_up_audio_enable_write_interrupt(audio); } return 0; }
int closeFile(short int fh) { return alt_up_sd_card_fclose(fh); }
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 }
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); }
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; }
int main() { arr = (unsigned int**)malloc(4377 * sizeof(unsigned int)); psController ps; int velocity = 0; int angle = 0; long ii = 0; int** collisionMatrix; int ee; int numfiles; char * filenames[MAXFILES]; //init_VGA_Pixel(); initVGA(); //pixel init_VGA_Char(); av_config_setup(); numfiles = init_sd(filenames); init_wav(arr); printf("timing audio\n"); //time_of_sound = time_audio(arr); printf("looping audio\n"); audio_isr_k = 0; alt_irq_register(AUDIO_IRQ, NULL, &audio_isr); alt_up_audio_enable_write_interrupt(audio); //====INITIALIZE AND DRAW TERRAIN USING BMP ===========================// short int fd; terrain background; fd = alt_up_sd_card_fopen("bg.bmp", false); initTerrain(fd, &background); drawBackground(background); alt_up_sd_card_fclose(fd); terrain terrain; fd = alt_up_sd_card_fopen("tr.bmp", false); initTerrain(fd, &terrain); drawTerrain(terrain); alt_up_sd_card_fclose(fd); //====INITIALIZE AND DRAW USER TANK USING BMP ===========================// fd = alt_up_sd_card_fopen("tank.bmp", false); tank user; initUserTank(&user, fd); drawTank(&user, background); alt_up_sd_card_fclose(fd); //====INITIALIZE AND DRAW ENEMY TANK USING BMP ===========================// tank enemy; fd = alt_up_sd_card_fopen("tk.bmp", false); initEnemyTank1(&enemy, fd); drawTank(&enemy,background); alt_up_sd_card_fclose(fd); //====SWAP BUFFERS =======================================================// updateVGA(); drawBackground(background); drawTerrain(terrain); drawTank(&enemy, background); for(ee=0;ee < 20;ee++) { moveTank(&user, RIGHT, 1); drawTank(&user,background); updateVGA(); drawTank(&user,background); //delay(); } //for(ii = 0; ii < 300000; ii++); collisionMatrix = init_CollisionMatrix(XMAXSCREEN_RESOLUTION + 1, XMAXSCREEN_RESOLUTION + 1); //alt_up_pixel_buffer_dma_draw_box(pixel_buffer, 0, 120, 340, 240, 0x7E0 , 0); //draw terrain set_CollisionAtRectArea(collisionMatrix, 0 , 0, 340, 240, EMPTY); set_CollisionAtRectArea(collisionMatrix, 0 , 161, 340, 240, TERRAIN_COLOR);//set the rectangular area drawn by terrain to filled with terrain while(1){ //angle = 0; velocity = 0; set_UserTank(&angle, &velocity, ps, &user, &enemy, background); //reads controller to set angle, power, and shoot shoot_Missile(angle, velocity,( user.x + user.t_VGA.width) - 1 , (240 - user.y + user.t_VGA.height) - 7, collisionMatrix, background); } clean_audio(arr); return 0; }
//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); }
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; }