Skip to content

Commit b988e4f

Browse files
authored
Merge pull request #780 from W0CHP/master
Added CPU Temp readout to OLED display type
2 parents 05db572 + 4270dea commit b988e4f

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

OLED.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ bool COLED::open()
251251
return true;
252252
}
253253

254+
float readTemperature(const std::string& filePath) {
255+
std::ifstream file(filePath);
256+
if (!file.is_open()) {
257+
std::cerr << "Error: Could not open file " << filePath << std::endl;
258+
return -1.0; // Return a negative value to indicate that CPU temp is not available
259+
}
260+
261+
float temperature;
262+
file >> temperature;
263+
file.close();
264+
265+
return temperature / 1000.0; // The temperature is stored in millidegrees Celsius, so a bit of conversion
266+
}
267+
254268
void COLED::setIdleInt()
255269
{
256270
m_mode = MODE_IDLE;
@@ -292,14 +306,14 @@ void COLED::setIdleInt()
292306
if (configFile.is_open()) {
293307
std::string line;
294308
while (std::getline(configFile, line)) {
295-
if (line.find("ssid=") != std::string::npos) {
296-
std::istringstream iss(line);
297-
std::string key, value;
298-
if (std::getline(iss, key, '=') && std::getline(iss, value)) {
299-
ssid = value;
300-
break;
301-
}
302-
}
309+
if (line.find("ssid=") != std::string::npos) {
310+
std::istringstream iss(line);
311+
std::string key, value;
312+
if (std::getline(iss, key, '=') && std::getline(iss, value)) {
313+
ssid = value;
314+
break;
315+
}
316+
}
303317
}
304318
configFile.close();
305319
} else {
@@ -319,11 +333,22 @@ void COLED::setIdleInt()
319333
}
320334
} else { // Connected to network - no Auto-AP mode; normal display layout...
321335
if (m_displayLogoScreensaver) {
322-
m_display.setCursor(0,OLED_LINE3);
336+
m_display.setCursor(0,OLED_LINE2);
323337
m_display.setTextSize(1);
324338
m_display.print(" -IDLE-");
325-
m_display.setCursor(0, OLED_LINE5);
339+
m_display.setCursor(0, OLED_LINE4);
326340
m_display.printf("%s", m_ipaddress.c_str());
341+
// Display temperature
342+
float tempCelsius = readTemperature("/sys/class/thermal/thermal_zone0/temp");
343+
if (tempCelsius >= 0.0) {
344+
// Round the temperature to the nearest whole number
345+
int roundedTempCelsius = static_cast<int>(std::round(tempCelsius));
346+
// Convert to Fahrenheit
347+
float tempFahrenheit = (roundedTempCelsius * 9/5) + 32;
348+
m_display.setCursor(0, OLED_LINE5);
349+
m_display.setTextSize(1);
350+
m_display.printf("Temp: %.0fF / %dC ",tempFahrenheit,roundedTempCelsius);
351+
}
327352
}
328353
}
329354
m_display.display();
@@ -744,7 +769,7 @@ void COLED::writeCWInt()
744769
m_display.clearDisplay();
745770

746771
m_display.setCursor(0,30);
747-
m_display.setTextSize(3);
772+
m_display.setTextSize(2);
748773
m_display.print("CW ID TX");
749774

750775
m_display.setTextSize(1);
@@ -763,6 +788,17 @@ void COLED::clearCWInt()
763788
m_display.print(" -IDLE-");
764789
m_display.setCursor(0,OLED_LINE3);
765790
m_display.printf("%s",m_ipaddress.c_str());
791+
// Display temperature
792+
float tempCelsius = readTemperature("/sys/class/thermal/thermal_zone0/temp");
793+
if (tempCelsius >= 0.0) {
794+
// Round the temperature to the nearest whole number
795+
int roundedTempCelsius = static_cast<int>(std::round(tempCelsius));
796+
// Convert to Fahrenheit
797+
float tempFahrenheit = (roundedTempCelsius * 9/5) + 32;
798+
m_display.setCursor(0, OLED_LINE5);
799+
m_display.setTextSize(1);
800+
m_display.printf("Temp: %.0fF / %dC ",tempFahrenheit,roundedTempCelsius);
801+
}
766802

767803
if (m_displayScroll)
768804
m_display.startscrolldiagleft(0x00,0x0f);

OLED.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <string>
3535
#include <fstream>
3636
#include <sstream>
37+
#include <iostream> // for cpu temp value extraction
38+
#include <cmath> // for cpu temp value rounding
3739

3840
#include "ArduiPi_OLED_lib.h"
3941
#include "Adafruit_GFX.h"

0 commit comments

Comments
 (0)