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

时间:2015-06-03 02:13:49

标签: c++ arduino

毫无疑问,由于我缺乏对C / C ++的百科全书知识,我在尝试初始化TFT屏幕类的多个实例时发现自己陷入了泥潭。 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文件的方式使用结构。

1 个答案:

答案 0 :(得分:1)

这看起来像命名空间的问题。 编译器错误告诉您编译器无法找到名称,因为名称tftSTD命名空间中,因此无法找到该名称。您需要修复它或更改代码的设计方式。

鉴于这是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中实例化此类一次。 (或者在进入主循环之前放置适当的位置)