声明 TFT 屏幕的多个实例 class?

Declaring multiple instances of a TFT screen class?

毫无疑问,由于我对 C/C++ 缺乏百科知识,我在尝试初始化 TFT 屏幕的多个实例时发现自己陷入了泥潭 class。 TFT 屏幕是 Adafruit_SSD1331,我想要一个小草图控件,而不是其中一个具有相同代码的控件。

这些是我遇到的错误:

slapbmp.ino:61:5: error: 'tft' in 'class Adafruit_SSD1331' does not name a type
slapbmp.ino:62:5: error: 'tft' in 'class Adafruit_SSD1331' does not name a type
slapbmp.ino:63:3: error: missing type-name in typedef-declaration

...当我尝试编译这段代码时:

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <SD.h>
#include <SPI.h>


// If we are using the hardware SPI interface, these are the pins (for future ref)
#define sclk 13
#define mosi 11
#define rst  9

#define cs   10
#define dc   8

#define cs2  5
#define dc2  4


// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

// to draw images from the SD card, we will share the hardware SPI interface

namespace STD {
  typedef struct Adafruit_SSD1331
  {
  } tft;
}  

namespace initScreens {
  typedef struct {
    Adafruit_SSD1331::tft scr1 = Adafruit_SSD1331(cs, dc, rst);
    Adafruit_SSD1331::tft scr2 = Adafruit_SSD1331(cs2, dc2, rst);
  };
};

// For Arduino Uno/Duemilanove, etc
//  connect the SD card with MOSI going to pin 11, MISO going to pin 12 and SCK going to pin 13 (standard)
//  Then pin 4 goes to CS (or whatever you have set up)
#define SD_CS 3    // Set the chip select line to whatever you use (4 doesnt conflict with the library)
#define SD_CS2 2

// the file itself
File bmpFile;

// information we extract about the bitmap file
int bmpWidth, bmpHeight;
uint8_t bmpDepth, bmpImageoffset;

void setup(void) {  //...

请注意,我正在尝试以一种无需修改任何*.h 文件的方式使用该结构。

这看起来像是命名空间的问题。 编译器错误告诉您编译器无法找到名称,找不到的原因是名称 tft 位于此处的 STD 命名空间内。您需要修复该问题或更改代码的设计方式。

鉴于这是 C++,我会做一些更改以利用 C++ 语言功能:

//get rid of some of the #defines
const int cs = 10;
const int dc = 8;

//Make a struct to contain info about the screens 
struct Screens {
    Adafruit_SSD1331 scr1;
    Adafruit_SSD1331 scr2;

    Screens():
       scr1(cs, dc, rst),
       scr2(cs2, dc2, rst)
    { }
};

如果这是使用 Arduino 约定,那么您可以在 setup 中实例化此 class 一次。 (或者在进入主循环之前放置在适当的地方)