Project

General

Profile

Revision 336

Updated wireless to use a circular buffer instead of a queue using malloc. Tested on both the computer and robots with a token ring, and was successful.

View differences:

xbee.c
54 54
#include <stdlib.h>
55 55
#include <string.h>
56 56

  
57
#include <queue.h>
58

  
59 57
#define XBEE_FRAME_START 0x7E
60 58

  
61 59
/*Frame Types*/
......
108 106
pthread_t* xbee_listen_thread;
109 107
#endif
110 108

  
111
Queue* xbee_queue;
109
// TODO: is this a good size?
110
#define XBEE_BUFFER_SIZE	256
111
// a buffer for data received from the XBee
112
char arrival_buf[XBEE_BUFFER_SIZE];
113
// location of last unread byte in buffer
114
volatile int buffer_last = 0;
115
// first unread byte in buffer
116
volatile int buffer_first = 0;
112 117

  
118

  
113 119
//used to store packets as they are read
114 120
char xbee_buf[128];
115 121
int currentBufPos = 0;
......
127 133

  
128 134
/**
129 135
 * Interrupt for the robot. Adds bytes received from the xbee
130
 * to the queue.
136
 * to the buffer.
131 137
 **/
132 138
#ifndef FIREFLY
133 139
ISR(USART1_RX_vect)
134 140
{
135 141
	char c = UDR1;
136
	queue_add(xbee_queue, (void*)(int)c);
142
	arrival_buf[buffer_last] = c;
143
	int t = buffer_last + 1;
144
	if (t == XBEE_BUFFER_SIZE)
145
		t = 0;
146
	if (t == buffer_first)
147
	{
148
		WL_DEBUG_PRINT("Out of space in buffer.\n");
149
	}
150
	buffer_last = t;
137 151
}
138 152
#else
139 153
SIGNAL(SIG_USART0_RECV)
140 154
{
141 155
	char c = UDR0;
142
	queue_add(xbee_queue, (void*)(int)c);
156
	arrival_buf[buffer_last] = c;
157
	int t = buffer_last + 1;
158
	if (t == XBEE_BUFFER_SIZE)
159
		t = 0;
160
	if (t == buffer_first)
161
	{
162
		WL_DEBUG_PRINT("Out of space in buffer.\n");
163
	}
164
	buffer_last = t;
143 165
}
144 166
#endif
145 167

  
......
154 176
	while (1)
155 177
	{
156 178
		xbee_read(&c, 1);
157
		queue_add(xbee_queue, (void*)(int)c);
179
		arrival_buf[buffer_last] = c;
180
		int t = buffer_last + 1;
181
		if (t == XBEE_BUFFER_SIZE)
182
			t = 0;
183
		if (t == buffer_first)
184
		{
185
			WL_DEBUG_PRINT("Out of space in buffer.\n");
186
		}
187
		buffer_last = t;
158 188
	}
159 189
	return 0;
160 190
}
......
166 196
 **/
167 197
int xbee_lib_init(void)
168 198
{
169
	xbee_queue = queue_create();
170
	if (xbee_queue == NULL) {
171
		return -1;
172
	}
173
	
199
	arrival_buf[0] = 'A';
200
	arrival_buf[1] = 'A';
201
	arrival_buf[2] = 'A';
174 202
	#ifdef ROBOT
175 203

  
176 204
	//enable the receiving interrupt
......
255 283
	lockf(xbee_stream, F_ULOCK, 0);
256 284
	close(xbee_stream);
257 285
	#endif
258
	queue_destroy(xbee_queue);
259 286
}
260 287

  
261 288
/**
......
366 393
	char* curr = s;
367 394
	while (curr - s < len)
368 395
	{
369
		if (queue_is_empty(xbee_queue))
396
		// check if buffer is empty
397
		if (buffer_last == buffer_first)
370 398
			continue;
371
		char c = (char)(int)queue_remove(xbee_queue);
399
		char c = arrival_buf[buffer_first++];
400
		if (buffer_first == XBEE_BUFFER_SIZE)
401
			buffer_first = 0;
372 402
		if (c == *curr)
373 403
			curr++;
374 404
		else
......
576 606
	if (currentBufPos == 0)
577 607
	{
578 608
		do
579
			if (queue_is_empty(xbee_queue))
609
		{
610
			if (buffer_first == XBEE_BUFFER_SIZE)
611
				buffer_first = 0;
612
			// check if buffer is empty
613
			if (buffer_first == buffer_last)
580 614
				return -1;
581
		while ((char)(int)queue_remove(xbee_queue) != XBEE_FRAME_START);
615
		}
616
		while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
617
		if (buffer_first == XBEE_BUFFER_SIZE)
618
			buffer_first = 0;
582 619
		xbee_buf[0] = XBEE_FRAME_START;
583 620
		currentBufPos++;
584 621
	}
......
600 637
				return -1;
601 638
			}
602 639
		}
603
		if (queue_is_empty(xbee_queue))
640
		// check if buffer is empty
641
		if (buffer_first == buffer_last)
604 642
			return -1;
605
		xbee_buf[currentBufPos++] = (char)(int)queue_remove(xbee_queue);
643
		xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
644
		if (buffer_first == XBEE_BUFFER_SIZE)
645
			buffer_first = 0;
606 646
	}
607 647
	
608 648
	currentBufPos = 0;

Also available in: Unified diff