maximkulkin / esp-ir-thermostat

Example of HomeKit smart thermostat that controls AC via IR
MIT License
4 stars 2 forks source link

Help with a button implementation #7

Closed renandw closed 1 year ago

renandw commented 1 year ago

Hello Maxim.

After two years I finally got the decoding right and it worked with my AC unit. Now I am trying to implement a button to control it in a wall or something like that. So far I managed to figured out how to do using single_press and double_press:

void button_callback(button_event_t event, void *context) {
        switch (event) {
        case button_event_single_press:
                printf("single press\n");
                target_state.value = HOMEKIT_UINT8(HOMEKIT_CURRENT_HEATING_COOLING_STATE_COOL);
                homekit_characteristic_notify(&target_state, target_state.value);
                update_state();
                break;
        case button_event_double_press:
                printf("double press\n");
                target_state.value = HOMEKIT_UINT8(HOMEKIT_CURRENT_HEATING_COOLING_STATE_OFF);
                homekit_characteristic_notify(&target_state, target_state.value);
                update_state();
                break;
        case button_event_tripple_press:
                printf("tripple press\n");
                break;
        case button_event_long_press:
                printf("long press\n");
                break;
        default:
                printf("unknown button event: %d\n", event);
        }
}

But when I try to do with a "IF" function If ON >> turn OFF If OFF >> turno ON I get an error: "Fujitsu command send failed". And nothing happens :/

any help would be great! thanks in advance

renandw commented 1 year ago

I don't know if this is the best way, but it worked for me

        case button_event_single_press:
                printf("single press\n");
                if(current_state.value.int_value == HOMEKIT_CURRENT_HEATING_COOLING_STATE_COOL){
                  target_state.value = HOMEKIT_UINT8(HOMEKIT_CURRENT_HEATING_COOLING_STATE_OFF);
                  homekit_characteristic_notify(&target_state, target_state.value);
                  printf("desligando\n");
                } else {
                  target_state.value = HOMEKIT_UINT8(HOMEKIT_CURRENT_HEATING_COOLING_STATE_COOL);
                  homekit_characteristic_notify(&target_state, target_state.value);
                  printf("ligando\n");
                }
                update_state();
                break;

thanks

maximkulkin commented 1 year ago

Both code pieces look fine, the problem, I guess, is in how you have implemented your IF logic. In the second example, it is safe to extract homekit_characteristic_notify() from inside IF branches to after the IF since it is the same in both cases. It does not really matter if you check current_state or target_state inside IF condition since I believe it is you who updates current_state inside update_state so it can't be anything but what target_state is set to.