[SOLVED] Use SPI library with 'multiple files'

Mod edit (@harrisonhjones): Solution: #include "Particle.h" in your file

I’ve got a very basic question, but I cannot figure out how to solve it.
When using the SPI.begin(), SPI.transfer() commands as described in https://docs.particle.io/reference/firmware/photon/#spi this works perfectly fine in the .ino file.
However when I create a file subroutine.cpp and try to call SPI.begin() or SPI.transfer() from this file I receive the

error: 'SPI' was not declared in this scope

upon compiling.
I tried including:

#include <SPI.h>

but this results in a

fatal error: SPI.h: No such file or directory

I assume this has something todo with running the .ino code through a pre-processor (Multiple files in Spark Build IDE) and not the .cpp code?

So to summarise: how to call the SPI commands from a non-.ino file?

Photon firmware: 0.6.0
Using the web IDE: https://build.particle.io/

Thanks!

Just insert this at the top of your .cpp file(s):

#include "Particle.h"

The reason is that .ino files get this inserted before compiling, but there are no built-in includes for .cpp files.

5 Likes

Perfect! Solved the problem directly!

2 Likes