#include <iostream>
#include <array>
#include <map>
#include <bitset>
using namespace std;
array< string, 8 > foreach_bit( uint8_t v, const string ( & s) [ 8 ] [ 2 ] ) {
bitset< 8 > bs( v) ;
array< string, 8 > r;
for ( int i = 0 ; i < 8 ; ++ i)
r[ i] = s[ i] [ bs[ i] ] ;
return r;
} ;
typedef array< string, 8 > ( * ExplainRegFunc) ( uint8_t ) ;
struct RegisterInfo {
string name;
ExplainRegFunc explainFun;
} ;
#define RESERVED_BITFIELD {"", "ERROR: reserved, 0 expected"}
#define EMPTY_BITFIELD {"", ""}
static map< int , RegisterInfo> regInfo = {
{ 0 , {
.name = "CONFIG" ,
.explainFun = [ ] ( uint8_t v) {
array< string, 8 > r = foreach_bit( v, {
{ "TX" , "RX" } , // 0
{ "DOWN" , "UP" } , // 1
{ "1 byte CRC" , "2 bytes CRC" } , // 2
{ "CRC off" , "" } , // 3
{ "" , "MAX_RT off" } , // 4
{ "" , "TX_DS off" } , // 5
{ "" , "RX_DR off" } , // 6
RESERVED_BITFIELD // 7
} ) ;
if ( r[ 3 ] ! = "" ) r[ 2 ] = "" ;
return r;
}
}
} ,
{ 3 , {
.name = "SETUP_AW" ,
.explainFun = [ ] ( uint8_t v) {
array< string, 8 > r = foreach_bit( v, {
EMPTY_BITFIELD,
EMPTY_BITFIELD,
RESERVED_BITFIELD,
RESERVED_BITFIELD,
RESERVED_BITFIELD,
RESERVED_BITFIELD,
RESERVED_BITFIELD,
RESERVED_BITFIELD
} ) ;
const string vars[ ] = { "illegal" , "3 bytes" , "4 bytes" , "6 bytes" } ;
r[ 1 ] = vars[ v & 0x3 ] ;
return r;
}
}
}
} ;
static void describeData( uint8_t value, const string & name, const array< string, 8 > & descr) {
cout << name << " = " << bitset< 8 > ( value) << endl;
char hlines[ 8 ] ;
for ( int i = 0 ; i < 8 ; i++ ) {
hlines[ 7 - i] = ( descr[ i] ! = "" ) ? '|' : ' ' ;
}
string offset( name.length ( ) + 3 , ' ' ) ;
for ( int i = 0 ; i < 8 ; i++ ) {
if ( descr[ i] == "" ) continue ;
hlines[ 7 - i] = 0 ;
cout << offset << hlines << '`' << descr[ i] << endl;
}
}
static void describeRegsiter( int address, uint8_t value) {
if ( regInfo.count ( address) == 0 ) {
cout << "ERROR: no info about register[" << address << "]" << endl;
return ;
}
const RegisterInfo & ri = regInfo[ address] ;
describeData( value, ri.name , ri.explainFun ( value) ) ;
}
int main( ) {
cout << "NRF24L01+ control util" << endl;
describeRegsiter( 0 , 0x77 ) ;
describeRegsiter( 3 , 0x00 ) ;
describeRegsiter( 3 , 0x13 ) ;
describeRegsiter( 4 , 0x77 ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YXJyYXk+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxiaXRzZXQ+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKYXJyYXk8c3RyaW5nLCA4PiBmb3JlYWNoX2JpdCh1aW50OF90IHYsIGNvbnN0IHN0cmluZyAoJnMpWzhdWzJdKSB7CiAgYml0c2V0PDg+IGJzKHYpOwogIGFycmF5PHN0cmluZywgOD4gcjsKICBmb3IgKGludCBpID0gMDsgaSA8IDg7ICsraSkKICAgIHJbaV0gPSBzW2ldW2JzW2ldXTsKICByZXR1cm4gcjsKfTsKCnR5cGVkZWYgYXJyYXk8c3RyaW5nLCA4PiAoKkV4cGxhaW5SZWdGdW5jKSh1aW50OF90KTsKCnN0cnVjdCBSZWdpc3RlckluZm8gewogIHN0cmluZyBuYW1lOwogIEV4cGxhaW5SZWdGdW5jIGV4cGxhaW5GdW47Cn07CgojZGVmaW5lIFJFU0VSVkVEX0JJVEZJRUxEIHsiIiwgIkVSUk9SOiByZXNlcnZlZCwgMCBleHBlY3RlZCJ9CiNkZWZpbmUgRU1QVFlfQklURklFTEQgeyIiLCAiIn0KCnN0YXRpYyBtYXA8aW50LCBSZWdpc3RlckluZm8+IHJlZ0luZm8gPSB7CiAgICB7MCwgewogICAgICAgICAgICAubmFtZSA9ICJDT05GSUciLAogICAgICAgICAgICAuZXhwbGFpbkZ1biA9IFtdKHVpbnQ4X3QgdikgewogICAgICAgICAgICAgIGFycmF5PHN0cmluZywgOD4gciA9IGZvcmVhY2hfYml0KHYsIHsKICAgICAgICAgICAgICAgICAgeyJUWCIsICJSWCJ9LCAgICAgICAgICAgICAgICAgIC8vIDAKICAgICAgICAgICAgICAgICAgeyJET1dOIiwgIlVQIn0sICAgICAgICAgICAgICAgIC8vIDEKICAgICAgICAgICAgICAgICAgeyIxIGJ5dGUgQ1JDIiwgIjIgYnl0ZXMgQ1JDIn0sIC8vIDIKICAgICAgICAgICAgICAgICAgeyJDUkMgb2ZmIiwgIiJ9LCAgICAgICAgICAgICAgIC8vIDMKICAgICAgICAgICAgICAgICAgeyIiLCAiTUFYX1JUIG9mZiJ9LCAgICAgICAgICAgIC8vIDQKICAgICAgICAgICAgICAgICAgeyIiLCAiVFhfRFMgb2ZmIn0sICAgICAgICAgICAgIC8vIDUKICAgICAgICAgICAgICAgICAgeyIiLCAiUlhfRFIgb2ZmIn0sICAgICAgICAgICAgIC8vIDYKICAgICAgICAgICAgICAgICAgUkVTRVJWRURfQklURklFTEQgICAgICAgICAgICAgIC8vIDcKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgICBpZiAoclszXSAhPSAiIikgclsyXSA9ICIiOwogICAgICAgICAgICAgIHJldHVybiByOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfSwKICAgIHszLCB7CiAgICAgICAgICAgIC5uYW1lID0gIlNFVFVQX0FXIiwKICAgICAgICAgICAgLmV4cGxhaW5GdW4gPSBbXSh1aW50OF90IHYpIHsKICAgICAgICAgICAgICBhcnJheTxzdHJpbmcsIDg+IHIgPSBmb3JlYWNoX2JpdCh2LCB7CiAgICAgICAgICAgICAgICAgIEVNUFRZX0JJVEZJRUxELAogICAgICAgICAgICAgICAgICBFTVBUWV9CSVRGSUVMRCwKICAgICAgICAgICAgICAgICAgUkVTRVJWR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