Cleanly get variables in and out of various CPP files

Any other software topic
Post Reply
tom91
Posts: 775
Joined: Fri Mar 01, 2019 9:15 pm
Location: Bristol

Cleanly get variables in and out of various CPP files

Post by tom91 »

I am a bit stumped. Tried googling to find out what how to get a propper structure built up to be able to share values between different CPP files.

I am currently a bit stuck can anyone shed a bit of light on this or help me with a link?

I am trying to pull data from various CPP sections of my code with getVariable() functions, but currently different calls in different cpp files gives different values.

This is not too confusing, due to how I believe the programmer works, however this must be quite easily possible somehow.

In this circumstance im trying to pull data with a "secondary action" its a cpp file that runs the functions for ODB2 comms. I can force this into other cpp files and this would solve my issues. But approaching this properly will help me vastly in the future.

A bit of rant but hopefully someone can shed some light on this.
tom91
Posts: 775
Joined: Fri Mar 01, 2019 9:15 pm
Location: Bristol

Re: Cleanly get variables in and out of various CPP files

Post by tom91 »

Okay done some more digging.

I need to declare the object with extern in front to allow multi file usage.

So in the main

Code: Select all

#include <Arduino.h>
#include "data.h"
#include "New.h"

data data;

New New;
And then in the New.cpp

Code: Select all

#include "New.h"
#include "data.h"

extern data data;

void New::SetVar(uint8_t temp)
{
  data.SetVarmyass(temp);
}
User avatar
celeron55
Posts: 574
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Contact:

Re: Cleanly get variables in and out of various CPP files

Post by celeron55 »

For extra points, put the extern line into for example main.h and then #include "main.h", when your non-extern line is in main.ino or main.cpp. This is the common way to organize things.
tom91
Posts: 775
Joined: Fri Mar 01, 2019 9:15 pm
Location: Bristol

Re: Cleanly get variables in and out of various CPP files

Post by tom91 »

celeron55 wrote: Tue Aug 02, 2022 7:24 pm For extra points, put the extern line into for example main.h and then #include "main.h", when your non-extern line is in main.ino or main.cpp. This is the common way to organize things.
Thank you for the advice. I have been mostly self taught writing code so somethings hit a new roadblock and get a learning opportunity.

The OBD2 Emulation code is working with the Torque Pro app now, its a gif so lick to see the data moving :D
SmartSelect_20220802-204611_Torque.gif
User avatar
EV_Builder
Posts: 954
Joined: Tue Apr 28, 2020 3:50 pm
Location: The Netherlands
Contact:

Re: Cleanly get variables in and out of various CPP files

Post by EV_Builder »

i missed this question until now but the best way of coding is OOP.

so make reusable classes and instantiate objects.

those objects have properties and that's the way togo.
Converting an Porsche Panamera
see http://www.wdrautomatisering.nl for bespoke modules.
User avatar
janosch
Posts: 189
Joined: Tue Jun 30, 2020 9:23 am
Location: London, UK
Contact:

Re: Cleanly get variables in and out of various CPP files

Post by janosch »

tom91 wrote: Tue Aug 02, 2022 7:06 pm ...
I need to declare the object with extern in front to allow multi file usage.
...
Hey Tom,

you can check how Johannes is doing it in his code:
https://github.com/jsphuebner/stm32-template
https://github.com/jsphuebner/stm32-car
(I adapted this for my needs and it works well, but embedded programming comes with a learning curve even for a seasoned developer)

Check his main function in stm32-car, he makes objects, and puts them on stack from main, making them available elsewhere in the program:
https://github.com/jsphuebner/stm32-car ... r.cpp#L794

Also, check out digio_prj, anain_prj and especially param_prj. Everything that is a param is exposed via UART so you can connect your laptop to it with a UART to USB dongle and type commands, or connect an ESP8266 and get a web interface like for the inverter.
https://github.com/jsphuebner/stm32-car ... aram_prj.h
He wrote a readme in both repositories that is worth taking a look at.

Otherwise sharing variables via extern works, you want to be careful on microprocessors though how much memory you allocate (at runtime that goes on the HEAP if you do malloc), how many objects you instantiate etc, so I would suggest you create all your objects at the beginning once and then no further ones at runtime. Also, with bare metal programming you don't have an operating system that helps you, so you have to be careful with this stuff.
User avatar
EV_Builder
Posts: 954
Joined: Tue Apr 28, 2020 3:50 pm
Location: The Netherlands
Contact:

Re: Cleanly get variables in and out of various CPP files

Post by EV_Builder »

Albeit taste and vision is a thing i dont like the parameter approach.
The reason is that you introduce further dependencies. Because those parameters need to exist and their nameing unique.

If parameters feed an object or class it should be done outside said class or object. That's cleanly IMHO.
Converting an Porsche Panamera
see http://www.wdrautomatisering.nl for bespoke modules.
Post Reply