raspberrypi_io.c 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * raspberrypi_io.c
  3. *
  4. * Created on: 23.06.2014
  5. * Author: root
  6. */
  7. #include "raspberrypi_io.h"
  8. #ifdef RASPI
  9. static _Bool initialized = 0;
  10. int initIO()
  11. {
  12. if (wiringPiSetup() == -1){
  13. initialized = 0;
  14. return 1;
  15. }
  16. initialized = 1;
  17. return 0;
  18. }
  19. int readTemp(float *temp){
  20. FILE *ptr_file;
  21. char buf[1000];
  22. char delimiter[] = "=";
  23. char *ptr_temp;
  24. long temperaturInmC;
  25. ptr_file =fopen("/sys/bus/w1/devices/10-000802c607f3/w1_slave","r");
  26. if (!ptr_file){
  27. puts("ERROR: no sensor");
  28. return 1;
  29. }
  30. fgets(buf, 1000, ptr_file);
  31. fgets(buf, 1000, ptr_file);
  32. ptr_temp = strtok(buf, (char*) delimiter);
  33. ptr_temp = strtok((void*)0, (char*) delimiter);
  34. temperaturInmC = atol(ptr_temp);
  35. fclose(ptr_file);
  36. *temp = (float)temperaturInmC/1000;
  37. return 0;
  38. }
  39. int writePin(_Bool state, int pin){
  40. if(initialized)
  41. {
  42. pinMode(0, OUTPUT);
  43. if(state==(1==1)){
  44. digitalWrite(pin, 1);
  45. }else{
  46. digitalWrite(pin, 0);
  47. }
  48. return 0;
  49. }
  50. return 1; //ERROR
  51. }
  52. #endif