动态变量名称构建、连接
Dynamic Variable Name Building, Concatenate
我的问题涉及如何使用 arduino 轻松访问大量变量的主题。我想知道是否有可能在循环中动态更改变量名。我的英语不是最好的,所以让我用我现在正在处理的代码来解释一下。
我有一台小型热敏打印机。打印方式来自adafruit thermal-printing-library
void Adafruit_Thermal::printBitmap(int w, int h, const uint8_t *bitmap, bool fromProgMem) {
...
}
我创建了一个如下所示的位图字体:
static const uint8_t PROGMEM Char_32[] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; // Char 032 ( )
static const uint8_t PROGMEM Char_33[] {
0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00
}; // Char 033 (!)
... for each caracter
我喜欢在这个 for 循环中一个字母一个字母地打印出来:
for(j = 0; j <= messageLength - 1; j++){ // Go through each character in the message.
int character = message[j]; // reads and stores the ASCII value of the current Character
printer.printBitmap(letter_width, letter_height, Char_XX); // i like to print the specific character
}
通常我会采用二维数组并打印如下:
static const uint8_t PROGMEM letter_data[][8] =
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Char 032 ( )
{0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00}, // Char 033 (!)
{0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00} // Char 034 (")
}
for(j = 0; j <= messageLength - 1; j++){ //Go through each character in the message.
int Character = message[j] - 32; // first visible ASCIIcharacter '!' is number 33. reads and stores the ASCII value of the current Character we are dealing with and -32 so the char correspnds to our array.
printer.printBitmap(letter_width, letter_height, letter_data[Character]);
}
但是,我的字母将比 8x8 像素大很多,而且我的数组也变大了。那么有没有可能解决这个问题?
根据您的描述,您 运行 遇到了内存使用问题。官方 Arduino 文档表明 lower-end Arduino 板有 31.5Kb of Flash Memory (32 - 0.5)—或 PROGMEM
,如您的代码中所标记—在其他系统中高达 248Kb (256 - 8) .
根据您的评论,如果每个字符占用 1224 字节的内存(或 1.2Kb),您将在以前的系统中用完 25 个字符的限制(甚至不足以覆盖字母表,仅此而已) !) 和后者的 202 个字符。
所以这确实取决于您要构建的确切系统,但很明显,以您使用的字符大小,即使在更大的系统中,您也将无法容纳所有字符(因为此内存容量将与其他类似大小的结构共享)。
所以,对您的问题的简短回答是:您需要压缩数据。我的建议是减少每个字母使用的数据量(可能将两个维度的大小减半),然后在运行时写入输出设备(即显示设备)时扩展它。
我的问题涉及如何使用 arduino 轻松访问大量变量的主题。我想知道是否有可能在循环中动态更改变量名。我的英语不是最好的,所以让我用我现在正在处理的代码来解释一下。
我有一台小型热敏打印机。打印方式来自adafruit thermal-printing-library
void Adafruit_Thermal::printBitmap(int w, int h, const uint8_t *bitmap, bool fromProgMem) {
...
}
我创建了一个如下所示的位图字体:
static const uint8_t PROGMEM Char_32[] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; // Char 032 ( )
static const uint8_t PROGMEM Char_33[] {
0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00
}; // Char 033 (!)
... for each caracter
我喜欢在这个 for 循环中一个字母一个字母地打印出来:
for(j = 0; j <= messageLength - 1; j++){ // Go through each character in the message.
int character = message[j]; // reads and stores the ASCII value of the current Character
printer.printBitmap(letter_width, letter_height, Char_XX); // i like to print the specific character
}
通常我会采用二维数组并打印如下:
static const uint8_t PROGMEM letter_data[][8] =
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Char 032 ( )
{0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00}, // Char 033 (!)
{0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00} // Char 034 (")
}
for(j = 0; j <= messageLength - 1; j++){ //Go through each character in the message.
int Character = message[j] - 32; // first visible ASCIIcharacter '!' is number 33. reads and stores the ASCII value of the current Character we are dealing with and -32 so the char correspnds to our array.
printer.printBitmap(letter_width, letter_height, letter_data[Character]);
}
但是,我的字母将比 8x8 像素大很多,而且我的数组也变大了。那么有没有可能解决这个问题?
根据您的描述,您 运行 遇到了内存使用问题。官方 Arduino 文档表明 lower-end Arduino 板有 31.5Kb of Flash Memory (32 - 0.5)—或 PROGMEM
,如您的代码中所标记—在其他系统中高达 248Kb (256 - 8) .
根据您的评论,如果每个字符占用 1224 字节的内存(或 1.2Kb),您将在以前的系统中用完 25 个字符的限制(甚至不足以覆盖字母表,仅此而已) !) 和后者的 202 个字符。
所以这确实取决于您要构建的确切系统,但很明显,以您使用的字符大小,即使在更大的系统中,您也将无法容纳所有字符(因为此内存容量将与其他类似大小的结构共享)。
所以,对您的问题的简短回答是:您需要压缩数据。我的建议是减少每个字母使用的数据量(可能将两个维度的大小减半),然后在运行时写入输出设备(即显示设备)时扩展它。