Revision 1456 branches/colonetmk2/code/projects/libwireless/lib/wl_token_ring.c

View differences:

wl_token_ring.c
35 35
#include <wl_token_ring.h>
36 36

  
37 37
#include <stdlib.h>
38
#include <stdio.h>
39 38

  
40 39
#include <wl_defs.h>
41 40
#include <wireless.h>
......
107 106
// the amount of time a robot has had its BOM on for
108 107
static int bom_on_count = 0;
109 108

  
110
#ifndef ROBOT
109
#if (!(defined ROBOT)) || (defined FIREFLY)
111 110
static void do_nothing(void) {}
112
static int get_nothing(void) {return -1;}
111
static SensorReading get_nothing(void) { SensorReading ret = { READING_UNKNOWN, READING_UNKNOWN }; return ret;}
112
#else
113
static SensorReading default_get_bom(void) { int dist, dir; bom_refresh(BOM_ALL); dir = bom_get_max10(&dist); SensorReading ret = { dir, dist}; return ret; }
113 114
#endif
114 115

  
115 116
#ifdef ROBOT
116 117
#ifndef FIREFLY
117 118
static void (*bom_on_function) (void) = bom_on;
118 119
static void (*bom_off_function) (void) = bom_off;
119
static int (*get_max_bom_function) (void) = get_max_bom;
120
static SensorReading (*get_max_bom_function) (void) = default_get_bom;
120 121
#else
121 122
static void (*bom_on_function) (void) = do_nothing;
122 123
static void (*bom_off_function) (void) = do_nothing;
123
static int (*get_max_bom_function) (void) = get_nothing;
124
static SensorReading (*get_max_bom_function) (void) = get_nothing;
124 125
#endif
125 126
#else
126 127
static void (*bom_on_function) (void) = do_nothing;
127 128
static void (*bom_off_function) (void) = do_nothing;
128
static int (*get_max_bom_function) (void) = get_nothing;
129
static SensorReading (*get_max_bom_function) (void) = get_nothing;
129 130
#endif
130 131

  
131 132
static PacketGroupHandler wl_token_ring_handler =
......
211 212
 * measurement of the maximum BOM reading is needed.
212 213
 **/
213 214
void wl_token_ring_set_bom_functions(void (*on_function) (void),
214
	void (*off_function) (void), int (*max_bom_function) (void))
215
	void (*off_function) (void), SensorReading (*max_bom_function) (void))
215 216
{
216 217
	bom_on_function = on_function;
217 218
	bom_off_function = off_function;
......
344 345
 * @return a BOM reading from robot source to robot dest,
345 346
 * in the range 0-15, or -1 if it is unknown
346 347
 **/
347
int wl_token_get_sensor_reading(int source, int dest)
348
SensorReading * wl_token_get_sensor_reading(int source, int dest)
348 349
{
349 350
	if (wl_token_is_robot_in_ring(dest) &&
350 351
			(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) {
351 352
		return sensor_matrix_get_reading(source, dest);
352 353
	}
353 354

  
354
	return -1;
355
	return NULL;
355 356
}
356 357

  
357 358
/**
......
364 365
 **/
365 366
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
366 367
// it reduces code size or not should be done to be sure.
367
int wl_token_get_my_sensor_reading(int dest)
368
SensorReading * wl_token_get_my_sensor_reading(int dest)
368 369
{
369 370
	return wl_token_get_sensor_reading(wl_get_xbee_id(), dest);
370 371
}
......
530 531
{
531 532
	int i, j;
532 533
	char nextRobot;
534
	SensorReading r;
533 535

  
534 536
	bom_on_count = -1;
535 537
	deathDelay = -1;
......
549 551
		}
550 552

  
551 553
		//set the sensor information we receive
552
		if (j < sensorDataLength / 2 && sensorData[2 * j] == i)
554
		if (j < sensorDataLength / 2 && sensorData[3 * j] == i)
553 555
		{
554 556
			//the robot we were going to accept has already been accepted
555 557
			if (accepted == i)
......
557 559
				accepted = -1;
558 560
				WL_DEBUG_PRINT("Someone accepted the robot we did.\r\n");
559 561
			}
560
			sensor_matrix_set_reading(source, i,
561
						sensorData[2 * j + 1]);
562
			r.dir = sensorData[3 * j + 1];
563
			r.dist = sensorData[3 * j + 2];
564
			sensor_matrix_set_reading(source, i, r);
562 565
			if (!sensor_matrix_get_in_ring(i))
563 566
			{
564 567
				WL_DEBUG_PRINT("Robot ");
......
669 672
{
670 673
	char nextRobot = 0xFF;
671 674
	int i = wl_get_xbee_id() + 1;
672
	char buf[2 * sensor_matrix_get_size()];
675
	char buf[3 * sensor_matrix_get_size()];
673 676
	if (accepted == -1)
674 677
	{
675 678
		while (1)
......
708 711
	for (i = 0; i < sensor_matrix_get_size(); i++) {
709 712
		if (sensor_matrix_get_in_ring(i) && i != wl_get_xbee_id())
710 713
		{
711
			buf[2*j] = i;
712
			buf[2*j + 1] = sensor_matrix_get_reading(wl_get_xbee_id(), i);
714
			SensorReading *r = sensor_matrix_get_reading(wl_get_xbee_id(), i);
715
			buf[3*j] = i;
716
			buf[3*j + 1] = r->dir;
717
			buf[3*j + 2] = r->dist;
713 718
			j++;
714 719
		}
715 720
	}
716 721

  
717
	int packetSize = 2 * j * sizeof(char);
722
	int packetSize = 3 * j * sizeof(char);
718 723
	WL_DEBUG_PRINT("Passing the token to robot ");
719 724
	WL_DEBUG_PRINT_INT(nextRobot);
720 725
	WL_DEBUG_PRINT(".\r\n");
......
738 743
 **/
739 744
static void wl_token_bom_on_receive(int source)
740 745
{
741
	int max, dist;
746
	SensorReading r;
742 747

  
743 748
	WL_DEBUG_PRINT("Robot ");
744 749
	WL_DEBUG_PRINT_INT(source);
......
746 751

  
747 752
	bom_on_count = 0;
748 753

  
749
	max = get_max_bom_function();
754
	r = get_max_bom_function();
750 755
	sensor_matrix_set_reading(wl_get_xbee_id(),
751
		source, max);
756
		source, r);
752 757

  
753 758
	WL_DEBUG_PRINT("Max: ");
754
	WL_DEBUG_PRINT_INT(max);
755
	WL_DEBUG_PRINT("\tVar: ");
756
	WL_DEBUG_PRINT_INT(dist);
759
	WL_DEBUG_PRINT_INT(r.dir);
760
	WL_DEBUG_PRINT("\tDist: ");
761
	WL_DEBUG_PRINT_INT(r.dist);
757 762
	WL_DEBUG_PRINT("\n\n");
758 763
}
759 764

  

Also available in: Unified diff