https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html
PROGMEM
关键字,原基于AVR单片机使用的,为了节省RAM空间,将字符串放入闪存中,然后在需要时将其加载到RAM中。
✨ESP8266作为32位单片机,在检索和操作存储在闪存flash中的字符串时,必须以4字节(32bit)为单位从闪存flash中读取,如果没有使用32位对齐方式访问读取,将会导致分割错误,ESP8266将崩溃。必须从闪存读取32位对齐在用于esp8266的Arduino IDE中,以下函数可以实现从使用program存储的闪存中检索字符串:
int memcmp_P(const void* buf1, PGM_VOID_P buf2P, size_t size);
void* memccpy_P(void* dest, PGM_VOID_P src, int c, size_t count);
void* memmem_P(const void* buf, size_t bufSize, PGM_VOID_P findP, size_t findPSize);
void* memcpy_P(void* dest, PGM_VOID_P src, size_t count);
char* strncpy_P(char* dest, PGM_P src, size_t size);
char* strcpy_P(dest, src)
char* strncat_P(char* dest, PGM_P src, size_t size);
char* strcat_P(dest, src)
int strncmp_P(const char* str1, PGM_P str2P, size_t size);
int strcmp_P(str1, str2P)
int strncasecmp_P(const char* str1, PGM_P str2P, size_t size);
int strcasecmp_P(str1, str2P)
size_t strnlen_P(PGM_P s, size_t size);
size_t strlen_P(strP)
char* strstr_P(const char* haystack, PGM_P needle);
int printf_P(PGM_P formatP, ...);
int sprintf_P(char *str, PGM_P formatP, ...);
int snprintf_P(char *str, size_t strSize, PGM_P formatP, ...);
int vsnprintf_P(char *str, size_t strSize, PGM_P formatP, va_list ap);
这里面有很多函数,实际上它们是标准c函数的版本,适合从esp8266 32位对齐的闪存中读取。它们都取a,本质上就是a。在底层,这些函数都使用一个进程来确保读取4个字节,并返回请求字节。(
_P ,PGM_P,const char *
)
#define PROGMEM ICACHE_RODATA_ATTR
#define ICACHE_RODATA_ATTR __attribute__((section(".irom.text")))
#define PGM_P const char *
#define PGM_VOID_P const void *
#define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))
//include WString.h
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
#define F(string_literal) (FPSTR(PSTR(string_literal)))
- ✨将变量放在flash的
.irom.text
部分的位置。
void myfunction(void) {
PGM_P xyz = PSTR("Store this string in flash");
const char * abc = PSTR("Also Store this string in flash");
}
static const char xyz[] PROGMEM = "This is a string stored in flash. Len = %u";
void setup() {
Serial.begin(115200); Serial.println();
Serial.println( FPSTR(xyz) ); // just prints the string, must convert it to FlashStringHelper first using FPSTR().
Serial.printf_P( xyz, strlen_P(xyz)); // use printf with PROGMEM string
}
void setup() {
Serial.begin(115200); Serial.println();
Serial.println( F("This is an inline string")); //
Serial.printf_P( PSTR("This is an inline string using printf %s"), "hello");
}
PROGMEM
将数组存储在闪存flash中并读取。const size_t len_xyz = 30;
const uint8_t xyz[] PROGMEM = {
0x53, 0x61, 0x79, 0x20, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x74, 0x6f, 0x20, 0x4d, 0x79, 0x20, 0x4c, 0x69, 0x74, 0x74,
0x6c, 0x65, 0x20, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x00};
void setup() {
Serial.begin(115200); Serial.println();
uint8_t * buf = new uint8_t[len_xyz];
if (buf) {
memcpy_P(buf, xyz, len_xyz);
Serial.write(buf, len_xyz); // output the buffer.
}
}
const size_t len_xyz = 30;
const uint8_t xyz[] PROGMEM = {
0x53, 0x61, 0x79, 0x20, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x74, 0x6f, 0x20, 0x4d, 0x79, 0x20, 0x4c, 0x69, 0x74, 0x74,
0x6c, 0x65, 0x20, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x00
};
void setup() {
Serial.begin(115200); Serial.println();
for (int i = 0; i < len_xyz; i++) {
uint8_t byteval = pgm_read_byte(xyz + i);
Serial.write(byteval); // output the buffer.
}
}
✨当处理大量文本时,如一个带有液晶显示屏的项目,设置一个字符串数组通常很方便。因为字符串本身就是数组,这实际上是一个二维数组的例子。
这些通常是庞大的结构,所以将它们放入程序内存中往往是可取的。下面的代码说明了这个思路。
// Define Strings
const char string_0[] PROGMEM = "String 0";
const char string_1[] PROGMEM = "String 1";
const char string_2[] PROGMEM = "String 2";
const char string_3[] PROGMEM = "String 3";
const char string_4[] PROGMEM = "String 4";
const char string_5[] PROGMEM = "String 5";
// Initialize Table of Strings
const char* const string_table[] PROGMEM = { string_0, string_1, string_2, string_3, string_4, string_5 };
char buffer[30]; // buffer for reading the string to (needs to be large enough to take the longest string
void setup() {
Serial.begin(9600);
Serial.println("OK");
}
void loop() {
for (int i = 0; i < 6; i++) {
strcpy_P(buffer, (char*)pgm_read_dword(&(string_table[i])));
Serial.println(buffer);
delay(500);
}
}