9 #include <avr/pgmspace.h>
33 #if defined(PRINT_NORESPONSE)
37 void r0ketbeam_setup(void)
40 nrf24_setDataRate(RF24_2MBPS
);
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');
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);
67 crc
^= (unsigned char)(crc
& 0xff) >> 4;
68 crc
^= (crc
<< 8) << 4;
69 crc
^= ((crc
& 0xff) << 4) << 1;
74 //CAUTION!!1! "id" is little-endian
75 void sendNick(uint32_t id
, char* name
)
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
)
98 pkt
.sequence
= sequence
;
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;
120 last_sent_packet = millis(); //FIXME little endianness, anyone?
121 //printf("[%i] ", delta);
123 uint32_t id = 0x78563412UL;
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()){
142 nrf24_read(&buf
, sizeof(buf
));
144 #ifdef PRINT_NORESPONSE
150 uart_puts_p(PSTR("RF24 RECV "));
151 uart_puthex_flip_32(buf
.id
);
154 uart_puthex_flip_32(buf
.sequence
);
157 uart_putdec(buf
.strength
);
160 uart_puthex(buf
.protocol
);
163 uint16_t crc
= flip16(crc16((uint8_t*)&buf
, sizeof(buf
)-2));
165 uart_puts_p(PSTR("RF24 ! CRC mismatch (expected/got): "));
168 uart_puthex_16(buf
.crc
);
172 #ifdef PRINT_NORESPONSE