Project

General

Profile

Revision 886

Synchronized trunk and bayboard versions.

View differences:

branches/autonomous_recharging/code/projects/libwireless/lib/queue.c
1
#include "queue.h"
2
#include <wl_defs.h>
3

  
4
#include <stdlib.h>
5
#include <stdio.h>
6

  
7
/**
8
 * @struct node_def
9
 * A node in the queue.
10
 **/
11
typedef struct node_def
12
{
13
	/** The item at this point in the queue. **/
14
	void* val;
15
	/** The next node in the queue. **/
16
	struct node_def* next;
17
} Node;
18

  
19
/**
20
 * Create a queue.
21
 *
22
 * @return the newly created queue.
23
 **/
24
Queue* queue_create()
25
{
26
	Queue* q = (Queue*)malloc(sizeof(Queue));
27
	q->head = NULL;
28
	q->size = 0;
29
	return q;
30
}
31

  
32
/**
33
 * Destroys a queue, freeing memory.
34
 *
35
 * @param q the queue to destroy
36
 **/
37
void queue_destroy(Queue* q)
38
{
39
	Node* t = q->head;
40
	while (t != NULL)
41
	{
42
		Node* t2 = t->next;
43
		free(t);
44
		t = t2;
45
	}
46
	free(q);
47
}
48

  
49
/**
50
 * Add an element to a queue.
51
 *
52
 * @param q the queue to add an element to
53
 * @param item the item to add to the queue
54
 **/
55
void queue_add(Queue* q, void* item)
56
{
57
	Node* n = (Node*)malloc(sizeof(Node));
58
	n->val = item;
59
	n->next = NULL;
60
	
61
	if (q->head == NULL)
62
	{
63
		q->head = n;
64
		q->tail = n;
65
	}
66
	else
67
	{
68
		q->tail->next = n;
69
		q->tail = n;
70
	}
71
	
72
	q->size++;
73
}
74

  
75
/**
76
 * Remove an element from the front of a queue.
77
 *
78
 * @param q the queue to remove the element from
79
 *
80
 * @return the element which was removed
81
 **/
82
void* queue_remove(Queue* q)
83
{
84
	Node* first = q->head;
85
	if (first == NULL)
86
	{
87
		WL_DEBUG_PRINT("Attempting to remove item \
88
			from empty queue.\r\n");
89
		WL_DEBUG_PRINT_INT(queue_size(q));
90
	}
91
	void* ret = first->val;
92
	q->head = first->next;
93
	if (q->tail == first)
94
		q->tail = NULL;
95
	free(first);
96
	q->size--;
97
	return ret;
98
}
99

  
100
/**
101
 * Remove all instances of a given element from a queue.
102
 *
103
 * @param q the queue to remove the elements from
104
 * @param item the element to remove all instances of
105
 **/
106
void queue_remove_all(Queue* q, void* item)
107
{
108
	Node* curr = q->head;
109
	Node* prev = NULL;
110
	
111
	while (curr != NULL)
112
	{
113
		Node* next = curr->next;
114
		if (curr->val == item)
115
		{
116
			if (q->head == curr)
117
				q->head = next;
118
			if (q->tail == curr)
119
				q->tail = prev;
120
			if (prev != NULL)
121
				prev->next = next;
122
			free(curr);
123
			q->size--;
124
		}
125
		else
126
			prev = curr;
127
		curr = next;
128
	}
129
}
130

  
131
/**
132
 * Get the number of elements in the queue.
133
 *
134
 * @param q the queue to get the size of
135
 * @return the size of the queue
136
 **/
137
int queue_size(Queue* q)
138
{
139
	return q->size;
140
}
141

  
142
/**
143
 * Check if the queue is empty.
144
 * 
145
 * @param q the queue to check
146
 * @return nonzero if the queue is empty, 0 otherwise
147
 **/
148
int queue_is_empty(Queue* q)
149
{
150
	return q->size == 0;
151
}
152

  
branches/autonomous_recharging/code/projects/libwireless/lib/queue.h
1
/**
2
 * @file queue.h
3
 * @brief A queue implementation
4
 * 
5
 * Implements a queue, a first in, first out data structure.
6
 *
7
 * @author Brian Coltin, Colony Project
8
 **/
9

  
10
struct node_def;
11

  
12
/**
13
 * @defgroup queue Queue
14
 * @brief A queue implementation
15
 * 
16
 * A queue implementation.
17
 *
18
 * @{
19
 **/
20

  
21
/**
22
 * @struct Queue
23
 * Represents a queue, a first in, first out data structure.
24
 **/
25
typedef struct
26
{
27
	/**
28
	 * The head of the queue, the next item to be removed.
29
	 **/
30
	struct node_def* head;
31
	/**
32
	 * The tail of the queue, the last item added.
33
	 **/
34
	struct node_def* tail;
35
	/**
36
	 * The number of elements in the queue.
37
	 **/
38
	int size;
39
} Queue;
40

  
41
/** @brief Create a new queue **/
42
Queue* queue_create(void);
43
/** @brief Destroy a queue **/
44
void queue_destroy(Queue* q);
45
/** @brief Add an element to a queue **/
46
void queue_add(Queue* q, void* item);
47
/** @brief Remove an element from a queue **/
48
void* queue_remove(Queue* q);
49
/** @brief Remove all instances of a given element from a queue **/
50
void queue_remove_all(Queue* q, void* item);
51
/** @brief Get the size of a queue **/
52
int queue_size(Queue* q);
53
/** @brief Check if the queue is empty **/
54
int queue_is_empty(Queue* q);
55

  
56
/** @} **/
57

  
branches/autonomous_recharging/code/projects/libwireless/lib/wl_token_ring.c
713 713
			j++;
714 714
		}
715 715
	}
716

  
716 717
	int packetSize = 2 * j * sizeof(char);
717 718
	WL_DEBUG_PRINT("Passing the token to robot ");
718 719
	WL_DEBUG_PRINT_INT(nextRobot);
......
778 779
	WL_DEBUG_PRINT("Our BOM has been flashed.\r\n");
779 780
	wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, NULL, 0, 0);
780 781

  
781
	//bom_on_function();
782
	bom_on_function();
782 783
	#ifdef ROBOT
783 784
	delay_ms(BOM_DELAY);
784 785
	#endif
branches/autonomous_recharging/code/projects/libwireless/lib/sensor_matrix.h
68 68
	 * is in the token ring and false otherwise.
69 69
	 **/
70 70
	unsigned char joined[MAXIMUM_XBEE_ID];
71

  
72
	// on the bayboard, we don't include the matrix to save memory.
73
#ifndef BAYBOARD
74
	/**
75
	 * The matrix. Each row represents the readings of one
76
	 * robot.
77
	 **/
78
	unsigned char matrix[MAXIMUM_XBEE_ID][MAXIMUM_XBEE_ID];
79
#endif
71 80
} SensorMatrix;
72 81

  
73 82
/**@brief Create a sensor matrix **/
branches/autonomous_recharging/code/projects/libwireless/lib/sensor_matrix.c
55 55
	for (i = 0; i < MAXIMUM_XBEE_ID; i++)
56 56
	{
57 57
		m.joined[i] = 0;
58
#ifndef BAYBOARD
59
		for (j = 0; j < MAXIMUM_XBEE_ID; j++)
60
			m.matrix[i][j] = READING_UNKNOWN;
61
#endif
58 62
	}
59 63
}
60 64

  
......
67 71
 */
68 72
void sensor_matrix_set_reading(int observer, int robot, int reading)
69 73
{
74
#ifndef BAYBOARD
75
	if (robot >= MAXIMUM_XBEE_ID || observer >= MAXIMUM_XBEE_ID)
76
	{
77
		WL_DEBUG_PRINT("ID too large.");
78
		return;
79
	}
70 80

  
81
	m.matrix[observer][robot] = (unsigned char)reading;
82
#endif
71 83
}
72 84

  
73 85
/**
......
80 92
 **/
81 93
int sensor_matrix_get_reading(int observer, int robot)
82 94
{
95
#ifndef BAYBOARD
96
	if (observer >= MAXIMUM_XBEE_ID || robot >= MAXIMUM_XBEE_ID)
97
		return -1;
98

  
99
	return (int)m.matrix[observer][robot];
100
#else
83 101
	return -1;
102
#endif
84 103
}
85 104

  
86 105
/**
......
93 112
{
94 113
	if (robot >= MAXIMUM_XBEE_ID)
95 114
	{
96
		usb_puts("ID too large.\n\r");
97 115
		WL_DEBUG_PRINT("ID too large.");
98 116
		return;
99 117
	}
branches/autonomous_recharging/code/projects/libwireless/lib/wireless.c
116 116
	rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
117 117
#else
118 118
	//TODO: FIX THIS
119
	#ifdef BAYBOARD
119 120
	rtc_init(10 * HALF_SECOND, &timer_handler);
121
	#else
122
	rtc_init(HALF_SECOND, &timer_handler);
123
	#endif
120 124
#endif
121 125
#else
122 126

  

Also available in: Unified diff