Example #1
0
File: comm.c Project: tonglil/bang
void receive_data_from_middleman(alt_up_rs232_dev* uart, Comm_data* cd) {
    // Now receive the message from the Middleman

    // First byte is the client id received
    while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0);
    alt_up_rs232_read_data(uart, &(cd->data), &(cd->parity));
    cd->client_id = (int)cd->data;
    // Second byte is the number of characters passed by Middleman, don't store
    while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0);
    alt_up_rs232_read_data(uart, &(cd->data), &(cd->parity));
    // Third byte is the number of characters in our message
    while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0);
    alt_up_rs232_read_data(uart, &(cd->data), &(cd->parity));
    cd->num_to_receive = (int)cd->data;
    cd->r_len = cd->num_to_receive;

    int i;
    for (i = 0; i < cd->num_to_receive; i++) {
        while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0);
        alt_up_rs232_read_data(uart, &(cd->data), &(cd->parity));

        cd->r_message[i] = cd->data;
    }
    for (i = 0; i < cd->num_to_receive; i++) {
    }

    // Acknowledge message received
    if (0) {
		alt_up_rs232_write_data(uart, (unsigned char) cd->client_id);
		alt_up_rs232_write_data(uart, 1);
		alt_up_rs232_write_data(uart, 0x0a);
		usleep(1500000);
    }
}
Example #2
0
void get_filenames()
{
	unsigned int oldvalue;
	bool _numfiles = false;
	int numfiles;

	printf("Getting filenames\n");

	do {
		alt_up_rs232_read_data(uart, &data, &parity);
	}while((int)data == (MAXFILES + 1));

	while(1) {
		if (rs_flag == true)
		{
			if (_numfiles == false) {
				alt_up_rs232_read_data(uart, &data, &parity);
				numfiles = (int)data;
				_numfiles = true;
			}
			else {
				do {
					alt_up_rs232_read_data(uart, &data, &parity);
				}while((int)data == numfiles);

			}
		}
	}
}
Example #3
0
/*
 * Initialization of Serail communication
 */
struct RS232 initRS232() {
	struct RS232 com_local;
	com_local.receivePackets = initQueue();
	com_local.sendPackets = initQueue();
	com_local.pendingPacketSize = initQueue();
	com_local.client_ack = 0;
	com_local.host_ack = 0;
	com_local.isRdySend = 0;
	com_local.failReceive = 0;
	com_local.stateMachine = (enum States*)malloc(sizeof(enum States));
	com_local.pastState = (enum States*)malloc(sizeof(enum States));
	*com_local.stateMachine = startInit;
	*com_local.pastState = startInit;
	com_local.num_packets = com_local.index_packets = 0;
	com_local.num_send_packets = com_local.index_send_packets = 0;
	com_local.packetBuf = NULL;

	printf("UART Initialization\n");
	alt_up_rs232_dev* uart = alt_up_rs232_open_dev(RS232_0_NAME);
	up_dev.RS232_dev = uart;

	printf("Clearing read buffer to start\n");
	while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) {
		alt_up_rs232_read_data(uart, &com.data[0], &com.parity);
	}
	alt_alarm_start(&alarm, alt_ticks_per_second(), RS232_ISR, (void*)&up_dev);

	printf("UART Initialization finished\n");
	return com_local;
}
Example #4
0
void clear_fifo()
{
	while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) {
		alt_up_rs232_read_data(uart, &data, &parity);
	}
	//	printf("Fifo cleared.\n");
}
Example #5
0
/*
 * Reset the RS232 communication to initial state
 */
void reset(alt_up_rs232_dev *serial_dev) {
	printf("Starting reseting connection\n");
	com.client_ack = com.host_ack = 0;
	com.failReceive = com.isRdySend = 0;
	com.num_packets = com.num_send_packets = 0;
	com.index_packets = com.index_send_packets = 0;

	struct Packet* p = NULL;
	int* len;
	while(com.sendPackets->size > 0) {
		p = (struct Packet*)dequeue(com.sendPackets);
		killPacket(&p);
	}

	while(com.receivePackets->size > 0) {
		p = (struct Packet*)dequeue(com.receivePackets);
		killPacket(&p);
	}

	while(com.pendingPacketSize->size > 0) {
		len = (int*)dequeue(com.pendingPacketSize);
		free(len);
		len = NULL;
	}
	if(&com.packetBuf != NULL) {
		free(com.packetBuf);
		com.packetBuf = NULL;
	}
	while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) != 0);
	alt_up_rs232_read_data(serial_dev, &com.data[0], &com.parity);
	setStates(startInit);
	printf("WARNNING!! the connection has been reseted.\n");
}
Example #6
0
File: comm.c Project: tonglil/bang
alt_up_rs232_dev* init_clear_uart(Comm_data* cd) {
    printf("UART Initialization\n");
    alt_up_rs232_dev* uart = alt_up_rs232_open_dev("/dev/rs232");

    printf("Clearing read buffer to start\n");
    while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) {
        alt_up_rs232_read_data(uart, &(cd->data), &(cd->parity));
    }

    return uart;
}
Example #7
0
/* UART task: read and write */
void taskRS232(void* pdata) {
	alt_u32 write_FIFO_space;
	alt_u16 read_FIFO_used;
	alt_u8 data_W8;
	alt_u8 data_R8;
	int enter = 0;
	unsigned p_error;
	alt_up_rs232_dev* rs232_dev;
	// open the RS232 UART port
	rs232_dev = alt_up_rs232_open_dev("/dev/rs232_0");
	if (rs232_dev == NULL)
		alt_printf("Error: could not open RS232 UART\n");
	else
		alt_printf("Opened RS232 UART device\n");
	alt_up_rs232_enable_read_interrupt(rs232_dev);
	while (1) {
		int sw = OSQPend(SWQ, 0, &err);
		if (sw == SW_WRITE) {
			alt_up_rs232_disable_read_interrupt(rs232_dev);
			if (enter == 0) {
				data_W8 = 'A';
				enter = 1;
			} else if (enter == 1) {
				data_W8 = '\n';
				enter = 0;
			}
			write_FIFO_space = alt_up_rs232_get_available_space_in_write_FIFO(
					rs232_dev);
			if (write_FIFO_space >= WRITE_FIFO_EMPTY) {
				alt_up_rs232_write_data(rs232_dev, data_W8);
				alt_printf("write %c to RS232 UART\n", data_W8);
			}
			OSTimeDlyHMSM(0, 0, 1, 0);
			alt_up_rs232_enable_read_interrupt(rs232_dev);
		}
		if (sw == SW_READ) {
			read_FIFO_used = alt_up_rs232_get_used_space_in_read_FIFO(
					rs232_dev);
			if (read_FIFO_used > READ_FIFO_EMPTY) {
				alt_printf("char stored in read_FIFO: %x\n", read_FIFO_used);
				alt_up_rs232_read_data(rs232_dev, &data_R8, &p_error);
				alt_printf("read %x from RS232 UART\n", data_R8);
			}
			OSTimeDlyHMSM(0, 0, 0, 5);
		}
	}
}
Example #8
0
//Sends the sound file names to android
void send_fnames(char * filenames[], int numfiles)
{
	int i,j;
	int size;
	char * ptr;

	rs_flag = false;

	clear_fifo();

	printf("Sending the message to the Middleman\n");

	for (j = 0; j < numfiles; j++) {
		alt_up_rs232_write_data(uart, 2);
		alt_up_rs232_write_data(uart, j);
		size = strlen(filenames[j]);
		ptr = filenames[j];

		//Make sure there is space
		while(alt_up_rs232_get_available_space_in_write_FIFO(uart) < (size + 2));

		// Now send the actual message to the Middleman
		for (i = 0; i < size; i++) {
			alt_up_rs232_write_data(uart, *(ptr + i));
		}

		wait();
		do {
			if (rs_flag == true) {
				alt_up_rs232_read_data(uart, &data, &parity);
			}

			////////////////////////////

			//TO BE COMMENTED OUT, this is my delay simulation for android
			/*
			testvalue++;
			if (testvalue == 1000000) {
				alt_up_rs232_write_data(uart, (unsigned char)ACK);
				testvalue = 0;
			}
			 */
			/////////////////////////////
		}while(data != ACK);
		clear_fifo();
	}
}
Example #9
0
//Gets the index for song to be played
int get_fnames(char * soundfiles[], int numwavfiles)
{
	int index = 0;

	do {
		while(rs_flag == false);
		alt_up_rs232_read_data(uart, &data, &parity);
	}while(((int)data == 3) || ((int)data == 0));
	printf("\n");

	index = (int)data - 4;
	if ((index < 0) || (index > numwavfiles)) {
		printf("Index received out of bounds\n");
		index = 0;
	}

	printf("Song Selected(if detected): %s, Index selected = %d\n", soundfiles[index], index);

	return index;
}
Example #10
0
void emulate_ack2()
{
	printf("Emulating ACK\n");
	int i;
	int stop;
	int once = 0;

	start_timer();
	while(1)
	{
		if(rs_flag == true) {
			alt_up_rs232_read_data(uart, &data, &parity);
			if (data == HEAD) {

			}
			else
				printf("Error, Header not received.\n");
		}
	}
	printf("\n");
	stop_timer();
	alt_up_rs232_write_data(uart, (unsigned char) ACK);
}
Example #11
0
void clearUART(){
	while (alt_up_rs232_get_used_space_in_read_FIFO(uart)){
		alt_up_rs232_read_data(uart, &data, &parity);
	}
}
Example #12
0
alt_u32 RS232_ISR(void* up_dev) {
	if(queue_lock == 1) return alt_ticks_per_second()/1000;
	alt_up_rs232_dev *serial_dev = ((struct alt_up_dev*)up_dev)->RS232_dev;
	unsigned char* cert;
	int i = 0;
	int len = 0;
	switch(*(com.stateMachine)) {
	case startInit:
		cert = sendStartInit();
		sendRS232(serial_dev, cert, 3);
		free(cert);
		cert = NULL;
		com.failReceive = 0;
		return alt_ticks_per_second()/20;
	case waitStart:
		i = 0;
		if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) {
			do {
				while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0);
				alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity);
				i++;
			} while(i < 3);
			if(checkStartAck(com.data[0]) == 1) {
				setStates(sendStates);
			} else
				setStates(startInit);
		} else {
			com.failReceive++;
			if(com.failReceive > 10)
				setStates(startInit);
		}
		return alt_ticks_per_second()/20;
	case checkClient:
		i = 0;
		if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) {
			do {
				while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0);
				alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity);
				i++;
			} while(i < 3);
			if(checkAck(com.data) == 1) {
				if(getBit(com.data[0], 1) == 1) {
					setStates(sendAck0);
					com.num_packets = ((int)(com.data[1] << 8))+(int)com.data[2];
					printf("num of packets will be receiving: %d\n", com.num_packets);
				} else
					setStates(sendStates);
			} else
				setStates(sendStates);
		} else {
			com.failReceive++;
			if(com.failReceive > 100) {
				reset(serial_dev);
			}
		}
		return alt_ticks_per_second()/20;
	case sendStates:
		cert = sendStats();
		sendRS232(serial_dev, cert, 3);
		free(cert);
		cert = NULL;
		com.failReceive = 0;
		return alt_ticks_per_second()/20;
	case sendAck0:
		cert = sendAck();
		sendRS232(serial_dev, cert, 3);
		if(cert[1] != 0)
			printf("sth is wrong");
		free(cert);
		cert = NULL;
		com.failReceive = 0;
		return alt_ticks_per_second()/20;
	case receiveData0:
		if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) > 2) {
			//read first 2 bytes for header information
			alt_up_rs232_read_data(serial_dev, &com.data[0], &com.parity);
			alt_up_rs232_read_data(serial_dev, &com.data[1], &com.parity);
			struct Packet* p = readPacketHeader(com.data[0], com.data[1]);
			i = 0;
			do {
				while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0);
				alt_up_rs232_read_data(serial_dev, &(p->data[i+HEADERSIZE]), &com.parity);
			//	printf("%c", p->data[i+HEADERSIZE]);
				i++;
			} while(p->data_size > i );
			//printf("\n");
			enqueue(com.receivePackets, (void*)p);
			com.index_packets++;
			setStates(sendAck0);
		} else {
			com.failReceive++;
			if(com.failReceive > 100) {
				reset(serial_dev);
			}
		}
		return alt_ticks_per_second()/20;
	case waitClient:
		i = 0;
		if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) {
			do {
				while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0);
				alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity);
				i++;
			} while(i < 3);
			if(checkAck(com.data) == 1) {
				setStates(sendData0);
			} else {
				com.failReceive++;
				if(com.failReceive > 100)
					setStates(sendStates);
			}
		} else {
			com.failReceive++;
			if(com.failReceive > 100) {
				reset(serial_dev);
			}
		}
		return alt_ticks_per_second()/20;
	case sendData0:
		if(com.packetBuf == NULL) {
			if((com.packetBuf = (struct Packet*)dequeue(com.sendPackets))==NULL) {
				printf("Packet sending fail, queue is empty");
				setStates(sendStates);
				return alt_ticks_per_second()/50;
			}
		} setAck(com.packetBuf, com.host_ack);

		for(i = 0; i< com.packetBuf->data_size+HEADERSIZE; i++) {
			alt_up_rs232_write_data(serial_dev, com.packetBuf->data[i]);
		}
		setStates(waitAck0);
		com.failReceive = 0;
		return alt_ticks_per_second()/20;
	case waitAck0:
		i = 0;
		if((len = alt_up_rs232_get_used_space_in_read_FIFO(serial_dev)) >= 3) {
			do {
				while (alt_up_rs232_get_used_space_in_read_FIFO(serial_dev) == 0);
				alt_up_rs232_read_data(serial_dev, &com.data[i], &com.parity);
				i++;
			} while(i < 3);
			if(checkAck(com.data) == 1) {
				com.index_send_packets++;
				killPacket(&(com.packetBuf));
				if(com.index_send_packets < com.num_send_packets)
					setStates(sendData0);
				else {
					com.index_send_packets = com.num_send_packets = 0;
					setStates(sendStates);
					com.isRdySend = 0;
				}
			} else {
				setStates(sendData0);
			}
		} else {
			com.failReceive++;
			if(com.failReceive > 100) {
				reset(serial_dev);
			}
		}
		return alt_ticks_per_second()/20;
	default:
		break;
	}

	return alt_ticks_per_second()/20;
}
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 #14
0
void send_filenames(char * filenames[], int numfiles)
{
	int i,j;
	int size;
	char * ptr;

	clear_fifo();

	printf("Sending the message to the Middleman\n");

	//Start with header
	alt_up_rs232_write_data(uart, (unsigned char) HEAD);

	//Function protocol
	alt_up_rs232_write_data(uart, (unsigned char) 3);

	//NumFiles
	alt_up_rs232_write_data(uart, (unsigned char) numfiles);

	/*
	wait();
	alt_up_rs232_read_data(uart, &data, &parity);
	printf("%d files.\n", (int)data);
	 */

	for (j = 0; j < numfiles; j++) {

		size = strlen(filenames[j]);
		ptr = filenames[j];

		//Make sure there is space
		while(alt_up_rs232_get_available_space_in_write_FIFO(uart) < ((size * 2) + 2));

		// Start with the number of bytes in our message
		alt_up_rs232_write_data(uart, (unsigned char)size);

		// Now send the actual message to the Middleman
		for (i = 0; i < size; i++) {
			alt_up_rs232_write_data(uart, *(ptr + i));

			//Send ETX
			alt_up_rs232_write_data(uart, (unsigned char) ETX);

		}

		alt_up_rs232_write_data(uart, (unsigned char) ETB);

		wait();
		//emulate_ack();
		emulate_ack2();

		wait();
		alt_up_rs232_read_data(uart, &data, &parity);
		//printf("%d\n", (int)data);
		if (data != ACK) {
			printf("Error, Acknowledge was not received.\n");
		}
		else
			printf("Acknowledge received.\n");

		clear_fifo();

		if (j == (numfiles - 1)) {
			//End of Transmission
			alt_up_rs232_write_data(uart, (unsigned char) EOT);
		}
	}
	wait();
	alt_up_rs232_read_data(uart, &data, &parity);
	printf("I received %d\n", (int)data);

}
Example #15
0
//Audio interrupt function, plays sound effects and/or loops audio
//REQ: audmode->id must point to correct id
void audio_isr(audisr * audmode, alt_u32 irq_id) {
	int i;
	unsigned int ** temp;
	unsigned int * second;

	if (audmode->resetmix == true) {
		audmode->resetmix = false;
		audio_isr_2 = 0;
	}
	//if (audmode->mode == 0) {
	if (audmode->id != audmode->oldid) {
		audio_isr_k = 0;
		seek = 0;
		init_copy(audmode->id, audmode->oldid);
		audmode->oldid = audmode->id;
	}
	if (audmode->id2 != audmode->oldid2) {
		audio_isr_2 = 0;
		audmode->oldid2 = audmode->id2;
	}
	if (audio_isr_k == 0) {
		copy_bgm(audmode->id);
		alt_up_rs232_write_data(uart, 0x6);
		alt_up_rs232_write_data(uart, (int)audiosecs[audmode->id]);
	}
	audio_log.bgmin = copyarr[audio_isr_k];

	//Mode 0, loop music
	if (audmode->mode == 0) {
		for (i = 0; i < 96; i++) {
			audio_log.bgmin[i] = volume_adjust(audio_log.bgmin[i], audmode->newvolume);
		}
	}
	else if (audmode->mode == 1) {
		temp = arr[audmode->id2];
		second = temp[audio_isr_2];
		for (i = 0; i < 96; i++) {
			unsigned int tempmix = audio_log.bgmin[i];
			audio_log.bgmin[i] = mix_adjust(tempmix,second[i],5);
		}
		audio_isr_2++;
		if (audio_isr_2 > audiosize[audmode->id2]) {
			audio_isr_2 = 0;
			audmode->mode = 0;
		}
	}
	if (alt_up_audio_write_interrupt_pending(audio) == 1) {
		alt_up_audio_write_fifo(audio, audio_log.bgmin, 96, ALT_UP_AUDIO_LEFT);
		alt_up_audio_write_fifo(audio, audio_log.bgmin, 96, ALT_UP_AUDIO_RIGHT);
		if (audmode->loop == true){
			audio_isr_k = (audio_isr_k + 1) % audiosize[audmode->id];
			seek = (seek + 1) % 333;
			if (seek == 0) {
				//alt_up_rs232_write_data(uart, 0x0A);
			}
			if (audio_isr_k == 0) {
				alt_up_rs232_write_data(uart, 0x3);
			}
		}
		else
		{
			if(audio_isr_k <= audiosize[audmode->id])
			{
				audio_isr_k += 1;
				seek = (seek + 1) % 333;
				if (seek == 0) {
					//alt_up_rs232_write_data(uart, 0x0A);
				}
			}
			else
			{
				seek = 0;
				audmode->mode = 0;
				audio_isr_2 = 0;
				audio_isr_k = 0;
				//alt_up_rs232_write_data(uart, 0x3);
				alt_up_rs232_write_data(uart, 0x4);
				wait();
				if (audmode->shuffle == false) {
					audmode->id += 1;
					if ((audmode->id >= audmode->files) && (audmode->listloop == true)) {
						audmode->id = 0;
						//printf("Restarting songs\n");
					}
					else if((audmode->id >= audmode->files) && (audmode->listloop == false)) {
						printf("End of Playlist\n");
						audmode->id = 0;
						alt_up_audio_disable_write_interrupt(audio);
					}
				}
				else {
					/*
					do {
						while(rs_flag == false);
						alt_up_rs232_read_data(uart, &data, &parity);
					}while((int)data == 0);
					 */
					wait();
					alt_up_rs232_read_data(uart, &data, &parity);
					int nextindex = (int)data;
					nextindex -= 4;
					//printf("Next Index: %d\n", nextindex);
					if ((nextindex < 0) || (nextindex > audmode->files)) {
						nextindex = 0;
						printf("Error, next Index: %d\n", nextindex);
					}
					audmode->id = nextindex;
				}
			}
		}
	}
	//}
	/*
	else if (audmode->mode == 1) {

		if (audmode->id != audmode->oldid) {
			//TEMP
			audmode->id2 = audmode->id++;
			if (audmode->id2 >= audmode->files) {
				audmode->id2 = 0;
			}
			if (audiosize[audmode->id] > audiosize[audmode->id2]) {
				audmode->large = audmode->id;
			}
			else {
				audmode->large = audmode->id2;
			}
			//
			audio_isr_k = 0;
			init_copy(audmode->large, audmode->oldid);
			audmode->oldid = audmode->id;
		}
		if (audio_isr_k == 0) {
			copy_bgm(audmode->large);
			alt_up_rs232_write_data(uart, 0x6);
		}
		audio_log.bgmin = copyarr[audio_isr_k];

		for (i = 0; i < 96; i++) {
			//audio_log.bgmin[i] = mix_adjust(audio_log.bgmin[i],  ,audmode->newvolume);
		}

		if (alt_up_audio_write_interrupt_pending(audio) == 1) {


			audio_isr_k = 0;
			audmode->id += 1;
			alt_up_rs232_write_data(uart, 0x3);
			if ((audmode->id >= audmode->files) && (audmode->listloop == true)) {
				audmode->id = 0;
				printf("Restarting songs\n");
			}
			else if((audmode->id >= audmode->files) && (audmode->listloop == false)) {
				printf("End of Playlist\n");
				audmode->id = 0;
				alt_up_audio_disable_write_interrupt(audio);
			}
		}

	}
	 */
}
Example #16
0
void recieveMessage(){

	int i;

	if (alt_up_rs232_get_used_space_in_read_FIFO(uart) > 0){
		printf("recieving data\n");

		alt_up_rs232_read_data(uart, &data, &parity);
		printf("data: %d,%i,%c\n",data,data,data);
		if (ID_p == 0){
			ID_r = data;
			ID_p = 1;
			printf("ID: %i\n",ID_r);
		}
		else if(size_p == 0){
			num_r = data;
			size_p = 1;
			printf("SIZE: %i\n",num_r);
		}
		else if(type_p == 0){
			type_r = data;
			type_p = 1;
			printf("TYPE: %c\n",type_r);
			if (type_r == 'P'){
				ID_p = 0;
				size_p = 0;
				type_p = 0;
				printf("P RECIEVED\n");
				ServerID = ID_r;
				printf("serverID: %d\n",ServerID);
				float start_time = getTime();
				while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0){
					if (getTime()-start_time >= 60) break;
				}
				alt_up_rs232_read_data(uart, &data, &parity);
				printf("second data byte: %d !!!\n",data);
				printf("NEXT\n");
			}
			else if (type_r == 'I'){
				printf("ID %d,NUM %d,TYPE: %c\n",ID_r,num_r,type_r);
				ID_p = 0;
				size_p = 0;
				type_p = 0;
				IDmessage[0] = ID_r;
				IDmessage[1] = 4;
				IDmessage[2] = 'I';//type
				IDmessage[3] = packetCount;//this should be packetCount
				IDmessage[4] = ID_r;//'1';
				IDmessage[5] = 'C';//this should be checksum
				IDmessage[6] = '\0';
				sendMessage(IDmessage);
				printf("send ID: %d\n",IDmessage[4]);
				printf("sent message: %s\n",IDmessage);
				packetBuffer[packetCount] = packetCount;
				if (packetCount < 10) packetCount ++;
				else packetCount = 1;
				packetTail ++;
			}
		}
		else if ((type_r == 'S') && (pc_p == 0)){
			pc_r = data;
			pc_p = 1;
			packetBuffer[pc_r] = NULL;
			packetHead ++;
		}
		else if ((type_r == 'S') && (bool_p == 0)){
			bool_p = 1;
			bool_r = data;
			printf("ID %i,num %i, type %c, pc %c, bool %c",ID_r,num_r,type_r,pc_r,bool_r);
			ID_p = 0;
			size_p = 0;
			type_p = 0;
			pc_p = 0;
			bool_p = 0;
		}
	}
}
Example #17
0
//DE2 waits for command from Android/middleman
void listen(char * soundfiles[], int numwavfiles, audisr * audmode)
{
	printf("Listening\n");
	int volume_index_up = 10;
	int volume_index_down = 10;
	int func;
	bool headerfound = false;
	int volume;

	clear_fifo();

	//stop_timer();
	start_timer();
	while(1)
	{
		if(rs_flag == true) {
			if (headerfound == false) {

				//Loop check header
				if (rs_flag == true) {
					alt_up_rs232_read_data(uart, &data, &parity);
					if ((int)data == 1) {
						headerfound = true;
						while((int)data == 1) {
							alt_up_rs232_read_data(uart, &data, &parity);
						}
					}
				}
			}

			else {

				//Function get
				alt_up_rs232_read_data(uart, &data, &parity);
				func = (int)data;

				if (func == QUEUE) {
					alt_up_audio_disable_write_interrupt(audio);
					audmode->id = get_fnames(soundfiles,numwavfiles);
					//alt_up_audio_enable_write_interrupt(audio);
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == PLAY) {
					alt_up_audio_enable_write_interrupt(audio);
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == STOP) {
					alt_up_audio_disable_write_interrupt(audio);
					audio_isr_k = 0;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == PAUSE) {
					alt_up_audio_disable_write_interrupt(audio);
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				/*
				else if (func == VOLUME) {
					int volumetemp;

					do {
						while(rs_flag == false);
						alt_up_rs232_read_data(uart, &data, &parity);
						volumetemp = (int)data;
					}while((volumetemp == VOLUME) || (volumetemp == 0));

					//	alt_up_rs232_read_data(uart, &data, &parity);
					volume = volumetemp;

					if (volume == VOL_UP) {
						volume_index_up += 3;
						if (volume_index_up > 127) {
							volume_index_up = 127;
						}
						printf("Up %d\n", volume_index_up);
						audmode->newvolume = volume_index_up;
						volume_index_down = 10;
					}
					else if (volume == VOL_DOWN) {
						volume_index_down--;
						if (volume_index_down < 1) {
							volume_index_down = 1;
						}
						printf("Down %d\n", volume_index_down);
						audmode->newvolume = volume_index_down;
						volume_index_up = 10;
					}
					else {
						printf("Error, unknown volume value %d\n", volume);

					}
					headerfound = false;

					alt_up_rs232_write_data(uart, ACK);

				}
				 */
				else if (func == VOL_UP) {
					volume_index_up += 5;
					if (volume_index_up > 127) {
						volume_index_up = 127;
					}
					printf("Up %d\n", volume_index_up);
					audmode->newvolume = volume_index_up;
					volume_index_down = 10;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == VOL_DOWN) {
					volume_index_down--;
					if (volume_index_down < 1) {
						volume_index_down = 1;
					}
					printf("Down %d\n", volume_index_down);
					audmode->newvolume = volume_index_down;
					volume_index_up = 10;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == SELECT) {
					int index = 0;
					do {
						if (rs_flag == true)
							alt_up_rs232_read_data(uart, &data, &parity);
					}while((int)data == SELECT);
					index = (int)data;
					if ((index < 0) || (index >= numwavfiles)) {
						printf("Error, index out of bounds, playing index 0\n");
						index = 0;
					}
					alt_up_audio_disable_write_interrupt(audio);
					audmode->id = index;
					printf("Changing to song: %s, Id: %d\n", soundfiles[audmode->id], audmode->id);
					alt_up_audio_enable_write_interrupt(audio);
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == S_LOOP) {
					printf("Loop song\n");
					audmode->loop = true;
					audmode->listloop = false;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == Q_LOOP) {
					printf("List loop\n");
					audmode->listloop = true;
					audmode->loop = false;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == C_LOOP) {
					printf("Cancel loops\n");
					audmode->listloop = false;
					audmode->loop = false;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == SEND) {
					send_fnames(soundfiles, numwavfiles);
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == NEXT) {
					alt_up_audio_disable_write_interrupt(audio);
					audmode->id += 1;
					if(audmode->id >= numwavfiles) {
						audmode->id = numwavfiles - 1;
					}
					printf("Changing to song Id: %d\n", audmode->id);
					alt_up_audio_enable_write_interrupt(audio);

					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == PREVIOUS) {
					alt_up_audio_disable_write_interrupt(audio);
					audmode->id -= 1;
					if(audmode->id < 0) {
						audmode->id = 0;
					}
					printf("Changing to song Id: %d\n", audmode->id);
					alt_up_audio_enable_write_interrupt(audio);
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == MIX) {
					audmode->mode = 1;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == SHUFFLE) {
					if(audmode->shuffle == true) {
						audmode->shuffle = false;
					}
					else {
						audmode->shuffle = true;
					}
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if (func == VISUALON) {
					visualflag = !visualflag;
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
				else if ((func != 1) && (func != 0)) {
					printf("Error, Function %d not implemented yet, or invalid\n", (int)data);
					headerfound = false;
					alt_up_rs232_write_data(uart, ACK);
				}
			}
		}
	}
	stop_timer();
}