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

define an empty constructor and overload DHT::begin #196

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

edWin-m
Copy link

@edWin-m edWin-m commented Dec 6, 2022

The use of an empty constructor allows for some more complex and flexible initialization logic without the need to write out every step explicitly.

@caternuson
Copy link

Can you provide more information on why this is necessary? What's an example use case that requires this empty constructor?

@edWin-m
Copy link
Author

edWin-m commented Dec 7, 2022

@caternuson An empty constructor will allow users to define an instance of DHT as a member of their classes. Furthermore, it grants users more options on how and at what stage of their program logic they can set the library config.

Use Case One

DHT dht;

void setup()
{
    dht.begin(D7, DHT11);
}

void loop()
{
    ...
    float humidity = dht.readHumidity();
    ...
}

Use Case Two

class MySensor {
public:
    MySensor();
    MySensor(uint8_t pin);
    void begin();
    void begin(uint8_t pin);
    ...
private:
    ...
    DHT dht;
    ...
};

Use Case Three

  • my_sensor.h
void my_sensor_loop();
void my_sensor_setup(uint8_t data_pin, sensor_callback_t cb_func);
  • my_sensor.cpp
...
static DHT sg_dht;
...

void my_sensor_setup(uint8_t data_pin, sensor_callback_t cb_func)
{
    ...
    sg_callback = cb_func;
    sg_dht.begin(data_pin, DHT11);
    ...
}

void my_sensor_loop()
{
    ...
    float humidity = sg_dht.readHumidity();
    float temperature = sg_dht.readTemperature();
    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants