Skip to content

Commit

Permalink
Merge pull request #1 from kernzerfall/cpp
Browse files Browse the repository at this point in the history
Ditch golang in the server and switch to C++
  • Loading branch information
kernzerfall authored Feb 28, 2021
2 parents 33ba71c + 015bf38 commit cec87bd
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"board": "arduino:avr:uno",
"programmer": "AVRISP mkII",
"sketch": "serialDisplay.ino",
"sketch": "client.ino",
"port": "COM4"
}
6 changes: 3 additions & 3 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"cppStandard": "c++20",
"intelliSenseMode": "clang-x64",
"forcedInclude": [
"C:\\arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
]
},
{
"name": "Linux",
"name": "WSL",
"includePath": [
"/mnt/c/arduino/tools/**",
"/mnt/c/arduino/libraries/**",
Expand All @@ -36,8 +37,7 @@
"cStandard": "c18",
"cppStandard": "c++20",
"intelliSenseMode": "clang-x64",
"forcedInclude": [
]
"forcedInclude": []
}
],
"version": 4
Expand Down
50 changes: 49 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
{
"files.associations": {
"cstring": "cpp"
"cstring": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"bit": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.16)
SET(CMAKE_CXX_STANDARD 20)
SET(CMAKE_CXX_STANDARD_REQUIRED TRUE)
SET(CMAKE_BUILD_TYPE Debug)
SET(CMAKE_BUILD_TYPE MinSizeRel)

if(WIN32)
# 0x061 = _WIN32_WINNT_WIN7, see sdkddver.h
if(MINGW)
# 0x601 = _WIN32_WINNT_WIN7, see sdkddver.h
add_compile_definitions(_WIN32_WINNT=0x601)
SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-allow-multiple-definition")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
endif()

project(assess)
project(ArduSerialStatsServer)

include_directories(
"${PROJECT_SOURCE_DIR}/include/"
Expand All @@ -20,4 +22,4 @@ add_library(rs232 SHARED "${PROJECT_SOURCE_DIR}/include/rs232/rs232.c")
add_library(resources SHARED "${PROJECT_SOURCE_DIR}/include/resources/resources.cpp")

add_executable(server "server.cpp")
target_link_libraries(server rs232 resources)
target_link_libraries(server rs232 resources)
12 changes: 6 additions & 6 deletions serialDisplay.ino → client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

class StateKeeper {
public:
byte buf[SerialConstant::Data::SIZE_SERIALBUF] = {0x00};
byte buf[SerialConstant::Size::SERIALBUF] = {0x00};
byte state = SerialConstant::State::IDLE;
byte next = SerialConstant::State::IDLE;
byte bIndex = 0x00;
Expand All @@ -30,7 +30,7 @@ class StateKeeper {
} temp;

void resetBuf(){
for(u16 i = 0; i < SerialConstant::Data::SIZE_SERIALBUF; i++) this->buf[i] = 0x00;
for(u16 i = 0; i < SerialConstant::Size::SERIALBUF; i++) this->buf[i] = 0x00;
this->bIndex = 0x00;
this->state = SerialConstant::State::IDLE;
}
Expand Down Expand Up @@ -157,11 +157,11 @@ void loop(){
case SerialConstant::Flag::DATA_END:
// If END of DataPacket found, find the appropriate handler for the data
switch(s.state){
case SerialConstant::Data::TYPE_DATETIME:
case SerialConstant::Data::DATETIME:
handleDateTime(); break;;
case SerialConstant::Data::TYPE_CPUTIL:
case SerialConstant::Data::CPUTIL:
handleCPUtil(); break;;
case SerialConstant::Data::TYPE_RAMUTIL:
case SerialConstant::Data::RAMUTIL:
handleRAMUtil(); break;;
}
s.resetBuf();
Expand All @@ -176,7 +176,7 @@ void loop(){
// If not at IDLE push back the next serial byte in the array
if(s.state != SerialConstant::State::IDLE){
// Overflow detection
if(s.bIndex > SerialConstant::Data::SIZE_SERIALBUF - 1 ){
if(s.bIndex > SerialConstant::Size::SERIALBUF - 1 ){
s.bIndex = 0;
// Reset if overflown
s.next = SerialConstant::State::HALT;
Expand Down
8 changes: 0 additions & 8 deletions include/Datatypes.hpp

This file was deleted.

26 changes: 0 additions & 26 deletions include/SerialConstant.hpp

This file was deleted.

12 changes: 12 additions & 0 deletions include/datatypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* This header contains some datatype definitions that are *\
\* commonly used in both the server and the board program */

#pragma once

using byte = unsigned char;
using u64 = unsigned long long;
using s64 = signed long long;
using u16 = unsigned short;
using s16 = signed short;
using cf32 = const float;
using f32 = float;
102 changes: 63 additions & 39 deletions include/resources/resources.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,90 @@
// CPU Load Percent (Credit to Jeremy Friesner): https://stackoverflow.com/questions/23143693/retrieving-cpu-load-percent-total-in-windows-with-c
#include <resources.hpp>

f32 Resources::floor(f32 x){
s64 o = (s64)trunc(10.0f*x);
o = o - (o % 10);
f32 k = ((float)o)/10.0f;
return k;
}

#ifdef _WIN32
#include <Windows.h>
static f32 CalculateCPULoad(u64 idleTicks, u64 totalTicks){
static u64 _previousTotalTicks = 0;
static u64 _previousIdleTicks = 0;
/* CPU Load Percent START *\
|* Credit: Jeremy Friesner *|
\* https://stackoverflow.com/questions/23143693/retrieving-cpu-load-percent-total-in-windows-with-c */
namespace jfriesner_stackoverflow {
static f32 calculateCPULoad(u64 idleTicks, u64 totalTicks){
static u64 _pTotalTicks = 0;
static u64 _pIdleTicks = 0;

u64 totalTicksSinceLastTime = totalTicks-_previousTotalTicks;
u64 idleTicksSinceLastTime = idleTicks-_previousIdleTicks;
u64 totalTicksSinceLast = totalTicks-_pTotalTicks;
u64 idleTicksSinceLast = idleTicks-_pIdleTicks;

f32 ret = 1.0f-((totalTicksSinceLastTime > 0) ? ((f32)idleTicksSinceLastTime)/totalTicksSinceLastTime : 0);
f32 ret = 1.0f-((totalTicksSinceLast > 0) ? ((f32)idleTicksSinceLast)/totalTicksSinceLast : 0);

_previousTotalTicks = totalTicks;
_previousIdleTicks = idleTicks;
return ret;
}
_pTotalTicks = totalTicks;
_pIdleTicks = idleTicks;
return ret;
}

static u64 FileTimeToInt64(const FILETIME &ft){
return (((u64)(ft.dwHighDateTime))<<32) | ((u64)ft.dwLowDateTime);
}
static u64 fileTimeToInt64(const FILETIME &ft){
return (((u64)(ft.dwHighDateTime))<<32) | ((u64)ft.dwLowDateTime);
}

// Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between
// You'll need to call this at regular intervals, since it measures the load between
// the previous call and the current one. Returns -1.0 on error.
f32 GetCPULoad(){
FILETIME idleTime, kernelTime, userTime;
#ifndef __INTELLISENSE__
return GetSystemTimes(&idleTime, &kernelTime, &userTime) ? CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime)+FileTimeToInt64(userTime)) : -1.0f;
#endif
// Range 0.0f - 1.0f
// Error -1.0f
// Must be called at regular intervals, measures "since last time called"
f32 getCPULoad(){
FILETIME idleTime, kernelTime, userTime;
#ifndef __INTELLISENSE__
return GetSystemTimes(&idleTime, &kernelTime, &userTime) ? calculateCPULoad(fileTimeToInt64(idleTime), fileTimeToInt64(kernelTime)+fileTimeToInt64(userTime)) : -1.0f;
#endif
}
}

using namespace std;
unsigned char Resources::getCPUtil(){
auto util = GetCPULoad();
auto util = jfriesner_stackoverflow::getCPULoad();
if(util < 0) throw new std::exception();
Sleep(1000);
util = GetCPULoad();
Sleep(CPU_LOAD_INTERVAL);
util = jfriesner_stackoverflow::getCPULoad();
if(util < 0 || util > 1.0f) throw new std::exception();
return (unsigned char)(floor(util*100.0f+0.5));
return static_cast<unsigned char>((floor(util*100.0f+0.5f)));
}

unsigned char Resources::getRAMUtil(){
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
return (unsigned char)statex.dwMemoryLoad;
return static_cast<unsigned char>(statex.dwMemoryLoad);
}

#else

byte Resources::getCPUtil(){
// read 1st val /proc/loadavg
// *100/no of cpuc
return 0xfe;
std::ifstream stat;
std::string line;

s64 total = 0.0f;
s64 work = 0.0f;
for(s16 i = 0; i < 2; ++i){
stat.open("/proc/stat");
while(std::getline(stat, line)){
if(line.substr(0,3).find("cpu") != std::string::npos){
line = line.substr(line.find_first_of(" ")+1);
std::istringstream iss(line);
s64 curr;
for(u16 j = 0; iss>>curr; j++){
if(j<3) work += ((i==0)?(-1):(1)) * curr;
total += ((i==0)?(-1):(1)) * curr;
}
// if cpu line found, no need to parse the rest of the file
break;
} else continue;
}
stat.close();
usleep(CPU_LOAD_INTERVAL * 1e+3);
}

f32 res = floor((f32)(work) / (f32)(total) * 100.0f + 0.5f);
return static_cast<byte>(res);
}
byte Resources::getRAMUtil(){
return 0xfe;
struct sysinfo si;
sysinfo(&si);
f32 ramutil = floor((f32)(si.totalram-si.freeram)/(f32)si.totalram * 100.0f + 0.5f);
return static_cast<byte>(ramutil);
}
#endif
14 changes: 7 additions & 7 deletions include/resources/resources.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#define CPU_LOAD_INTERVAL 1000 // millis

#include <datatypes.hpp>
#include <exception>
#include <cmath>
Expand All @@ -9,16 +11,14 @@
#include <ctime>
#else
#include <fstream>
#endif

#ifdef __WIN32
static f32 CalculateCPULoad(u64 idleTicks, u64 totalTicks);
static u64 FileTimeToInt64(const FILETIME &ft);
f32 GetCPULoad();
#include <sstream>
#include <iostream>
#include <memory>
#include <unistd.h>
#include <sys/sysinfo.h>
#endif

namespace Resources {
byte getCPUtil();
byte getRAMUtil();
f32 floor(f32);
};
Loading

0 comments on commit cec87bd

Please sign in to comment.