tahamajs's picture
download
raw
3.66 kB
#include <CPS4042/Hardwares/Boards/Esp8266.h>
#include <CPS4042/Hardwares/Sensors/VL530x.h>
#include <CPS4042/Hardwares/Sensors/SimpleSensor.h>
#include <CPS4042/Hardwares/Comm/HardDisk.h>
#include <CPS4042/Hardwares/Comm/I2CMux.h>
#include <CPS4042/Sketchs/Sensor.h>
#include <CPS4042/Sketchs/HardDiskSketch.h>
#include <CPS4042/Sketchs/MuxSketch.h>
#include <CPS4042/Sketchs/CombinedController.h>
#include <CPS4042/Wires/Link.h>
#include <iostream>
#include <thread>
#include <cstdlib>
#include <ctime>
#include <atomic>
int main() {
std::srand(static_cast<unsigned>(std::time(nullptr)));
std::cout << "╔════════════════════════════════════════╗\n"
<< "║ CPS4042 – Protocol Simulation ║\n"
<< "╚════════════════════════════════════════╝\n";
Boards::Esp8266 esp;
Comm::HardDisk hardDisk;
Comm::I2CMux mux;
Sensors::Vl530x sensor1;
Sensors::SimpleSensor sensor2(50, 100);
// Power wiring (Vdd / Gnd)
auto lvHD = std::make_shared<Link>(), lgHD = std::make_shared<Link>();
esp.gpio().vdd1.attachLink(lvHD); hardDisk.gpio().vdd.attachLink(lvHD);
esp.gpio().gnd1.attachLink(lgHD); hardDisk.gpio().gnd.attachLink(lgHD);
auto lvMux = std::make_shared<Link>(), lgMux = std::make_shared<Link>();
esp.gpio().vdd2.attachLink(lvMux); mux.gpio().vdd.attachLink(lvMux);
esp.gpio().gnd2.attachLink(lgMux); mux.gpio().gnd.attachLink(lgMux);
// USART1: ESP (tx/rx) <-> HardDisk (rx/tx)
auto txHD = std::make_shared<Link>(), rxHD = std::make_shared<Link>();
esp.gpio().tx.attachLink(txHD); hardDisk.gpio().rx.attachLink(txHD);
esp.gpio().rx.attachLink(rxHD); hardDisk.gpio().tx.attachLink(rxHD);
// USART2: ESP (tx2/rx2) <-> MUX (rx/tx)
auto txMux = std::make_shared<Link>(), rxMux = std::make_shared<Link>();
esp.gpio().tx2.attachLink(txMux); mux.gpio().rx.attachLink(txMux);
esp.gpio().rx2.attachLink(rxMux); mux.gpio().tx.attachLink(rxMux);
// I2C lines: MUX <-> sensors
auto sda1 = std::make_shared<Link>(), scl1 = std::make_shared<Link>();
mux.gpio().sda.attachLink(sda1); sensor1.gpio().sda.attachLink(sda1);
mux.gpio().scl.attachLink(scl1); sensor1.gpio().scl.attachLink(scl1);
auto sda2 = std::make_shared<Link>(), scl2 = std::make_shared<Link>();
mux.gpio().sda.attachLink(sda2); sensor2.gpio().sda.attachLink(sda2);
mux.gpio().scl.attachLink(scl2); sensor2.gpio().scl.attachLink(scl2);
mux.attachSensor(0, &sensor1);
mux.attachSensor(1, &sensor2);
// Instantiate and start sketches for all devices
HardDiskSketch hdSketch(&hardDisk);
MuxSketch muxSketch(&mux);
Sensor sensorSketch1(&sensor1, 0, 4000);
CombinedController controller(&esp, &hardDisk, &mux);
hdSketch.start();
muxSketch.start();
sensorSketch1.start();
std::atomic<bool> stopUpdater{false};
std::thread sensor2Updater([&sensor2, &stopUpdater]() {
while (!stopUpdater.load()) {
uint16_t d = 50 + (std::rand() % 51);
sensor2.updateDistance(d);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
});
controller.start();
std::cout << "\nSimulation running via hardware protocol links... Press Enter to stop.\n";
std::cin.get();
esp.stop();
hardDisk.stop();
mux.stop();
stopUpdater.store(true);
if (sensor2Updater.joinable()) sensor2Updater.join();
return 0;
}

Xet Storage Details

Size:
3.66 kB
·
Xet hash:
33f87bce77099914e229274acdc6267bce66f52bf16b4069ec3c2c61c878163b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.