Project

General

Profile

Revision 921

i2c library for the bay boards
detects xbee presence to determine master/slave

View differences:

branches/autonomous_recharging/code/projects/Test/i2c.c
1
#include "bayboard_lib.h"
2
#include "../libwireless/lib/wireless.h"
3
#include "../libwireless/lib/wl_token_ring.h"
4

  
5
int xbee_present(void);
6
static int xbee_send(char*, int);
7
static int xbee_send_string(char*, int);
8
static int xbee_enter_command_mode(void);
9
static int xbee_exit_command_mode(void);
10
static int xbee_wait_for_ok(void);
11
static int xbee_wait_for_string(char*, int);
12

  
13
int xbee_present(void){
14
	bayboard_init(ALL_ON);
15
	set_orb(255,0,0);
16
	if(xbee_enter_command_mode() == -1){
17
		return 0;
18
	}
19
	if(xbee_exit_command_mode() == -1){
20
		return 0;
21
	}
22
	return 1;
23
}
24

  
25
static int xbee_send(char* buf, int size)
26
{
27
	int i;
28
	for (i = 0; i < size; i++) {
29
		xbee_putc(buf[i]);
30
	}
31
	return 0;
32
}
33

  
34
static int xbee_send_string(char* c, int len)
35
{
36
	return xbee_send(c, len);
37
}
38

  
39
/**
40
 * Enter into command mode.
41
 **/
42
static int xbee_enter_command_mode()
43
{
44
	if (xbee_send_string("+++\r", 4) != 0) {
45
		return -1;
46
	}
47

  
48
	if (xbee_wait_for_ok() != 0) {
49
	  return -1;
50
	}
51
	  return 0;
52
}
53

  
54
/**
55
 * Exit from command mode.
56
 **/
57
static int xbee_exit_command_mode()
58
{
59
	if (xbee_send_string("ATCN\r", 5) != 0) {
60
		return -1;
61
	}
62
	xbee_wait_for_ok();
63

  
64
	return 0;
65
}
66

  
67
/**
68
 * Wait until the string "OK\r" is received from the XBee.
69
 **/
70
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
71
// it reduces code size or not should be done to be sure.
72
static int xbee_wait_for_ok()
73
{
74
	return xbee_wait_for_string("OK\r", 3);
75
}
76

  
77
/**
78
 * Delay until the specified string is received from
79
 * the XBee. Discards all other XBee data.
80
 *
81
 * @param s the string to receive
82
 * @param len the length of the string
83
 **/
84
static int xbee_wait_for_string(char* s, int len)
85
{
86
	
87
	long long timeout = 0;
88
	int cur = 0;
89
	
90
	while (cur < len) {
91
		if(timeout > 160000){
92
			return -1;
93
		}
94
		if(xbee_getc_nb(s + cur) == 0){
95
			timeout = 0;
96
			cur++;
97
		}
98
		timeout++;
99
	}
100

  
101
	return 0;
102
}

Also available in: Unified diff