Skip to content

Commit dfc2d3e

Browse files
committed
PC monitor example
1 parent 5ae4383 commit dfc2d3e

4 files changed

Lines changed: 122 additions & 2 deletions

File tree

examples/Basic_Duplex/ESPM3_Bridge.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <../src/TinyLink.h>
2-
#include <../src/adapters/TinyArduinoAdapter.h>
1+
#include <../../src/TinyLink.h>
2+
#include <../../src/adapters/TinyArduinoAdapter.h>
33
#include "SharedData.h"
44

55
TinyArduinoAdapter adapter(Serial);

examples/PC_Monitor/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# PC Monitor: Desktop to MCU 🖥️
2+
3+
This example demonstrates how to use *TinyLink* in a native C++ desktop environment. It allows your computer to act as a high-performance peer to your microcontrollers, enabling real-time monitoring and command injection via a standard USB-to-Serial adapter.
4+
5+
## 🌟 Features
6+
7+
*Cross-Platform*: Uses `TinyPosixAdapter` for Linux/macOS and `TinyWindowsAdapter` for Windows.
8+
*Bi-Directional*: Receives sensor data from the MCU and sends command packets back every 5 seconds.
9+
*Asynchronous*: Uses the `onReceive` callback to handle incoming COBS frames without blocking the main loop.
10+
11+
## 🛠 Hardware Setup
12+
13+
You will need a *USB-to-TTL Serial Adapter* (e.g., CP2102, FT232RL, or CH340).
14+
15+
PC (via USB Adapter) Connection MCU (MH-Tiny88 / ESP)
16+
TX ────────────── RX
17+
RX ────────────── TX
18+
GND ────────────── GND (Common)
19+
20+
> ⚠️ Warning: If connecting to an *ESP-M3*, ensure your USB adapter is set to *3.3V mode*. If connecting to an *MH-Tiny88*, ensure it is in *5V mode*.
21+
22+
## 🚀 How to Build & Run
23+
24+
1. Identify your Port
25+
26+
*Windows*: Check Device Manager (e.g., `COM3`).
27+
*Linux/macOS*: Run ls `/dev/tty*` (e.g., `/dev/ttyUSB0` or √/dev/cu.usbserial-xxx√).
28+
29+
2. Compile
30+
31+
Open a terminal in this directory and use any standard C++ compiler:
32+
33+
### Linux / macOS:
34+
35+
```bash
36+
bash
37+
g++ main.cpp -I../../src -o tiny_monitor
38+
./tiny_monitor
39+
```
40+
41+
### Windows (MinGW/MSYS2):
42+
43+
```bash
44+
bash
45+
g++ main.cpp -I../../src -o tiny_monitor.exe
46+
./tiny_monitor.exe
47+
```
48+
49+
## 📂 Code Overview
50+
51+
*SharedData.h*: Defines the struct MyData used by both devices. *Must be identical on both sides*.
52+
*PCAdapter*: A type-alias that automatically selects the correct OS-specific driver.
53+
*onMcuData*: The callback function triggered whenever a valid Fletcher-16 verified packet arrives.

examples/PC_Monitor/SharedData.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef SHARED_DATA_H
2+
#define SHARED_DATA_H
3+
4+
#include <stdint.h>
5+
6+
struct MyData {
7+
uint32_t uptime;
8+
float temperature;
9+
uint8_t commandId;
10+
};
11+
12+
#endif

examples/PC_Monitor/main.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include "../../src/TinyLink.h"
4+
5+
// 1. Choose the correct adapter for your OS
6+
#ifdef _WIN32
7+
#include "../../src/adapters/TinyWindowsAdapter.h"
8+
typedef TinyWindowsAdapter PCAdapter;
9+
const char* PORT = "COM3"; // Change to your COM port
10+
#else
11+
#include "../../src/adapters/TinyPosixAdapter.h"
12+
typedef TinyPosixAdapter PCAdapter;
13+
const char* PORT = "/dev/ttyUSB0"; // Change to your TTY device
14+
#endif
15+
16+
#include "../Basic_Duplex/SharedData.h" // Reuse our shared struct
17+
18+
// 2. Callback for when the MCU sends data to the PC
19+
void onMcuData(const MyData& data) {
20+
std::cout << "\n[INCOMING] "
21+
<< "Uptime: " << data.uptime << "ms | "
22+
<< "Temp: " << std::fixed << std::setprecision(2) << data.temperature << "°C | "
23+
<< "Cmd: " << (int)data.commandId << std::endl;
24+
}
25+
26+
int main() {
27+
// 3. Initialize Hardware Adapter
28+
PCAdapter hw(PORT, 9600);
29+
if (!hw.isOpen()) {
30+
std::cerr << "Failed to open port " << PORT << std::endl;
31+
return 1;
32+
}
33+
34+
// 4. Initialize TinyLink
35+
TinyLink<MyData, PCAdapter> link(hw);
36+
link.onReceive(onMcuData);
37+
38+
std::cout << "TinyLink Monitor started on " << PORT << "..." << std::endl;
39+
std::cout << "Press Ctrl+C to exit." << std::endl;
40+
41+
while (true) {
42+
link.update(); // Continuous polling for COBS frames
43+
44+
// Example: Send a command from PC to MCU every 5 seconds
45+
static auto lastSend = hw.millis();
46+
if (hw.millis() - lastSend > 5000) {
47+
MyData cmd = { (uint32_t)hw.millis(), 0.0f, 1 };
48+
link.send(TYPE_DATA, cmd);
49+
std::cout << "[SENT] Command 1 to MCU" << std::endl;
50+
lastSend = hw.millis();
51+
}
52+
}
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)