0

Geetings,我试图从头文件(.h)中读取字符串文件到 arduino。它的编译很好,但我不能调用字符串。当我串行打印字符串时,显示器上只会出现乱码。这是我的arduino代码:

#include <epd4in2.h>
#include <epdif.h>
#include <epdpaint.h>
#include <fonts.h>

#include <SPI.h>
#include "epd4in2.h"
#include "imagedata.h"
#include "epdpaint.h"

#include "list.h"

#define COLORED     0
#define UNCOLORED   1

String test1;
String test2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.print("port test");

  Epd epd;

  if (epd.Init() != 0) {
    Serial.println("e-Paper init failed");
    return;
  }

  test1 = String(DATA_1[0]);\
  Serial.println(test1);
  test2 = String(DATA_1[1]);
  Serial.println(test2);

  /* This clears the SRAM of the e-paper display */
  epd.ClearFrame();

 /* Due to RAM not enough in Arduino UNO, a frame buffer is not allowed. */
 unsigned char image[1500];
 Paint paint(image, 400, 28);    //width should be the multiple of 8 

 paint.Clear(UNCOLORED);
 paint.DrawStringAt(0, 0, "test1", &Font24, COLORED);
 epd.SetPartialWindow(paint.GetImage(), 100, 40, paint.GetWidth(), 
 paint.GetHeight());

 paint.Clear(COLORED);
 paint.DrawStringAt(100, 2, "test2", &Font24, UNCOLORED);
 epd.SetPartialWindow(paint.GetImage(), 0, 64, paint.GetWidth(), 
 paint.GetHeight());

 /* This displays the data from the SRAM in e-Paper module */
 epd.DisplayFrame();

 /* Deep sleep */
 epd.Sleep();
 }

void loop() {
  // put your main code here, to run repeatedly:

}

这里是 .h 文件中的数据:

extern const String DATA_1[] PROGMEM = { "bat", "2244"}; 
extern const String DATA_2[] PROGMEM = { "bmp", "15662"}; 
extern const String DATA_3[] PROGMEM = { "docx", "16670"}; 
extern const String DATA_4[] PROGMEM = { "jpg", "150645"}; 
extern const String DATA_5[] PROGMEM = { "mp3", "9882324"}; 
extern const String DATA_6[] PROGMEM = { "txt", "807"}; 
extern const String DATA_7[] PROGMEM = { "url", "118"};

我使用 .bat 脚本创建了这个列表。

我对此很陌生,因此将不胜感激。亲切的问候路易吉

4

1 回答 1

0

当您使用 PROGMEM 时,初始化值存储在程序存储器中,并将指向该位置的指针提供给变量。

但是当你使用

test1 = String(DATA_1[0]);

代码在 ram 而不是程序存储器中搜索相同的地址。

你可以在这里读到它。 https://www.nongnu.org/avr-libc/user-manual/pgmspace.html

我以前也遇到过同样的问题,但我存储的是字节,而不是字符串。所以我能够侥幸使用 pgm_read_byte。您可能需要付出一些努力才能使程序正常运行。

我希望这个帮助能祝你好运。

于 2018-05-14T16:53:24.123 回答