I’m trying to figure out how to call a custom function in a switch.
I have a custom uart polling component where I pass some sensor values that it reads. I also want to be able to send some uart commands that are implemented in a function in the custom component.
I can’t figure out how to do this. Now I’m a hardware guy and know enough software to be dangerous!
My switch is define like this:
Clearly I’m not a wizard either! I would have thought that because the custom component is already registered and the function is a public function, it would know about that function.
Since there is only one component for that ID, you use component 0. You then have to cast it to whatever custom component Class is assigned to that component.
Another thing you can do is have a look at the generated build files that esphome creates from the yaml, specifically main.cpp. Those files show up in the directory
I was playing around and found another maybe simpler way of accomplishing this by setting up your class variable my_mppt2 as global instead of localized under the custom component instantiation.
What you do is add this line after your class definition in your header file:
BlueSkyWiFi * my_mppt2;
Then when you assign the class in the custom component, you simply remove the “auto” definition from the instantiation, which will assign to the previously globally defined my_mppt2 variable above.
my_mppt2 = new BlueSkyWiFi …
So what that means then, is in your other lambda functions you are able to access your class functions using:
my_mppt2->send_CC_ON();
I don’t see any minuses from doing it this way so far. Works fine in my testing.