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.
Cleanly get variables in and out of various CPP files
-
- Posts: 1307
- Joined: Fri Mar 01, 2019 9:15 pm
- Location: Bristol
- Has thanked: 103 times
- Been thanked: 216 times
Re: Cleanly get variables in and out of various CPP files
Okay done some more digging.
I need to declare the object with extern in front to allow multi file usage.
So in the main
And then in the New.cpp
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;
Code: Select all
#include "New.h"
#include "data.h"
extern data data;
void New::SetVar(uint8_t temp)
{
data.SetVarmyass(temp);
}
- celeron55
- Posts: 776
- Joined: Thu Jul 04, 2019 3:04 pm
- Location: Finland
- Has thanked: 28 times
- Been thanked: 110 times
- Contact:
Re: Cleanly get variables in and out of various CPP files
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.
-
- Posts: 1307
- Joined: Fri Mar 01, 2019 9:15 pm
- Location: Bristol
- Has thanked: 103 times
- Been thanked: 216 times
Re: Cleanly get variables in and out of various CPP files
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
- EV_Builder
- Posts: 1199
- Joined: Tue Apr 28, 2020 3:50 pm
- Location: The Netherlands
- Has thanked: 16 times
- Been thanked: 33 times
- Contact:
Re: Cleanly get variables in and out of various CPP files
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.
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 BMS modules.
see http://www.wdrautomatisering.nl for bespoke BMS modules.
- janosch
- Posts: 307
- Joined: Tue Jun 30, 2020 9:23 am
- Location: London, UK
- Has thanked: 71 times
- Been thanked: 59 times
- Contact:
Re: Cleanly get variables in and out of various CPP files
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.
- EV_Builder
- Posts: 1199
- Joined: Tue Apr 28, 2020 3:50 pm
- Location: The Netherlands
- Has thanked: 16 times
- Been thanked: 33 times
- Contact:
Re: Cleanly get variables in and out of various CPP files
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.
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 BMS modules.
see http://www.wdrautomatisering.nl for bespoke BMS modules.