Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / Test / i2c.c @ 992

History | View | Annotate | Download (1.96 KB)

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

    
14
/**
15
 * Detects whether or not an xbee is present.
16
 **/
17
int xbee_present(void){
18
        bayboard_init(ALL_ON);
19
        set_orb(255,0,0);
20
        if(xbee_enter_command_mode() == -1){
21
                return 0;
22
        }
23
        if(xbee_exit_command_mode() == -1){
24
                return 0;
25
        }
26
        return 1;
27
}
28

    
29

    
30
/**
31
 * Sends a string of size size to the xbee module.
32
 *
33
 * @param buf the string to send
34
 * @param size the length of the string
35
 **/
36
static int xbee_send(char* buf, int size)
37
{
38
        int i;
39
        for (i = 0; i < size; i++) {
40
                xbee_putc(buf[i]);
41
        }
42
        return 0;
43
}
44

    
45
/**
46
 * Wrapper for xbee_send.
47
 *
48
 * @param c the string to send
49
 * @param len the length of the string
50
 **/
51
static int xbee_send_string(char* c, int len)
52
{
53
        return xbee_send(c, len);
54
}
55

    
56
/**
57
 * Enter into command mode.
58
 **/
59
static int xbee_enter_command_mode()
60
{
61
        if (xbee_send_string("+++\r", 4) != 0) {
62
                return -1;
63
        }
64

    
65
        if (xbee_wait_for_ok() != 0) {
66
          return -1;
67
        }
68
        return 0;
69
}
70

    
71
/**
72
 * Exit from command mode.
73
 **/
74
static int xbee_exit_command_mode()
75
{
76
        if (xbee_send_string("ATCN\r", 5) != 0) {
77
                return -1;
78
        }
79
        xbee_wait_for_ok();
80

    
81
        return 0;
82
}
83

    
84
/**
85
 * Wait until the string "OK\r" is received from the XBee.
86
 **/
87
static int xbee_wait_for_ok()
88
{
89
        return xbee_wait_for_string("OK\r", 3);
90
}
91

    
92
/**
93
 * Delay until the specified string is received from
94
 * the XBee. Discards all other XBee data.
95
 *
96
 * @param s the string to receive
97
 * @param len the length of the string
98
 **/
99
static int xbee_wait_for_string(char* s, int len)
100
{
101
        
102
        long long timeout = 0;
103
        int cur = 0;
104
        
105
        while (cur < len) {
106
                if(timeout > 160000){
107
                        return -1;
108
                }
109
                if(xbee_getc_nb(s + cur) == 0){
110
                        timeout = 0;
111
                        cur++;
112
                }
113
                timeout++;
114
        }
115

    
116
        return 0;
117
}