Fixed issue that caused the user application to print temperature twice

This commit is contained in:
uzy lol 2024-12-08 18:00:10 -08:00
parent 7e87cabb3c
commit 8ee6467330

View File

@ -9,9 +9,10 @@
#define GREEN "\033[1;32m"
#define RESET "\033[0m"
void read_temp(int fd, const char *unit, int is_simulated) {
void read_temp(int fd, const char *unit) {
char buffer[128];
ssize_t bytes_read;
int is_simulated = 0;
// Writing the desired unit or "simulate" command to the device
ssize_t bytes_written = write(fd, unit, strlen(unit));
@ -25,8 +26,14 @@ void read_temp(int fd, const char *unit, int is_simulated) {
return; // Exit the function if reading fails
} else {
buffer[bytes_read] = '\0'; // Null-terminate the string
// Check if the temperature is simulated
if (strstr(buffer, "(simulated temperature)") != NULL) {
is_simulated = 1;
}
if (is_simulated) {
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s (simulated temperature)\n", buffer);
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
} else {
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
}
@ -49,19 +56,16 @@ int main() {
if (choice == 'C' || choice == 'c') {
// Requesting the temperature in Celsius
read_temp(fd, "C", 0);
read_temp(fd, "C");
} else if (choice == 'F' || choice == 'f') {
// Requesting the temperature in Fahrenheit
read_temp(fd, "F", 0);
read_temp(fd, "F");
} else {
printf("Invalid choice. Please choose 'C' or 'F'.\n");
close(fd);
return -1;
}
// Requesting simulated temperature
read_temp(fd, "simulate", 1);
close(fd);
return 0;
}