From 92373ba7f53f451f2e60bb557817982caa782463 Mon Sep 17 00:00:00 2001 From: maniacbug Date: Mon, 27 Jun 2011 12:54:42 -0700 Subject: [PATCH] Refactor repeated parts of printDetails into own methods --- README.md | 2 ++ RF24.cpp | 81 ++++++++++++++++++++++++------------------------------- RF24.h | 16 +++++++++++ 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index c0e71c0..b473b22 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Arduino driver for nRF24L01 2.4GHz Wireless Transceiver +CLEANUPS BRANCH: Bits and pieces of cleanup that I haven't tested on the radios yet. + Design Goals: This library is designed to be... * Maximally compliant with the intended operation of the chip diff --git a/RF24.cpp b/RF24.cpp index 8874820..e8b7bdf 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -208,6 +208,27 @@ void RF24::print_observe_tx(uint8_t value) /******************************************************************/ +void RF24::print_byte_register(prog_char* name, uint8_t reg) +{ + printf_P(PSTR("%S\t = 0x%02x\n\r"),read_register(reg)); +} + +/******************************************************************/ + +void RF24::print_address_register(prog_char* name, uint8_t reg) +{ + uint8_t buffer[5]; + read_register(reg,buffer,sizeof buffer); + printf_P(PSTR("%S\t = 0x"),name); + + uint8_t* bufptr = buffer + sizeof buffer; + while( bufptr-- > buffer ) + printf_P(PSTR("%02x"),*bufptr); + printf_P(PSTR("\n\r")); +} + +/******************************************************************/ + RF24::RF24(uint8_t _cepin, uint8_t _cspin): ce_pin(_cepin), csn_pin(_cspin), payload_size(32), ack_payload_available(false) { @@ -238,54 +259,22 @@ uint8_t RF24::getPayloadSize(void) void RF24::printDetails(void) { - uint8_t buffer[5]; - uint8_t status = read_register(RX_ADDR_P0,buffer,5); - print_status(status); - printf_P(PSTR("RX_ADDR_P0 = 0x")); - uint8_t *bufptr = buffer + 5; - while( bufptr-- > buffer ) - printf_P(PSTR("%02x"),*bufptr); - printf_P(PSTR("\n\r")); + print_status(get_status()); - status = read_register(RX_ADDR_P1,buffer,5); - printf_P(PSTR("RX_ADDR_P1 = 0x")); - bufptr = buffer + 5; - while( bufptr-- > buffer ) - printf_P(PSTR("%02x"),*bufptr); - printf_P(PSTR("\n\r")); + print_address_register(PSTR("RX_ADDR_P0"),RX_ADDR_P0); + print_address_register(PSTR("RX_ADDR_P1"),RX_ADDR_P1); + print_byte_register(PSTR("RX_ADDR_P2"),RX_ADDR_P2); + print_byte_register(PSTR("RX_ADDR_P3"),RX_ADDR_P3); + print_byte_register(PSTR("RX_ADDR_P4"),RX_ADDR_P4); + print_byte_register(PSTR("RX_ADDR_P5"),RX_ADDR_P5); + print_address_register(PSTR("TX_ADDR"),TX_ADDR); - status = read_register(RX_ADDR_P2,buffer,1); - printf_P(PSTR("RX_ADDR_P2 = 0x%02x"),*buffer); - printf_P(PSTR("\n\r")); - - status = read_register(RX_ADDR_P3,buffer,1); - printf_P(PSTR("RX_ADDR_P3 = 0x%02x"),*buffer); - printf_P(PSTR("\n\r")); - - status = read_register(TX_ADDR,buffer,5); - printf_P(PSTR("TX_ADDR = 0x")); - bufptr = buffer + 5; - while( bufptr-- > buffer ) - printf_P(PSTR("%02x"),*bufptr); - printf_P(PSTR("\n\r")); - - status = read_register(RX_PW_P0,buffer,1); - printf_P(PSTR("RX_PW_P0 = 0x%02x\n\r"),*buffer); - - status = read_register(RX_PW_P1,buffer,1); - printf_P(PSTR("RX_PW_P1 = 0x%02x\n\r"),*buffer); - - read_register(EN_AA,buffer,1); - printf_P(PSTR("EN_AA = %02x\n\r"),*buffer); - - read_register(EN_RXADDR,buffer,1); - printf_P(PSTR("EN_RXADDR = %02x\n\r"),*buffer); - - read_register(RF_CH,buffer,1); - printf_P(PSTR("RF_CH = %02x\n\r"),*buffer); - - read_register(RF_SETUP,buffer,1); - printf_P(PSTR("RF_SETUP = %02x\n\r"),*buffer); + print_byte_register(PSTR("RX_PW_P0"),RX_PW_P0); + print_byte_register(PSTR("RX_PW_P1"),RX_PW_P1); + print_byte_register(PSTR("EN_AA"),EN_AA); + print_byte_register(PSTR("EN_RXADDR"),EN_RXADDR); + print_byte_register(PSTR("RF_CH"),RF_CH); + print_byte_register(PSTR("RF_SETUP"),RF_SETUP); } /******************************************************************/ diff --git a/RF24.h b/RF24.h index 386b628..9b39768 100644 --- a/RF24.h +++ b/RF24.h @@ -161,6 +161,22 @@ protected: */ void print_observe_tx(uint8_t value) ; + /** + * Print the name and value of an 8-bit register to stdout + * + * @param name Name of the register + * @param reg Which register. Use constants from nRF24L01.h + */ + void print_byte_register(prog_char* name, uint8_t reg); + + /** + * Print the name and value of a 40-bit address register to stdout + * + * @param name Name of the register + * @param reg Which register. Use constants from nRF24L01.h + */ + void print_address_register(prog_char* name, uint8_t reg); + /** * Turn on or off the special features of the chip *