Advertisement
Guest User

Intel Galileo Code Examples

a guest
Sep 5th, 2014
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. /*
  2. This example shows how to use a system call to set current date & time with
  3. UNIX 'date' command, then get the current time, redirect to a .txt file,
  4. and read the contents of the text file back into the sketch.
  5. */
  6. char buf[9];
  7. void setup() {
  8.  
  9. Serial.begin(115200);
  10. system("date 010112002013"); //sets the date & time to 12:00 1st Jan 2013
  11. }
  12. void loop() {
  13.  
  14. system("date '+%H:%M:%S' > /home/root/time.txt"); //get current time in the format- hours:minutes:secs
  15. //and save in text file time.txt located in /home/root
  16. FILE *fp;
  17. fp = fopen("/home/root/time.txt", "r");
  18. fgets(buf, 9, fp);
  19. fclose(fp);
  20.  
  21. Serial.print("The current time is ");
  22. Serial.println(buf);
  23. delay(1000);
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30. /*
  31. This example shows how to read the temperature sensor on the Galileo's
  32. onboard ADC, AD7298, using the iio (Industrial I/O) subsystem.
  33. NOTE: This does not provide an accurate reading of the room tenmperature,
  34. because the ADC is placed near the Quark SoC on the PCB, which gets quite warm.
  35. As a result the ADC will always be a few degrees warmer than the actual room
  36. temperature.
  37. */
  38. char scale[4];
  39. char raw[4];
  40. char offset[4];
  41. int raw_i;
  42. int scale_i;
  43. int offset_i;
  44. int temp;
  45. void setup() {
  46.  
  47. Serial.begin(115200);
  48.  
  49. }
  50. void loop() {
  51.  
  52. temp = getADCTemp();
  53. Serial.print("Temperature is ");
  54. Serial.print(temp);
  55. Serial.println(" degrees celcius.");
  56. delay(1000);
  57.  
  58. }
  59. int getADCTemp(){
  60. FILE *fp_raw;
  61. fp_raw = fopen("/sys/bus/iio/devices/iio:device0/in_temp0_raw", "r"); //read the values from scale, raw and offset files.
  62. fgets(raw, 4, fp_raw); //we need all three values, because the formula for
  63. fclose(fp_raw); //calulating the actual temperature in milli-degrees Celcius
  64. //is: TEMP = (RAW + OFFSET) * SCALE
  65. FILE *fp_scale;
  66. fp_scale = fopen("/sys/bus/iio/devices/iio:device0/in_temp0_scale", "r");
  67. fgets(scale, 4, fp_scale);
  68. fclose(fp_scale);
  69.  
  70. FILE *fp_offset;
  71. fp_offset = fopen("/sys/bus/iio/devices/iio:device0/in_temp0_offset", "r");
  72. fgets(offset, 4, fp_offset);
  73. fclose(fp_offset);
  74.  
  75. raw_i = atoi(raw); //we have the values now, but they are in ASCII form-
  76. scale_i = atoi(scale); //we need them as integers so we can use them for calculations.
  77. offset_i = atoi(offset);
  78.  
  79. int temp = (raw_i + offset_i) * scale_i; //Calculate temperature in milli-degrees celcius
  80. temp /= 1000; //divide by 1000 to convert to degrees celcius
  81. return temp;
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. /*
  90. This example uses Linux system calls to create a python script which writes
  91. number 0-9 to a file, log.txt, one number per second. Then execute the
  92. python script in the background, and regularly read the contents of the logfile
  93. in the sketch while the python script is updating it.
  94. */
  95. char output[3];
  96. void setup() {
  97.  
  98. Serial.begin(115200);
  99.  
  100. system("echo '#!/usr/bin/python' > myScript.py");
  101. system("echo 'import time' >> myScript.py");
  102. system("echo 'for i in range(10):' >> myScript.py");
  103. system("echo ' with open(\"log.txt\", \"w\") as fh:' >> myScript.py");
  104. system("echo ' fh.write(\"{0}\".format(i))' >> myScript.py");
  105. system("echo ' time.sleep(1)' >> myScript.py");
  106. system("chmod a+x myScript.py");
  107. system("./myScript.py &");
  108. }
  109. void loop() {
  110.  
  111. FILE *fp;
  112. fp = fopen("log.txt", "r");
  113. fgets(output, 2, fp);
  114. fclose(fp);
  115. Serial.println(output);
  116. delay(1000);
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement