fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <map>
  4. #include <bitset>
  5.  
  6. using namespace std;
  7.  
  8. array<string, 8> foreach_bit(uint8_t v, const string (&s)[8][2]) {
  9. bitset<8> bs(v);
  10. array<string, 8> r;
  11. for (int i = 0; i < 8; ++i)
  12. r[i] = s[i][bs[i]];
  13. return r;
  14. };
  15.  
  16. typedef array<string, 8> (*ExplainRegFunc)(uint8_t);
  17.  
  18. struct RegisterInfo {
  19. string name;
  20. ExplainRegFunc explainFun;
  21. };
  22.  
  23. #define RESERVED_BITFIELD {"", "ERROR: reserved, 0 expected"}
  24. #define EMPTY_BITFIELD {"", ""}
  25.  
  26. static map<int, RegisterInfo> regInfo = {
  27. {0, {
  28. .name = "CONFIG",
  29. .explainFun = [](uint8_t v) {
  30. array<string, 8> r = foreach_bit(v, {
  31. {"TX", "RX"}, // 0
  32. {"DOWN", "UP"}, // 1
  33. {"1 byte CRC", "2 bytes CRC"}, // 2
  34. {"CRC off", ""}, // 3
  35. {"", "MAX_RT off"}, // 4
  36. {"", "TX_DS off"}, // 5
  37. {"", "RX_DR off"}, // 6
  38. RESERVED_BITFIELD // 7
  39. });
  40. if (r[3] != "") r[2] = "";
  41. return r;
  42. }
  43. }
  44. },
  45. {3, {
  46. .name = "SETUP_AW",
  47. .explainFun = [](uint8_t v) {
  48. array<string, 8> r = foreach_bit(v, {
  49. EMPTY_BITFIELD,
  50. EMPTY_BITFIELD,
  51. RESERVED_BITFIELD,
  52. RESERVED_BITFIELD,
  53. RESERVED_BITFIELD,
  54. RESERVED_BITFIELD,
  55. RESERVED_BITFIELD,
  56. RESERVED_BITFIELD
  57. });
  58. const string vars[] = { "illegal", "3 bytes", "4 bytes", "6 bytes"};
  59. r[1] = vars[v & 0x3];
  60. return r;
  61. }
  62. }
  63. }
  64. };
  65.  
  66. static void describeData(uint8_t value, const string &name, const array<string, 8> &descr) {
  67. cout << name << " = " << bitset<8>(value) << endl;
  68. char hlines[8];
  69. for (int i = 0; i < 8; i++) {
  70. hlines[7 - i] = (descr[i] != "") ? '|' : ' ';
  71. }
  72. string offset(name.length() + 3, ' ');
  73. for (int i = 0; i < 8; i++) {
  74. if (descr[i] == "") continue;
  75. hlines[7 - i] = 0;
  76. cout << offset << hlines << '`' << descr[i] << endl;
  77. }
  78. }
  79.  
  80. static void describeRegsiter(int address, uint8_t value) {
  81. if (regInfo.count(address) == 0) {
  82. cout << "ERROR: no info about register[" << address << "]" << endl;
  83. return;
  84. }
  85. const RegisterInfo &ri = regInfo[address];
  86. describeData(value, ri.name, ri.explainFun(value));
  87. }
  88.  
  89. int main() {
  90. cout << "NRF24L01+ control util" << endl;
  91. describeRegsiter(0, 0x77);
  92. describeRegsiter(3, 0x00);
  93. describeRegsiter(3, 0x13);
  94. describeRegsiter(4, 0x77);
  95. return 0;
  96. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
NRF24L01+ control util
CONFIG = 01110111
          |||| |`RX
          |||| `UP
          |||`CRC off
          ||`MAX_RT off
          |`TX_DS off
          `RX_DR off
SETUP_AW = 00000000
                 `illegal
SETUP_AW = 00010011
              |  `6 bytes
              `ERROR: reserved, 0 expected
ERROR: no info about register[4]