コード例 #1
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);
    }
}
コード例 #2
0
ファイル: SD.c プロジェクト: addisonbgross/eece391-software
/*
 * init_sd : open connection to sd card port
 */
void init_sd()
{
	alt_up_sd_card_dev *device_reference = NULL;
	char *interface = "/dev/Altera_UP_SD_Card_Avalon_Interface_0";

	if ( ( device_reference = alt_up_sd_card_open_dev( interface ) ) == NULL ) {
		printf( "ERROR! SD card path is bad: %s\n", interface );
	} else {
		if ( device_reference != NULL ) {
			if ( ( SD_CONNECTED == 0 ) && ( alt_up_sd_card_is_Present() ) ) {
				if ( alt_up_sd_card_is_FAT16() ) {
					printf( "< SD Initialized >\n" );
					SD_CONNECTED = 1;
				} else {
					printf( "ERROR! Unknown file system on SD card\n" );
				}
			} else if ( ( SD_CONNECTED == 1) && ( alt_up_sd_card_is_Present() == 0 ) ) {
				printf( "ERROR! card disconnected.\n" );

				SD_CONNECTED = 0;
			}
		} else {
			printf( "ERROR! can't open sd card device\n" );
		}
	}
}
コード例 #3
0
ファイル: Audio.c プロジェクト: RadixSort/previously
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;
}
コード例 #4
0
ファイル: utilites.c プロジェクト: eurasianalliance/Module1
/*
 * Overview: exercise 3 reads sd_card and list its file.
 *
 */
void sd_card(void){
	char file_name[10];
	alt_up_sd_card_dev *device_reference = NULL;
	int connected = 0;

	device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME);
	if (device_reference != NULL) {
		while(1) {
			if ((connected == 0) && (alt_up_sd_card_is_Present())) {
				//printf("Card connected.\n");
				if (alt_up_sd_card_is_FAT16()) {
					//printf("FAT16 file system detected.\n");
					if(alt_up_sd_card_find_first(".", (char*) file_name)==0){
						//printf("%s\n",file_name);
						while(alt_up_sd_card_find_next((char*)file_name)==0)
						{
							//printf("%s\n", file_name);
						}
					}
				} else {
					//printf("Unknown file system.\n");
				}
				connected = 1;
			} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
				//printf("Card disconnected.\n");
				connected = 0;
			}
			break;
		}
	} else {
		//printf("Card could not be opened\n");
	}
	//printf("sd_card function finished\n");
}
コード例 #5
0
ファイル: hello_world.c プロジェクト: RadixSort/previously
int main(void) {
	char buffer_name[10];
	short int handler;
	alt_up_sd_card_dev *device_reference = NULL;
	int connected = 0;
	device_reference = alt_up_sd_card_open_dev("/dev/SD_Card");

	if (device_reference != NULL) {
		while(1) {
			if ((connected == 0) && (alt_up_sd_card_is_Present())) {
			printf("Card connected.\n");
				if (alt_up_sd_card_is_FAT16()) {
					printf("FAT16 file system detected.\n");

					//Print out the list in the sd card
					handler = alt_up_sd_card_find_first("", buffer_name);
					printf("%s \n", buffer_name);
					while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1)
						printf("%s \n", buffer_name);

				}else{
					printf("Unknown file system.\n");
				}
				connected = 1;
			} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
				printf("Card disconnected.\n");
				connected = 0;
			}
		}
	}
	return 0;
}
コード例 #6
0
void load_file(char* filename, void (*func)(short)){
	bool found_file = false;

	if (get_device_reference() == NULL || !alt_up_sd_card_is_Present() || !alt_up_sd_card_is_FAT16()){
		printf("Can't find device, or device not configured properly\n");
		return;
	}

	char filename_all_caps[strlen(filename)];
	to_caps(filename, filename_all_caps);
	char found_file_name[13];
	if (alt_up_sd_card_find_first(".", found_file_name) != 0){
		printf("Couldn't find root dir\n");
		return;
	}

	do {
		if (strcmp(found_file_name, filename_all_caps)== 0){
			short int file = alt_up_sd_card_fopen(found_file_name, false);
			if (file >= 0){
				printf("found file %s in SD\n", filename_all_caps);
				(*func)(file);
				found_file = true; //want to close file, so use this rather than returning
			}
			alt_up_sd_card_fclose(file);
		}
	}while(!found_file && alt_up_sd_card_find_next(found_file_name) == 0);
}
コード例 #7
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);

			}
	}
}
コード例 #8
0
ファイル: sd_card.c プロジェクト: jordenh/LaserSharknadoSW
int openSdCard(){
	sdDev = alt_up_sd_card_open_dev("/dev/Altera_UP_SD_Card_Avalon_Interface_0");

	if(sdDev != NULL && alt_up_sd_card_is_FAT16()){
		return 0;
	} else {
		return -1;
	}
}
コード例 #9
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;
}
コード例 #10
0
ファイル: audio.c プロジェクト: RedTn/niosmusic
//Initializes SD card, outputs filenames on sd card
int init_sd(char * filenames[]){
	int j;
	int i = 0;
	char thepath[] = AUD_DIRECT;	//Directory for filenames we want to read
	short int check_file_name;

	for (j = 0; j < MAXFILES; j++) {
		filenames[j] = (char *)malloc(sizeof(char) * MAX_NAME_LEN);
		if (filenames[j] == NULL)
			printf("Error, out of memory (2)\n");
	}

	alt_up_sd_card_dev * sd_card = NULL;
	int connected = 0;
	sd_card = alt_up_sd_card_open_dev("/dev/Interface");

	if (sd_card != NULL) {
		while(1) {
			if ((connected == 0) && (alt_up_sd_card_is_Present())) {
				printf("Card connected.\n");
				if (alt_up_sd_card_is_FAT16()) {
					printf("FAT16 file system detected.\n");
					check_file_name = alt_up_sd_card_find_first(thepath,filenames[i]);
					if (check_file_name == 1)
						printf("Invalid Directory.\n");
					else if (check_file_name == 2)
						printf("Invalid SD Card.\n");
					else if (check_file_name == -1)
						printf("No files found.\n");
					else {
						i++;
					}
					while(alt_up_sd_card_find_next(filenames[i]) != -1){
						i++;
					}
					arr = (unsigned int ***)malloc(sizeof(unsigned int) * i);
					if (arr == NULL){
						printf("Error, arr null\n");
					}
					return i;		//If card connected, returns amount of files
				} else {
					printf("Unknown file system.\n");
				}
				connected = 1;
			} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
				printf("Card disconnected.\n");
				connected = 0;
			}
		}
	}
	else {
		printf("Error.\n");
		return -1;
	}
}
コード例 #11
0
ファイル: sd_card.c プロジェクト: kevhui/EECE381-Tanks
void initSD() {
	int connected = 0;
	alt_up_sd_card_dev *device_reference = NULL;
	device_reference = alt_up_sd_card_open_dev(SD_CARD_NAME);
	if (device_reference != NULL) {

		if ((connected == 0) && (alt_up_sd_card_is_Present())) {
			printf("Card connected. \n");
			printf("fat16: %d",alt_up_sd_card_is_FAT16());
			if (alt_up_sd_card_is_FAT16())
				printf("FAT16 file system detected. \n");
			else
				printf("Unknown file system. \n");
			connected = 1;
		} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
			printf("Card disconnected. \n");
			connected = 0;
		}
	}
}
コード例 #12
0
ファイル: SD_CARD.c プロジェクト: brian618/Team3QPF
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;
}
コード例 #13
0
ファイル: sd_card.c プロジェクト: kevhui/EECE381-Tanks
void printSD() {
	char* fileName;
	if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) {

		if (alt_up_sd_card_find_first("", fileName) == 0) {
			printf("Files detected: \n");
			do {
				printf("%s \n", fileName);
			} while (alt_up_sd_card_find_next(fileName) == 0);
		} else if (alt_up_sd_card_find_first("", fileName) == -1)
			printf("No files. \n");
	}

}
コード例 #14
0
bool loadSDCard(alt_up_sd_card_dev* device) {
	if((device = alt_up_sd_card_open_dev("/dev/SD_Card")) != NULL) {
		if (alt_up_sd_card_is_Present()) {
			printf("Card connected.\n");
			if (alt_up_sd_card_is_FAT16()) {
				printf("FAT16 file system detected.\n");
				return true;
			} else {
				printf("Unknown file system.\n");
			}
		}
	}
	return false;
}
コード例 #15
0
ファイル: audio.c プロジェクト: RedTn/niostank
int init_sd(char * filenames[]){
	int i = 0;
	char thepath[] = "/";
	short int check_file_name;

	alt_up_sd_card_dev * sd_card = NULL;
	int connected = 0;
	sd_card = alt_up_sd_card_open_dev("/dev/Interface");

	if (sd_card != NULL) {
		while(1) {
			if ((connected == 0) && (alt_up_sd_card_is_Present())) {
				printf("Card connected.\n");
				if (alt_up_sd_card_is_FAT16()) {
					printf("FAT16 file system detected.\n");
					check_file_name = alt_up_sd_card_find_first(thepath,filenames[i]);
					if (check_file_name == 1)
						printf("Invalid Directory.\n");
					else if (check_file_name == 2)
						printf("Invalid SD Card.\n");
					else if (check_file_name == -1)
						printf("No files found.\n");
					else {
						//	printf("First file name = %s.\n", filenames[i]);
						i++;
					}
					while(alt_up_sd_card_find_next(filenames[i]) != -1){
						//	printf("Next file name = %s.\n", filenames[i]);
						i++;
					}
					return i;
				} else {
					printf("Unknown file system.\n");
				}
				connected = 1;
			} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
				printf("Card disconnected.\n");
				connected = 0;
			}
		}
	}
	else {
		printf("Error.\n");
		return -1;
	}
}
コード例 #16
0
ファイル: SD.c プロジェクト: addisonbgross/eece391-software
/*
 * 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;
	}
}
コード例 #17
0
ファイル: SD_CARD.c プロジェクト: brian618/Team3QPF
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;
}
コード例 #18
0
ファイル: utilites.c プロジェクト: eurasianalliance/Module1
/*
 * 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");
	}
}
コード例 #19
0
ファイル: SD.c プロジェクト: addisonbgross/eece391-software
/*
 * 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 "";
	}

}
コード例 #20
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;

}
コード例 #21
0
/*
 * Detect and connect to SD Card
 */
void connectToSDCard()
{
	if (sd_card_reference != NULL)
	{
		// repeated check the connectivity of the SD CARD
		while (alt_up_sd_card_is_Present() == false);
		while (alt_up_sd_card_is_Present())
		{
			printf("Card connected.\n");
			if (alt_up_sd_card_is_FAT16())
			{
				printf("FAT16 file system detected.\n");
				break;
			}
			else
			{
				printf("Unknown file system.\n");
			}
		}
	}
}
コード例 #22
0
void initSD(){
	int connected = 0;
	alt_up_sd_card_dev *device_reference = NULL;
	device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME);
	if (device_reference != NULL) {

			if ((connected == 0) && (alt_up_sd_card_is_Present())) {
				//printf("Card connected. \n");
				//printf("fat16: %d",alt_up_sd_card_is_FAT16());
				if (alt_up_sd_card_is_FAT16()); //printf("FAT16 file system detected. \n");
				else printf("Unknown file system. \n");
				connected = 1;
			} else if ((connected == 1) && (alt_up_sd_card_is_Present()
					== false)) {
				printf("Card disconnected. \n");
				connected = 0;
			}
	}
	findDatabaseVersion();
	setDatabase(0);
}
コード例 #23
0
ファイル: sdcard.c プロジェクト: loganfg/cpen391-21
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;
}
コード例 #24
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;

}
コード例 #25
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;
}
コード例 #26
0
ファイル: SDCard.c プロジェクト: NolanMRamsden/Group13Module1
/**
 * Looks for a FAT16 partition
 * Return: True if FAT16 partition exists. False otherwise.
 */
int sdcard_isFAT16(void)
{
	int is_fat16 = alt_up_sd_card_is_FAT16();
	return is_fat16;
}
コード例 #27
0
ファイル: launchGame.c プロジェクト: aaewong/Cannon-Man
int main(void) {
	/* Related object data */
	system_t* system;
	player_t* player1;
	player_t* player2;

	alt_timestamp_start();
	int start_time = alt_timestamp();
	int wind;
	int restart = 0;
	/* initialize all hardware dev */
	system = system_init(VIDEO_PIXEL_BUFFER_DMA_NAME,
			"/dev/video_character_buffer_with_dma_0",
			ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME, PS2_0_NAME);
	if (!alt_up_sd_card_is_Present()) {
		printf("SD card not present\n");
		return -2;
	} else {
		printf("SD card detected.\n");
	}
	if (!alt_up_sd_card_is_FAT16()) {
		printf("SD card is not FAT16.\n");
		return -3;
	} else {
		printf("SD card is FAT 16.\n");
	}
	fireSound = initSoundbank("shotgun.wav");
	explosionSound = initSoundbank("big_bomb.wav");
	printf("Done Initializing\n");
	int end_time = alt_timestamp();
	srand(end_time - start_time);
	clearScreen(system);
	printf("Press enter to start game");
	restartGame(system);
	while (restart == 1) {
	/* initialize required objects */
	player1 = makePlayer(1, "Lomash");
	player2 = makePlayer(2, "TJ");

	/*
	 * Draw the screen for the first time.
	 */
	store_background_data();
	draw_background(system);
	drawPlayers(system);
	draw_ground(system);
	draw_windbox(system);
	draw_player1GUI(system);
	draw_player2GUI(system);
	update_wind(system, wind);
	draw_health(player1, system);
	draw_health(player2, system);
	update_power(player1, system);
	update_power(player2, system);
	usleep(2000000); // sleep to wait for video buffer to load
	while (TRUE) {

	printf(
	 "\n\n\n\n===========================================================\n");
	 printf("Player 1 Health = %d \t\t Player 2 Health = %d\n",
	 player1->health, player2->health);
	 wind = rand() % 11 - 5;
	 draw_windbox(system);
	 update_wind(system, wind);


//	 Player 1's turn

	 player_1_jump(system);
	 player_1_jump(system);
	 printf("Getting Player 1 Power.\n");
	 getKeyboardInput(1, player1, system); // Power
	 skipOneEnter(system);
	 printf("Getting Player 1 Angle.\n");
	 getKeyboardInput(2, player1, system); // Angle

//	 Player 1 Animation
	 printf("Starting animation\n");
	 clear_angle_drawer(system);
	 animate_cannon1(system);
	 playSound(fireSound, system->audio);
	 switch (animateShooting(system, player1, wind)) { // different value for result
	 case 1: {
	 update_health(player2, system, -DAMAGE);
	 printf(
	 "Player 1 hit player 2.\n Remaining Health for Player 2: %d\n",
	 player2->health);
	 break;
	 }
	 case 2: {
	 update_health(player1, system, -DAMAGE);
	 printf(
	 "Player 1 hit player 1.\n Remaining Health for Player 1: %d\n",
	 player1->health);
	 break;
	 }
	 default: {
	 break;
	 }
	 }
	 printf("Ended animation\n");

//	 Post-animation Calculation
	 if (player1->health <= 0 || player2->health <= 0) {
	 break;
	 }


//	 Player 2's turn


	 wind = rand() % 11 - 5;
	 draw_windbox(system);
	 update_wind(system, wind);
	 player_2_jump(system);
	 player_2_jump(system);
	 printf("Getting Player 2 Velocity.\n");
	 getKeyboardInput(1, player2, system);
//	 Player 2 Angle-Selection
	 skipOneEnter(system);
	 printf("Getting Player 2 Angle.\n");
	 getKeyboardInput(2, player2, system);

//	 Player 2 Animation
	 printf("Starting animation\n");
	 clear_angle_drawer(system);
	 animate_cannon2(system);
	 playSound(fireSound, system->audio);
//	 Post-animation Calculation
	 switch (animateShooting(system, player2, wind)) { // different value for result
	 case 1: {
	 update_health(player1, system, -DAMAGE);
	 printf(
	 "Player 2 hit player 1.\n Remaining Health for Player 1: %d\n",
	 player1->health);
	 break;
	 }
	 case 2: {
	 update_health(player2, system, -DAMAGE);
	 printf(
	 "Player 2 hit player 2.\n Remaining Health for Player 2: %d\n",
	 player2->health);
	 break;
	 }
	 default: {
	 break;
	 }
	 }

	 if (player1->health <= 0 || player2->health <= 0) {
	 break;
	 }
	};
	/*
	 * Find out who won.
	 */
		if (player1->health <= 0) {
			printf("Player 2 Wins!!! \n");
			draw_P2WIN(system);
		} else if (player2->health <= 0) {
			printf("Player 1 Wins!!!\n");
			draw_P1WIN(system);
		} else {
			printf("we shouldn't be here.\n");
		}
		restart = restartGame(system);
	}
	return 0; // FIN
}
コード例 #28
0
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
}
コード例 #29
0
int filenames_read(char* filename, int charmax){

	//Test if the SD card is inserted and is in a valid file format for reading

	if ((alt_up_sd_card_is_Present())) {
		if (alt_up_sd_card_is_FAT16()) {

			int offset = 0;
			int filecount = 0;

			int returnval = alt_up_sd_card_find_first(0x0, filename);
			if (returnval < 0){
				printf("the read failed");
				return -2;
			}

			/*
			 * Read the first filename within the SD card as long as there is sufficient space within the character buffer
			 */
			if((returnval == 0))
			{
				filecount++;
				while(*(filename+offset) != '.')
					offset++;
				offset+=4;
				*(filename+offset) = ';';
				offset++;
			}

			/* As long as there is sufficient space within the character buffer, will read every name within the
			 * SD card to a buffer.
			 */
			while (1){

				returnval = alt_up_sd_card_find_next((filename+offset));

				if (returnval == -1 ){
					break;
				}

				filecount++;

				if((charmax-offset) <= 12){
					break;
				}
				while(*(filename+offset) != '.'){
					offset++;
				}
				offset+=4;
				*(filename+offset) = ';';
				offset++;
			}

			return filecount;

		}
	}

	return -1;

}