ShellAlbert / Jetson-TX2

new platform for processing video & audio
GNU Lesser General Public License v3.0
2 stars 0 forks source link

read temperature #12

Open ShellAlbert opened 5 years ago

ShellAlbert commented 5 years ago

QQ截图20190705163919

ShellAlbert commented 5 years ago

QQ截图20190705164116

include / 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