|
|
@@ -32,10 +32,11 @@ int PL1167_nRF24::open() {
|
|
|
return recalc_parameters();
|
|
|
}
|
|
|
|
|
|
-int PL1167_nRF24::recalc_parameters()
|
|
|
-{
|
|
|
+int PL1167_nRF24::recalc_parameters() {
|
|
|
+ int nrf_address_length = _syncwordLength;
|
|
|
+
|
|
|
+ // +2 for CRC
|
|
|
int packet_length = _maxPacketLength + 2;
|
|
|
- int nrf_address_pos = _syncwordLength;
|
|
|
|
|
|
if (packet_length > sizeof(_packet) || nrf_address_length < 3) {
|
|
|
return -1;
|
|
|
@@ -133,16 +134,16 @@ int PL1167_nRF24::transmit(uint8_t channel) {
|
|
|
uint8_t tmp[sizeof(_packet)];
|
|
|
int outp=0;
|
|
|
|
|
|
- uint16_t crc;
|
|
|
- if (_crc) {
|
|
|
- crc = calc_crc(_packet, _packet_length);
|
|
|
- }
|
|
|
+ uint16_t crc = calc_crc(_packet, _packet_length);
|
|
|
|
|
|
- for (int inp = 0; inp < _packet_length + (_crc ? 2 : 0) + 1; inp++) {
|
|
|
+ // +1 for packet length
|
|
|
+ // +2 for crc
|
|
|
+ // = 3
|
|
|
+ for (int inp = 0; inp < _packet_length + 3; inp++) {
|
|
|
if (inp < _packet_length) {
|
|
|
- tmp[outp++] = reverse_bits(_packet[inp]);}
|
|
|
- else if (_crc && inp < _packet_length + 2) {
|
|
|
- tmp[outp++] = reverse_bits((crc >> ( (inp - _packet_length) * 8)) & 0xff);
|
|
|
+ tmp[outp++] = reverseBits(_packet[inp]);}
|
|
|
+ else if (inp < _packet_length + 2) {
|
|
|
+ tmp[outp++] = reverseBits((crc >> ( (inp - _packet_length) * 8)) & 0xff);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -172,45 +173,35 @@ int PL1167_nRF24::internal_receive() {
|
|
|
// HACK HACK HACK: Reset radio
|
|
|
open();
|
|
|
|
|
|
-#ifdef DEBUG_PRINTF
|
|
|
- printf("Packet received: ");
|
|
|
- for (int i = 0; i < _receive_length; i++) {
|
|
|
- printf("%02X", tmp[i]);
|
|
|
- }
|
|
|
- printf("\n");
|
|
|
-#endif
|
|
|
-
|
|
|
for (int inp = 0; inp < _receive_length; inp++) {
|
|
|
- tmp[outp++] = reverse_bits(tmp[inp]);
|
|
|
+ tmp[outp++] = reverseBits(tmp[inp]);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
#ifdef DEBUG_PRINTF
|
|
|
- printf("Packet transformed: ");
|
|
|
+ Serial.printf_P(PSTR("Packet received (%d bytes): "), outp);
|
|
|
for (int i = 0; i < outp; i++) {
|
|
|
- printf("%02X", tmp[i]);
|
|
|
+ Serial.printf_P(PSTR("%02X "), tmp[i]);
|
|
|
}
|
|
|
- printf("\n");
|
|
|
+ Serial.print(F("\n"));
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
- if (_crc) {
|
|
|
- if (outp < 2) {
|
|
|
+ if (outp < 2) {
|
|
|
#ifdef DEBUG_PRINTF
|
|
|
- printf("Failed CRC: outp < 2\n");
|
|
|
+ Serial.println(F("Failed CRC: outp < 2"));
|
|
|
#endif
|
|
|
- return 0;
|
|
|
- }
|
|
|
- uint16_t crc = calc_crc(tmp, outp - 2);
|
|
|
- if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ uint16_t crc = calc_crc(tmp, outp - 2);
|
|
|
+ uint16_t recvCrc = (tmp[outp - 1] << 8) | tmp[outp - 2];
|
|
|
+
|
|
|
+ if ( crc != recvCrc ) {
|
|
|
#ifdef DEBUG_PRINTF
|
|
|
- uint16_t recv_crc = ((tmp[outp - 2] & 0xFF) << 8) | (tmp[outp - 1] & 0xFF);
|
|
|
- printf("Failed CRC: expected %d, got %d\n", crc, recv_crc);
|
|
|
+ Serial.printf_P(PSTR("Failed CRC: expected %04X, got %04X"), crc, recvCrc);
|
|
|
#endif
|
|
|
- return 0;
|
|
|
- }
|
|
|
- outp -= 2;
|
|
|
+ return 0;
|
|
|
}
|
|
|
+ outp -= 2;
|
|
|
|
|
|
memcpy(_packet, tmp, outp);
|
|
|
|
|
|
@@ -240,14 +231,4 @@ static uint16_t calc_crc(uint8_t *data, size_t data_length) {
|
|
|
}
|
|
|
}
|
|
|
return state;
|
|
|
-}
|
|
|
-
|
|
|
-static uint8_t reverse_bits(uint8_t data) {
|
|
|
- uint8_t result = 0;
|
|
|
- for (int i = 0; i < 8; i++) {
|
|
|
- result <<= 1;
|
|
|
- result |= data & 1;
|
|
|
- data >>= 1;
|
|
|
- }
|
|
|
- return result;
|
|
|
-}
|
|
|
+}
|