diff --git a/src/mqtt_esp32/mqtt_esp32.ino b/src/mqtt_esp32/mqtt_esp32.ino index fad86e25a0978f2755a6509f4b682b85c24afb23..e66ffe9329f946d9c0e859d7ebdee2f534673d59 100644 --- a/src/mqtt_esp32/mqtt_esp32.ino +++ b/src/mqtt_esp32/mqtt_esp32.ino @@ -4,7 +4,7 @@ #define MATRIX_SIZE 10 // 10x10 #define IMG_DATA_SIZE MATRIX_SIZE * MATRIX_SIZE * 3 //DATA SIZE FOR ONE MATRIX = RGB * MATRIX_SIZE -#define IS_TEXT 1 +#define IS_TEXT 0 // WiFi const char *ssid = "uni-ete2"; // Enter your WiFi name @@ -13,7 +13,8 @@ const char *password = "uni-ete2-esp32"; // Enter WiFi password // MQTT Broker const char *mqtt_broker = "192.168.1.103"; const char *topic_send = "fromesp"; -const char *topic_rcv = "toesp"; +const char *topic_img = "toesp/image"; +const char *topic_txt = "toesp/text"; const int mqtt_port = 1883; WiFiClient espClient; @@ -64,7 +65,13 @@ void setup() // Publish and subscribe client.publish(topic_send, "Hi, I'm ESP32 ^^"); - client.subscribe(topic_rcv); + client.subscribe(topic_img); + client.subscribe(topic_txt); +} + +int d2_to_flat(uint8_t x, uint8_t y, uint32_t pitch) +{ + return (x * pitch) + y; } uint8_t* mqtt_packet_to_raw(uint8_t* packet, unsigned int* length) @@ -97,35 +104,59 @@ void send_data_to_matrix(uint8_t idx, uint8_t idy, uint8_t* data, uint32_t lengt uint8_t send_len = 13; - // SEND RGB DATA - for(int i = 0; i < length; i+=3) + // RGB DATA PROCESS + // HARDCODED + for(int x = (cluster_size_y - 1 - idy) * 10; x < (cluster_size_y - idy) * 10; x+=1) { - // Serial.print((char)data[i]); - // Serial.print((char)data[i+1]); - // Serial.print((char)data[i+2]); - - // COMPUTE COLOR IN 16 BITS - uint8_t r = data[i] & 0x1F; - uint8_t g = data[i+1] & 0x3F; - uint8_t b = data[i+2] & 0x1F ; - uint16_t rgb = r << 11 | g << 5 | b; - // Serial.print(rgb); - // Serial.print(' '); - - // SPLIT ON 8 BITS - uint8_t rgb_0 = rgb >> 8; - uint8_t rgb_1 = rgb & 0xFF; - //Serial.println(rgb_0); - // Serial.print(' '); - //Serial.println(rgb_1); - - // SEND RGB - //SerialPort.print((char) rgb_0); - //SerialPort.print((char) rgb_1); - //send_len += 2; - raw_data[send_len++] = rgb_0; - raw_data[send_len++] = rgb_1; + for(int y = idx * 30; y < (idx+1) * 30; y+=3) + { + // Serial.println() + int i = d2_to_flat(x, y, 30 * cluster_size_y); + + // COMPUTE COLOR IN 16 BITS + uint8_t r = data[i] & 0x1F; + uint8_t g = data[i+1] & 0x3F; + uint8_t b = data[i+2] & 0x1F ; + uint16_t rgb = r << 11 | g << 5 | b; + + // SPLIT ON 8 BITS + uint8_t rgb_0 = rgb >> 8; + uint8_t rgb_1 = rgb & 0xFF; + + // SEND RGB + raw_data[send_len++] = rgb_0; + raw_data[send_len++] = rgb_1; + } } + + // for(int i = 0; i < length; i+=3) + // { + // // Serial.print((char)data[i]); + // // Serial.print((char)data[i+1]); + // // Serial.print((char)data[i+2]); + + // // COMPUTE COLOR IN 16 BITS + // uint8_t r = data[i] & 0x1F; + // uint8_t g = data[i+1] & 0x3F; + // uint8_t b = data[i+2] & 0x1F ; + // uint16_t rgb = r << 11 | g << 5 | b; + // // Serial.print(rgb); + // // Serial.print(' '); + + // // SPLIT ON 8 BITS + // uint8_t rgb_0 = rgb >> 8; + // uint8_t rgb_1 = rgb & 0xFF; + // //Serial.println(rgb_0); + // // Serial.print(' '); + // //Serial.println(rgb_1); + + // // SEND RGB + // //SerialPort.print((char) rgb_0); + // //SerialPort.print((char) rgb_1); + // //send_len += 2; + // raw_data[send_len++] = rgb_0; + // raw_data[send_len++] = rgb_1; + // } // // SEND MATRIX ID // Serial.println(idx); // Serial.println(idy); @@ -172,7 +203,7 @@ void callback(char *topic, byte *payload, unsigned int length) // SerialPort.print((char)202); // SerialPort.print((char)0xaa); - if(IS_TEXT) + if(strcmp(topic, topic_txt) == 0) { char data[100]; char header[] = "PT;1,0;MOVING;16711680;0.1;"; @@ -211,17 +242,16 @@ void callback(char *topic, byte *payload, unsigned int length) } // SEND UART PACKET FOR EACH MATRIX - for(int x = 0; x < cluster_size_x; x++) + for(int x_mat = 0; x_mat < cluster_size_x; x_mat++) { - for(int y = cluster_size_y - 1; y >= 0; y--) + for(int y_mat = cluster_size_y - 1; y_mat >= 0; y_mat--) { //Serial.println("FOR LOOP"); - // SEND UART PACKET - send_data_to_matrix(x, y, raw_data, IMG_DATA_SIZE); + send_data_to_matrix(x_mat, y_mat, raw_data, IMG_DATA_SIZE); // MOVE FORWARD IN DATA - raw_data += IMG_DATA_SIZE; + //raw_data += IMG_DATA_SIZE; } } }