From daec7eb9e823d99fe0657bbf5ab7089d85784b23 Mon Sep 17 00:00:00 2001 From: CalisJI Date: Fri, 9 Aug 2024 11:09:16 +0700 Subject: [PATCH] Update README --- README.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/main.cpp | 28 ---------------------------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 1d35260..9d8e9af 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Project display object's point on matrix ### Data input example Serial Port -``` +```python import numpy as np import serial import struct @@ -26,4 +26,47 @@ ser.write(data_to_send) # Đóng cổng serial ser.close() +``` +### Receive Data + +```c++ +void loop() { + while (Serial.available() > 0) { + uint8_t byteReceived = Serial.read(); + + if (bufferIndex < bufferSize) { + buffer[bufferIndex++] = byteReceived; + } else { + // Xử lý tình huống quá tải + Serial.println("Buffer overflow"); + bufferIndex = 0; // Reset buffer index in case of overflow + } + } + if (bufferIndex >= 2 * sizeof(int)) { + // Đã nhận đủ kích thước dữ liệu + memcpy(&numRows, buffer, sizeof(int)); + memcpy(&numCols, buffer + sizeof(int), sizeof(int)); + + int expectedSize = numRows * numCols * floatSize; + + if (bufferIndex >= (2 * sizeof(int) + expectedSize)) { + Serial.println("Data received:"); + float data[numRows][numCols]; + for (int row = 0; row < numRows; ++row) { + for (int col = 0; col < numCols; ++col) { + int index = (2 * sizeof(int)) + (row * numCols + col) * floatSize; + float value; + memcpy(&value, &buffer[index], floatSize); + data[row][col] = value; + Serial.print(value, 4); // In với 4 chữ số thập phân + Serial.print(" "); + } + Serial.println(); + } + + // Reset chỉ số chỉ mục sau khi xử lý + bufferIndex = 0; + } + } +} ``` \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a956f1b..795bd2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,34 +22,6 @@ void loop() { bufferIndex = 0; // Reset buffer index in case of overflow } } - - // if (bufferIndex > 0) { - // // Đã nhận dữ liệu - // Serial.println("Data received:"); - // for (int i = 0; i < bufferIndex; ++i) { - // Serial.print(buffer[i]); - // Serial.print(" "); - // } - // Serial.println(); - - // // Reset chỉ số chỉ mục sau khi xử lý - // bufferIndex = 0; - // } - // if (bufferIndex > 0) { - // Đã nhận dữ liệu - // Serial.println("Data received:"); - // int numFloats = bufferIndex / sizeof(float); - // for (int i = 0; i < numFloats; ++i) { - // float value; - // memcpy(&value, &buffer[i * sizeof(float)], sizeof(float)); - // Serial.print(value, 4); // In với 4 chữ số thập phân - // Serial.print(" "); - // } - // Serial.println(); - - // Reset chỉ số chỉ mục sau khi xử lý - // bufferIndex = 0; - // } if (bufferIndex >= 2 * sizeof(int)) { // Đã nhận đủ kích thước dữ liệu memcpy(&numRows, buffer, sizeof(int));