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

Pointer to users struct/class in HABaseDeviceType class #168

Open
JacobChrist opened this issue May 22, 2023 · 1 comment
Open

Pointer to users struct/class in HABaseDeviceType class #168

JacobChrist opened this issue May 22, 2023 · 1 comment
Labels
selected for development Feature will be implemented in the next version

Comments

@JacobChrist
Copy link

JacobChrist commented May 22, 2023

Has it been considered, or is there already some mechanism to allow the HABaseDeviceType class to track a pointer to user data?

This would allow doing something like this to allow reuse of the call back function:

class ZoneData {
    public:
        uint8_t led_pin;
        uint8_t ssr_pin;
    ZoneData(uint8_t led_pin, uint8_t ssr_pin) : 
        led_pin(led_pin), 
        ssr_pin(ssr_pin)
    {
    }
};

WiFiClient client;
HADevice device;
HAMqtt mqtt(client, device);
HASwitch led("led", (void*) new ZoneData( LED_BUILTIN, LED_BUILTIN));
HASwitch zone[] = { 
    HASwitch("Zone0", (void*) new ZoneData( 6, 16)),
    HASwitch("Zone1", (void*) new ZoneData( 7, 17)),
    HASwitch("Zone2", (void*) new ZoneData( 8, 18)),
    HASwitch("Zone3", (void*) new ZoneData( 9, 19)),
    HASwitch("Zone4", (void*) new ZoneData(10, 20)),
    HASwitch("Zone5", (void*) new ZoneData(11, 21)),
    HASwitch("Zone6", (void*) new ZoneData(12, 22)),
    HASwitch("Zone7", (void*) new ZoneData(13, 26)),
    HASwitch("Zone8", (void*) new ZoneData(14, 27)),
    HASwitch("Zone9", (void*) new ZoneData(15, 28)),
};

// snip

void HAIntegration::switchHandler(bool state, HASwitch* sender) {
    digitalWrite(((ZoneData*)(sender->getData()))->led_pin, (state ? HIGH : LOW));
    digitalWrite(((ZoneData*)(sender->getData()))->ssr_pin, (state ? HIGH : LOW));
    sender->setState(state);  // report state back to Home Assistant
    Serial.printf("%s new state: %s (%d, %d)\n", 
        sender->getName(), state ? "on" : "off",
        ((ZoneData*)(sender->getData()))->led_pin,
        ((ZoneData*)(sender->getData()))->ssr_pin
        );
}
@JacobChrist
Copy link
Author

I would be happy to do a pull request for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
selected for development Feature will be implemented in the next version
Projects
None yet
Development

No branches or pull requests

2 participants