I'm trying to use the Adafruit SPI Flash (on an ItsyBitsy M0 Express if it matters) and I've been chasing my own tail on trying to get it to compile for the better part of a day. I suspect this isn't so much an issue with the library as with me simply not understanding how to do a class within a class but I'm hoping someone here can help.
Yes, I've searched the forum already. I've tried about 1/2 dozen different methods I've found on this forum and other but haven't been able to get any of them to work.
What I'm trying to do is to use an instance of Adafruit_SPIFlash _flash and an instance of Adafruit_W25Q16BV_FatFs inside a class of my own creation.
What I want is for all functions inside my class to have access to the two above instances. Nothing else in the program needs them so I don't want them in the main sketch.
The library is fine, the board is fine, I can compile, load and run the sample programs from the library.
I've tried so many different methods that I've lost track, but below was my last attempt. The compiler errors are coming on the lines where I try to initialize the flash classes.
#ifndef config_h
#define config_h
#include "Arduino.h"
#include <Adafruit_SPIFlash.h>
#include <Adafruit_SPIFlash_FatFs.h>
class diyConfig {
private:
String _Version; // i.e. "v2.0.b1"
String _fileName; // i.e. "config.txt" or "spod.cfg"
byte _firstRun; // 9 = No, anything else = Yes,
byte _var1;
byte _var2;
byte _var3;
byte _var4;
byte _var5;
boolean _fileSystemGood;
boolean createConfig();
Adafruit_SPIFlash _flash;
Adafruit_W25Q16BV_FatFs _fatfs;
public:
diyConfig(String fileName, String Version);
String getVersion();
boolean getfilesystemGood();
boolean readConfig();
boolean saveConfig();
};
#endif
#include "Arduino.h"
#include <SPI.h>
#include <Adafruit_SPIFlash.h>
#include <Adafruit_SPIFlash_FatFs.h>
#include "config.h"
//Constants for SPI Flash
#define FLASH_TYPE SPIFLASHTYPE_W25Q16BV
#if (SPI_INTERFACES_COUNT == 1)
#define FLASH_SS SS // Flash chip SS pin.
#define FLASH_SPI_PORT SPI // What SPI port is Flash on?
#else
#define FLASH_SS SS1 // Flash chip SS pin.
#define FLASH_SPI_PORT SPI1 // What SPI port is Flash on?
#endif
diyConfig::diyConfig(String fileName, String Version)
{
_fileName = fileName;
_Version = Version;
//Initialize SPIFlash system
Adafruit_SPIFlash _flash(FLASH_SS, &FLASH_SPI_PORT); // Use hardware SPI
Adafruit_W25Q16BV_FatFs _fatfs(flash);
if (!_flash.begin(FLASH_TYPE)) {
_fileSystemGood = true;
}
else {
_fileSystemGood = false;
}
} //diyConfig Constructor
boolean diyConfig::saveConfig() {
} //saveConfig
boolean diyConfig::createConfig() {
} ///createConfig
boolean diyConfig::readConfig() {
String tmpStr;
File readFile = _fatfs.open(_fileName, FILE_READ);
if (!readFile) {
//File wasn't found, call the createConfig routine to set up a default file.
createConfig();
return false;
}
else {
//Read the lines here
String dummyStr = readFile.readStringUntil('\n');
_Version = readFile.readStringUntil('\n');
tmpStr = readFile.readStringUntil('\n');
_var1 = tmpStr.toInt();
tmpStr = readFile.readStringUntil('\n');
_var2 = tmpStr.toInt();
tmpStr = readFile.readStringUntil('\n');
_var3 = tmpStr.toInt();
tmpStr = readFile.readStringUntil('\n');
_var4 = tmpStr.toInt();
tmpStr = readFile.readStringUntil('\n');
_var5 = tmpStr.toInt();
readFile.close();
return true;
}
} //readConfig
-K
config.h (690 Bytes)
config.cpp (1.81 KB)
You need to use an Initializer List. See Item #3 HERE .
Also, don't run '_flash.begin' in your class's constructor. Instead provide your class with a .begin() method to be called from setup().
system
March 12, 2019, 2:59pm
3
The compiler errors are coming on the lines where I try to initialize the flash classes.
But, you failed to share them.
PaulS:
But, you failed to share them.
Given that my question was how to properly do a class within a class I didn't think they were actually relevant, but here you go if they'll help:
Arduino: 1.8.5 (Windows 7), Board: "Adafruit ItsyBitsy M0"
sketch\config.cpp: In constructor 'diyConfig::diyConfig(String, String)':
config.cpp:17: error: no matching function for call to 'Adafruit_SPIFlash::Adafruit_SPIFlash()'
diyConfig::diyConfig(String fileName, String Version)
^
sketch\config.cpp:17:53: note: candidates are:
In file included from sketch\config.cpp:3:0:
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:96:3: note: Adafruit_SPIFlash::Adafruit_SPIFlash(int8_t, SPIClass*)
Adafruit_SPIFlash(int8_t ss, SPIClass *spiinterface=&SPI);
^
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:96:3: note: candidate expects 2 arguments, 0 provided
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:95:3: note: Adafruit_SPIFlash::Adafruit_SPIFlash(int8_t, int8_t, int8_t, int8_t)
Adafruit_SPIFlash(int8_t clk, int8_t miso, int8_t mosi, int8_t ss);
^
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:95:3: note: candidate expects 4 arguments, 0 provided
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:93:7: note: constexpr Adafruit_SPIFlash::Adafruit_SPIFlash(const Adafruit_SPIFlash&)
class Adafruit_SPIFlash : public Print {
^
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:93:7: note: candidate expects 1 argument, 0 provided
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:93:7: note: constexpr Adafruit_SPIFlash::Adafruit_SPIFlash(Adafruit_SPIFlash&&)
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash.h:93:7: note: candidate expects 1 argument, 0 provided
config.cpp:17: error: no matching function for call to 'Adafruit_W25Q16BV_FatFs::Adafruit_W25Q16BV_FatFs()'
diyConfig::diyConfig(String fileName, String Version)
^
sketch\config.cpp:17:53: note: candidates are:
In file included from sketch\config.cpp:4:0:
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash_FatFs.h:186:3: note: Adafruit_W25Q16BV_FatFs::Adafruit_W25Q16BV_FatFs(Adafruit_SPIFlash&)
Adafruit_W25Q16BV_FatFs(Adafruit_SPIFlash& flash):
^
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash_FatFs.h:186:3: note: candidate expects 1 argument, 0 provided
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash_FatFs.h:184:7: note: constexpr Adafruit_W25Q16BV_FatFs::Adafruit_W25Q16BV_FatFs(const Adafruit_W25Q16BV_FatFs&)
class Adafruit_W25Q16BV_FatFs: public Adafruit_SPIFlash_FatFs {
^
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash_FatFs.h:184:7: note: candidate expects 1 argument, 0 provided
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash_FatFs.h:184:7: note: constexpr Adafruit_W25Q16BV_FatFs::Adafruit_W25Q16BV_FatFs(Adafruit_W25Q16BV_FatFs&&)
C:\Users\kad\Documents\Arduino\libraries\Adafruit_SPIFlash/Adafruit_SPIFlash_FatFs.h:184:7: note: candidate expects 1 argument, 0 provided
config.cpp:24: error: 'flash' was not declared in this scope
Adafruit_W25Q16BV_FatFs _fatfs(flash);
^
exit status 1
no matching function for call to 'Adafruit_SPIFlash::Adafruit_SPIFlash()'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
-K
system
March 12, 2019, 3:39pm
5
I didn't think they were actually relevant
Well, they clearly show that you are not creating instances of the classes correctly. So, I think that they ARE relevant.
diyConfig::diyConfig(String fileName, String Version): _flash(FLASH_SS, &FLASH_SPI_PORT), _fatfs(flash)
{
is the proper way. Delete the local variables in the constructor then.
gfvalvo:
You need to use an Initializer List. See Item #3 HERE .
Also, don't run '_flash.begin' in your class's constructor. Instead provide your class with a .begin() method to be called from setup().
Thank you, that technique did the trick.
-K