diff --git a/src/mqtt_esp32/mqtt_esp.ino b/src/mqtt_esp32/mqtt_esp32.ino similarity index 86% rename from src/mqtt_esp32/mqtt_esp.ino rename to src/mqtt_esp32/mqtt_esp32.ino index 1c7339cf021bc0d24163ffa37bc15de20be2555d..890e69b8c25ff8013978ce3ad968290a03e3bf7a 100644 --- a/src/mqtt_esp32/mqtt_esp.ino +++ b/src/mqtt_esp32/mqtt_esp32.ino @@ -68,12 +68,18 @@ void setup() uint8_t* mqtt_packet_to_raw(uint8_t* packet, unsigned int* length) { + int cnt = 0; for(int i = 0; i < *length; i++) { - if(packet[i] == ':') + if(packet[i] == '\n') { - *length -= i - 2 - 2; - return packet + i + 2; + cnt++; + } + + if(cnt >= 3) + { + *length -= i - 1; + return packet + i + 1; } } @@ -82,21 +88,30 @@ uint8_t* mqtt_packet_to_raw(uint8_t* packet, unsigned int* length) void send_data_to_matrix(uint8_t idx, uint8_t idy, uint8_t* data, uint32_t length) { + // ADD ID TO LEN length += 2; // SEND UART PACKET HEADER SerialPort.print((char)0x77); - SerialPort.print((char)length); + SerialPort.print((char)length-100); + uint8_t crc = 0; // SEND MATRIX ID SerialPort.print((char)idx); SerialPort.print((char)idy); - uint8_t crc = 0; + // COMPUTE CRC FOR ID + crc = crc ^ idx; + crc = crc ^ idy; + // SEND RGB DATA 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; @@ -108,7 +123,8 @@ void send_data_to_matrix(uint8_t idx, uint8_t idy, uint8_t* data, uint32_t lengt uint8_t rgb_1 = rgb & 0x0F; // COMPUTE CRC - crc = crc ^ rgb_0 ^ rgb_1; + crc = crc ^ rgb_0; + crc = crc ^ rgb_1; // SEND RGB SerialPort.print((char) rgb_0); @@ -116,7 +132,7 @@ void send_data_to_matrix(uint8_t idx, uint8_t idy, uint8_t* data, uint32_t lengt } // SEND UART PACKET FOOTER - SerialPort.print((char)(crc ^ length)); + SerialPort.print((char)(crc ^ (length-100))); SerialPort.print((char)0xAA); } @@ -128,12 +144,18 @@ void callback(char *topic, byte *payload, unsigned int length) // GET RAW DATA uint8_t* raw_data = mqtt_packet_to_raw(payload, &length); + if(raw_data == NULL) + { + return; + } // SEND UART PACKET FOR EACH MATRIX for(int x = 0; x < cluster_size_x; x++) { for(int y = cluster_size_y - 1; y >= 0; y--) { + Serial.println("FOR LOOP"); + // SEND UART PACKET send_data_to_matrix(x, y, raw_data, IMG_DATA_SIZE); diff --git a/src/mqtt_hepialight/MAIN.PY b/src/mqtt_hepialight/MAIN.PY index be26910636ef865d0af773485e59a58c12b9cef8..e0bfe0ccc5e32ceec138a1ce8097cdda171674d1 100644 --- a/src/mqtt_hepialight/MAIN.PY +++ b/src/mqtt_hepialight/MAIN.PY @@ -1 +1,47 @@ -afficher_texte("MQTT") \ No newline at end of file +r = 0xA2142F +p = 0x7E2F8E + +data32 = [0xFFFFFF] * 100 + +r_16 = 0xF800 +w_16 = 0xFFFF +y_16 = 0xfea7 +b_16 = 0x0000 + +data16 = [b_16, y_16] * 50 + +def color16_to_32(color): + color = int(color) + r = ((color >> 11) & 0xFF) * 2**3 + g = ((color >> 5) & 0xFF) * 2**2 + b = (color & 0xFF) * 2**3 + return r << 16 | g << 8 | b + +def disp_img_data(data): + for i in range(NBR_LIGNES): + for j in range(NBR_COLONNES): + allumer_led(i, j, data[i * NBR_LIGNES + j]) + + +def data_to_color16(data): + d_color16 = [] + + id_x = data[0] + id_y = data[1] + + afficher_texte(f"{len(data)}") + + for i in range(0, len(data), 2): + d_color16.append(data[i] << 8 | data[i+1]) + + return list(map(color16_to_32, d_color16)) + +#data32 = list(map(color16_to_32, data16)) +r_data = [] +while(True): + r_data = recevoir_msg(N) + if len(r_data) > 0: + break + +data32 = data_to_color16(r_data) +disp_img_data(data32) \ No newline at end of file diff --git a/src/mqtt_hepialight/USERLIB.PY b/src/mqtt_hepialight/USERLIB.PY index 1ac1973288a53b0bbc20b0bb4c61f08dfd598e11..e5f31673722965015c79e2030bff3ea96b9f2d00 100644 --- a/src/mqtt_hepialight/USERLIB.PY +++ b/src/mqtt_hepialight/USERLIB.PY @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import time -from Hepialight import screen, touch, accel +from Hepialight import touch, screen, accel, uart # Constantes NBR_LIGNES = 10 @@ -21,7 +21,6 @@ def delai(delai_en_sec): def _colorToInt(hexColor): return int(hexColor[1:], 16) - _COLORS = { "R": _colorToInt("#A2142F"), "B": _colorToInt("#0072BD"), @@ -33,6 +32,13 @@ _COLORS = { ".": _colorToInt("#000000") } +# Coordonnées +N = 0 +S = 1 +E = 2 +O = 3 + + # use https://goo.gl/gGYfJV to add new chars _TEXT_DICT = { ' ': [], @@ -224,4 +230,20 @@ def max(x, y): return x if x>y else y def min(x, y): - return x if x<y else y \ No newline at end of file + return x if x<y else y + +def envoyer_msg(ID, data): + '''Envoi une valeur vers une des carte voisines + :ID: quelle carte + :data: la valeur + ''' + + uart.send_to(ID, data) + + +def recevoir_msg(ID): + '''Détecte si le pavé tactile supérieur droit est pressé + :returns: True si pressé, False sinon + ''' + + return uart.recv(ID) \ No newline at end of file