2 * Copyright (c) 2013, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <aversive/queue.h>
44 #include "xbee_user.h"
48 struct rc_proto_power_levels {
52 static struct rc_proto_power_levels power_levels[MAX_POWER_LEVEL];
54 /* update power level when we receive the answer from DB */
55 static int update_power_level(void *frame, unsigned len, void *arg)
57 struct xbee_atresp_hdr *atresp = (struct xbee_atresp_hdr *)frame;
58 int level = (intptr_t)arg;
61 /* XXX check if this test is correct */
62 if (len < sizeof(struct xbee_atresp_hdr) + sizeof(uint8_t)) {
68 power_levels[level].power_db = db;
69 power_levels[level].ttl = 2;
73 /* when we receive a power probe, ask the DB value to the xbee */
74 void rc_proto_rx_power_probe(int power_level)
77 xbeeapp_send_atcmd("DB", NULL, 0, 0, update_power_level, NULL);
80 /* send a hello message */
81 // XXX iovec for xbee ?
82 int8_t rc_proto_send_hello(uint64_t addr, void *data, uint8_t data_len)
85 struct rc_proto_echo_req hdr;
86 char buf[XBEE_MAX_FRAME_LEN - sizeof(struct rc_proto_echo_req)];
89 if (data_len > sizeof(frame.buf))
92 frame.hdr.type = RC_PROTO_HELLO;
93 frame.hdr.datalen = data_len;
94 memcpy(frame.buf, data, data_len);
95 return xbeeapp_send_msg(addr, &frame,
96 data_len + sizeof(struct rc_proto_echo_req), 1);
102 #define SERVO_NBITS 10
103 uint16_t servos[N_SERVO] = { 0x123, 0x234, 0x321, 0x123, 0x234, 0x321 };
105 int8_t servo2buf(uint8_t *buf, uint8_t len, uint8_t servo_mask,
106 uint8_t pow, uint8_t seq)
110 uint8_t servo_bits = 0;
118 buf[i++] = servo_mask;
119 buf[i++] = ((seq & 0x1f) << 3) | (pow & 0x7);
122 for (num = 0; num < N_SERVO; num++) {
123 if (!(servo_mask & (1 << num)))
125 servo_val = servos[num];
126 servo_bits = SERVO_NBITS;
128 tmp = (servo_val >> (servo_bits - remain_bits));
129 tmp &= ((1 << remain_bits) - 1);
134 servo_bits = 10 - remain_bits;
135 tmp = servo_val & ((1 << servo_bits) - 1);
136 tmp <<= (8 - servo_bits);
141 if (servo_bits == 8) {
146 remain_bits = 8 - servo_bits;
149 if (remain_bits != 8)
155 int8_t buf2servo(uint8_t *buf, uint8_t len)
157 uint8_t mask, count = 0;
171 for (num = 0; num < N_SERVO; num++) {
176 case 1: if (len != 4) return -1; break;
177 case 2: if (len != 5) return -1; break;
178 case 3: if (len != 6) return -1; break;
179 case 4: if (len != 7) return -1; break;
180 case 5: if (len != 9) return -1; break;
181 case 6: if (len != 10) return -1; break;
185 for (num = 0; ((1<<num) & mask) == 0; num++) {
192 val |= (buf[3] >> 6);
194 for (num++; ((1<<num) & mask) == 0; num++) {
201 val |= (buf[4] >> 4);
203 for (num++; ((1<<num) & mask) == 0; num++) {
210 val |= (buf[5] >> 2);
212 for (num++; ((1<<num) & mask) == 0; num++) {
221 for (num++; ((1<<num) & mask) == 0; num++) {
228 val |= (buf[8] >> 6);
230 for (num++; ((1<<num) & mask) == 0; num++) {
237 val |= (buf[9] >> 4);