Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read temperature #12

Open
ShellAlbert opened this issue Jul 5, 2019 · 1 comment
Open

read temperature #12

ShellAlbert opened this issue Jul 5, 2019 · 1 comment

Comments

@ShellAlbert
Copy link
Owner

QQ截图20190705163919

@ShellAlbert
Copy link
Owner Author

QQ截图20190705164116
#include <stdio.h> /* for FILE, fopen, fread, fclose, printf and sprintf /
#include <stdlib.h> /
for atof */

int main (int argc, char **argv) {
   FILE *filePtr;
   char therm_path[64];  /* 64 bytes buffer for building current pseudo file path */
   char readBuf[16];     /* 16 bytes buffer for reading ASCII encoded value from pseudo file */
   float temp;         
   for (unsigned int curZone = 0; curZone < 8; curZone++) {
      unsigned int readBytes = 0;
      sprintf(therm_path, "/sys/devices/virtual/thermal/thermal_zone%u/temp", curZone);
      printf ("Reading %s:   ", therm_path);
      filePtr = fopen(therm_path, "r");
      if (!filePtr) {
    	printf ("Error, failed to open file\n");
    	continue;
      }
      while (readBytes < 16) {
         int curRead = fread(readBuf + readBytes, 1, 16 - readBytes, filePtr);
          if (curRead > 0)
    	     readBytes += curRead;
          else 
             break; /* nothing more to be read */
      }
      fclose (filePtr);

      if (readBytes > 0)
         *(readBuf + readBytes - 1) = 0; /* Remove last char '\n' */
      printf("[%s]  ", readBuf);

      temp = atof(readBuf);
      temp /= 1000.f;
      printf("%.02f\n", temp);
   }

   exit(0);
}

QQ截图20190705164126
QQ截图20190705164136
QQ截图20190705164147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant