c ++ arduino结构化数组中的多个显示对象
c++ arduino multiple display objects in a structured array
我通过 8 通道 i2c Mux 声明了多个 OLED 显示器 运行:
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display5(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
我希望能够使用这样的标识符访问这些内容:
display[0].print("etc");
display[1].print("etc");
display[2].print("etc");
所以我可以调用这样的函数:
sendTitleDisplay(1);
void sendTitleDisplay(uint8_t id)
{
display[id].print("title example");
}
我将如何定义这些对象以便我可以以这种数组格式访问它们?
您可以像这样初始化数组:
Adafruit_SSD1306 display[]{
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
};
这将为每个 Adafruit_SSD1306
实例使用第一个新推荐的构造函数,clkDuring
使用默认值 400000UL
,clkAfter
使用默认值值 100000UL
.
// NEW CONSTRUCTORS -- recommended for new projects
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
uint32_t clkAfter = 100000UL);
Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);
// DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
int8_t rst_pin, int8_t cs_pin);
Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
Adafruit_SSD1306(int8_t rst_pin = -1);
我通过 8 通道 i2c Mux 声明了多个 OLED 显示器 运行:
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display5(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
我希望能够使用这样的标识符访问这些内容:
display[0].print("etc");
display[1].print("etc");
display[2].print("etc");
所以我可以调用这样的函数:
sendTitleDisplay(1);
void sendTitleDisplay(uint8_t id)
{
display[id].print("title example");
}
我将如何定义这些对象以便我可以以这种数组格式访问它们?
您可以像这样初始化数组:
Adafruit_SSD1306 display[]{
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
};
这将为每个 Adafruit_SSD1306
实例使用第一个新推荐的构造函数,clkDuring
使用默认值 400000UL
,clkAfter
使用默认值值 100000UL
.
// NEW CONSTRUCTORS -- recommended for new projects
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
uint32_t clkAfter = 100000UL);
Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);
// DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
int8_t rst_pin, int8_t cs_pin);
Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
Adafruit_SSD1306(int8_t rst_pin = -1);