//External reference to transmit ring buffer extern unsigned char t_buffer[]; // transmit ring buffer extern unsigned char t_start; // ring start extern unsigned char t_end; // ring end extern unsigned char interfaceStyle; // Human readable or Machine readable #define RXenable UCSRB.7 #define TXenable UCSRB.5 //******************* Communication *********************** // Add a single character to the transmit buffer void AddTx(char c){ unsigned char newt_end; // if the buffer is full, block operation newt_end = (t_end+1)%RINGSIZE; while(newt_end == t_start) {} // add data and update counter t_buffer[t_end] = c; t_end = newt_end; TXenable=1; } // Adds a string to the transmit buffer void WriteSerString(char *ws) { // wsp will be destroyed during addition to the transmit buffer char *wsp; strcpy(wsp,ws); // add wsp to buffer by element while(*wsp) AddTx(*wsp++); } // Adds a numerical byte as hex to the transmit buffer void WriteSerHex(char character) { char c; c = character>>4; if (c>9) c = c + '0' + 7; else c = c + '0'; AddTx(c); c = (character << 4) >> 4; if (c>9) c = c + '0' + 7; else c = c + '0'; AddTx(c); } // Adds a number to the transmit buffer void WriteSerNumber(long num) { unsigned long posnum; char number[16]; int j = 16; number[--j] = '\0'; if (num < 0) { posnum = (unsigned long) -(num+1); posnum++; } else { posnum = (unsigned long) num; } number[--j] = ('0' + (char)(posnum % 10)); posnum /= 10; while(posnum != 0) { number[--j] = ('0' + (char) (posnum % 10)); posnum /= 10; } if (num < 0) number[--j] = '-'; WriteSerString(&number[j]); } // Adds a number to the transmit buffer void WriteSerInt(int num) { unsigned int posnum; char number[16]; int j = 16; number[--j] = '\0'; if (num < 0) { posnum = (unsigned long) -(num+1); posnum++; } else { posnum = (unsigned long) num; } number[--j] = ('0' + (char)(posnum % 10)); posnum /= 10; while(posnum != 0) { number[--j] = ('0' + (char) (posnum % 10)); posnum /= 10; } if (num < 0) number[--j] = '-'; WriteSerString(&number[j]); } // Adds a data packet to the transmit buffer void WriteSerPacket(int time, char* datapacket){ unsigned char i; if(interfacestyle == 'h'){ AddTx('8'); AddTx(' '); WriteSerInt(time); AddTx(' '); for (i=1; i<=5; i++) { WriteSerHex(datapacket[i]); AddTx(' '); } AddTx('\r'); AddTx('\n'); } else if (interfacestyle == 'm') { AddTx(8); AddTx((time>>8)); AddTx((time<<8)>>8); AddTx(datapacket[1]); AddTx(datapacket[2]); AddTx(datapacket[3]); AddTx(datapacket[4]); AddTx(datapacket[5]); } }