Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <stdio.h>
- #include <string.h>
- #include "config.h"
- //#include "lcd.h"
- //////////////////SERVICE////BOOTH/////////////////////////////
- int head,tail;
- int maxsize;
- char q[100][20];
- bool isEmpty()
- {
- if (head==tail) return true;
- return false;
- }
- bool isFull()
- {
- if ((head+1)%maxsize==tail) return true;
- return false;
- }
- bool push(char indata[])
- {
- if (isFull()) return true;
- strcpy(q[head],indata);
- head=(head+1)%maxsize;
- return true;
- }
- char* front()
- {
- if (isEmpty()) return NULL;
- return q[tail];
- }
- bool pop()
- {
- if (isEmpty()) return false;
- tail=(tail+1)%maxsize;
- return true;
- }
- int size()
- {
- int ret=head-tail;
- if (ret<0) ret+=maxsize;
- return ret;
- }
- typedef struct
- {
- uchar rtype; //receiver type
- uchar rid; //receiver id
- uchar stype; //sender type
- uchar sid; //sender id
- uchar command; //command type
- char data[5]; //main data
- char checksum[6]; //checksum
- uchar ebit; // Enzam bit
- }DATA;
- void printData(DATA data)
- {
- printf("rtype: %c\n\r",data.rtype);
- printf("rid: %c\n\r",data.rid);
- printf("stype: %c\n\r",data.stype);
- printf("sid: %c\n\r",data.sid);
- printf("command: %c\n\r",data.command);
- //printf("data: %s\n\r",data.data);
- }
- //01 23 4 56789 012345 6
- DATA perseData(char data[])
- {
- printf("got - %s\n\r",data);
- int i;
- DATA ret;
- ret.rtype=data[0];
- ret.rid=data[1];
- ret.stype=data[2];
- ret.sid=data[3];
- ret.command=data[4];
- for (i=0; i<5; i++) ret.data[i]=data[i+5];
- for (i=0; i<6; i++) ret.checksum[i]=data[i+10];
- ret.ebit=data[16];
- return ret;
- }
- ////////////////////process//////////////////////
- void processData(DATA data)
- {
- //NOISE//
- if (data.rtype=='S' && data.rid==SELFID)
- {
- printf("YES i am that guy\n\r");
- printData(data);
- }
- else return ;
- switch(data.command)
- {
- case STB_SERVICE_INIT:
- break;
- case STB_SERVICE_INIT_REPLY:
- //if data.data then show to lcd
- //else work.push(nextuser);
- //work.pop()
- if (!strcmp((char*)data.data,"-----"))
- {
- //LCDWriteString("Why ??");
- }
- else
- {
- char s[20];
- sprintf(s,"PCS%c%d------------",SELFID,STB_SERVICE_NEXTTOKEN);
- push(s);
- }
- pop();
- break;
- case STB_SERVICE_NEWUSER:
- break;
- case STB_SERVICE_NEWUSER_REPLY:
- break;
- case STB_SERVICE_NEXTTOKEN:
- break;
- case STB_SERVICE_NEXTTOKEN_REPLY:
- //if data.data then
- //{ show to lcd
- // work.pop()
- //}
- //else show --lcd
- if (strcmp(data.data,"------"))
- {
- //LCDWriteString("Why ??");
- pop();
- }
- else
- {
- //char s[20];
- //sprintf(s,"PCS%c%d------------",SELFID,STB_SERVICE_NEXTTOKEN);
- // show -- lcd
- }
- break;
- case STB_SERVICE_PING:
- //printf("%s\n\r",workq);
- printf("%s",front());
- break;
- case STB_SERVICE_PING_REPLY:
- break;
- }
- }
- //========================================================
- int SendCmd(char cmd,FILE *stream)
- {
- //Do nothing until UDR is ready for more data to be written to it
- while ((UCSRA & (1 << UDRE)) == 0x00) ;
- /* Copy 9th bit to TXB8 */
- UCSRB &= ~(1<<TXB8);
- if ( cmd & 0x0100 )
- UCSRB |= (1<<TXB8);
- /* Put data into buffer, sends the data */
- UDR = cmd; // Echo back the received byte back to the computer
- return 0;
- }
- int RecieveCmd(FILE *stream)
- {
- unsigned char ReceivedByte;
- while ((UCSRA & (1 << RXC)) == 0x00) {} // Do nothing until data have been recieved and is ready to be read from UDR
- ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived"
- return ReceivedByte;
- }
- //*******************************************/
- ISR(USART_RXC_vect){ // Handler for RXD interrupt
- uchar in = UDR;
- char m[20];
- int i;
- //for (i=1; i<17; i++) scanf("%c",&m[i]);
- gets(m+1);
- m[0]=in;
- DATA buffer=perseData(m);
- processData(buffer);
- }
- /*/////////////////BUTTON/////INT////////////////////
- INT0
- work.push(nextuser);
- ///////////////////////////////////////////////////*/
- int main(){
- DDRB = 0xFF;
- DDRC = 0xFF;
- DDRD &= ~1;
- DDRD |= 2;
- //LCDInit(LS_NONE);
- //LCDClear();
- //GICR |= (1<<INT0);
- //MCUCR |= (1<<ISC00) | (1<<ISC01);
- sei();
- //SERIAL PORT
- UCSRB |= (1 << RXEN) | (1 << TXEN) | (1<<RXCIE); // Turn on the transmission and reception circuitry
- UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1) ; // Use 8-bit character sizes
- UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
- UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
- stdout = fdevopen(SendCmd,NULL);
- stdin = fdevopen(NULL,RecieveCmd);
- //printf("Welcome!\n\rThis is develop by Nayeem, Enzam, Shafiul ,Hira& Dipal.\n\rMachineStarted\n\r 0-INIT\n\r 1-SETvalue\n\r 2-STAT\n\r");
- //printf("Welcome! This transmitter will transmit nothing\n\rZZZZZ\n\r");
- head=tail=0;
- maxsize=100;
- char s[20];
- sprintf(s,"PCS%c%d------------",SELFID,STB_SERVICE_INIT);
- push(s);
- while(1){
- //work.push(init);
- printf("i am aLIVE %c\n\r",SELFID);
- _delay_ms(7000);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment