From 9a2364e35582f7917b28f916c0cb18f4f05e7762 Mon Sep 17 00:00:00 2001 From: Ada Date: Fri, 13 Oct 2023 22:16:46 +0200 Subject: [PATCH 1/7] Make simulator pixels a very dark gray if they are off This makes it easier to count distances between elements. --- simulation/VirtualLedBoard/mainwindow.cpp | 15 +++++++++++---- simulation/VirtualLedBoard/mainwindow.h | 3 ++- simulation/VirtualLedBoard/udpserver.cpp | 9 +++++---- simulation/VirtualLedBoard/udpserver.h | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index da8445b..9a416aa 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -32,13 +32,20 @@ void MainWindow::drawImage(QImage *target) { } -void MainWindow::setLED(uint8_t x, uint8_t y) { +void MainWindow::setLED(uint8_t x, uint8_t y, bool state) { /* draw inital screen */ QPainter painter(this->mOffscreenPanel); painter.setRenderHint(QPainter::Antialiasing, true); - painter.setPen(QPen(COLOR_FOREGROUND, 1)); - painter.setBrush(COLOR_FOREGROUND); - painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), LED_DIAMETER/2 + (y * LED_DISTANCE), LED_DIAMETER, LED_DIAMETER); + if (state) { + painter.setPen(QPen(COLOR_FOREGROUND, 1)); + painter.setBrush(COLOR_FOREGROUND); + } else { + painter.setPen(QPen(COLOR_FOREGROUND_OFF, 1)); + painter.setBrush(COLOR_FOREGROUND_OFF); + } + painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), + LED_DIAMETER/2 + (y * LED_DISTANCE), + LED_DIAMETER, LED_DIAMETER); } void MainWindow::updatePanel(void) { diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index d32b5f4..8be2da2 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -10,6 +10,7 @@ #define COLOR_BACKGROUND Qt::black #define COLOR_FOREGROUND QColor(255, 127, 0, 255) +#define COLOR_FOREGROUND_OFF QColor(25, 25, 25, 255) QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -27,7 +28,7 @@ class MainWindow : public QMainWindow ~MainWindow(); public slots: - void setLED(uint8_t x, uint8_t y); + void setLED(uint8_t x, uint8_t y, bool state); void updatePanel(void); private: diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index e1e9f14..dd28725 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -20,10 +20,10 @@ UdpLedServer::UdpLedServer (QObject *parent, MainWindow *window) window, &MainWindow::updatePanel); - + bool init = true; for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { for(int y=0; y < PANEL_HEIGHT; y++) { - changeLEDstate(x, y); + changeLEDstate(x, y, init); } } updatePanelContent(); @@ -57,8 +57,9 @@ void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { uint16_t mask = 1; for(int y=0; y < PANEL_HEIGHT; y++) { for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { - if (datagram.data().at(currentIndex) & mask) { - this->changeLEDstate(x, y); + bool state = mask & datagram.data().at(currentIndex); + this->changeLEDstate(x, y, state); + if (state) { qDebug() << x << "x" << y << " set"; } mask = (mask << 1); diff --git a/simulation/VirtualLedBoard/udpserver.h b/simulation/VirtualLedBoard/udpserver.h index 023370b..dd901b4 100644 --- a/simulation/VirtualLedBoard/udpserver.h +++ b/simulation/VirtualLedBoard/udpserver.h @@ -22,7 +22,7 @@ class UdpLedServer : public QObject void processTheDatagram(QNetworkDatagram datagram); signals: - void changeLEDstate(uint8_t x, uint8_t y); + void changeLEDstate(uint8_t x, uint8_t y, bool state); void updatePanelContent(void); }; From 5a9d7d0d9539f442adb30bc508a4cc942c32c40a Mon Sep 17 00:00:00 2001 From: Ada Date: Fri, 13 Oct 2023 22:21:40 +0200 Subject: [PATCH 2/7] Add explanation to non-obvious mask --- simulation/VirtualLedBoard/udpserver.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index dd28725..b56933f 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -57,10 +57,17 @@ void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { uint16_t mask = 1; for(int y=0; y < PANEL_HEIGHT; y++) { for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { - bool state = mask & datagram.data().at(currentIndex); - this->changeLEDstate(x, y, state); - if (state) { - qDebug() << x << "x" << y << " set"; + /* + * The datagram.data().at(currentIndex) here is a char with packed + * bits. Each bit needs to be separately extracted and hence the + * `mask` is used for this. A bit-wise shift is applied to advance + * to the next bit in the char until the char is exhausted. Then + * the next char from the datagram is read. + */ + bool state = mask & datagram.data().at(currentIndex); + this->changeLEDstate(x, y, state); + if (state) { + qDebug() << x << "x" << y << " set"; } mask = (mask << 1); if (mask >= 256) { From 0e0d63ebb65507e1aa653b62802ab98e180b6f52 Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 14 Oct 2023 19:56:47 +0200 Subject: [PATCH 3/7] Rust simulator --- rust-simulation/Cargo.lock | 718 ++++++++++++++++++++++++++++++++++++ rust-simulation/Cargo.toml | 12 + rust-simulation/src/main.rs | 130 +++++++ 3 files changed, 860 insertions(+) create mode 100644 rust-simulation/Cargo.lock create mode 100644 rust-simulation/Cargo.toml create mode 100644 rust-simulation/src/main.rs diff --git a/rust-simulation/Cargo.lock b/rust-simulation/Cargo.lock new file mode 100644 index 0000000..cc70a42 --- /dev/null +++ b/rust-simulation/Cargo.lock @@ -0,0 +1,718 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "aliasable" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "az" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" + +[[package]] +name = "bytemuck" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "deflate" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" +dependencies = [ + "adler32", + "byteorder", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "embedded-graphics" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0649998afacf6d575d126d83e68b78c0ab0e00ca2ac7e9b3db11b4cbe8274ef0" +dependencies = [ + "az", + "byteorder", + "embedded-graphics-core", + "float-cmp", + "micromath", +] + +[[package]] +name = "embedded-graphics-core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba9ecd261f991856250d2207f6d8376946cd9f412a2165d3b75bc87a0bc7a044" +dependencies = [ + "az", + "byteorder", +] + +[[package]] +name = "embedded-graphics-simulator" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "684467a8b75b1314d913b78a338ebdeac93f421206d85c87876193fc81dc4556" +dependencies = [ + "base64", + "embedded-graphics", + "image", + "ouroboros", + "sdl2", +] + +[[package]] +name = "env_logger" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "errno" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + +[[package]] +name = "gif" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3edd93c6756b4dfaf2709eafcc345ba2636565295c198a9cfbf75fa5e3e00b06" +dependencies = [ + "color_quant", + "weezl", +] + +[[package]] +name = "hermit-abi" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "image" +version = "0.23.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "gif", + "jpeg-decoder", + "num-iter", + "num-rational", + "num-traits", + "png", + "scoped_threadpool", + "tiff", +] + +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + +[[package]] +name = "jpeg-decoder" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2" +dependencies = [ + "rayon", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" + +[[package]] +name = "linux-raw-sys" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "micromath" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39617bc909d64b068dcffd0e3e31679195b5576d0c83fadc52690268cc2b2b55" + +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "ouroboros" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1358bd1558bd2a083fed428ffeda486fbfb323e698cdda7794259d592ca72db" +dependencies = [ + "aliasable", + "ouroboros_macro", +] + +[[package]] +name = "ouroboros_macro" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f7d21ccd03305a674437ee1248f3ab5d4b1db095cf1caf49f1713ddf61956b7" +dependencies = [ + "Inflector", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "png" +version = "0.16.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "deflate", + "miniz_oxide 0.3.7", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "regex" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d119d7c7ca818f8a53c300863d4f87566aac09943aef5b355bb83969dae75d87" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "465c6fc0621e4abc4187a2bda0937bfd4f722c2730b29562e19689ea796c9a4b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d84fdd47036b038fc80dd333d10b6aab10d5d31f4a366e20014def75328d33" + +[[package]] +name = "rust-simulation" +version = "0.1.0" +dependencies = [ + "embedded-graphics", + "embedded-graphics-simulator", + "env_logger", + "log", +] + +[[package]] +name = "rustix" +version = "0.38.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" +dependencies = [ + "bitflags 2.4.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sdl2" +version = "0.35.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a" +dependencies = [ + "bitflags 1.3.2", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.35.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0" +dependencies = [ + "cfg-if", + "libc", + "version-compare", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "tiff" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437" +dependencies = [ + "jpeg-decoder", + "miniz_oxide 0.4.4", + "weezl", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version-compare" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "weezl" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/rust-simulation/Cargo.toml b/rust-simulation/Cargo.toml new file mode 100644 index 0000000..40439ac --- /dev/null +++ b/rust-simulation/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rust-simulation" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +embedded-graphics = "0.8.1" +embedded-graphics-simulator = "0.5.0" +env_logger = "0.10.0" +log = "0.4.20" diff --git a/rust-simulation/src/main.rs b/rust-simulation/src/main.rs new file mode 100644 index 0000000..22cd3bf --- /dev/null +++ b/rust-simulation/src/main.rs @@ -0,0 +1,130 @@ +use log; +use env_logger; +use std::{thread, time::Duration}; +use std::net::UdpSocket; +use embedded_graphics::{ + pixelcolor::BinaryColor, + mono_font::{ascii::FONT_10X20, MonoTextStyle}, + text::Text, + image::{Image, ImageRaw}, + prelude::*, +}; +use embedded_graphics_simulator::{ + BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, +}; + +const IMAGE_SIZE_BYTE: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; // one byte contains 8 LEDs, one in each bit +const PANEL_WIDTH: u32 = 32 ; +const PANEL_WIDTH_BYTE: u32 = PANEL_WIDTH / 8; // one byte contains 8 LEDs, one in each bit +const PANEL_COUNT: u32 = 5 ; +const PANEL_PIXELS: u32 = PANEL_WIDTH * 40; +const PANEL_BYTES: u32 = PANEL_PIXELS / 8; // one byte contains 8 LEDs, one in each bit +const IMAGE_WIDTH: u32 = PANEL_COUNT * PANEL_WIDTH; +const IMAGE_WIDTH_BYTE: u32 = IMAGE_WIDTH / 8; // one byte contains 8 LEDs, one in each bit +const IMAGE_HEIGHT: u32 = 40; +const IMAGE_HEIGHT_BYTE: u32 = 40; +const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize; +const PACKAGE_LENGTH: usize = (IMAGE_LENGTH + 1) as usize; + +struct Display { + window: Window, + display: SimulatorDisplay, +} + +impl Display { + fn new() -> Display { + let output_settings = OutputSettingsBuilder::new() + .theme(BinaryColorTheme::OledBlue) + .scale(4) + .build(); + Display{ + display: SimulatorDisplay::new(Size::new(32*5, 40)), + window: Window::new("LED Board", &output_settings), + } + } + fn clear(&mut self) { + self.display.clear(BinaryColor::Off).unwrap(); + } + fn update(&mut self) { + self.window.update(&self.display); + } + fn has_quit(&mut self) -> bool { + self.window.events().any(|e| e == SimulatorEvent::Quit) + } +} + +struct Client { + addr: String, + port: i32, + socket: UdpSocket, + display: Display +} + +type ImgData = [u8; IMAGE_LENGTH]; + +impl Client { + fn new(addr: &str, port: i32) -> Client { + let addr_full = format!("{addr}:{port}"); + log::info!("Listening on {addr_full}"); + let socket = UdpSocket::bind(addr_full).expect("Couldn't bind"); + Client{ + addr: addr.to_string(), + port, + socket, + display: Display::new() + } + } + fn recv_image(&mut self) -> ImgData { + let mut data = [0; PACKAGE_LENGTH]; + // Read packages until one is PACKAGE_LENGTH + loop { + match self.socket.recv_from(&mut data) { + Err(error) => panic!("{:}", error), + Ok((PACKAGE_LENGTH, src_addr)) => { + log::debug!("Received image from {}", src_addr); + let data = &mut data[1..]; + for i in 0..(IMAGE_LENGTH as usize) { + data[i] = data[i].reverse_bits(); + } + return data.try_into().unwrap(); + }, + Ok((bytes_read, src_addr)) => { + log::info!("Responding to status checking message from {}", src_addr); + let data = &mut data[..bytes_read]; + match self.socket.send_to(&data, &src_addr) { + Ok(_) => {}, + Err(e) => log::error!("{:}", e), + }; + } + } + } + } + fn run(&mut self) -> Result<(), ()> { + self.display.clear(); + self.display.update(); + 'running: loop { + self.display.clear(); + let data = self.recv_image(); + let image_raw = ImageRaw::::new(&data, IMAGE_WIDTH); + let image = Image::new(&image_raw, Point::zero()); + image.draw(&mut self.display.display).unwrap(); + self.display.update(); + if self.display.has_quit() { + break 'running Ok(()); + } + } + } +} + +fn main() { + env_logger::init(); + let mut client = Client::new("0.0.0.0", 4242); + client.run().unwrap(); + /* + let mut simulator = Simulator::new(); + match simulator.run() { + Ok(v) => println!("Graceful exit {}", v), + Err(_) => eprintln!("Failed!"), + } + */ +} From f0d8c6aae5fb9c9cb9f51707f8e9658d5725214a Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 14 Oct 2023 20:11:04 +0200 Subject: [PATCH 4/7] Remove superfluous code, add initial text --- rust-simulation/src/main.rs | 76 ++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/rust-simulation/src/main.rs b/rust-simulation/src/main.rs index 22cd3bf..ed807fe 100644 --- a/rust-simulation/src/main.rs +++ b/rust-simulation/src/main.rs @@ -1,29 +1,23 @@ -use log; -use env_logger; -use std::{thread, time::Duration}; -use std::net::UdpSocket; use embedded_graphics::{ - pixelcolor::BinaryColor, - mono_font::{ascii::FONT_10X20, MonoTextStyle}, - text::Text, image::{Image, ImageRaw}, + mono_font::{ascii::FONT_6X10, MonoTextStyle}, + pixelcolor::BinaryColor, prelude::*, + text::{Alignment, Baseline, Text, TextStyleBuilder}, }; use embedded_graphics_simulator::{ BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, }; +use env_logger; +use log; +use std::net::UdpSocket; -const IMAGE_SIZE_BYTE: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; // one byte contains 8 LEDs, one in each bit -const PANEL_WIDTH: u32 = 32 ; -const PANEL_WIDTH_BYTE: u32 = PANEL_WIDTH / 8; // one byte contains 8 LEDs, one in each bit -const PANEL_COUNT: u32 = 5 ; -const PANEL_PIXELS: u32 = PANEL_WIDTH * 40; -const PANEL_BYTES: u32 = PANEL_PIXELS / 8; // one byte contains 8 LEDs, one in each bit +const PANEL_WIDTH: u32 = 32; +const PANEL_COUNT: u32 = 5; const IMAGE_WIDTH: u32 = PANEL_COUNT * PANEL_WIDTH; -const IMAGE_WIDTH_BYTE: u32 = IMAGE_WIDTH / 8; // one byte contains 8 LEDs, one in each bit const IMAGE_HEIGHT: u32 = 40; -const IMAGE_HEIGHT_BYTE: u32 = 40; -const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize; +const IMAGE_WIDTH_BYTE: u32 = IMAGE_WIDTH / 8; +const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; const PACKAGE_LENGTH: usize = (IMAGE_LENGTH + 1) as usize; struct Display { @@ -37,8 +31,8 @@ impl Display { .theme(BinaryColorTheme::OledBlue) .scale(4) .build(); - Display{ - display: SimulatorDisplay::new(Size::new(32*5, 40)), + Display { + display: SimulatorDisplay::new(Size::new(32 * 5, 40)), window: Window::new("LED Board", &output_settings), } } @@ -54,10 +48,8 @@ impl Display { } struct Client { - addr: String, - port: i32, socket: UdpSocket, - display: Display + display: Display, } type ImgData = [u8; IMAGE_LENGTH]; @@ -67,11 +59,9 @@ impl Client { let addr_full = format!("{addr}:{port}"); log::info!("Listening on {addr_full}"); let socket = UdpSocket::bind(addr_full).expect("Couldn't bind"); - Client{ - addr: addr.to_string(), - port, + Client { socket, - display: Display::new() + display: Display::new(), } } fn recv_image(&mut self) -> ImgData { @@ -87,30 +77,47 @@ impl Client { data[i] = data[i].reverse_bits(); } return data.try_into().unwrap(); - }, + } Ok((bytes_read, src_addr)) => { log::info!("Responding to status checking message from {}", src_addr); let data = &mut data[..bytes_read]; match self.socket.send_to(&data, &src_addr) { - Ok(_) => {}, + Ok(_) => {} Err(e) => log::error!("{:}", e), }; } } } } - fn run(&mut self) -> Result<(), ()> { + fn draw_info(&mut self) { + let content = format!("Listening on {:}", self.socket.local_addr().unwrap()); + let text_style = TextStyleBuilder::new() + .baseline(Baseline::Middle) + .alignment(Alignment::Center) + .build(); + let char_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); + Text::with_text_style( + &content, + self.display.display.bounding_box().center(), + char_style, + text_style, + ) + .draw(&mut self.display.display) + .unwrap(); + } + fn run(&mut self) { self.display.clear(); + self.draw_info(); self.display.update(); 'running: loop { self.display.clear(); let data = self.recv_image(); let image_raw = ImageRaw::::new(&data, IMAGE_WIDTH); let image = Image::new(&image_raw, Point::zero()); - image.draw(&mut self.display.display).unwrap(); + image.draw(&mut self.display.display).unwrap(); self.display.update(); if self.display.has_quit() { - break 'running Ok(()); + break 'running; } } } @@ -119,12 +126,5 @@ impl Client { fn main() { env_logger::init(); let mut client = Client::new("0.0.0.0", 4242); - client.run().unwrap(); - /* - let mut simulator = Simulator::new(); - match simulator.run() { - Ok(v) => println!("Graceful exit {}", v), - Err(_) => eprintln!("Failed!"), - } - */ + client.run(); } From 58e1a6e7dc676dd7c253d707e68858f4a94cc9b6 Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 14 Oct 2023 20:19:42 +0200 Subject: [PATCH 5/7] Other palette --- rust-simulation/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-simulation/src/main.rs b/rust-simulation/src/main.rs index ed807fe..95d5504 100644 --- a/rust-simulation/src/main.rs +++ b/rust-simulation/src/main.rs @@ -28,7 +28,7 @@ struct Display { impl Display { fn new() -> Display { let output_settings = OutputSettingsBuilder::new() - .theme(BinaryColorTheme::OledBlue) + .theme(BinaryColorTheme::Default) .scale(4) .build(); Display { From dba15df9aeb0eb5a9eed8be7a02d887f00d573ad Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 14 Oct 2023 20:36:29 +0200 Subject: [PATCH 6/7] Add CLI --- rust-simulation/Cargo.lock | 128 +++++++++++++++++++++++++++++++++++- rust-simulation/Cargo.toml | 1 + rust-simulation/src/main.rs | 16 ++++- 3 files changed, 142 insertions(+), 3 deletions(-) diff --git a/rust-simulation/Cargo.lock b/rust-simulation/Cargo.lock index cc70a42..35a6936 100644 --- a/rust-simulation/Cargo.lock +++ b/rust-simulation/Cargo.lock @@ -35,6 +35,54 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -83,12 +131,58 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "clap_lex" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" + [[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "crc32fast" version = "1.3.2" @@ -225,6 +319,12 @@ dependencies = [ "weezl", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.3" @@ -401,7 +501,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -425,7 +525,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", - "syn", + "syn 1.0.109", "version_check", ] @@ -511,6 +611,7 @@ checksum = "56d84fdd47036b038fc80dd333d10b6aab10d5d31f4a366e20014def75328d33" name = "rust-simulation" version = "0.1.0" dependencies = [ + "clap", "embedded-graphics", "embedded-graphics-simulator", "env_logger", @@ -565,6 +666,12 @@ dependencies = [ "version-compare", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.109" @@ -576,6 +683,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "termcolor" version = "1.3.0" @@ -602,6 +720,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version-compare" version = "0.1.1" diff --git a/rust-simulation/Cargo.toml b/rust-simulation/Cargo.toml index 40439ac..44428d8 100644 --- a/rust-simulation/Cargo.toml +++ b/rust-simulation/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.4.6", features = ["derive"] } embedded-graphics = "0.8.1" embedded-graphics-simulator = "0.5.0" env_logger = "0.10.0" diff --git a/rust-simulation/src/main.rs b/rust-simulation/src/main.rs index 95d5504..ecb0aa3 100644 --- a/rust-simulation/src/main.rs +++ b/rust-simulation/src/main.rs @@ -11,6 +11,7 @@ use embedded_graphics_simulator::{ use env_logger; use log; use std::net::UdpSocket; +use clap::Parser; const PANEL_WIDTH: u32 = 32; const PANEL_COUNT: u32 = 5; @@ -123,8 +124,21 @@ impl Client { } } +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Port to bind to + #[arg(short, long, default_value_t=4242)] + port: i32, + + /// Address to bind to + #[arg(short, long, default_value_t=String::from("0.0.0.0"))] + addr: String +} + fn main() { + let args = Args::parse(); env_logger::init(); - let mut client = Client::new("0.0.0.0", 4242); + let mut client = Client::new("0.0.0.0", args.port); client.run(); } From 4c0480f0f368b7fe54ab258e1b60271d0e4754b4 Mon Sep 17 00:00:00 2001 From: Ada Date: Sat, 14 Oct 2023 20:42:45 +0200 Subject: [PATCH 7/7] Minor style fixes Rename the application struct Remove a hardcoded variable pair Missing comma --- rust-simulation/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rust-simulation/src/main.rs b/rust-simulation/src/main.rs index ecb0aa3..708f208 100644 --- a/rust-simulation/src/main.rs +++ b/rust-simulation/src/main.rs @@ -33,7 +33,7 @@ impl Display { .scale(4) .build(); Display { - display: SimulatorDisplay::new(Size::new(32 * 5, 40)), + display: SimulatorDisplay::new(Size::new(IMAGE_WIDTH, IMAGE_HEIGHT)), window: Window::new("LED Board", &output_settings), } } @@ -48,19 +48,19 @@ impl Display { } } -struct Client { +struct DisplayServer { socket: UdpSocket, display: Display, } type ImgData = [u8; IMAGE_LENGTH]; -impl Client { - fn new(addr: &str, port: i32) -> Client { +impl DisplayServer { + fn new(addr: &str, port: i32) -> DisplayServer { let addr_full = format!("{addr}:{port}"); log::info!("Listening on {addr_full}"); let socket = UdpSocket::bind(addr_full).expect("Couldn't bind"); - Client { + DisplayServer { socket, display: Display::new(), } @@ -83,7 +83,7 @@ impl Client { log::info!("Responding to status checking message from {}", src_addr); let data = &mut data[..bytes_read]; match self.socket.send_to(&data, &src_addr) { - Ok(_) => {} + Ok(_) => {}, Err(e) => log::error!("{:}", e), }; } @@ -139,6 +139,6 @@ struct Args { fn main() { let args = Args::parse(); env_logger::init(); - let mut client = Client::new("0.0.0.0", args.port); + let mut client = DisplayServer::new("0.0.0.0", args.port); client.run(); }