-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kernzerfall/cpp
Ditch golang in the server and switch to C++
- Loading branch information
Showing
13 changed files
with
297 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.