代码架构调整

This commit is contained in:
Villivateur Von 2021-08-29 16:24:40 +08:00
parent c178761cda
commit a5666f2a00
7 changed files with 240 additions and 166 deletions

View File

@ -9,6 +9,8 @@
"list": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"initializer_list": "cpp"
"initializer_list": "cpp",
"cmath": "cpp",
"cstdint": "cpp"
}
}

48
src/client_network.cpp Normal file
View File

@ -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;
}

19
src/client_network.h Normal file
View File

@ -0,0 +1,19 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
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);
};

123
src/display.cpp Normal file
View File

@ -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);
}

20
src/display.h Normal file
View File

@ -0,0 +1,20 @@
#include <Arduino.h>
#include <TM1638plus.h>
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);
};

View File

@ -1,179 +1,31 @@
#include <Arduino.h>
#include <TM1638plus.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#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));
}

10
src/monitor_items.h Normal file
View File

@ -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"