VS Arduino
Visual Micro >> Visual Studio 2017, 2019, 2022 >> Share class files between Arduino project and Win32 project
https://www.visualmicro.com/forums/YaBB.pl?num=1588944872

Message started by Pascal Roobrouck on May 8th, 2020 at 1:34pm

Title: Share class files between Arduino project and Win32 project
Post by Pascal Roobrouck on May 8th, 2020 at 1:34pm
I am developing a 'larger' application, and in order to UnitTest the different components, I created additional unit-testing projects (within the Visual Studio Solution), added a reference to my home project, and included the .h and .cpp files from the main project (Add | existing...).
This all works very well, as long as all projects (main project and uni-ttesting projects) are all Arduino-type projects.

Now I want to set a step further, and add some Win32 console projects, so I can do some testing of my components in the Windows environment, for those things that do not touch the MCU hardware. I can only make this work when copying all the .h and .cpp files from the main Arduino project, to the Win32 project folder. I cannot make it work through a reference..

Has anyone done this with success ?
Thanks.


Title: Re: Share class files between Arduino project and Win32 project
Post by Simon Hopkinson on May 8th, 2020 at 1:58pm
In the below Instructable and Video we use standard desktop Unit Testing tools, and Shared projects to test the generic MCU code on your Desktop PC, without having to clone the CPP and H files for the different purposes.

Instructable
YouTube Video

Does this help?

Title: Re: Share class files between Arduino project and Win32 project
Post by Pascal Roobrouck on May 8th, 2020 at 2:04pm
This is certainly useful. Let me digest the tutorials and I will give you feedback on how it went.
Thanks VisualMicro team, great product and fast support!

Title: Re: Share class files between Arduino project and Win32 project
Post by Pascal Roobrouck on May 8th, 2020 at 2:32pm
Your tutorial is for sure an answer to my needs.
I've learned I need to have the 'shared' code in a special 'Arduino Shared Code' type of project. That was new to me.

Further I am still stuck on the following : I use mostly the uint32_t kind of data types, as they have a clearly defined width. They are defined in Arduino.h (for Arduino projects) and in <stdint.h> (for Windows projects).
I tried to solve this with some preprocessor trick, but it is not working.. It looks as if the shared code, when compiled for Win32 does not get the WIN32 token, so stdint.h does not get included..

#ifndef  WIN32
#include <Arduino.h>
#else
#include <stdint.h>
#endif

Title: Re: Share class files between Arduino project and Win32 project
Post by Simon Hopkinson on May 8th, 2020 at 3:15pm
I have tried this in the "SharedCodeProject" from the instructable, and it worked fine for me, though it can depend on a variety of settings.

_WIN32 is defined by VS, so it may be safer to check for that instead / aswell.

[code]#if defined(_WIN32) && !defined(WIN32)
     #define WIN32
#endif

#ifndef WIN32
#include <Arduino.h>
#else
#include <stdint.h>
#endif[/code]


If it still doesn't work, check the top of the output when building the console project (SharedProjectUnitTests project in example) to ensure its Configuration is set to Win32
(if not check the Properties to ensure  the platform set to "Win32" for the "SharedCode" project when building the console project)


Title: Re: Share class files between Arduino project and Win32 project
Post by Pascal Roobrouck on May 8th, 2020 at 3:21pm
ok, thanks.
Just out of curiosity : why do you put the shared code in a '\src\ subfolder ?

Title: Re: Share class files between Arduino project and Win32 project
Post by Simon Hopkinson on May 8th, 2020 at 3:42pm
This is just to begin to build the improved structures also allowed by the Arduino system, which help avoid conflicts as project complexity grows.

The projects are combined as part of the Arduino build process, so not having a structured merged result can easily create issues when all projects come together.

By putting them in \src\, and sub-folders below this to avoid conflicts when building for all platforms (see https://www.visualmicro.com/page/Understanding-Project-File-Structure.aspx). There is also an "\src\arduino folders read me.txt" with details on this when you create a new Arduino Project with Visual Micro.

You do not have to put shared code in an src folder but remember that arduino only supports cpp/c code in project root, \src or any level of sub folder of the src folder. Shared code has to be copied and merged with the project code during build. This happens in a temp folder but needs to be kept in mind.


Title: Re: Share class files between Arduino project and Win32 project
Post by Pascal Roobrouck on May 25th, 2020 at 9:12am
Hello,

I just wanted to reach out and say thank you because the proposed solution is really working very well.
I have now separated my source code into generic and platformSpecific modules.
All generic code can be easily unitTested using Visual Studio's built in Unit Testing.
You can even debug such a unit test with all of Visual Studio's debugging features.

This has significantly improved my development setup and code quality, and from now on I am using this setup as a template for all my developments. So : thank you!

Pascal

VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.