add websocket
parent
6528ec2605
commit
8405391c45
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"unordered_set": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"system_error": "cpp"
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,19 +8,19 @@
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:nodemcu-32s2]
|
[env:esp32-s3-devkitc-1]
|
||||||
platform = espressif32@6.3.2
|
platform = espressif32
|
||||||
board = nodemcu-32s2
|
board = esp32-s3-devkitc-1
|
||||||
board_build.mcu = esp32
|
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
board_build.mcu = esp32s3
|
||||||
lib_deps =
|
lib_deps =
|
||||||
mrfaptastic/ESP32 HUB75 LED MATRIX PANEL DMA Display@^3.0.12
|
mrfaptastic/ESP32 HUB75 LED MATRIX PANEL DMA Display@^3.0.12
|
||||||
https://github.com/mrcodetastic/ESP32-HUB75-MatrixPanel-DMA.git
|
https://github.com/mrcodetastic/ESP32-HUB75-MatrixPanel-DMA.git
|
||||||
adafruit/Adafruit GFX Library@^1.11.10
|
adafruit/Adafruit GFX Library@^1.11.10
|
||||||
fastled/FastLED@^3.7.1
|
fastled/FastLED@^3.7.1
|
||||||
bblanchon/ArduinoJson@^7.1.0
|
bblanchon/ArduinoJson@^7.1.0
|
||||||
; build_flags = -DCORE_DEBUG_LEVEL=5
|
links2004/WebSockets@^2.5.3
|
||||||
; -DBOARD_HAS_PSRAM
|
build_flags =
|
||||||
; -mfix-esp32-psram-cache-issue
|
-DBOARD_HAS_PSRAM
|
||||||
|
-mfix-esp32-psram-cache-issue
|
||||||
|
|
448
src/main.cpp
448
src/main.cpp
|
@ -1,4 +1,4 @@
|
||||||
//#include </ESP32-HUB75-MatrixPanel-DMA-master/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h>
|
// #include </ESP32-HUB75-MatrixPanel-DMA-master/src/ESP32-HUB75-MatrixPanel-I2S-DMA.h>
|
||||||
#include <ESP32-VirtualMatrixPanel-I2S-DMA.h>
|
#include <ESP32-VirtualMatrixPanel-I2S-DMA.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
@ -7,281 +7,273 @@
|
||||||
#include <FastLED.h>
|
#include <FastLED.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <WebSocketsServer.h>
|
||||||
const char* ssid = "VSMI-Guest";
|
// const char* ssid = "VSMI-Guest";
|
||||||
const char* password = "h3ll0vsmi";
|
// const char* password = "h3ll0vsmi";
|
||||||
|
WebSocketsServer webSocket = WebSocketsServer(81); // Sử dụng cổng 81
|
||||||
// const char* ssid = "VIETTEL_txXZQ27z";
|
std::vector<uint8_t> frame_data; // Dùng để lưu trữ dữ liệu nhận được
|
||||||
// const char* password = "CCF5A3K9";
|
const char *ssid = "VIETTEL_txXZQ27z";
|
||||||
|
const char *password = "CCF5A3K9";
|
||||||
|
|
||||||
// Set your Static IP address
|
// Set your Static IP address
|
||||||
IPAddress local_IP(192, 168, 1, 243);
|
// IPAddress local_IP(192, 168, 1, 243);
|
||||||
IPAddress gateway(192, 168, 1, 1);
|
// IPAddress gateway(192, 168, 1, 1);
|
||||||
IPAddress subnet(255, 255, 255, 0);
|
|
||||||
|
|
||||||
// IPAddress local_IP(192, 168, 2, 243);
|
|
||||||
// IPAddress gateway(192, 168, 2, 1);
|
|
||||||
// IPAddress subnet(255, 255, 255, 0);
|
// IPAddress subnet(255, 255, 255, 0);
|
||||||
|
|
||||||
|
IPAddress local_IP(192, 168, 2, 243);
|
||||||
|
IPAddress gateway(192, 168, 2, 1);
|
||||||
|
IPAddress subnet(255, 255, 255, 0);
|
||||||
// IPAddress primaryDNS(8, 8, 8, 8); //optional
|
// IPAddress primaryDNS(8, 8, 8, 8); //optional
|
||||||
// IPAddress secondaryDNS(8, 8, 4, 4); //optional
|
// IPAddress secondaryDNS(8, 8, 4, 4); //optional
|
||||||
#define R1_PIN 25
|
#define R1_PIN 4
|
||||||
#define G1_PIN 26
|
#define G1_PIN 5
|
||||||
#define B1_PIN 27
|
#define B1_PIN 6
|
||||||
#define R2_PIN 14
|
#define R2_PIN 7
|
||||||
#define G2_PIN 12
|
#define G2_PIN 15
|
||||||
#define B2_PIN 13
|
#define B2_PIN 16
|
||||||
#define A_PIN 23
|
#define A_PIN 18
|
||||||
#define B_PIN 22 // Changed from library default
|
#define B_PIN 8 // Changed from library default
|
||||||
#define C_PIN 5
|
#define C_PIN 3
|
||||||
#define D_PIN 17
|
#define D_PIN 42
|
||||||
#define E_PIN 32 // required for 1/32 scan panels, like 64x64px. Any available pin would do, i.e. IO32
|
#define E_PIN 39 // required for 1/32 scan panels, like 64x64px. Any available pin would do, i.e. IO32
|
||||||
#define LAT_PIN 4
|
#define LAT_PIN 40
|
||||||
#define OE_PIN 15
|
#define OE_PIN 2
|
||||||
#define CLK_PIN 16
|
#define CLK_PIN 41
|
||||||
|
|
||||||
|
|
||||||
// Configure for your panel(s) as appropriate!
|
// Configure for your panel(s) as appropriate!
|
||||||
#define PANEL_WIDTH 64
|
#define PANEL_WIDTH 64
|
||||||
#define PANEL_HEIGHT 64
|
#define PANEL_HEIGHT 64
|
||||||
#define PANELS_NUMBER 4
|
#define PANELS_NUMBER 4
|
||||||
|
|
||||||
#define PANE_WIDTH PANEL_WIDTH * PANELS_NUMBER
|
#define PANE_WIDTH PANEL_WIDTH *PANELS_NUMBER
|
||||||
#define PANE_HEIGHT PANEL_HEIGHT
|
#define PANE_HEIGHT PANEL_HEIGHT
|
||||||
|
|
||||||
#define MAX_UDP_SIZE 1450
|
#define MAX_UDP_SIZE 1468
|
||||||
CRGB currentColor;
|
CRGB currentColor;
|
||||||
CRGBPalette16 palettes[] = {HeatColors_p, LavaColors_p, RainbowColors_p, RainbowStripeColors_p, CloudColors_p};
|
CRGBPalette16 palettes[] = {HeatColors_p, LavaColors_p, RainbowColors_p, RainbowStripeColors_p, CloudColors_p};
|
||||||
CRGBPalette16 currentPalette = palettes[0];
|
CRGBPalette16 currentPalette = palettes[0];
|
||||||
CRGB ColorFromCurrentPalette(uint8_t index = 0, uint8_t brightness = 255, TBlendType blendType = LINEARBLEND) {
|
CRGB ColorFromCurrentPalette(uint8_t index = 0, uint8_t brightness = 255, TBlendType blendType = LINEARBLEND)
|
||||||
return ColorFromPalette(currentPalette, index, brightness, blendType);
|
{
|
||||||
|
return ColorFromPalette(currentPalette, index, brightness, blendType);
|
||||||
}
|
}
|
||||||
uint16_t time_counter = 0, cycles = 0, fps = 0;
|
uint16_t time_counter = 0, cycles = 0, fps = 0;
|
||||||
unsigned long fps_timer;
|
unsigned long fps_timer;
|
||||||
WiFiUDP udp;
|
WiFiUDP udp;
|
||||||
unsigned int localUdpPort = 5000;
|
unsigned int localUdpPort = 5000;
|
||||||
const int bufferSize = PANE_WIDTH*PANE_HEIGHT*2; // Adjust based on your data size
|
const int bufferSize = PANE_WIDTH * PANE_HEIGHT * 2; // Adjust based on your data size
|
||||||
char incomingPacket[bufferSize]; // Buffer for incoming packets
|
char incomingPacket[bufferSize]; // Buffer for incoming packets
|
||||||
|
|
||||||
//char *incomingPacket_psram = (char *) ps_malloc(bufferSize*sizeof(char));
|
char *incomingPacket_psram = (char *)ps_malloc(bufferSize * sizeof(char));
|
||||||
|
|
||||||
#define NUM_PIXELS PANE_WIDTH*PANE_HEIGHT
|
#define NUM_PIXELS PANE_WIDTH *PANE_HEIGHT
|
||||||
|
|
||||||
// Biến trạng thái
|
// Biến trạng thái
|
||||||
int dataOffset = 0;
|
int dataOffset = 0;
|
||||||
bool dataComplete = false;
|
bool dataComplete = false;
|
||||||
uint16_t arr[PANE_HEIGHT*PANE_WIDTH];
|
|
||||||
MatrixPanel_I2S_DMA *dma_display = nullptr;
|
MatrixPanel_I2S_DMA *dma_display = nullptr;
|
||||||
//Another way of creating config structure
|
// Another way of creating config structure
|
||||||
//Custom pin mapping for all pins
|
// Custom pin mapping for all pins
|
||||||
HUB75_I2S_CFG::i2s_pins _pins={R1_PIN, G1_PIN, B1_PIN, R2_PIN, G2_PIN, B2_PIN, A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, LAT_PIN, OE_PIN, CLK_PIN};
|
HUB75_I2S_CFG::i2s_pins _pins = {R1_PIN, G1_PIN, B1_PIN, R2_PIN, G2_PIN, B2_PIN, A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, LAT_PIN, OE_PIN, CLK_PIN};
|
||||||
HUB75_I2S_CFG mxconfig(
|
HUB75_I2S_CFG mxconfig(
|
||||||
PANEL_WIDTH, // width
|
PANEL_WIDTH, // width
|
||||||
PANEL_HEIGHT, // height
|
PANEL_HEIGHT, // height
|
||||||
PANELS_NUMBER, // chain length
|
PANELS_NUMBER, // chain length
|
||||||
_pins, // pin mapping
|
_pins, // pin mapping
|
||||||
HUB75_I2S_CFG::FM6126A // driver chip
|
HUB75_I2S_CFG::FM6126A // driver chip
|
||||||
);
|
);
|
||||||
|
|
||||||
//Pixel pixels[NUM_PIXELS];
|
// Pixel pixels[NUM_PIXELS];
|
||||||
uint8_t tempBuffer[NUM_PIXELS * 2];
|
uint8_t tempBuffer[NUM_PIXELS * 2];
|
||||||
|
bool recieve_done = false;
|
||||||
|
// Use PSRAM
|
||||||
|
// uint8_t *tempBuffer = (uint8_t *)ps_malloc(sizeof(uint8_t *));
|
||||||
|
|
||||||
//Use PSRAM
|
int receivedBytes = 0; // Số byte đã nhận
|
||||||
//uint8_t *tempBuffer = (uint8_t *)ps_malloc(sizeof(uint8_t *));
|
|
||||||
|
|
||||||
int receivedBytes = 0; // Số byte đã nhận
|
|
||||||
bool newFrame = true;
|
bool newFrame = true;
|
||||||
uint32_t rgb565_to_rgb888(uint16_t rgb565) {
|
uint32_t rgb565_to_rgb888(uint16_t rgb565)
|
||||||
|
{
|
||||||
// Tách các kênh màu từ giá trị RGB565
|
// Tách các kênh màu từ giá trị RGB565
|
||||||
uint8_t r = (rgb565 >> 11) & 0x1F; // 5 bit cho đỏ
|
uint8_t r = (rgb565 >> 11) & 0x1F; // 5 bit cho đỏ
|
||||||
uint8_t g = (rgb565 >> 5) & 0x3F; // 6 bit cho xanh lục
|
uint8_t g = (rgb565 >> 5) & 0x3F; // 6 bit cho xanh lục
|
||||||
uint8_t b = rgb565 & 0x1F; // 5 bit cho xanh dương
|
uint8_t b = rgb565 & 0x1F; // 5 bit cho xanh dương
|
||||||
|
|
||||||
// Chuyển đổi các kênh màu từ 5/6 bit sang 8 bit
|
// Chuyển đổi các kênh màu từ 5/6 bit sang 8 bit
|
||||||
r = (r * 255 + 15) / 31; // Chuyển đổi từ 5 bit sang 8 bit
|
r = (r * 255 + 15) / 31; // Chuyển đổi từ 5 bit sang 8 bit
|
||||||
g = (g * 255 + 31) / 63; // Chuyển đổi từ 6 bit sang 8 bit
|
g = (g * 255 + 31) / 63; // Chuyển đổi từ 6 bit sang 8 bit
|
||||||
b = (b * 255 + 15) / 31; // Chuyển đổi từ 5 bit sang 8 bit
|
b = (b * 255 + 15) / 31; // Chuyển đổi từ 5 bit sang 8 bit
|
||||||
|
|
||||||
// Ghép các giá trị RGB888 thành một giá trị 32 bit
|
// Ghép các giá trị RGB888 thành một giá trị 32 bit
|
||||||
return (r << 16) | (g << 8) | b;
|
return (r << 16) | (g << 8) | b;
|
||||||
}
|
}
|
||||||
void receivePixels(char* packet, int length) {
|
void receivePixels(const std::vector<uint8_t> &data)
|
||||||
// if (newFrame) {
|
{
|
||||||
// newFrame = false;
|
// Chuyển đổi dữ liệu từ RGB565
|
||||||
// }
|
for (int y = 0; y < PANE_HEIGHT; y++)
|
||||||
if (receivedBytes + length > sizeof(tempBuffer)) {
|
{
|
||||||
length = sizeof(tempBuffer) - receivedBytes; // Giới hạn độ dài
|
for (int x = 0; x < PANE_WIDTH; x++)
|
||||||
}
|
{
|
||||||
memcpy(tempBuffer + receivedBytes, packet, length);
|
if (x <= 128)
|
||||||
receivedBytes += length;
|
{
|
||||||
// Kiểm tra xem toàn bộ dữ liệu có được nhận đầy đủ chưa
|
int index = y * 128 + x;
|
||||||
if (receivedBytes >= NUM_PIXELS * 2) {
|
uint16_t rgb565 = (data[index * 2] << 8) | data[index * 2 + 1];
|
||||||
//dma_display->clearScreen();
|
dma_display->drawPixel(x, y, rgb565);
|
||||||
for (int y = 0; y < PANE_HEIGHT; y++) {
|
}
|
||||||
for (int x = 0; x < PANE_WIDTH; x++) {
|
else
|
||||||
|
{
|
||||||
|
int x1 = x - 128;
|
||||||
|
int index = ((128 * 64) + (y * 128 + x1));
|
||||||
|
uint16_t rgb565 = (data[index * 2] << 8) | data[index * 2 + 1];
|
||||||
|
dma_display->drawPixel(x, y, rgb565);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(PANE_WIDTH<=128)
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
switch(type) {
|
||||||
|
case WStype_BIN:
|
||||||
|
// Nhận dữ liệu và thêm vào frame_data
|
||||||
|
frame_data.insert(frame_data.end(), payload, payload + length);
|
||||||
|
// Kiểm tra nếu dữ liệu đã đầy đủ
|
||||||
|
if (frame_data.size() == 16384*2) { // 16,384 bytes
|
||||||
|
// Xử lý dữ liệu frame_data
|
||||||
|
receivePixels(frame_data);
|
||||||
|
//Serial.println(frame_data.size());
|
||||||
|
// Ví dụ: chuyển đổi và hiển thị lên ma trận LED
|
||||||
|
frame_data.clear(); // Xóa dữ liệu sau khi xử lý
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void receivePixels(char *packet, int length)
|
||||||
|
{
|
||||||
|
// if (newFrame) {
|
||||||
|
// newFrame = false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (receivedBytes + length > sizeof(tempBuffer))
|
||||||
|
{
|
||||||
|
length = sizeof(tempBuffer) - receivedBytes; // Giới hạn độ dài
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(tempBuffer + receivedBytes, packet, length);
|
||||||
|
receivedBytes += length;
|
||||||
|
// Kiểm tra xem toàn bộ dữ liệu có được nhận đầy đủ chưa
|
||||||
|
if (receivedBytes == NUM_PIXELS * 2)
|
||||||
|
{
|
||||||
|
// dma_display->clearScreen();
|
||||||
|
for (int y = 0; y < PANE_HEIGHT; y++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < PANE_WIDTH; x++)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (PANE_WIDTH <= 128)
|
||||||
{
|
{
|
||||||
int index = (y * PANE_WIDTH + x);
|
int index = (y * PANE_WIDTH + x);
|
||||||
uint16_t color = (tempBuffer[index*2] << 8) | tempBuffer[index*2 + 1];
|
uint16_t color = (tempBuffer[index * 2] << 8) | tempBuffer[index * 2 + 1];
|
||||||
uint32_t rgb888 = rgb565_to_rgb888(color);
|
uint32_t rgb888 = rgb565_to_rgb888(color);
|
||||||
uint8_t r = (rgb888 >> 16) & 0xFF; // Kênh đỏ
|
uint8_t r = (rgb888 >> 16) & 0xFF; // Kênh đỏ
|
||||||
uint8_t g = (rgb888 >> 8) & 0xFF; // Kênh xanh lục
|
uint8_t g = (rgb888 >> 8) & 0xFF; // Kênh xanh lục
|
||||||
uint8_t b = rgb888 & 0xFF; // Kênh xanh dương
|
uint8_t b = rgb888 & 0xFF; // Kênh xanh dương
|
||||||
//pixels[index].color = color;
|
// pixels[index].color = color;
|
||||||
//dma_display->drawPixel(x, y, color);
|
// dma_display->drawPixel(x, y, color);
|
||||||
if(y<=31)dma_display->drawPixelRGB888(x,y,r,g,b);
|
// if(y<=31)dma_display->drawPixelRGB888(x,y,r,g,b);
|
||||||
else dma_display->drawPixelRGB888(x,y,r,b,g);
|
// else dma_display->drawPixelRGB888(x,y,r,b,g);
|
||||||
|
dma_display->drawPixelRGB888(x, y, r, g, b);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(x<=128){
|
if (x <= 128)
|
||||||
int index = (y * PANE_WIDTH + x);
|
{
|
||||||
uint16_t color = (tempBuffer[index*2] << 8) | tempBuffer[index*2 + 1];
|
int index = (y * 128 + x);
|
||||||
|
uint16_t color = (tempBuffer[index * 2] << 8) | tempBuffer[index * 2 + 1];
|
||||||
uint32_t rgb888 = rgb565_to_rgb888(color);
|
uint32_t rgb888 = rgb565_to_rgb888(color);
|
||||||
uint8_t r = (rgb888 >> 16) & 0xFF; // Kênh đỏ
|
uint8_t r = (rgb888 >> 16) & 0xFF; // Kênh đỏ
|
||||||
uint8_t g = (rgb888 >> 8) & 0xFF; // Kênh xanh lục
|
uint8_t g = (rgb888 >> 8) & 0xFF; // Kênh xanh lục
|
||||||
uint8_t b = rgb888 & 0xFF; // Kênh xanh dương
|
uint8_t b = rgb888 & 0xFF; // Kênh xanh dương
|
||||||
//pixels[index].color = color;
|
dma_display->drawPixelRGB888(x, y, r, g, b);
|
||||||
//dma_display->drawPixel(x, y, color);
|
}
|
||||||
if(y<=31)dma_display->drawPixelRGB888(x,y,r,g,b);
|
else
|
||||||
else dma_display->drawPixelRGB888(x,y,r,b,g);
|
{
|
||||||
}
|
|
||||||
else
|
int x1 = x - 128;
|
||||||
{
|
int index = ((128 * 64) + (y * 128 + x1));
|
||||||
int y1 = y + 1;
|
uint16_t color = (tempBuffer[index * 2] << 8) | tempBuffer[index * 2 + 1];
|
||||||
int x1 = x-128;
|
|
||||||
int index = (y * PANE_WIDTH + x);
|
|
||||||
uint16_t color = (tempBuffer[index*2] << 8) | tempBuffer[index*2 + 1];
|
|
||||||
uint32_t rgb888 = rgb565_to_rgb888(color);
|
uint32_t rgb888 = rgb565_to_rgb888(color);
|
||||||
uint8_t r = (rgb888 >> 16) & 0xFF; // Kênh đỏ
|
uint8_t r = (rgb888 >> 16) & 0xFF; // Kênh đỏ
|
||||||
uint8_t g = (rgb888 >> 8) & 0xFF; // Kênh xanh lục
|
uint8_t g = (rgb888 >> 8) & 0xFF; // Kênh xanh lục
|
||||||
uint8_t b = rgb888 & 0xFF; // Kênh xanh dương
|
uint8_t b = rgb888 & 0xFF; // Kênh xanh dương
|
||||||
//pixels[index].color = color;
|
dma_display->drawPixelRGB888(x, y, r, g, b);
|
||||||
//dma_display->drawPixel(x, y, color);
|
}
|
||||||
if(y1<=31)dma_display->drawPixelRGB888(x1,y1,r,g,b);
|
|
||||||
else dma_display->drawPixelRGB888(x1,y1,r,b,g);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
recieve_done = false;
|
||||||
// Đặt lại bộ nhớ tạm và biến nhận bytes
|
// Đặt lại bộ nhớ tạm và biến nhận bytes
|
||||||
//memset(tempBuffer, 0, sizeof(tempBuffer));
|
memset(tempBuffer, 0, sizeof(tempBuffer));
|
||||||
receivedBytes = 0;
|
receivedBytes = 0;
|
||||||
newFrame = true;
|
newFrame = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
|
||||||
void receivePixels(char* packet, int length) {
|
|
||||||
if (newFrame) {
|
|
||||||
newFrame = false;
|
|
||||||
// Xóa màn hình nếu cần
|
|
||||||
// dma_display->clearScreen();
|
|
||||||
}
|
|
||||||
int BUFFER_SIZE = PANE_WIDTH*PANE_HEIGHT*2;
|
|
||||||
int remainingSpace = BUFFER_SIZE - receivedBytes;
|
|
||||||
int bytesToCopy = (length > remainingSpace) ? remainingSpace : length;
|
|
||||||
|
|
||||||
// Sao chép dữ liệu vào buffer tạm
|
|
||||||
memcpy(tempBuffer + receivedBytes, packet, bytesToCopy);
|
|
||||||
receivedBytes += bytesToCopy;
|
|
||||||
|
|
||||||
// Kiểm tra xem toàn bộ dữ liệu có được nhận đầy đủ chưa
|
|
||||||
if (receivedBytes >= BUFFER_SIZE) {
|
|
||||||
dataComplete = true;
|
|
||||||
|
|
||||||
// Xử lý dữ liệu khi đã nhận đầy đủ
|
|
||||||
int index = 0;
|
|
||||||
for (int i = 0; i < BUFFER_SIZE; i += 2) {
|
|
||||||
if (index < PANE_WIDTH * PANE_HEIGHT) {
|
|
||||||
uint16_t color = (tempBuffer[i] << 8) | tempBuffer[i + 1];
|
|
||||||
pixels[index].color = color;
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Xóa bộ nhớ tạm và đặt lại biến nhận bytes
|
|
||||||
receivedBytes = 0;
|
|
||||||
memset(tempBuffer, 0, BUFFER_SIZE);
|
|
||||||
newFrame = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
// void displayPixels() {
|
|
||||||
// for (int y = 0; y < PANE_HEIGHT; y++) {
|
|
||||||
// for (int x = 0; x < PANE_WIDTH; x++) {
|
|
||||||
// int index = (y * PANE_WIDTH + x);
|
|
||||||
// uint16_t color = pixels[index].color;
|
|
||||||
// if (color != 0) {
|
|
||||||
// dma_display->drawPixel(x, y, color);
|
|
||||||
// }
|
|
||||||
// else{
|
|
||||||
// dma_display->drawPixel(x, y, 0);
|
|
||||||
// }
|
|
||||||
// pixels[index].color = 0;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
void plasma_palette()
|
void plasma_palette()
|
||||||
{
|
{
|
||||||
// for (int x = 0; x < PANE_WIDTH; x++) {
|
|
||||||
// for (int y = 0; y < PANE_HEIGHT; y++) {
|
|
||||||
// int16_t v = 128;
|
|
||||||
// uint8_t wibble = sin8(time_counter);
|
|
||||||
// v += sin16(x * wibble * 3 + time_counter);
|
|
||||||
// v += cos16(y * (128 - wibble) + time_counter);
|
|
||||||
// v += sin16(y * x * cos8(-time_counter) / 8);
|
|
||||||
|
|
||||||
// currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
for (int x = 0; x < PANE_WIDTH; x++)
|
||||||
// if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
{
|
||||||
// else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
if (x <= 128)
|
||||||
// }
|
{
|
||||||
// }
|
for (int y = 0; y < PANE_HEIGHT; y++)
|
||||||
|
{
|
||||||
for (int x = 0; x < PANE_WIDTH; x++) {
|
|
||||||
if(x<=128){
|
|
||||||
for (int y = 0; y < PANE_HEIGHT; y++) {
|
|
||||||
int16_t v = 128;
|
int16_t v = 128;
|
||||||
uint8_t wibble = sin8(time_counter);
|
uint8_t wibble = sin8(time_counter);
|
||||||
v += sin16(x * wibble * 3 + time_counter);
|
v += sin16(x * wibble * 3 + time_counter);
|
||||||
v += cos16(y * (128 - wibble) + time_counter);
|
v += cos16(y * (128 - wibble) + time_counter);
|
||||||
v += sin16(y * x * cos8(-time_counter) / 8);
|
v += sin16(y * x * cos8(-time_counter) / 8);
|
||||||
|
|
||||||
currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
||||||
if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
// if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||||
else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
// else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
||||||
|
dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (x>128 && x<=256){
|
else if (x > 128 && x <= 256)
|
||||||
|
{
|
||||||
|
|
||||||
for (int y = 0; y < PANE_HEIGHT; y++) {
|
for (int y = 0; y < PANE_HEIGHT; y++)
|
||||||
|
{
|
||||||
int16_t v = 128;
|
int16_t v = 128;
|
||||||
uint8_t wibble = sin8(time_counter);
|
uint8_t wibble = sin8(time_counter);
|
||||||
int x1 = x-128;
|
int x1 = x - 128;
|
||||||
int y1 = y+64;
|
int y1 = y + 64;
|
||||||
v += sin16(x1 * wibble * 3 + time_counter);
|
v += sin16(x1 * wibble * 3 + time_counter);
|
||||||
v += cos16(y1 * (128 - wibble) + time_counter);
|
v += cos16(y1 * (128 - wibble) + time_counter);
|
||||||
v += sin16(y1 * x1 * cos8(-time_counter) / 8);
|
v += sin16(y1 * x1 * cos8(-time_counter) / 8);
|
||||||
|
|
||||||
currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
||||||
if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
// if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||||
else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
// else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
||||||
|
dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else
|
||||||
for (int y = 0; y < PANE_HEIGHT; y++) {
|
{
|
||||||
|
for (int y = 0; y < PANE_HEIGHT; y++)
|
||||||
|
{
|
||||||
int16_t v = 128;
|
int16_t v = 128;
|
||||||
uint8_t wibble = sin8(time_counter);
|
uint8_t wibble = sin8(time_counter);
|
||||||
int x1 = x-256;
|
int x1 = x - 256;
|
||||||
int y1 = y+64;
|
int y1 = y + 64;
|
||||||
v += sin16(x1 * wibble * 3 + time_counter);
|
v += sin16(x1 * wibble * 3 + time_counter);
|
||||||
v += cos16(y1 * (128 - wibble) + time_counter);
|
v += cos16(y1 * (128 - wibble) + time_counter);
|
||||||
v += sin16(y1 * x1 * cos8(-time_counter) / 8);
|
v += sin16(y1 * x1 * cos8(-time_counter) / 8);
|
||||||
|
|
||||||
currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
currentColor = ColorFromPalette(currentPalette, (v >> 8)); //, brightness, currentBlendType);
|
||||||
if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
// if(y<=31)dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||||
else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
// else dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.b, currentColor.g);
|
||||||
|
dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -290,30 +282,35 @@ void plasma_palette()
|
||||||
++cycles;
|
++cycles;
|
||||||
++fps;
|
++fps;
|
||||||
|
|
||||||
if (cycles >= 1024) {
|
if (cycles >= 1024)
|
||||||
|
{
|
||||||
time_counter = 0;
|
time_counter = 0;
|
||||||
cycles = 0;
|
cycles = 0;
|
||||||
currentPalette = palettes[random(0,sizeof(palettes)/sizeof(palettes[0]))];
|
currentPalette = palettes[random(0, sizeof(palettes) / sizeof(palettes[0]))];
|
||||||
}
|
}
|
||||||
|
|
||||||
// print FPS rate every 5 seconds
|
// print FPS rate every 5 seconds
|
||||||
// Note: this is NOT a matrix refresh rate, it's the number of data frames being drawn to the DMA buffer per second
|
// Note: this is NOT a matrix refresh rate, it's the number of data frames being drawn to the DMA buffer per second
|
||||||
if (fps_timer + 5000 < millis()){
|
if (fps_timer + 5000 < millis())
|
||||||
//Serial.printf_P(PSTR("Effect fps: %d\n"), fps/5);
|
{
|
||||||
fps_timer = millis();
|
// Serial.printf_P(PSTR("Effect fps: %d\n"), fps/5);
|
||||||
fps = 0;
|
fps_timer = millis();
|
||||||
|
fps = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void setup() {
|
void setup()
|
||||||
// put your setup code here, to run once:
|
{
|
||||||
|
// put your setup code here, to run once:
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
if (!WiFi.config(local_IP, gateway, subnet)) {
|
if (!WiFi.config(local_IP, gateway, subnet))
|
||||||
Serial.println("STA Failed to configure");
|
{
|
||||||
|
Serial.println("STA Failed to configure");
|
||||||
}
|
}
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED)
|
||||||
|
{
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.println("Connecting to WiFi...");
|
Serial.println("Connecting to WiFi...");
|
||||||
}
|
}
|
||||||
|
@ -321,31 +318,46 @@ void setup() {
|
||||||
Serial.print("Connected to WiFi.");
|
Serial.print("Connected to WiFi.");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
udp.begin(localUdpPort);
|
udp.begin(localUdpPort);
|
||||||
|
|
||||||
|
webSocket.begin();
|
||||||
|
webSocket.onEvent(webSocketEvent);
|
||||||
|
|
||||||
dma_display = new MatrixPanel_I2S_DMA(mxconfig);
|
dma_display = new MatrixPanel_I2S_DMA(mxconfig);
|
||||||
dma_display->begin(); // setup the LED matrix
|
dma_display->begin(); // setup the LED matrix
|
||||||
dma_display->setBrightness8(50); //0-255
|
dma_display->setBrightness8(50); // 0-255
|
||||||
dma_display->clearScreen();
|
dma_display->clearScreen();
|
||||||
|
//dma_display->fillScreenRGB888(0, 255, 0);
|
||||||
currentPalette = RainbowColors_p;
|
currentPalette = RainbowColors_p;
|
||||||
}
|
}
|
||||||
void loop() {
|
void loop()
|
||||||
int packetSize = udp.parsePacket();
|
{
|
||||||
if (packetSize) {
|
webSocket.loop();
|
||||||
int len = udp.read(incomingPacket, MAX_UDP_SIZE);
|
//int packetSize = udp.parsePacket();
|
||||||
if (len > 0) {
|
// if (packetSize)
|
||||||
receivePixels(incomingPacket, len);
|
// {
|
||||||
}
|
// int len = udp.read(incomingPacket, MAX_UDP_SIZE);
|
||||||
|
// if (len > 0)
|
||||||
// // Use PSRAM
|
// {
|
||||||
// int len = udp.read(incomingPacket_psram, MAX_UDP_SIZE);
|
// // Serial.println(len);
|
||||||
// if (len > 0) {
|
// if (len < 1000)
|
||||||
// receivePixels(incomingPacket_psram, len);
|
// ;
|
||||||
// }
|
// {
|
||||||
}
|
// recieve_done = true;
|
||||||
else {
|
// }
|
||||||
//plasma_palette();
|
// receivePixels(incomingPacket, len);
|
||||||
//Serial.println("No packet received");
|
// }
|
||||||
}
|
|
||||||
//plasma_palette();
|
|
||||||
|
|
||||||
|
// // //Use PSRAM
|
||||||
|
// // int len = udp.read(incomingPacket_psram, MAX_UDP_SIZE);
|
||||||
|
// // if (len > 0) {
|
||||||
|
// // //receivePixels(incomingPacket_psram, len);
|
||||||
|
// // Serial.println(len);
|
||||||
|
// // }
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// // plasma_palette();
|
||||||
|
// // Serial.println("No packet received");
|
||||||
|
// }
|
||||||
|
// plasma_palette();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue