Moving to a modular architecture
[cerebrum.git] / r0ketbeam.c
blobe3ba4a383eb64ff651cb3b16ced9db34e64e8fd1
2 #include <RF24.h>
4 #include "r0ketbeam.h"
5 #include "util.h"
6 #include "uart.h"
8 #include <avr/io.h>
9 #include <avr/pgmspace.h>
11 #include <string.h>
13 typedef struct {
14 uint8_t length;
15 uint8_t protocol;
16 uint8_t flags;
17 uint8_t strength;
18 uint32_t sequence;
19 uint32_t id;
20 uint8_t button1;
21 uint8_t button2;
22 uint16_t crc;
23 } beacon_data_t;
25 typedef struct {
26 uint8_t length;
27 uint8_t protocol;
28 uint32_t id;
29 char name[8];
30 uint16_t crc;
31 } beacon_name_t;
33 #if defined(PRINT_NORESPONSE)
34 int inc;
35 #endif
37 void r0ketbeam_setup(void)
39 nrf24_begin();
40 nrf24_setDataRate(RF24_2MBPS);
41 nrf24_setChannel(81);
42 nrf24_setAutoAck(0);
43 nrf24_setPayloadSize(16);
44 nrf24_setCRCLength(RF24_CRC_8);
45 nrf24_openReadingPipe(1, 0x0102030201LL);
46 nrf24_openWritingPipe(0x0102030201LL);
47 uart_puts_p(PSTR("RF24 Carrier: "));
48 uint8_t carrier = nrf24_testCarrier();
49 uart_putc(carrier?'Y':'N');
50 uart_putc('\n');
51 nrf24_startListening();
54 uint16_t flip16(uint16_t number)
56 return number>>8|number<<8;
59 uint16_t crc16(uint8_t* buf, int len)
61 /* code from r0ket/firmware/basic/crc.c */
62 uint16_t crc = 0xffff;
63 for (int i=0; i < len; i++)
65 crc = (unsigned char)(crc >> 8) | (crc << 8);
66 crc ^= buf[i];
67 crc ^= (unsigned char)(crc & 0xff) >> 4;
68 crc ^= (crc << 8) << 4;
69 crc ^= ((crc & 0xff) << 4) << 1;
71 return crc;
74 //CAUTION!!1! "id" is little-endian
75 void sendNick(uint32_t id, char* name)
77 beacon_name_t pkt;
78 pkt.length = 16;
79 pkt.protocol = 0x23;
80 pkt.id = id;
81 memset(&pkt.name, 0, sizeof(pkt.name));
82 strncpy((char*)&pkt.name, name, min(8,strlen(name)));
83 pkt.crc = flip16(crc16((uint8_t*)&pkt, sizeof(pkt)-2));
85 nrf24_stopListening();
86 nrf24_openWritingPipe(0x0102030201LL);
87 nrf24_write(&pkt, sizeof(pkt));
88 nrf24_startListening();
91 //CAUTION!!1! "id" and "sequence" are little-endian
92 void sendDummyPacket(uint32_t id, uint32_t sequence)
94 beacon_data_t pkt;
95 pkt.length = 16;
96 pkt.protocol = 0x17;
97 pkt.id = id;
98 pkt.sequence = sequence;
99 pkt.flags = 0x00;
100 pkt.button1 = 0xFF;
101 pkt.button2 = 0xFF;
103 pkt.crc = flip16(crc16((uint8_t*)&pkt, sizeof(pkt)-2));
105 nrf24_stopListening();
106 nrf24_write((uint8_t*)&pkt, sizeof(pkt));
107 nrf24_startListening();
110 int last_sent_packet = 0;
111 char* nick = "c_leuse";
113 void r0ketbeam_loop(void)
115 //sorry, since we are not arduino, we do not yet have a millis() function.
116 //int delta = millis() - last_sent_packet;
118 if (delta > 1234)
120 last_sent_packet = millis(); //FIXME little endianness, anyone?
121 //printf("[%i] ", delta);
123 uint32_t id = 0x78563412UL;
125 char myNick[9];
126 sprintf((char*)&myNick, "*[%u]", (unsigned int)millis());
127 myNick[sizeof(myNick)-1] = '\0';
129 Serial.print("# Sending nick '");
130 Serial.print(myNick);
131 Serial.print("' as 0x");
132 Serial.print(id, HEX);
133 Serial.println(" ...");
134 sendNick(id, (char*)&myNick);
136 //sendDummyPacket(id, last_sent_packet);
140 if (nrf24_available()){
141 beacon_data_t buf;
142 nrf24_read(&buf, sizeof(buf));
144 #ifdef PRINT_NORESPONSE
145 if (inc > 0){
146 uart_putc('\n');
148 inc = 0;
149 #endif
150 uart_puts_p(PSTR("RF24 RECV "));
151 uart_puthex_flip_32(buf.id);
152 uart_putc(':');
153 uart_putc(' ');
154 uart_puthex_flip_32(buf.sequence);
155 uart_putc(' ');
156 uart_putc('(');
157 uart_putdec(buf.strength);
158 uart_putc(')');
159 uart_putc(' ');
160 uart_puthex(buf.protocol);
161 uart_putc('\n');
163 uint16_t crc = flip16(crc16((uint8_t*)&buf, sizeof(buf)-2));
164 if (crc != buf.crc){
165 uart_puts_p(PSTR("RF24 ! CRC mismatch (expected/got): "));
166 uart_puthex_16(crc);
167 uart_putc('/');
168 uart_puthex_16(buf.crc);
169 uart_putc('\n');
171 }else{
172 #ifdef PRINT_NORESPONSE
173 if (inc++ == 80) {
174 uart_putc('\n');
175 inc = 0;
177 uart_putc('.');
178 //_delay_ms(100);
179 #endif