From 793b912efd3eadb2003180c9d330a0d95fb102f2 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Tue, 17 Sep 2019 11:47:58 -0400 Subject: [PATCH] Thermal display (closes #99) (#102) * Display the temperature status. Moved statusGet code from drawNetwork() into drawStatus(). Added types in common.h to fix building with latest libnx nacp.h, with the pc-build. --- Makefile.pc | 2 +- common/common.h | 3 +++ common/menu.c | 30 ++++++++++++++++++------------ common/status.c | 20 ++++++++++++++++++-- common/status.h | 2 +- common/thermalstatus.h | 7 +++++++ nx_main/nx_thermalstatus.c | 14 ++++++++++++++ pc_main/pc_thermalstatus.c | 15 +++++++++++++++ 8 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 common/thermalstatus.h create mode 100644 nx_main/nx_thermalstatus.c create mode 100644 pc_main/pc_thermalstatus.c diff --git a/Makefile.pc b/Makefile.pc index e15c645..134b71f 100644 --- a/Makefile.pc +++ b/Makefile.pc @@ -9,7 +9,7 @@ EXTRA_CFLAGS="-D__USE_MINGW_ANSI_STDIO" EXTRA_LDFLAGS="-lws2_32" endif -test : pc_main/main.cpp pc_main/pc_launch.c pc_main/pc_power.c pc_main/pc_netstatus.c \ +test : pc_main/main.cpp pc_main/pc_launch.c pc_main/pc_power.c pc_main/pc_netstatus.c pc_main/pc_thermalstatus.c \ common/menu.c common/font.c common/language.c common/launch.c common/worker.c common/status.c \ common/menu-entry.c common/menu-list.c common/message-box.c common/text.c \ common/ui.c common/assets.c common/math.c common/theme.c \ diff --git a/common/common.h b/common/common.h index b59f77f..10757f6 100644 --- a/common/common.h +++ b/common/common.h @@ -23,6 +23,8 @@ typedef uint8_t u8; typedef uint32_t u32; typedef uint64_t u64; +typedef int8_t s8; +typedef int32_t s32; typedef u32 Result; typedef void (*workerThreadFunc)(void *); @@ -65,6 +67,7 @@ typedef union { #include "power.h" #include "netloader.h" #include "netstatus.h" +#include "thermalstatus.h" #include "status.h" void menuStartupPath(void); diff --git a/common/menu.c b/common/menu.c index b0f7d96..c745e64 100644 --- a/common/menu.c +++ b/common/menu.c @@ -487,18 +487,17 @@ void drawCharge() { } } -void drawNetwork(int tmpX) { - bool netstatusFlag=0; - AssetId id; - if (statusGet(&netstatusFlag, &id)) { - if (netstatusFlag) - drawIcon(tmpX, 0 + 47 + 10 + 3, 24, 24, assetsGetDataBuffer(id), themeCurrent.textColor); - } +void drawNetwork(int tmpX, AssetId id) { + drawIcon(tmpX, 0 + 47 + 10 + 3, 24, 24, assetsGetDataBuffer(id), themeCurrent.textColor); } u32 drawStatus() { + bool netstatusFlag=0; + bool temperatureFlag=0; + s32 temperature=0; + AssetId id; - char timeString[9]; + char tmpstr[32]; time_t unixTime = time(NULL); struct tm* timeStruct = localtime((const time_t *)&unixTime); @@ -507,14 +506,21 @@ u32 drawStatus() { int minutes = timeStruct->tm_min; int seconds = timeStruct->tm_sec; - sprintf(timeString, "%02d:%02d:%02d", hours, minutes, seconds); + snprintf(tmpstr, sizeof(tmpstr)-1, "%02d:%02d:%02d", hours, minutes, seconds); - u32 tmpX = GetTextXCoordinate(interuimedium20, 1180, timeString, 'r'); + u32 tmpX = GetTextXCoordinate(interuimedium20, 1180, tmpstr, 'r'); - DrawText(interuimedium20, tmpX, 0 + 47 + 10, themeCurrent.textColor, timeString); + DrawText(interuimedium20, tmpX, 0 + 47 + 10, themeCurrent.textColor, tmpstr); drawCharge(); - drawNetwork(tmpX); + + if (statusGet(&netstatusFlag, &id, &temperatureFlag, &temperature)) { + if (netstatusFlag) drawNetwork(tmpX, id); + if (temperatureFlag) { + snprintf(tmpstr, sizeof(tmpstr)-1, "%.1f°C", ((float)temperature) / 1000); + DrawText(interuiregular14, 1180 + 4, 0 + 47 + 10 + + 21 + 6, themeCurrent.textColor, tmpstr); + } + } return tmpX; } diff --git a/common/status.c b/common/status.c index b0f0d9e..77b2041 100644 --- a/common/status.c +++ b/common/status.c @@ -10,6 +10,8 @@ static bool s_statusExit; static bool s_statusReady; static bool s_statusNetFlag; static AssetId s_statusNetAssetId; +static bool s_statusTemperatureFlag; +static s32 s_statusTemperature; // This uses netstatusGetDetails from a dedicated thread, since nifmGetInternetConnectionStatus can block for a few seconds. @@ -18,8 +20,13 @@ static int statusThreadProc(void* unused) mtx_lock(&s_statusMtx); struct timespec timeout = {0}; - bool tmpflag; + bool tmpflag=0; + bool thermalflag=0; + bool thermal_initialized=0; AssetId tmpid; + s32 temperature; + + thermal_initialized = thermalstatusInit(); clock_gettime(CLOCK_MONOTONIC, &timeout); timeout.tv_sec++; @@ -32,12 +39,16 @@ static int statusThreadProc(void* unused) break; tmpflag = netstatusGetDetails(&tmpid); + if (thermal_initialized) thermalflag = thermalstatusGetDetails(&temperature); mtx_lock(&s_statusAccessMtx); s_statusNetFlag = tmpflag; s_statusNetAssetId = tmpid; + s_statusTemperatureFlag = thermalflag; + s_statusTemperature = temperature; + s_statusReady = 1; mtx_unlock(&s_statusAccessMtx); @@ -48,10 +59,12 @@ static int statusThreadProc(void* unused) mtx_unlock(&s_statusMtx); + if (thermal_initialized) thermalstatusExit(); + return 0; } -bool statusGet(bool *netstatusFlag, AssetId *netstatusAssetId) { +bool statusGet(bool *netstatusFlag, AssetId *netstatusAssetId, bool *temperatureFlag, s32 *temperature) { if (!s_statusReady) return 0; mtx_lock(&s_statusAccessMtx); @@ -59,6 +72,9 @@ bool statusGet(bool *netstatusFlag, AssetId *netstatusAssetId) { *netstatusFlag = s_statusNetFlag; *netstatusAssetId = s_statusNetAssetId; + *temperatureFlag = s_statusTemperatureFlag; + *temperature = s_statusTemperature; + mtx_unlock(&s_statusAccessMtx); return 1; diff --git a/common/status.h b/common/status.h index 7e52eba..4e32c07 100644 --- a/common/status.h +++ b/common/status.h @@ -3,5 +3,5 @@ bool statusInit(void); void statusExit(void); -bool statusGet(bool *netstatusFlag, AssetId *netstatusAssetId); +bool statusGet(bool *netstatusFlag, AssetId *netstatusAssetId, bool *temperatureFlag, s32 *temperature); diff --git a/common/thermalstatus.h b/common/thermalstatus.h new file mode 100644 index 0000000..12c1183 --- /dev/null +++ b/common/thermalstatus.h @@ -0,0 +1,7 @@ +#pragma once +#include "common.h" + +bool thermalstatusInit(void); +void thermalstatusExit(void); +bool thermalstatusGetDetails(s32 *temperature); + diff --git a/nx_main/nx_thermalstatus.c b/nx_main/nx_thermalstatus.c new file mode 100644 index 0000000..3b35baf --- /dev/null +++ b/nx_main/nx_thermalstatus.c @@ -0,0 +1,14 @@ +#include "../common/common.h" + +bool thermalstatusInit(void) { + return R_SUCCEEDED(tsInitialize()); +} + +void thermalstatusExit(void) { + tsExit(); +} + +bool thermalstatusGetDetails(s32 *temperature) { + return R_SUCCEEDED(tsGetTemperatureMilliC(TsLocation_Internal, temperature)); +} + diff --git a/pc_main/pc_thermalstatus.c b/pc_main/pc_thermalstatus.c new file mode 100644 index 0000000..1ecac2f --- /dev/null +++ b/pc_main/pc_thermalstatus.c @@ -0,0 +1,15 @@ +#include "../common/common.h" + +bool thermalstatusInit(void) { + return false; +} + +void thermalstatusExit(void) { + +} + +bool thermalstatusGetDetails(s32 *temperature) { + *temperature = 0; + return false; +} +