Example #1
0
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");
	}

}
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);
}
Example #3
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);
    }
}
Example #4
0
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;
}
Example #5
0
/*
 * 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");

			}
		}
	}
}
Example #6
0
/*
 * 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");
			}
		}
	}
}
Example #7
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;
}
Example #8
0
//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);

			}
	}
}
Example #9
0
/*
 * 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;
	}
}
Example #10
0
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;
}
Example #11
0
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;
}
Example #12
0
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;
}
Example #13
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;
}
Example #14
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;
}
Example #15
0
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;
}
Example #16
0
/*
 * 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 "";
	}

}
Example #17
0
//initializes audio buffer
int initAudio(char* fname) {
	alt_up_sd_card_dev *device_reference = NULL;
	device_reference = alt_up_sd_card_open_dev(SD_CARD_NAME);

	open_sd();

	av_config_setup();
	init_audio_buff(ab);

	ab->audio = alt_up_audio_open_dev(AUDIO_0_NAME);
	alt_up_audio_reset_audio_core(ab->audio);

	file_handle = alt_up_sd_card_fopen(fname, 0);
	offset(file_handle, ab);

	fill_buff(file_handle, ab);

	return file_handle;
}
Example #18
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 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");
}
Example #20
0
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;
}
Example #21
0
/**
* Opens a file for use
* Parameters: name (file name, including the directory), create_new_if_not_exist (set to True to create a file if it doesn't exist already)
* Result: Index to file record. -1 if file not opened. -2 if file has already been opened
*/
short int sdcard_fopen(char *file_name, bool create_new_if_not_exist)
{
	short int handle = alt_up_sd_card_fopen("total.txt",false);
	return handle;
}
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
}
Example #23
0
short int openFile(char *fileName) {
	return alt_up_sd_card_fopen(fileName, 0);
}
Example #24
0
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);
}
Example #25
0
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);
}
Example #26
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;

}
Example #27
0
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;
}
Example #28
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;
}
Example #30
0
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;
}