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

Add example for irrigation system #251

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

Conversation

buschco
Copy link

@buschco buschco commented Sep 21, 2019

Hey I did my best to implement an irrigation homekit service. It is similar to the dynamic_services example.
Feedback is welcome 🙂

@buschco buschco changed the title Implemented irrigation Add example for irrigation system Sep 21, 2019
examples/watering/main.c Outdated Show resolved Hide resolved
examples/watering/main.c Outdated Show resolved Hide resolved
examples/watering/main.c Outdated Show resolved Hide resolved
void task_fn(void * pvParameters) {
gate *thisGate = (gate*)pvParameters;
while(true) {
homekit_characteristic_t *rd = thisGate->service->characteristics[5];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of relying on hard coded index, use homekit_service_characteristic_by_type:

homekit_characteristic_t *rd = homekit_service_characteristic_by_type(
    thisGate->service, HOMEKIT_CHARACTERISTIC_REMAINING_DURATION
);

Or even pre-cache it inside gate structure for improved performance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

homekit_service_characteristic_by_type makes it a lot simpler 👍
But I don't quite understand how to implement the pre-cache thing.

gates[i].number = i;
gates[i].gpio = valve_gpios[i];

xTaskCreate(task_fn, valve_name_value, 256, &gates[i], tskIDLE_PRIORITY, &gates[i].task);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to have a single task for all valves. All tasks needs to do is to wake up every second and check which valves are active and decrement their remaining duration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried my best here but I still need to store the task state as a variable. I tried to use vTaskGetInfo() to get information about the task state but I got type undefined reference to vTaskGetInfo from the compiler 😕

examples/watering/main.c Outdated Show resolved Hide resolved
examples/watering/main.c Outdated Show resolved Hide resolved
examples/watering/main.c Outdated Show resolved Hide resolved
10,
.getter_ex=generic_getter,
.setter_ex=generic_setter,
.callback=HOMEKIT_CHARACTERISTIC_CALLBACK(on_update_generic),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what update sequence is there, but it feels that if this value is updated, you need to have a meaningful reaction (like update REMAINING_DURATION).

examples/watering/main.c Outdated Show resolved Hide resolved
Comment on lines 135 to 138
gates[i].active = homekit_service_characteristic_by_type(nextService, "B0");;
gates[i].remaining_duration = homekit_service_characteristic_by_type(nextService, "D4");;
gates[i].set_duration = homekit_service_characteristic_by_type(nextService, "D3");;
gates[i].in_use = homekit_service_characteristic_by_type(nextService, "D2");;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about saving some CPU cycles and creating characteristics first. Also, (for future) it is easier to use HOMEKIT_CHARACTERISTIC_<name> (e.g. HOMEKIT_CHARACTERISTIC_ACTIVE) defines instead of hard-coding UUIDs.

gates[i].active = NEW_HOMEKIT_CHARACTERISTIC(
    ACTIVE, 0,
    .callback=HOMEKIT_CHARACTERISTIC_CALLBACK(
        on_update_active, .context=(void*)&gates[i],
    ),
);

// ...

homekit_service_t *nextService = NEW_HOMEKIT_SERVICE(VALVE, .characteristics=(homekit_characteristic_t*[]) {
    // ...
    gates[i].active,
    gates[i].remaining_duration,
    gates[i].set_duration,
    gates[i].in_use,
    NULL
});

examples/watering/main.c Outdated Show resolved Hide resolved
homekit_characteristic_t* in_use;
} valve_t;

valve_t gates[4];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valve_t gates[valve_count];

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then the compiler complains: error: variably modified 'gates' at file scope

@buschco
Copy link
Author

buschco commented Oct 19, 2019

I improved some points you mentioned.

examples/watering/main.c Outdated Show resolved Hide resolved
@buschco buschco force-pushed the irrigation branch 2 times, most recently from ad3b9e4 to 4e282b6 Compare April 13, 2020 08:56
After `Remaining Duration` reached 0 the valve would not turn of and
stayed in `idle`.
The bug was caused by turning of the `gate_task` to early.
@buschco
Copy link
Author

buschco commented Jun 12, 2020

Over the last weeks I improved my code a lot. As far I can see these points are not fixed right now:

1. Pre-cache characteristics

Or even pre-cache it inside gate structure for improved performance.

I don't quite understand how to implement this.

2. Use vTaskGetInfo() instead of storing task state in a variable

I use task_running to determine if the task is suspended or running. I saw vTaskGetInfo() in the freertos docs, but the compiler warned me:
type undefined reference to vTaskGetInfo

3. Create characteristics first

How about saving some CPU cycles and creating characteristics first.

I don't quite understand how to implement this.

4. Use valve_count instead of hard coding array length

This might be a no-brainer but I am quite lost at this one 🙈

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