From a6106b4e639407e347e701b51ce102c025527933 Mon Sep 17 00:00:00 2001
From: Andrea Guglielmini <il.guglio95@gmail.com>
Date: Mon, 25 Nov 2019 15:51:40 +0100
Subject: [PATCH] [nRF24] Added setCrcFiltering(bool), setAutoAck(bool)

---
 keywords.txt                |  1 +
 src/modules/nRF24/nRF24.cpp | 33 +++++++++++++++++++++++++++++++++
 src/modules/nRF24/nRF24.h   | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+)

diff --git a/keywords.txt b/keywords.txt
index 02739f5a..985e2ecd 100644
--- a/keywords.txt
+++ b/keywords.txt
@@ -133,6 +133,7 @@ setTransmitPipe	KEYWORD2
 setReceivePipe	KEYWORD2
 disablePipe	KEYWORD2
 getStatus	KEYWORD2
+setAutoAck	KEYWORD2
 
 # HTTP
 get	KEYWORD2
diff --git a/src/modules/nRF24/nRF24.cpp b/src/modules/nRF24/nRF24.cpp
index 8fb205fc..dc9895a9 100644
--- a/src/modules/nRF24/nRF24.cpp
+++ b/src/modules/nRF24/nRF24.cpp
@@ -481,6 +481,39 @@ size_t nRF24::getPacketLength(bool update) {
   return((size_t)length);
 }
 
+int16_t nRF24::setCrcFiltering(bool crcOn) {
+  return _mod->SPIsetRegValue(NRF24_REG_CONFIG, crcOn ? NRF24_CRC_ON : NRF24_CRC_OFF, 3, 3);
+}
+
+int16_t nRF24::setAutoAck(bool autoAckOn){
+  return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_ALL_ON : NRF24_AA_ALL_OFF, 5, 0);
+}
+
+int16_t nRF24::setAutoAck(uint8_t pipeNum, bool autoAckOn){
+  switch(pipeNum) {
+    case 0:
+      return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_P0_ON : NRF24_AA_P0_OFF, 0, 0);
+      break;
+    case 1:
+      return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_P1_ON : NRF24_AA_P1_OFF, 1, 1);
+      break;
+    case 2:
+      return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_P2_ON : NRF24_AA_P2_OFF, 2, 2);
+      break;
+    case 3:
+      return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_P3_ON : NRF24_AA_P3_OFF, 3, 3);
+      break;
+    case 4:
+      return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_P4_ON : NRF24_AA_P4_OFF, 4, 4);
+      break;
+    case 5:
+      return _mod->SPIsetRegValue(NRF24_REG_EN_AA, autoAckOn ? NRF24_AA_P5_ON : NRF24_AA_P5_OFF, 5, 5);
+      break;
+    default:
+      return (ERR_INVALID_PIPE_NUMBER);
+  }
+}
+
 void nRF24::clearIRQ() {
   // clear status bits
   _mod->SPIsetRegValue(NRF24_REG_STATUS, NRF24_RX_DR | NRF24_TX_DS | NRF24_MAX_RT, 6, 4);
diff --git a/src/modules/nRF24/nRF24.h b/src/modules/nRF24/nRF24.h
index 1ca8a080..a1cfec7b 100644
--- a/src/modules/nRF24/nRF24.h
+++ b/src/modules/nRF24/nRF24.h
@@ -69,6 +69,8 @@
 #define NRF24_PRX                                     0b00000001  //  0     0     enable primary Rx
 
 // NRF24_REG_EN_AA
+#define NRF24_AA_ALL_OFF                              0b00000000  //  5     0     auto-ACK on all pipes: disabled
+#define NRF24_AA_ALL_ON                               0b00111111  //  5     0                         enabled (default)
 #define NRF24_AA_P5_OFF                               0b00000000  //  5     5     auto-ACK on pipe 5: disabled
 #define NRF24_AA_P5_ON                                0b00100000  //  5     5                         enabled (default)
 #define NRF24_AA_P4_OFF                               0b00000000  //  4     4     auto-ACK on pipe 4: disabled
@@ -408,6 +410,36 @@ class nRF24: public PhysicalLayer {
     */
     size_t getPacketLength(bool update = true);
 
+
+    /*!
+     \brief Enable CRC filtering and generation.
+
+     \param crcOn Set or unset CRC check.
+
+     \returns \ref status_codes
+   */
+    int16_t setCrcFiltering(bool crcOn = true);
+
+    /*!
+     \brief Enable or disable auto-acknowlede packets
+
+     \param crcOn Enable (true) or disable (false) auto-acks.
+
+     \returns \ref status_codes
+   */
+    int16_t setAutoAck(bool autoAckOn = true);
+
+    /*!
+     \brief Enable or disable auto-acknowlede packets
+
+     \param crcOn Enable (true) or disable (false) auto-acks.
+
+     \param pipeNum Number of pipe to which enable / disable auto-acks.
+
+     \returns \ref status_codes
+   */
+    int16_t setAutoAck(uint8_t pipeNum, bool autoAckOn = true);
+
 #ifndef RADIOLIB_GODMODE
   private:
 #endif