Project

General

Profile

Revision 418

added return values to a bunch of libwireless functions. Makefile for colonetserver now compiles libwireless when necessary. added static to a bunch of libwireless vars. commented out colonet timeout

View differences:

sensor_matrix.c
1 1
/**
2 2
 * Copyright (c) 2007 Colony Project
3
 * 
3
 *
4 4
 * Permission is hereby granted, free of charge, to any person
5 5
 * obtaining a copy of this software and associated documentation
6 6
 * files (the "Software"), to deal in the Software without
......
9 9
 * copies of the Software, and to permit persons to whom the
10 10
 * Software is furnished to do so, subject to the following
11 11
 * conditions:
12
 * 
12
 *
13 13
 * The above copyright notice and this permission notice shall be
14 14
 * included in all copies or substantial portions of the Software.
15
 * 
15
 *
16 16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
......
41 41
#define DEFAULT_SENSOR_MATRIX_SIZE 20
42 42

  
43 43
/*Sensor Matrix Functions*/
44
void sensor_matrix_expand(SensorMatrix* m, int nextSize);
44
static void sensor_matrix_expand(SensorMatrix* m, int nextSize);
45 45

  
46 46
/**
47 47
 * Initializes the sensor matrix.
......
52 52
{
53 53
	SensorMatrix* m;
54 54
	int i;
55
	
55

  
56 56
	m = (SensorMatrix*)malloc(sizeof(SensorMatrix));
57 57
	if (!m)
58 58
	{
......
79 79
		WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n");
80 80
		return NULL;
81 81
	}
82
	
82

  
83 83
	for (i = 0; i < m->size; i++)
84 84
	{
85 85
		m->matrix[i] = NULL;
......
106 106

  
107 107
/**
108 108
 * Adds robot with XBee id id to the sensor matrix.
109
 * 
109
 *
110 110
 * @param m the sensor matrix
111 111
 * @param id the XBee ID of the robot to add
112 112
 **/
113 113
void sensor_matrix_add_robot(SensorMatrix* m, int id)
114 114
{
115 115
	int i;
116
	if (id >= m->size)
116
	if (id >= m->size) {
117 117
		sensor_matrix_expand(m, id + 1);
118
	if (m->matrix[id] != NULL)
118
	}
119

  
120
	if (m->matrix[id] != NULL) {
119 121
		return;
120
	
122
	}
123

  
121 124
	m->matrix[id] = (int*)malloc(m->size * sizeof(int));
122 125
	if (!(m->matrix[id]))
123 126
	{
......
125 128
		return;
126 129
	}
127 130

  
128
	for (i = 0; i < m->size; i++)
129
		if (m->matrix[i] != NULL)
131
	for (i = 0; i < m->size; i++) {
132
		if (m->matrix[i] != NULL) {
130 133
			m->matrix[i][id] = -1;
134
		}
135
	}
131 136
}
132 137

  
133 138
/**
......
146 151
		WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
147 152
		return;
148 153
	}
149
	
154

  
150 155
	free(m->matrix[id]);
151 156
	m->matrix[id] = NULL;
152
	
157

  
153 158
	for (i = 0 ; i < m->size; i++)
154 159
		if (m->matrix[i] != NULL)
155 160
			m->matrix[i][id] = -1;
156
	
161

  
157 162
	m->joined[id] = 0;
158 163
}
159 164

  
......
165 170
 * @param size the new size of the sensor matrix
166 171
 **/
167 172
//Note: this has probably not been tested, hopefully it works
168
void sensor_matrix_expand(SensorMatrix* m, int nextSize)
173
static void sensor_matrix_expand(SensorMatrix* m, int nextSize)
169 174
{
170 175
	int i, j;
171 176
	WL_DEBUG_PRINT("Expanding sensor matrix.\r\n");
172
	
177

  
173 178
	int** tempMatrix = (int**)malloc(nextSize * sizeof(int*));
174 179
	if (!tempMatrix)
175 180
	{
176 181
		WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n");
177 182
		return;
178 183
	}
179
	
184

  
180 185
	for (i = 0; i < nextSize; i++)
181 186
		tempMatrix[i] = NULL;
182
	
187

  
183 188
	//copy over old sensor data
184 189
	for (i = 0; i < m->size; i++)
185 190
		if (m->matrix[i] != NULL)
......
190 195
				WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n");
191 196
				return;
192 197
			}
193
			for (j = 0; j < m->size; j++)
198
			for (j = 0; j < m->size; j++) {
194 199
				tempMatrix[i][j] = m->matrix[i][j];
195
			for (j = m->size; j < nextSize; j++)
200
			}
201

  
202
			for (j = m->size; j < nextSize; j++) {
196 203
				tempMatrix[i][j] = -1;
197
			
204
			}
205

  
198 206
			free(m->matrix[i]);
199 207
		}
200
	
208

  
201 209
	free(m->matrix);
202 210
	m->matrix = tempMatrix;
203 211
	m->size = nextSize;
......
209 217
		WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n");
210 218
		return;
211 219
	}
212
	
213
	for (i = 0; i < m->size; i++)
220

  
221
	for (i = 0; i < m->size; i++) {
214 222
		tempJoined[i] = m->joined[i];
215
	for (i = m->size; i < nextSize; i++)
223
	}
224

  
225
	for (i = m->size; i < nextSize; i++) {
216 226
		tempJoined[i] = 0;
217
	
227
	}
228

  
218 229
	free(m->joined);
219 230
	m->joined = tempJoined;
220 231
}
221 232

  
222 233
/**
223 234
 * Sets the sensor reading for robot robot to reading.
224
 * 
235
 *
225 236
 * @param m the sensor matrix to set the reading for
226 237
 * @param observer the id of the robot who made the reading
227 238
 * @param robot the id of the robot who the reading is for
......
229 240
 */
230 241
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading)
231 242
{
232
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL)
243
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) {
233 244
		sensor_matrix_add_robot(m, observer);
245
	}
246

  
234 247
	m->matrix[observer][robot] = reading;
235 248
}
236 249

  
......
245 258
 **/
246 259
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
247 260
{
248
	if (observer >= m->size || robot >= m->size)
261
	if (observer >= m->size || robot >= m->size) {
249 262
		return -1;
263
	}
264

  
250 265
	return m->matrix[observer][robot];
251 266
}
252 267

  
......
259 274
 **/
260 275
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
261 276
{
262
	if (robot >= m->size)
277
	if (robot >= m->size) {
263 278
		sensor_matrix_expand(m, robot + 1);
264
	if (in == 1)
279
	}
280

  
281
	if (in == 1) {
265 282
		sensor_matrix_add_robot(m, robot);
266
	if (in == 1 && m->joined[robot] == 0)
283
	}
284

  
285
	if (in == 1 && m->joined[robot] == 0) {
267 286
		m->numJoined++;
268
	if (in == 0 && m->joined[robot] == 1)
287
	}
288

  
289
	if (in == 0 && m->joined[robot] == 1) {
269 290
		m->numJoined--;
291
	}
292

  
270 293
	m->joined[robot] = in;
271 294
}
272 295

  
273 296
/**
274 297
 * Checks if the given robot is in the token ring.
275
 * 
298
 *
276 299
 * @param m the sensor matrix
277 300
 * @param robot the ID of the robot to check
278 301
 *
......
280 303
 **/
281 304
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
282 305
{
283
	if (robot >= m->size)
306
	if (robot >= m->size) {
284 307
		return -1;
308
	}
309

  
285 310
	return m->joined[robot];
286 311
}
287 312

  
......
289 314
 * Returns the size of the sensor matrix.
290 315
 *
291 316
 * @param m the sensor matrix
292
 * 
317
 *
293 318
 * @return the size of the sensor matrix
294 319
 **/
295 320
int sensor_matrix_get_size(SensorMatrix* m)
......
309 334
{
310 335
	return m->numJoined;
311 336
}
312

  

Also available in: Unified diff