简体   繁体   English

C ++ / Arduino将指针传递给存储在PROGMEM中的2D数组

[英]C++/Arduino Passing pointer to 2D array stored in PROGMEM

I have several const int 2D arrays globally stored in PROGMEM. 我有几个const int 2D数组全局存储在PROGMEM中。 for example: 例如:

const int image1[][17]PROGMEM = {
  {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
  {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
  {1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0},
  {1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0},
  {1,1,1,0,0,0,1,1,1,0,0,1,1,1,1,0,0}
}

I would like to read out this 2d image array in a function. 我想在函数中读出这个2d图像数组。 Which image to read out should be specified in the argument. 应在参数中指定要读出的图像。

void printImage(image)
{
  // do something with element i,j of image
  pgm_read_byte(image[i][j])
}

I am not very expierenced with the use of pointers etc. but I know that's the way to do it. 我对指针等的使用并不十分了解,但我知道这是做到这一点的方法。 Can you show me how to make it work? 你能告诉我如何让它发挥作用吗?

EDIT1: How I do it now (it works, but it is not elegant); EDIT1:我现在怎么做(它有效,但不优雅); I have the function printImage1() without any arguments, and in the body function I use: 我有没有任何参数的函数printImage1(),并在我使用的body函数中:

pgm_read_byte(&image1[i][j])

to read out image1. 读出image1。 For image2, image3 etc. I copy the function printImage1 and change imgage1 from above to image2, image3 etc. This is redundant programming thats why I want to specify the image in the argumant using only one function printImage. 对于image2,image3等我复制了printImage1函数并将imgage1从上面改为image2,image3等。这是冗余编程,这就是为什么我只想使用一个函数printImage来指定参数中的图像。

pgm_read_byte takes the address of the byte you want to read. pgm_read_byte获取您要读取的字节的地址。 You could use 你可以用

pgm_read_byte(&image[i][j])

or 要么

pgm_read_byte(image[i] + j)

Note, however, that it is unusual to use pgm_read_byte on an array of int (which are 2 bytes wide on AVR). 但请注意,在int数组(在AVR上为2字节宽)上使用pgm_read_byte是不常见的。 You should probably make image1 a 2D array of uint8_t or use pgm_read_word . 您应该将image1 pgm_read_word uint8_t的2D数组或使用pgm_read_word

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM