From fde689f705ecb740edf1882670f1d23f0585b2b9 Mon Sep 17 00:00:00 2001 From: CalisJI Date: Fri, 9 Aug 2024 11:11:22 +0700 Subject: [PATCH] add new --- .gitignore | 5 + .vscode/extensions.json | 10 ++ .vscode/settings.json | 13 ++ include/README | 39 ++++++ lib/README | 46 +++++++ platformio.ini | 21 +++ src/index.htm | 107 +++++++++++++++ src/main.cpp | 289 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++ 9 files changed, 541 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/index.htm create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..dc21fb8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "files.associations": { + "optional": "cpp", + "istream": "cpp", + "ostream": "cpp", + "system_error": "cpp", + "array": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..fb1bc82 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,21 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nodemcu-32s] +platform = espressif32 +board = nodemcu-32s +board_build.mcu = esp32 +framework = arduino +monitor_speed = 115200 +lib_deps = + fastled/FastLED@^3.7.0 + me-no-dev/AsyncTCP@^1.1.1 + bblanchon/ArduinoJson@^7.1.0 + kitesurfer1404/WS2812FX@^1.4.4 \ No newline at end of file diff --git a/src/index.htm b/src/index.htm new file mode 100644 index 0000000..5de5fac --- /dev/null +++ b/src/index.htm @@ -0,0 +1,107 @@ + + + + + + Led Strip Controller + + + +
+
+ + +
+ +
+ + +
+
+ + +
+
+ + + + \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..6d9bc22 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,289 @@ +#include +#include +#include +#include +#include +#define NUM_LEDS 4 +#define LED_PIN 2 +const char* ssid = "VSMI-Guest"; +const char* password = "h3ll0vsmi"; +// Set your Static IP address +IPAddress local_IP(192, 168, 1, 10); +// Set your Gateway IP address +IPAddress gateway(192, 168, 1, 1); + +IPAddress subnet(255, 255, 255, 0); +WebServer server(80); +WiFiUDP udp; +#define NUM_PIXELS 100 +#define MAX_UDP_SIZE 1450 +unsigned int localUdpPort = 5000; +const int bufferSize = NUM_PIXELS*2; // Adjust based on your data size +char incomingPacket[bufferSize]; // Buffer for incoming packets +uint8_t tempBuffer[NUM_PIXELS * 2]; +int receivedBytes = 0; // Số byte đã nhận +//const size_t capacity = JSON_ARRAY_SIZE(1000) + 4*JSON_STRING_SIZE(6); // Dung lượng cần thiết cho JSON +JsonDocument jsonDocument; +#pragma region HTML page +const char* htmlPage = R"=====( + + + + + + Led Strip Controller + + + +
+
+ + +
+ +
+ + +
+
+ + +
+
+ + + + +)====="; +#pragma endregion +#pragma region LED Config +#define LED_TYPE NEOPIXEL +#define COLOR_ORDER RGB +CRGB leds[NUM_LEDS]; +// Chuyển đổi giá trị HEX thành CRGB +CRGB hexToRGB(const String& hexStr) { + uint32_t color; + // Convert HEX String to uint32_t + if (hexStr.length() == 6) { + color = strtol(hexStr.c_str(), NULL, 16); // Chuyển đổi chuỗi HEX thành giá trị số + } else { + color = 0; // Nếu chuỗi không hợp lệ, trả về màu đen + } + + return CRGB((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF); +} +CRGB rgb565ToCRGB(uint16_t rgb565) { + // Tách các thành phần màu từ giá trị RGB565 + uint8_t red = (rgb565 >> 11) & 0x1F; // 5 bit đỏ + uint8_t green = (rgb565 >> 5) & 0x3F; // 6 bit xanh lá + uint8_t blue = rgb565 & 0x1F; // 5 bit xanh dương + + // Mở rộng giá trị màu từ 5 bit hoặc 6 bit lên 8 bit + red = (red * 255 + 15) / 31; // Chuyển từ 5 bit lên 8 bit + green = (green * 255 + 31) / 63; // Chuyển từ 6 bit lên 8 bit + blue = (blue * 255 + 15) / 31; // Chuyển từ 5 bit lên 8 bit + + return CRGB(red, green, blue); +} +// Cập nhật LED strip từ mảng dữ liệu màu +// Cập nhật LED strip từ mảng chuỗi HEX (String) +void updateLEDsFromHexArray(JsonArray jsonArray) { + int i = 0; + for (JsonVariant value : jsonArray) { + leds[i] = hexToRGB(value.as()); + i++; + } + FastLED.show(); +} +void updateLEDArray(int newLength) { + FastLED.addLeds(leds, newLength); +} + +void receivePixels(char* packet, int length) { + if (receivedBytes + length > sizeof(tempBuffer)) { + length = sizeof(tempBuffer) - receivedBytes; // Giới hạn độ dài + } + memcpy(tempBuffer + receivedBytes, packet, length); + receivedBytes += length; + updateLEDArray(receivedBytes); + // Kiểm tra xem toàn bộ dữ liệu có được nhận đầy đủ chưa + for (int i = 0; i < receivedBytes; i++) { + uint16_t color = (tempBuffer[i*2] << 8) | tempBuffer[i*2 + 1]; + leds[i] = rgb565ToCRGB(color); + } + FastLED.show(); + // Đặt lại bộ nhớ tạm và biến nhận bytes + memset(tempBuffer, 0, sizeof(tempBuffer)); + receivedBytes = 0; +} +#pragma endregion + +#pragma region WEB Config +char buffer[250]; +void handleInput() +{ + String brightness = server.arg("brightness"); + // Chuyển đổi giá trị nhập thành số + int value1 = brightness.toInt(); + + // Thực hiện hành động dựa trên giá trị nhập + Serial.printf("brightness: %d\n", value1); + server.send(200); +} +void handleLeds() +{ + String Led = server.arg("leds"); + // Chuyển đổi giá trị nhập thành số + int updateLEDs = Led.toInt(); + updateLEDArray(updateLEDs); + Serial.printf("leds: %d\n", updateLEDs); + server.send(200); +} +void handlePost() { + + if (server.hasArg("plain") == false) { + + } + String body = server.arg("plain"); + + deserializeJson(jsonDocument, body); + JsonArray jsonArray = jsonDocument["data"].as();; + + updateLEDsFromHexArray(jsonArray); + server.send(200, "application/json", "{}"); +} +void setup_routing() { + server.on("/data", HTTP_POST, handlePost); + // Cấu hình server + server.on("/", []() { + server.send(200, "text/html", htmlPage); + }); + server.on("/inputs", []() { + handleInput(); + }); + server.on("/setLeds", []() { + handleLeds(); + }); + server.begin(); +} +#pragma endregion + + +void setup() +{ + FastLED.addLeds(leds, NUM_LEDS); + Serial.begin(115200); + if (!WiFi.config(local_IP, gateway, subnet)) { + Serial.println("STA Failed to configure"); + } + Serial.print("Connecting to "); + Serial.println(ssid); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + udp.begin(localUdpPort); + // Print local IP address and start web server + Serial.println(""); + Serial.println("WiFi connected."); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + setup_routing(); + Serial.println(FastLED.count()); +} +void loop() { + server.handleClient(); + int packetSize = udp.parsePacket(); + if (packetSize) { + int len = udp.read(incomingPacket, MAX_UDP_SIZE); + + Serial.println(len); + if (len > 0) { + receivePixels(incomingPacket, len); + } + } else { + //Serial.println("No packet received"); + } + // leds[0] = CRGB::White; FastLED.show(); delay(30); + // leds[0] = CRGB::Black; FastLED.show(); delay(30); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html