Conditionally including library based on board

29 views
Skip to first unread message

Trygve Laugstøl

unread,
Aug 12, 2015, 11:06:57 AM8/12/15
to Developers
Hi

I have a sketch that I want to support both a Leonardo and a ATTiny85-based board. The ATTiny85 chip doesn't have a hardware SPI implementation, but USI (universial serial interface) that can be used to implement both SPI an I2C.

I was hoping to be able to do something like this:

#ifdef ..
#include <SPI.h>
#endif

#ifdef ..
.. some other implementation for USI
#endif

This works to prevent the inclusion of the SPI.h file when compiling, but the SPI library is still included and so SPI.cpp is compiled. I assume this is because the IDE just looks at the #include lines on the ino file instead of running it through the preprocessor.

Is this case supposed to work and if so, how can i get it to work?

My software already depends on the user having a particular hardware so perhaps I could do some magic with my own boards.txt file, but I was hoping to not having to go there.

-- 
Trygve

Federico Fissore

unread,
Aug 12, 2015, 11:20:59 AM8/12/15
to devel...@arduino.cc
Trygve Laugstøl ha scritto il 10/08/2015 alle 20:29:
>
> This works to prevent the inclusion of the SPI.h file when compiling,
> but the SPI library is still included and so SPI.cpp is compiled. I
> assume this is because the IDE just looks at the #include lines on the
> ino file instead of running it through the preprocessor.
>
> Is this case supposed to work and if so, how can i get it to work?


Unfortunately this is yet-another preprocessor bug. There are many
issues [1] Some work has been done in the past [2] but it failed to work
with complex sketches and its code rotted

I'm currently working on an alternative fix, but it's too early for any demo

Regards

Federico

[1] https://github.com/arduino/Arduino/labels/Component%3A%20Preprocessor
[2] https://github.com/arduino/Arduino/pull/2729

Paul Stoffregen

unread,
Aug 13, 2015, 6:50:56 AM8/13/15
to devel...@arduino.cc
On 08/12/2015 08:20 AM, Federico Fissore wrote:
> Trygve Laugstøl ha scritto il 10/08/2015 alle 20:29:
>>
>> This works to prevent the inclusion of the SPI.h file when compiling,
>> but the SPI library is still included and so SPI.cpp is compiled. I
>> assume this is because the IDE just looks at the #include lines on the
>> ino file instead of running it through the preprocessor.
>>
>> Is this case supposed to work and if so, how can i get it to work?
>
>
> Unfortunately this is yet-another preprocessor bug. There are many
> issues [1] Some work has been done in the past [2] but it failed to
> work with complex sketches and its code rotted
>
> I'm currently working on an alternative fix, but it's too early for
> any demo


After I finish a couple projects I'm doing now, I was planning to dust
#2792 and update for the latest IDE code.

Do you want to take that path, or go with coan & ctags, or maybe do
something else?


Federico Fissore

unread,
Aug 13, 2015, 7:50:09 AM8/13/15
to devel...@arduino.cc
Paul Stoffregen ha scritto il 13/08/2015 alle 12:50:
> After I finish a couple projects I'm doing now, I was planning to dust
> #2792 and update for the latest IDE code.
>
> Do you want to take that path, or go with coan & ctags, or maybe do
> something else?
>

Unfortunately, it is something else.

Arduino Create needs an easier CLI tool for compiling sketches. Running
the current IDE from the command line is fine, but since it's thought
for a single user environment, using it in a concurrent multi user
environment such as a web site make things a harder

Unfortunately again (because I love talking about what I code), there is
still much to decide about this new tool, so you'll have to wait until
the end of August for more news

Regards

Federico

Paul Stoffregen

unread,
Sep 1, 2015, 5:10:51 AM9/1/15
to devel...@arduino.cc
Any more thoughts on how you'd like to deal with this, now that we've
reached the end of August?


Federico Fissore

unread,
Sep 4, 2015, 3:24:34 PM9/4/15
to devel...@arduino.cc
I'v ended up using gcc -M for gathering includes and gcc -E to replace
the current buggy preprocessor
ctags remains as it works very well for generating missing prototypes
and putting them at the right position in the source code

I'm still waiting for some paperwork from the lawyer. Massimo is dealing
with that. I hope to give some good news soon

Federico

Andrew Kroll

unread,
Sep 4, 2015, 3:26:21 PM9/4/15
to devel...@arduino.cc

+1 for doing things in a way that uses the tool that was already provided.
:-)

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.
Reply all
Reply to author
Forward
0 new messages