How to use array here need help thanks

Hi all.
the TFT_eSPI.h's pushImage class works well in the sketch.
how to use array for the jpeg[i] please?

missed the pictures, let me know if need.

Thanks
Adam

#include <TFT_eSPI.h>
#include <SPI.h>
#include "jpeg1.h"
#include "jpeg2.h"
#include "jpeg3.h"
#include "jpeg4.h"
#include "jpeg5.h"

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

void setup(){
    Serial.begin(115200);
    Serial.println("Start");
    tft.init();
    tft.setRotation(3);
    tft.setSwapBytes(true);

    //...............................
  tft.fillCircle(120, 120, 118, TFT_GREEN);
  tft.fillCircle(120, 120, 110, TFT_BLACK);
  
    //...............................
    
}

void loop(){
   tft.pushImage(0, 0,  320, 240, jpeg1);
    delay(150);  
    tft.pushImage(50, 50,  150, 150, jpeg2);
    delay(150);  
    tft.pushImage(0, 0,  320, 240, jpeg3);
    delay(150);  
    tft.pushImage(0, 0,  320, 240, jpeg4);
    delay(150);
    tft.pushImage(0, 0,  320, 240, jpeg5);
    delay(150);
}

Hello

Use an array of pointers

uint16_t const * const jpegs[] = { &jpeg1, &jpeg2, ... };

1 Like

I need to get better at this stuff, so I'll have a go just to see what I get wrong!

const unsigned short * jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};
1 Like

@guix why is the & needed? The name of the array is already a pointer, isn't it?

1 Like

Thanks.

the new code: missed '*'? same result.

void loop() {
  uint16_t const * const jpegs[5] = { &jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5};
  for (int k = 0; k < 5; k++) {
    tft.pushImage(0, 0,  320, 240,  const jpegs[5]);
    delay(150);
  }
}

got ERROR at the array line:

Arduino: 1.8.19 (Windows 7), Board: "ESP32 Dev Module, FTDI Adapter, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Core 1, Core 1, None, Disabled"

ILI9341_Images_GD:9:76: error: cannot convert 'const short unsigned int (*)[76800]' to 'const uint16_t* const' {aka 'const short unsigned int* const'} in initialization

  uint16_t const * const jpegs[5] = { &jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5};

                                                                            ^

ILI9341_Images_GD:9:76: error: cannot convert 'const short unsigned int (*)[76800]' to 'const uint16_t* const' {aka 'const short unsigned int* const'} in initialization

ILI9341_Images_GD:9:76: error: cannot convert 'const short unsigned int (*)[76800]' to 'const uint16_t* const' {aka 'const short unsigned int* const'} in initialization

ILI9341_Images_GD:9:76: error: cannot convert 'const short unsigned int (*)[76800]' to 'const uint16_t* const' {aka 'const short unsigned int* const'} in initialization

ILI9341_Images_GD:9:76: error: cannot convert 'const short unsigned int (*)[76800]' to 'const uint16_t* const' {aka 'const short unsigned int* const'} in initialization

E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD\ILI9341_Images_GD.ino: In function 'void loop()':

ILI9341_Images_GD:51:38: error: expected primary-expression before 'const'

     tft.pushImage(0, 0,  320, 240,   const jpegs[5]);

                                      ^~~~~

exit status 1

cannot convert 'const short unsigned int (*)[76800]' to 'const uint16_t* const' {aka 'const short unsigned int* const'} in initialization



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Thanks.
same as above.

BTW: and the '[76800]' in the error message is the length of the image data.

void loop()
{
   const unsigned short * jpeg[5] = {&jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5};

     for (int k = 0; k < 5; k++) {
       tft.pushImage(0, 0,  320, 240, * jpeg[k]);
      delay(150);
     }

error:

Arduino: 1.8.19 (Windows 7), Board: "ESP32 Dev Module, FTDI Adapter, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Core 1, Core 1, None, Disabled"
E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD\ILI9341_Images_GD.ino: In function 'void loop()':

ILI9341_Images_GD:41:77: error: cannot convert 'const short unsigned int (*)[76800]' to 'const short unsigned int*' in initialization

     const unsigned short * jpeg[5] = {&jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5};

                                                                             ^

ILI9341_Images_GD:41:77: error: cannot convert 'const short unsigned int (*)[76800]' to 'const short unsigned int*' in initialization

ILI9341_Images_GD:41:77: error: cannot convert 'const short unsigned int (*)[76800]' to 'const short unsigned int*' in initialization

ILI9341_Images_GD:41:77: error: cannot convert 'const short unsigned int (*)[76800]' to 'const short unsigned int*' in initialization

ILI9341_Images_GD:41:77: error: cannot convert 'const short unsigned int (*)[76800]' to 'const short unsigned int*' in initialization

ILI9341_Images_GD:44:48: error: no matching function for call to 'pushImage(int, int, int, int, const short unsigned int&)'

        tft.pushImage(0, 0,  320, 240, * jpeg[k]);

                                                ^

In file included from E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD\ILI9341_Images_GD.ino:1:

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\TFT_eSPI_ILI9341/TFT_eSPI.h:561:12: note: candidate: 'void TFT_eSPI::pushImage(int32_t, int32_t, int32_t, int32_t, uint16_t*)' <near match>

   void     pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data);

            ^~~~~~~~~

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\TFT_eSPI_ILI9341/TFT_eSPI.h:561:12: note:   conversion of argument 5 would be ill-formed:

ILI9341_Images_GD:44:39: error: invalid conversion from 'short unsigned int' to 'uint16_t*' {aka 'short unsigned int*'} [-fpermissive]

        tft.pushImage(0, 0,  320, 240, * jpeg[k]);

                                       ^~~~~~~~~

In file included from E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD\ILI9341_Images_GD.ino:1:

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\TFT_eSPI_ILI9341/TFT_eSPI.h:566:12: note: candidate: 'void TFT_eSPI::pushImage(int32_t, int32_t, int32_t, int32_t, const uint16_t*)' <near match>

   void     pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data);

            ^~~~~~~~~

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\TFT_eSPI_ILI9341/TFT_eSPI.h:566:12: note:   conversion of argument 5 would be ill-formed:

ILI9341_Images_GD:44:39: error: invalid conversion from 'short unsigned int' to 'const uint16_t*' {aka 'const short unsigned int*'} [-fpermissive]

        tft.pushImage(0, 0,  320, 240, * jpeg[k]);

                                       ^~~~~~~~~

In file included from E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD\ILI9341_Images_GD.ino:1:

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\TFT_eSPI_ILI9341/TFT_eSPI.h:571:12: note: candidate: 'void TFT_eSPI::pushImage(int32_t, int32_t, int32_t, int32_t, uint8_t*, bool, uint16_t*)' <near match>

   void     pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t  *data, bool bpp8 = true, uint16_t *cmap = nullptr);

            ^~~~~~~~~

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\TFT_eSPI_ILI9341/TFT_eSPI.h:571:12: note:   conversion of argument 5 would be ill-formed:

ILI9341_Images_GD:44:39: error: invalid conversion from 'short unsigned int' to 'uint8_t*' {aka 'unsigned char*'} [-fpermissive]

        tft.pushImage(0, 0,  320, 240, * jpeg[k]);

                                       ^~~~~~~~~

exit status 1

cannot convert 'const short unsigned int (*)[76800]' to 'const short unsigned int*' in initialization



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I don't think that * should be there.

1 Like

Thanks.
I didn't put '*' first, put then got same error.

actually, just one line code inside loop const unsigned short * jpeg[5] = {&jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5}; got same compiling error.

auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

?

1 Like

Thanks.
got ERROR:

Arduino: 1.8.19 (Windows 7), Board: "ESP32 Dev Module, FTDI Adapter, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Core 1, Core 1, None, Disabled"


E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD_ls\ILI9341_Images_GD_ls.ino: In function 'void loop()':

ILI9341_Images_GD_ls:57:13: error: 'jpeg' declared as array of 'auto'

  auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

             ^

ILI9341_Images_GD_ls:57:18: error: 'jpeg1' was not declared in this scope

  auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

                  ^~~~~

ILI9341_Images_GD_ls:57:25: error: 'jpeg2' was not declared in this scope

  auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

                         ^~~~~

ILI9341_Images_GD_ls:57:32: error: 'jpeg3' was not declared in this scope

  auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

                                ^~~~~

ILI9341_Images_GD_ls:57:39: error: 'jpeg4' was not declared in this scope

  auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

                                       ^~~~~

ILI9341_Images_GD_ls:57:46: error: 'jpeg5' was not declared in this scope

  auto jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

                                              ^~~~~

ILI9341_Images_GD_ls:62:21: error: 'pict' was not declared in this scope

     Serial.println( pict[1]);

                     ^~~~

E:\ENGINEERING\DIY\Electronic\ESP32\good\ILI9341_Images_GD_ls\ILI9341_Images_GD_ls.ino:62:21: note: suggested alternative: 'pict5'

     Serial.println( pict[1]);

                     ^~~~

                     pict5

exit status 1

'jpeg' declared as array of 'auto'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Post a github link to where you got those six .h files.

1 Like

yes:

An array of pointers:

const uint16_t *jpgArray[] = {&jpeg1[0], &jpeg2[0], &jpeg3[0], &jpeg4[0], &jpeg5[0]};
1 Like

Great!
that works well.
many thanks to you gfvalvo.

Ok, you got it, well done! But my suggestion was

const unsigned short * jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

and your (correct) suggestion is

const uint16_t *jpgArray[] = {&jpeg1[0], &jpeg2[0], &jpeg3[0], &jpeg4[0], &jpeg5[0]};

"unisgned short" is the same as uint16_t, I'm pretty sure, at least on AVR.

So the only other "difference" is &jpeg1[0] versus jpeg1.

&jpeg1[0] is a pointer to an int

jpeg1 is a pointer to an array of int

In reality, they would have the same value.

Is that right?

1 Like

but different types...
It may has the same numerical values, but the fact that it types are different has some consequences. For example, if you increment &jpeg1[0] it will jump to the next member of the array, but if you increment jpeg1, it will jump over the whole array etc.

edited:
To be correct your line should be
const unsigned short ** jpeg[5] = {jpeg1, jpeg2, jpeg3, jpeg4, jpeg5};

2 Likes

While &jpeg1[0] and jpeg1 are indeed of different types, the types of the elements of array jpeg are the same (uint16_t const*) for the examples shown in post #15.

A type preserving array of pointers to arrays may look something like this,

uint16_t const (* const jpeg[])[76800] {&jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5};

which can be used as follows:

Serial.println((*jpeg[0])[0]);  // First element of first JPG.
Serial.println((*jpeg[1])[1]);  // Second element of second JPG.
// ...

I kept trying different combinations, but couldn't get that data type right.

Perhaps have the compiler do it for us:

decltype(jpeg1) * const jpgArrayrr[] = {&jpeg1, &jpeg2, &jpeg3, &jpeg4, &jpeg5};

then:

  Serial.println((*jpgArrayrr[0])[0], HEX);  // First element of first JPG.
  Serial.println((*jpgArrayrr[1])[1], HEX);  // Second element of second JPG.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.