Library TFT_eSPI. Assign color to each pixel. ESP32-2432S028R

Good morning,
I purchased a ESP32-2432S028R which has an ESP32-wroom_32, 2.8" display with ILI9341 driver.
I would like to see on the screen the color of each pixel that I receive in OSC.
I send the 3 data R, G, B at 254*128 pixels.
What is the correct function to use from the TFT_eSPI library?

Here the program I made but I can only see a white mouvement on the screen.

 #include <OSCBoards.h>
#include <OSCBundle.h>
#include <OSCData.h>
#include <OSCMatch.h>
#include <OSCMessage.h>
#include <OSCTiming.h>
#include <SLIPEncodedSerial.h>
/*---------------------------------------------------------------------------------------------

  Open Sound Control (OSC) library for the ESP8266/ESP32

  Example for receiving open sound control (OSC) messages on the ESP8266/ESP32
  Send integers '0' or '1' to the address "/led" to turn on/off the built-in LED of the esp8266.

  This example code is in the public domain.

--------------------------------------------------------------------------------------------- */
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>

#include <WiFiMulti.h>
WiFiMulti wifiMulti;

#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();
#define	BLACK 0x0000
#define WHITE 0xFFFF

WiFiUDP Udp;

const unsigned int outPort = 9999;    // remote port (not needed for receive)
const unsigned int localPort = 8004;  // local port to listen for UDP packets (here's where we send the packets)

OSCErrorCode error;
unsigned int ledState = LOW;  // LOW means led is *on*. not use

//------------ END  ESP and OSC setting
#define LED_BUILTIN 16
#define BUILTIN_LED LED_BUILTIN

int numberOfPIXELS = 254*128;
#define numberOfpixels numberOfPIXELS// 64

int size;

void setup() {
  //tft.init();
  //tft.fillScreen(TFT_BLACK); // Background is black
   Serial.begin(115200);
   pinMode(BUILTIN_LED, OUTPUT);
   digitalWrite(BUILTIN_LED, ledState);  // turn *on* led
   delay(10);

    // Connect to the best WiFi network
    wifiMulti.addAP("SFR_A080_EXT", "rtws8bze84czbb5aat33"); // Loic
    wifiMulti.addAP("E5576_BD52_EXT", "564d76L4Mbm"); // miniBox

    Serial.println("Connecting Wifi...");
    if(wifiMulti.run() == WL_CONNECTED) 
    {
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(WiFi.localIP());
    }
 
   IPAddress local_IP(192, 168, 8, 104);// 145
    // Set your Gateway IP address
   IPAddress gateway(192, 168, 1, 1);
   IPAddress subnet(255, 255, 0, 0);

    WiFi.mode(WIFI_STA);
  if (WiFi.config(local_IP, gateway, subnet) == false) {
    Serial.print("echec de config");
  }

  while (WiFi.status() != WL_CONNECTED) {
  ////  digitalWrite(BUILTIN_LED, HIGH);  // turn *on* led
    delay(500);
     digitalWrite(BUILTIN_LED, LOW);  // turn *on* led
    Serial.print(".");
    } 
  
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Starting UDP");
  Udp.begin(localPort);
  Serial.print("Local port: ");
  
#ifdef ESP32
  Serial.print("LocalportESP32: ");
  Serial.println(localPort);
#else

  Serial.println(Udp.localPort());
#endif
  //  turn off led when connected to a wifi network
  digitalWrite(BUILTIN_LED, HIGH);  // turn off* led
 
}

void loop() {
    OSCMessage msg;
    size = Udp.parsePacket();
    
    if (size > 0) {
    while (size--) {
      msg.fill(Udp.read());
    }
    if (!msg.hasError()) {
      msg.dispatch("/matrixdata",assignDataParsed); // 
    }

    else {
      error = msg.getError();
      Serial.print("error: ");
      Serial.println(error);
    }
  }
}

void assignDataParsed(OSCMessage &msg) {
  for (uint8_t i = 0; i < numberOfpixels; i++) {
    tft.drawPixel(msg.getInt(i*3+0), i, WHITE);      
  }
}

Hi @bvking
On the french speaking forum, please speak in french, or ask moderators to move your topic to the root of arduino forum, thanks.

Pandaroux007

T'inquiètes pas @bvking parle parfaitement le français, tout simplement il ne sait plus sur quel forum il se trouve.

Voici le prototype, qui explique comment utiliser cette fonction :

// Draw a pixel blended with the background pixel colour (bg_color) specified,  return blended colour
// If the bg_color is not specified, the background pixel colour will be read from TFT or sprite
uint16_t drawPixel(int32_t x, int32_t y, uint32_t color, uint8_t alpha, uint32_t bg_color = 0x00FFFFFF);

Merci beaucoup lesept.

Dans mon cas, j'ai 3 données par couleur.
Comment je peux transformer mes 3 données r, g ,b (3 int de 0 à 255) pour l'assigner à
uint32_t color. ?
Apparemment il faut que la donnée averagecouleur soit en hexadécimal?

J'ai crée une variable qui est la moyenne des trois valeurs r, g , b.
Maintenant comment convertir une int de 0 à 255 en hexadecimal?

j'ai essayé le programme ci dessous pour un écran de 50*10 pixels, mais évidemment c'est pas aussi simple.

void assignDataParsed(OSCMessage &msg) {

  for (uint8_t i = 0; i < (50); i++) {
     for (uint8_t j = 0; j < (10); j++) {
        averageColor=int ((msg.getInt(j*3+0)+msg.getInt(j*3+1)+msg.getInt(j*3+2))/3);
        Serial.print((i)),Serial.print ( " " ), Serial.print((j)); Serial.print ( " " ); Serial.println ( averageColor);     
         tft.drawEllipse((i*10)+200, (j*10)+200, 10, 10,  averageColor); //
         tft.drawPixel(i+150, j+150, averageColor, WHITE, BLACK); 
  }
  
  }
}

En fait il n'est pas nécessaire de travailler en hexadécimal, l'important c'est que ça soit un uint32_t donc un entier non signé de 32 bits. Le microcontrôleur travaille en binaire, l'hexadécimal est uniquement là pour la facilité de l'utilisateur.

Donc tu as 3 valeurs en uint8_t pour r, g et b. Pour obtenir la couleur, tu peux faire :
uint32_t couleur = r << 16 + g << 8 + b;

J'ai vu qu'il existe une fonction drawPixel plus simple d'utilisation :
drawPixel(int32_t x, int32_t y, uint32_t color)

Il y a, dans la librairie, des fonctions pour passer d'un format de couleurs dans un autre.

  // Colour conversion
           // Convert 8-bit red, green and blue to 16 bits
  uint16_t color565(uint8_t red, uint8_t green, uint8_t blue);

           // Convert 8-bit colour to 16 bits
  uint16_t color8to16(uint8_t color332);
           // Convert 16-bit colour to 8 bits
  uint8_t  color16to8(uint16_t color565);

           // Convert 16-bit colour to/from 24-bit, R+G+B concatenated into LS 24 bits
  uint32_t color16to24(uint16_t color565);
  uint32_t color24to16(uint32_t color888);
1 Like

Merci pour l'aide.

Mais tous compte fait je pense que je dois faire cette conversion depuis max/msp pour gagner de la ram dans l'arduino.

En fait l'écran fait 240*320. Je me demande comment je peux recevoir de manière optimale 76800 données !!?

Je peux envoyer que 1500 données depuis max/msp. Alors je devrais découper mon image en 52 case car 240*320/1500 = 51.7. Je dois trouver un meilleur moyen d'envoyer des infos depuis max/msp et un meilleur moyen d'assigner chaque pixel avec l'Arduino.

Mais faute de mieux, j'ai programmé l'arduino de manière à ce qu'elle reçoive 1500 donnés à chaque loop successive afin de pouvoir contrôler un écran de 3000 pixels, soit 50*60 pixels.

Pourriez vous me dire si je suis sur la bonne voie?

#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>

#include <WiFiMulti.h>
WiFiMulti wifiMulti;

#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();
#define	BLACK 0x0000
#define WHITE 0xFFFF

WiFiUDP Udp;

const unsigned int outPort = 9999;    // remote port (not needed for receive)
const unsigned int localPort = 8004;  // local port to listen for UDP packets (here's where we send the packets)

OSCErrorCode error;
unsigned int ledState = LOW;  // LOW means led is *on*. not use

//------------ END  ESP and OSC setting
#define LED_BUILTIN 16
#define BUILTIN_LED LED_BUILTIN

//int numberOfPIXELS = 254*128;
int numberOfPIXELS = 64;

#define numberOfpixels numberOfPIXELS// 64

int size;
int averageColor;
int frame, modFrame;

void setup() {
  frame=0;
  tft.init();
  tft.fillScreen(TFT_BLACK); // Background is black

 // averageColor=int ((msg.getInt(i*3+0)+msg.getInt(i*3+1)+msg.getInt(i*3+2))/3);
   Serial.begin(115200);
   pinMode(BUILTIN_LED, OUTPUT);
   digitalWrite(BUILTIN_LED, ledState);  // turn *on* led
   delay(10);

    // Connect to the best WiFi network
    wifiMulti.addAP("SFR-d078", "XXXXXXX"); // sandra
    wifiMulti.addAP("SFR_A080_EXT", "XXXXXXX"); // Loic
    wifiMulti.addAP("E5576_BD52_EXT", "XXXXXXX"); // miniBox

    Serial.println("Connecting Wifi...");
    if(wifiMulti.run() == WL_CONNECTED) 
    {
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(WiFi.localIP());
    }
 
   IPAddress local_IP(192, 168, 8, 104);// 145
    // Set your Gateway IP address
   IPAddress gateway(192, 168, 1, 1);
   IPAddress subnet(255, 255, 0, 0);

    WiFi.mode(WIFI_STA);
  if (WiFi.config(local_IP, gateway, subnet) == false) {
    Serial.print("echec de config");
  }

  while (WiFi.status() != WL_CONNECTED) {
  ////  digitalWrite(BUILTIN_LED, HIGH);  // turn *on* led
    delay(500);
     digitalWrite(BUILTIN_LED, LOW);  // turn *on* led
    Serial.print(".");
    } 
  
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Starting UDP");
  Udp.begin(localPort);
  Serial.print("Local port: ");
  
#ifdef ESP32
  Serial.print("LocalportESP32: ");
  Serial.println(localPort);
#else

  Serial.println(Udp.localPort());
#endif
  //  turn off led when connected to a wifi network
  digitalWrite(BUILTIN_LED, HIGH);  // turn off* led
 
}

void loop() {

    tft.fillScreen(TFT_BLACK); // Background is black
    OSCMessage msg;
    size = Udp.parsePacket();
    
    if (size > 0) {
    while (size--) {
      msg.fill(Udp.read());
    }
    if (!msg.hasError()) {
      msg.dispatch("/matrixdata",assignDataParsed); // 
      msg.dispatch("/matrixdata1",assignDataParsed1)
    }

    else {
      error = msg.getError();
      Serial.print("error: ");
      Serial.println(error);
    }
  }
}

void assignDataParsed(OSCMessage &msg) {
  for (uint8_t i = 0; i < (50/1); i++) {
     for (uint8_t j = 0; j < (30/2); j++) {
     int r= msg.getInt(j*3+0);
     int g= msg.getInt(j*3+1);
     int b= msg.getInt(j*3+2);

     uint32_t couleur = r << 16 + g << 8 + b;
     Serial.print ( " " ); Serial.println ( couleur); 

     averageColor = r << 16 + g << 8 + b;
         tft.drawPixel(i, j,  averageColor); 
  } 
  }
}

void assignDataParsed1(OSCMessage &msg) {
  for (uint8_t i = 0; i < (50/1); i++) {
     for (uint8_t j = 0; j < (30/2); j++) {
     int r= msg.getInt(j*3+0);
     int g= msg.getInt(j*3+1);
     int b= msg.getInt(j*3+2);

     uint32_t couleur = r << 16 + g << 8 + b;
     Serial.print ( " " ); Serial.println ( couleur); 

     averageColor = r << 16 + g << 8 + b;
         tft.drawPixel(i, j+15,  averageColor); 
  } 
  }
}



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