From a5666f2a0045823dcee9c5a3c9598366fcce3513 Mon Sep 17 00:00:00 2001 From: Villivateur Von Date: Sun, 29 Aug 2021 16:24:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9E=B6=E6=9E=84=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 +- src/client_network.cpp | 48 +++++++++++ src/client_network.h | 19 +++++ src/display.cpp | 123 ++++++++++++++++++++++++++++ src/display.h | 20 +++++ src/main.cpp | 182 ++++------------------------------------- src/monitor_items.h | 10 +++ 7 files changed, 240 insertions(+), 166 deletions(-) create mode 100644 src/client_network.cpp create mode 100644 src/client_network.h create mode 100644 src/display.cpp create mode 100644 src/display.h create mode 100644 src/monitor_items.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 590e6fb..f0491cb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,8 @@ "list": "cpp", "unordered_map": "cpp", "vector": "cpp", - "initializer_list": "cpp" + "initializer_list": "cpp", + "cmath": "cpp", + "cstdint": "cpp" } } \ No newline at end of file diff --git a/src/client_network.cpp b/src/client_network.cpp new file mode 100644 index 0000000..7e945ae --- /dev/null +++ b/src/client_network.cpp @@ -0,0 +1,48 @@ +#include "client_network.h" + +void ClientNetwork::Init() +{ + WiFi.begin("VVAILL", "channy161021"); + + Serial.print("Connecting"); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); + + status = FAIL; +} + +STATUS ClientNetwork::FetchNewData() +{ + status = FAIL; + if (WiFi.status() == WL_CONNECTED) { + WiFiClient client; + http = new HTTPClient(); + if (http->begin(client, "http://iot.vvzero.com/topuino/getdata?UUID=1e788f28-7a5e-4888-96ff-71ab8b1876f8")) { + if (http->GET() == HTTP_CODE_OK) { + if (deserializeJson(receivedData, http->getString().c_str()) == DeserializationError::Code::Ok) { + status = OK; + } + } + http->end(); + } + delete http; + } + return status; +} + +uint8_t ClientNetwork::GetPercent(String name) +{ + return status == OK ? receivedData[name] : 0; +} + +uint32_t ClientNetwork::GetRate(String name) +{ + return status == OK ? receivedData[name] : 0; +} diff --git a/src/client_network.h b/src/client_network.h new file mode 100644 index 0000000..20415bc --- /dev/null +++ b/src/client_network.h @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include + +class ClientNetwork +{ +private: + StaticJsonDocument<512> receivedData; + HTTPClient* http; + STATUS status; + +public: + void Init(); + STATUS FetchNewData(); + uint8_t GetPercent(String name); + uint32_t GetRate(String name); +}; \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp new file mode 100644 index 0000000..d82de6c --- /dev/null +++ b/src/display.cpp @@ -0,0 +1,123 @@ +#include "display.h" + +#define STROBE_TM1 13 +#define STROBE_TM2 12 // strobe = GPIO connected to strobe line of module +#define STROBE_TM3 14 +#define CLOCK_TM 4 // clock = GPIO connected to clock line of module +#define DIO_TM 5 // data = GPIO connected to data line of module +#define HIGH_FREQ false //default false, If using a high freq CPU > ~100 MHZ set to true. + +DisplayPanel::DisplayPanel() +{ + // Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU) + percentLed = new TM1638plus(STROBE_TM1, CLOCK_TM, DIO_TM, HIGH_FREQ); + diskIoLed = new TM1638plus(STROBE_TM2, CLOCK_TM, DIO_TM, HIGH_FREQ); + netIoLed = new TM1638plus(STROBE_TM3, CLOCK_TM, DIO_TM, HIGH_FREQ); + percentLed->displayBegin(); + diskIoLed->displayBegin(); + netIoLed->displayBegin(); +} + +uint16_t DisplayPanel::PercentToBitmap(uint8_t percent) +{ + uint32_t tempPercentInt; + uint8_t tempPercentRem; + uint16_t tempDisplayBit; + + tempPercentInt = percent * 15 / 100; + tempPercentRem = percent * 15 % 100; + if (tempPercentRem > 50) { + tempPercentInt += 1; + } + tempDisplayBit = 0x1; + for (uint8_t i = 0; i < tempPercentInt; i++) { + tempDisplayBit <<= 1; + tempDisplayBit += 1; + } + + return tempDisplayBit; +} + +void DisplayPanel::DisplayCpuPercent(uint8_t percent) +{ + uint16_t bitmap = PercentToBitmap(percent); + percentLed->display7Seg(0, bitmap % 0x100); + percentLed->display7Seg(1, bitmap / 0x100); +} + +void DisplayPanel::DisplayMemPercent(uint8_t percent) +{ + uint16_t bitmap = PercentToBitmap(percent); + percentLed->display7Seg(2, bitmap % 0x100); + percentLed->display7Seg(3, bitmap / 0x100); +} + +void DisplayPanel::DisplayDisk0Percent(uint8_t percent) +{ + uint16_t bitmap = PercentToBitmap(percent); + percentLed->display7Seg(4, bitmap % 0x100); + percentLed->display7Seg(5, bitmap / 0x100); +} + +void DisplayPanel::DisplayDisk1Percent(uint8_t percent) +{ + uint16_t bitmap = PercentToBitmap(percent); + percentLed->display7Seg(6, bitmap % 0x100); + percentLed->display7Seg(7, bitmap / 0x100); +} + +void DisplayPanel::DisplayDiskRate(uint32_t byteRdPerSec, uint32_t byteWrPerSec) +{ + uint16_t displayRd; + uint16_t displayWr; + char displayStr[9]; + + if (byteRdPerSec < 1024 * 1024) { + displayRd = byteRdPerSec / 1024; + diskIoLed->setLED(0, 0); + diskIoLed->setLED(1, 1); + } else { + displayRd = byteRdPerSec / 1024 / 1024; + diskIoLed->setLED(0, 1); + diskIoLed->setLED(1, 0); + } + if (byteWrPerSec < 1024 * 1024) { + displayWr = byteWrPerSec / 1024; + diskIoLed->setLED(2, 0); + diskIoLed->setLED(3, 1); + } else { + displayWr = byteWrPerSec / 1024 / 1024; + diskIoLed->setLED(2, 1); + diskIoLed->setLED(3, 0); + } + sprintf(displayStr, "%4u%4u", displayRd, displayWr); + diskIoLed->displayText(displayStr); +} + +void DisplayPanel::DisplayNetRate(uint32_t byteTxPerSec, uint32_t byteRxPerSec) +{ + uint16_t displayTx; + uint16_t displayRx; + char displayStr[9]; + + if (byteTxPerSec < 1024 * 1024) { + displayTx = byteTxPerSec / 1024; + netIoLed->setLED(0, 0); + netIoLed->setLED(1, 1); + } else { + displayTx = byteTxPerSec / 1024 / 1024; + netIoLed->setLED(0, 1); + netIoLed->setLED(1, 0); + } + if (byteRxPerSec < 1024 * 1024) { + displayRx = byteRxPerSec / 1024; + netIoLed->setLED(2, 0); + netIoLed->setLED(3, 1); + } else { + displayRx = byteRxPerSec / 1024 / 1024; + netIoLed->setLED(2, 1); + netIoLed->setLED(3, 0); + } + sprintf(displayStr, "%4u%4u", displayTx, displayRx); + netIoLed->displayText(displayStr); +} diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..cf13f6b --- /dev/null +++ b/src/display.h @@ -0,0 +1,20 @@ +#include +#include + +class DisplayPanel +{ +private: + uint16_t PercentToBitmap(uint8_t percent); + TM1638plus* percentLed; + TM1638plus* diskIoLed; + TM1638plus* netIoLed; + +public: + DisplayPanel(); + void DisplayCpuPercent(uint8_t percent); + void DisplayMemPercent(uint8_t percent); + void DisplayDisk0Percent(uint8_t percent); + void DisplayDisk1Percent(uint8_t percent); + void DisplayDiskRate(uint32_t byteRdPerSec, uint32_t byteWrPerSec); + void DisplayNetRate(uint32_t byteTxPerSec, uint32_t byteRxPerSec); +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f4f7560..9d0e4f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,179 +1,31 @@ #include -#include -#include -#include -#include -#include +#include "display.h" +#include "client_network.h" +#include "monitor_items.h" -#define STROBE_TM1 13 -#define STROBE_TM2 12 // strobe = GPIO connected to strobe line of module -#define STROBE_TM3 14 -#define CLOCK_TM 4 // clock = GPIO connected to clock line of module -#define DIO_TM 5 // data = GPIO connected to data line of module -bool high_freq = false; //default false, If using a high freq CPU > ~100 MHZ set to true. - -StaticJsonDocument<512> receivedData; - -// Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU) -TM1638plus tm1(STROBE_TM1, CLOCK_TM, DIO_TM, high_freq); -TM1638plus tm2(STROBE_TM2, CLOCK_TM, DIO_TM, high_freq); -TM1638plus tm3(STROBE_TM3, CLOCK_TM, DIO_TM, high_freq); +DisplayPanel* displayPanel; +ClientNetwork* netClient; void setup() { - tm1.displayBegin(); - tm2.displayBegin(); - tm3.displayBegin(); Serial.begin(115200); Serial.println(); - pinMode(13, OUTPUT); - pinMode(12, OUTPUT); - pinMode(14, OUTPUT); - digitalWrite(13, LOW); - digitalWrite(12, LOW); - digitalWrite(14, LOW); - - WiFi.begin("VVAILL", "channy161021"); - - Serial.print("Connecting"); - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print("."); - } - Serial.println(); - - Serial.print("Connected, IP address: "); - Serial.println(WiFi.localIP()); -} - -uint16_t PercentToBitmap(uint8_t percent) -{ - uint32_t tempPercent16; - uint16_t tempDisplayBit; - - tempPercent16 = percent * 15 / 100; - tempDisplayBit = 0x1; - for (uint8_t i = 0; i < tempPercent16; i++) { - tempDisplayBit <<= 1; - tempDisplayBit += 1; - } - - return tempDisplayBit; -} - - - -void DisplayCpuPercent(uint8_t percent) -{ - uint16_t bitmap = PercentToBitmap(percent); - tm1.display7Seg(0, bitmap % 0x100); - tm1.display7Seg(1, bitmap / 0x100); -} - -void DisplayMemPercent(uint8_t percent) -{ - uint16_t bitmap = PercentToBitmap(percent); - tm1.display7Seg(2, bitmap % 0x100); - tm1.display7Seg(3, bitmap / 0x100); -} - -void DisplayDisk0Percent(uint8_t percent) -{ - uint16_t bitmap = PercentToBitmap(percent); - tm1.display7Seg(4, bitmap % 0x100); - tm1.display7Seg(5, bitmap / 0x100); -} - -void DisplayDisk1Percent(uint8_t percent) -{ - uint16_t bitmap = PercentToBitmap(percent); - tm1.display7Seg(6, bitmap % 0x100); - tm1.display7Seg(7, bitmap / 0x100); -} - -void DisplayDiskRate(uint32_t byteRdPerSec, uint32_t byteWrPerSec) -{ - uint16_t displayRd; - uint16_t displayWr; - char displayStr[9]; - - if (byteRdPerSec < 1024 * 1024) { - displayRd = byteRdPerSec / 1024; - tm2.setLED(0, 0); - tm2.setLED(1, 1); - } else { - displayRd = byteRdPerSec / 1024 / 1024; - tm2.setLED(0, 1); - tm2.setLED(1, 0); - } - if (byteWrPerSec < 1024 * 1024) { - displayWr = byteWrPerSec / 1024; - tm2.setLED(2, 0); - tm2.setLED(3, 1); - } else { - displayWr = byteWrPerSec / 1024 / 1024; - tm2.setLED(2, 1); - tm2.setLED(3, 0); - } - sprintf(displayStr, "%4u%4u", displayRd, displayWr); - tm2.displayText(displayStr); -} - -void DisplayNetRate(uint32_t byteTxPerSec, uint32_t byteRxPerSec) -{ - uint16_t displayTx; - uint16_t displayRx; - char displayStr[9]; - - if (byteTxPerSec < 1024 * 1024) { - displayTx = byteTxPerSec / 1024; - tm3.setLED(0, 0); - tm3.setLED(1, 1); - } else { - displayTx = byteTxPerSec / 1024 / 1024; - tm3.setLED(0, 1); - tm3.setLED(1, 0); - } - if (byteRxPerSec < 1024 * 1024) { - displayRx = byteRxPerSec / 1024; - tm3.setLED(2, 0); - tm3.setLED(3, 1); - } else { - displayRx = byteRxPerSec / 1024 / 1024; - tm3.setLED(2, 1); - tm3.setLED(3, 0); - } - sprintf(displayStr, "%4u%4u", displayTx, displayRx); - tm3.displayText(displayStr); + displayPanel = new DisplayPanel(); + netClient = new ClientNetwork(); + netClient->Init(); } void loop() { - if (WiFi.status() == WL_CONNECTED) { - WiFiClient client; - HTTPClient http; - - if (http.begin(client, "http://iot.vvzero.com/topuino/getdata?UUID=1e788f28-7a5e-4888-96ff-71ab8b1876f8")) { - int httpCode = http.GET(); - if (httpCode == HTTP_CODE_OK) { - DeserializationError jsonError = deserializeJson(receivedData, http.getString().c_str()); - if (jsonError) { - Serial.println(http.getString().c_str()); - Serial.print(F("deserializeJson() failed: ")); - Serial.println(jsonError.f_str()); - } else { - DisplayCpuPercent(receivedData["CPU_PERCENT"]); - DisplayMemPercent(receivedData["MEM_PERCENT"]); - DisplayDisk0Percent(receivedData["DISK_PERCENT"]); - DisplayDisk1Percent(receivedData["DISK1_PERCENT"]); - DisplayDiskRate(receivedData["DISK_READ_RATE"], receivedData["DISK_WRITE_RATE"]); - DisplayNetRate(receivedData["NET_SENT_RATE"], receivedData["NET_RECV_RATE"]); - } - } - http.end(); - } - } delay(1000); + if (netClient->FetchNewData() != OK) { + return; + } + displayPanel->DisplayCpuPercent(netClient->GetPercent(CPU_PERCENT)); + displayPanel->DisplayMemPercent(netClient->GetPercent(MEM_PERCENT)); + displayPanel->DisplayDisk0Percent(netClient->GetPercent(DISK0_PERCENT)); + displayPanel->DisplayDisk1Percent(netClient->GetPercent(DISK1_PERCENT)); + displayPanel->DisplayDiskRate(netClient->GetRate(DISK_READ_RATE), netClient->GetRate(DISK_WRITE_RATE)); + displayPanel->DisplayNetRate(netClient->GetRate(NET_SENT_RATE), netClient->GetRate(NET_RECV_RATE)); } diff --git a/src/monitor_items.h b/src/monitor_items.h new file mode 100644 index 0000000..2cdfe47 --- /dev/null +++ b/src/monitor_items.h @@ -0,0 +1,10 @@ +// Topuino and Topuino_Server must share this header file + +#define CPU_PERCENT "CPU_PERCENT" +#define MEM_PERCENT "MEM_PERCENT" +#define DISK0_PERCENT "DISK_PERCENT" +#define DISK1_PERCENT "DISK1_PERCENT" +#define DISK_READ_RATE "DISK_READ_RATE" +#define DISK_WRITE_RATE "DISK_WRITE_RATE" +#define NET_SENT_RATE "NET_SENT_RATE" +#define NET_RECV_RATE "NET_RECV_RATE" \ No newline at end of file