Update README
parent
cf216680a5
commit
daec7eb9e8
45
README.md
45
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
|
||||
|
@ -27,3 +27,46 @@ ser.write(data_to_send)
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
28
src/main.cpp
28
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));
|
||||
|
|
Loading…
Reference in New Issue