Arduino-HomeKit-ESP8266 icon indicating copy to clipboard operation
Arduino-HomeKit-ESP8266 copied to clipboard

Using Classes for getters and setters

Open Zacknetic opened this issue 4 years ago • 0 comments
trafficstars

Hey there,

Firstly, thanks for creating this repository and the very descriptive examples. It's extremely useful.

One issue I have been struggling with is how to deal with making getters and setters for multiple identical accessories. For example, I am controlling 5 water valves. Creating 5 setters and getters for each characteristic (active, in_use, etc...) is a bit superfluous.

for example:

is_active_A.setter = is_active_A_setter;
is_active_B.setter = is_active_B_setter;
is_active_C.setter = is_active_C_setter;

I know there is .setter_ex which passes the characteristic, but what I'd really like to do is just create a class. Unfortunately, I'm running into errors which may be unsolvable.

Here's an example of what I am attempting to do by making a class:

#define PIN_SWITCH_A D7
extern "C" homekit_characteristic_t valve_A_is_active;

class Valve {

  private:
    homekit_characteristic_t is_active;
    int PIN_SWITCH;

  public:
    Valve (homekit_characteristic_t is_active_, int PIN_SWITCH_){
      is_active = is_active_;
      PIN_SWITCH = PIN_SWITCH_;
      is_active.setter = is_active_setter;
    }

    void is_active_setter (const homekit_value_t value){
      bool on = value.bool_value;
      is_active.value.bool_value = on;  //sync the value
      homekit_characteristic_notify(&is_active, is_active.value);
      digitalWrite(PIN_SWITCH, on ? HIGH : LOW);
      LOG_D("Valve: %s", on ? "OPEN" : "CLOSED");
    }
};

Valve valve_A(&valve_A_is_active, PIN_SWITCH_A);

And the error I get: cannot convert 'Valve::is_active_setter' from type 'void* (Valve::)(homekit_value_t)' to type 'void (*)(homekit_value_t)'

Clearly, there is an issue with pointers, but my c++ is not very strong. How would you suggest solving this issue? I'd prefer a Class, that way it would be very easy to scale up the number of accessories.

Thank you!

Zacknetic avatar Apr 26 '21 15:04 Zacknetic