Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default constructor request #539

Closed
edwino-stein opened this issue Oct 28, 2019 · 2 comments · Fixed by #750
Closed

Default constructor request #539

edwino-stein opened this issue Oct 28, 2019 · 2 comments · Fixed by #750
Assignees

Comments

@edwino-stein
Copy link

The RF24 class does not have a default constructor and, in some projects, may force heap allocation when the instance must be created and initialized during runtime. Heap allocation is a bad idea on microcontrollers like Arduino.

For example, in my project, I need inicializate an instance of RF24 with dynamic data during runtime, but because Arduino compatibility, the instance must be allocated in stack.

My sugestion is for implementation of a default constructor, and a new begin method with paramters of the current constructor.

RF24::RF24():
p_variant(false), payload_size(32),
dynamic_payloads_enabled(false), addr_width(5), csDelay(5)
{
    pipe0_reading_address[0]=0;
}

bool RF24::begin(uint16_t _cepin, uint16_t _cspin)
{
    // Initizalize the ce_pin and ce_pin values
    this->ce_pin = _cepin;
    this->csn_pin = _cspin;

   // Call the old begin method
    return this->begin();
}
@2bndy5
Copy link
Member

2bndy5 commented Mar 31, 2021

How would we go about preventing mis-use of this feature? Example:

#define RF24_CE 7
#define RF24_CS 8

RF24 radio();

void setup() {
  radio.begin(RF24_CE); // this is a bad scenario; complier will weep
  radio.begin(RF24_CS); // this is a bad scenario; compiler will weep
  radio.begin();        // this is a bad scenario because we used the blank c'tor above
  radio.begin(RF24_CE, RF24_CS); // this is the only proper way to use the suggested feature.

  // ...
}

Initially I think begin() should return false if either pins are 65535 (as set by the empty c'tor), but not many people that post code use the begin() method's return value in debugging (judging from the code I've seen when helping troubleshoot these issues).

@2bndy5 2bndy5 self-assigned this Mar 31, 2021
@2bndy5
Copy link
Member

2bndy5 commented Mar 31, 2021

begin() should return false if either pins are 65535

Found isValid(); an artifact function from maniacBug days that assumes the ce_pin and csn_pin is of uint8_t datatype. I'm adjusting this to utilize the full bit length of uint16_t (and moving the definition into the RF24.cpp like everything else is).

2bndy5 added a commit that referenced this issue Mar 31, 2021
@2bndy5 2bndy5 linked a pull request Mar 31, 2021 that will close this issue
kripton pushed a commit to kripton/RF24 that referenced this issue Apr 10, 2021
convert _SPI calls to pointers where applicable

typo made BCM driver fail

overload begin() and amended docs about begin()

fix faulty #ifdef logic

doxygen v1.9.1 deprecated COLS_IN_ALPHA_INDEX tag

bump version to v1.4.0

exclude only RF24_LINUX

apply ptr treatment to Teensy, LittleWire, & Due

trigger ArduinoCLI action

doxygen action shouldn't create a docs folder

no need to specify output dir in doxygen action

make my life simpler: use new RF24_SPI_PTR macro

trigger ArduinoCLI action

forgot about c'tor; fix 2-space indent in begin()

abstract SPI::begin() from _init_pins()

adjust csn_pin before SPI.begin() on BCM driver

conforming whitespace

add printf.h fix from @TonioChingon in nRF24#739

rename atxmega.md & add link to supported product

add Due to ArduinoCLI action

fix teensy support; add PIO CI for teensy

[PIO CI] use quoted matrix entries

[PIO CI] use single quotes?

I hate YML

Gimme that badge

add MRAA to Linux CI action

typo

[Linux CI] MRAA install needs special permission

try with "sudo bash -c"

[Linux CI] MRAA not cross compiling (I think)

add example code snippets to Arduino support pg

doc typos

Due can't use SPI_PTR; delete ATXMEGA/readme.md
(use docs/atxmega.md instead)

fix nRF24#414; note need mbed example in arduino.md

fix printf() only for adafruit/ArduinoCore-SAMD

add msg about COMMON_ISSUES to README

add estimated mbed example to arduino.md

avr/pgmspace.h was added to Due core in 2013

oops, undo my testing change

remove useless Due config file nRF24#752

ammend support for SPI_UART in csn()

Confirm working 2nd SPI bus on esp8266 nRF24#648

fix indent

c-n-p artifact; more indent inconcsistencies

comment out theroretical example snippets

add pinout img from RF24Audio repo

doxygen sux at reading imgs

implement nRF24#539

my CnP skills are lacking

need to test py wrapper

[py_wrap] begin fails to compile

[py wrap] oops left the old begin still in there

[py wrap] use thin wrapper for begin_w/_pins

[pr wrap] try explicit begin(void)

boost.py docs suck

[py wrap] try using same name for begin(pin, pin)

[py wrap] c'tor overload is bad

[py wrap] "optional<>" template didn't take

[py wrap] unmatched parenthesis

advise that teensy doesn't need overloaded begin()

docs review
2bndy5 added a commit that referenced this issue Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants