Revision 717
added a bunch of TODO comments that will improve the style, efficiency, and possibly memory usage of the wireless library
sensor_matrix.c | ||
---|---|---|
40 | 40 |
|
41 | 41 |
#define DEFAULT_SENSOR_MATRIX_SIZE 20 |
42 | 42 |
|
43 |
|
|
44 |
//TODO: can a robot ever have more than one sensor matrix? |
|
45 |
// Can there ever be more than one sensor matrix object in existance at one time? |
|
46 |
// If not, then why not make a global sensor matrix variable. Then you don't have |
|
47 |
// to pass the sensor matrix into each of these functions. |
|
48 |
// Not passing in the sensor matrix object into all these functions will save a |
|
49 |
// couple ops before each call to one of these functions because it will no longer have |
|
50 |
// to set up passing this as a parameter. However, with a global variable, the |
|
51 |
// function must, at some point, load the address of the global variable into a register |
|
52 |
// so this may or may not actually save code space. |
|
53 |
|
|
54 |
|
|
43 | 55 |
/*Sensor Matrix Functions*/ |
44 | 56 |
static void sensor_matrix_expand(SensorMatrix* m, int nextSize); |
45 | 57 |
|
... | ... | |
53 | 65 |
SensorMatrix* m; |
54 | 66 |
int i; |
55 | 67 |
|
68 |
//TODO: shouldn't use malloc on embedded systems if avoidable |
|
69 |
// Using malloc may save us SRAM usage because you don't need to allocate the second |
|
70 |
// dimension unless needed, but it increases the code size because of the malloc |
|
71 |
// code and introduces possibilities of memory leaks. Making a static 2 dimensional array |
|
72 |
// will increase sram usage but simplify code. The size of this matrix can be set low to |
|
73 |
// reduce memory consumption and increased (and code recompiled) as more robots are constructed |
|
74 |
// and need to be used |
|
56 | 75 |
m = (SensorMatrix*)malloc(sizeof(SensorMatrix)); |
57 | 76 |
if (!m) |
58 | 77 |
{ |
... | ... | |
113 | 132 |
void sensor_matrix_add_robot(SensorMatrix* m, int id) |
114 | 133 |
{ |
115 | 134 |
int i; |
135 |
|
|
136 |
//TODO: instead of expanding the sensor matrix, since this is a memory constrained system |
|
137 |
// devise a new way such that the size of the matrix is independent of the id values |
|
116 | 138 |
if (id >= m->size) { |
117 | 139 |
sensor_matrix_expand(m, id + 1); |
118 | 140 |
} |
... | ... | |
169 | 191 |
* @param m the sensor matrix to expand |
170 | 192 |
* @param size the new size of the sensor matrix |
171 | 193 |
**/ |
194 |
//TODO: remove this method when the SensorMatrix is made a static array and doesn't use malloc anymore |
|
195 |
// and after it has been changed so the size of the matrix does not depend on the values of the ids of |
|
196 |
// the robots. |
|
172 | 197 |
//Note: this has probably not been tested, hopefully it works |
173 | 198 |
static void sensor_matrix_expand(SensorMatrix* m, int nextSize) |
174 | 199 |
{ |
... | ... | |
182 | 207 |
return; |
183 | 208 |
} |
184 | 209 |
|
210 |
//TODO: using memset here instead of this loop, *might* be less instructions and *might* reduce code size but not sure |
|
185 | 211 |
for (i = 0; i < nextSize; i++) |
186 | 212 |
tempMatrix[i] = NULL; |
187 | 213 |
|
... | ... | |
272 | 298 |
* @param robot the robot to set as a member / nonmember of the token ring |
273 | 299 |
* @param in 1 if the robot is in the token ring, 0 otherwise |
274 | 300 |
**/ |
301 |
//TODO: add comments inside this function |
|
275 | 302 |
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in) |
276 | 303 |
{ |
277 | 304 |
if (robot >= m->size) { |
... | ... | |
282 | 309 |
sensor_matrix_add_robot(m, robot); |
283 | 310 |
} |
284 | 311 |
|
312 |
//TODO: does this mean you are adding the robot? |
|
285 | 313 |
if (in == 1 && m->joined[robot] == 0) { |
286 | 314 |
m->numJoined++; |
287 | 315 |
} |
288 | 316 |
|
317 |
//TODO: does this mean you are removing the robot? |
|
289 | 318 |
if (in == 0 && m->joined[robot] == 1) { |
290 | 319 |
m->numJoined--; |
291 | 320 |
} |
... | ... | |
317 | 346 |
* |
318 | 347 |
* @return the size of the sensor matrix |
319 | 348 |
**/ |
349 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
350 |
// it reduces code size or not should be done to be sure. |
|
320 | 351 |
int sensor_matrix_get_size(SensorMatrix* m) |
321 | 352 |
{ |
322 | 353 |
return m->size; |
... | ... | |
330 | 361 |
* |
331 | 362 |
* @return the number of robots in the token ring |
332 | 363 |
**/ |
364 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
365 |
// it reduces code size or not should be done to be sure. |
|
333 | 366 |
int sensor_matrix_get_joined(SensorMatrix* m) |
334 | 367 |
{ |
335 | 368 |
return m->numJoined; |
Also available in: Unified diff