Declare a included library object globally

Hi everyone!

I’ve been working on hacking my roomba in HA and basically I have it 95% working, running off a Wemos D1 Mini. I could stop here but there’s this one other thing I’m trying to do and it’s got me scratching my head.

For most of the operations on a Roomba, I can imply use Serial.write() and it works fine, but when I need to read data back (battery, operation state, etc), there’s no super straightforward way of doing it so I’m leaning on the the Roomba library for Arduino by Mike McCauley, which makes getting the sensor data out pretty straight forward since it sets up all the structures you need for this. However, in order to do this, I have to initialize a new Roomba object every time I want to make a sensor call, which works, but the initialization process also resets some states I’m trying to preserve, which makes it less than ideal.

So my question: Is it possible for me to initialize a object as a global, rather that in the lambda context?

Some sample code here that is working.
First, I initialize some stuff in the boot loop. This declaration is only valid during this specific lambda’s context but it works because it gets the hardware ready to do its thing.

...  
includes:
    - includes/Roomba.h
    - includes/Roomba.cpp
  on_boot:
    then:
      - lambda: |-
          Roomba myRoomba(&Serial, Roomba::Baud115200);
          myRoomba.start();
...

However, when I need to do a read operation later, I have to declare a new object. This causes a couple of things to be reinitialized on the hardware that I don’t want.

...
  - id: read_sensors
    then:
      - lambda: |-
          Roomba myRoomba(&Serial, Roomba::Baud115200); 
          myRoomba.start();
...

Since I don’t want to reset internal states just to read sensor data, is it possible to declare a global pointer, and have the boot initialized object be pass off to that so it persists across all the lambdas? If I could do that, I’d probably move a lot of my calls from direct Serial.write()s all over to library calls for consistency and readability. Looking for something relatively simple and elegant and hoping there’s an obvious way to do this that I just haven’t stumbled upon in the documentation.

Apologies if all my terminology is all over the place and I hope that makes sense. I’m a bit of an amateur at this.

Yes:

https://esphome.io/guides/automations.html#global-variables

Yes:

https://esphome.io/guides/automations.html#global-variables

Right, so I get that part, as I have a number of ints and floats. But I don’t understand the syntax to declare something more complicated than a C++ primitive.

Like…

...
globals:
...
  - id: battery_percent
    type: float
    restore_value: yes
    initial_value: "-1"
  - id: my_roomba 
    type: Roomba::Roomba #what's the type?????

My assumption is that if I can get this declared here, I can just say id(my_roomba).start(); in the lambda, but I just don’t know what the correct syntax for the declaration is. I’ve tried a couple things but the compiler just doesn’t know what to do with it.

type: std::string

I seem to still not quite understanding the syntax well enough to pull it all together…

Per the above suggestion, I’ve got this global:

globals:
...
  - id: my_roomba
    type: std::string

and I’m initializing it in boot like this (and this is clearly the part I’m not getting):

  includes:
    - includes/Roomba.h
    - includes/Roomba.cpp
  on_boot:
    then:
      - lambda: |-
          // Roomba myRoomba(&Serial, Roomba::Baud115200); // for compairson
          // myRoomba.start();

          Roomba id(my_roomba)(&Serial, Roomba::Baud115200);
          id(my_roomba).start();

And when I compile:

/config/esphome/roomba.yaml: In lambda function:
/config/esphome/roomba.yaml:21:23: error: expected initializer before '->' token
           Roomba id(my_roomba)(&Serial, Roomba::Baud115200);
                       ^
/config/esphome/roomba.yaml:22:26: error: 'class std::basic_string<char>' has no member named 'start'
           id(my_roomba).start();
                          ^

I guess I just don’t understand how to initialize an object here.