Merge pull request #30 from Callum-/master
[matches/MCTX3420.git] / BBB code / GetData_real.c
1 DataPoint * GetData(int sensor_id, DataPoint * d)
2 {       
3         //TODO: We should ensure the time is *never* allowed to change on the server if we use gettimeofday
4         //              Another way people might think of getting the time is to count CPU cycles with clock()
5         //              But this will not work because a) CPU clock speed may change on some devices (RPi?) and b) It counts cycles used by all threads
6         gettimeofday(&(d->time_stamp), NULL);
7         
8         switch (sensor_id)
9         {
10                 //TODO: test buffer size on actual hardware
11                 //              maybe map the sensor path to an array/structure that can be looked up via sensor_id?
12                 //              not sure about the best place to do unit conversions, especially for nonlinear sensors
13                 case SENSOR_TEST0:
14                 {
15                         int sensor = open("/sys/devices/platform/tsc/ain0", O_RDONLY); //need unique path for each sensor ADC, check path in documentation
16                         char buffer[128];                                                                                       //ADCs on Beaglebone are 12 bits?
17                         int read = read(sensor, buffer, sizeof(buffer);
18                         if (read != -1) {
19                                 buffer[read] = NULL;                                                                            //string returned by read is not null terminated
20                                 int value = atoi(buffer);
21                                 double convert = (value/4096) * 1800;                                           //sample conversion from ADC input to 'true value'
22                 d->value = convert;
23                                 lseek(sensor, 0, 0);                                                                            //after read string must be rewound to start of file using lseek
24             }
25                         else {
26                                 perror("Failed to get value from sensor");
27                         }
28                         close(sensor);
29                         break;
30                 }
31                 case SENSOR_TEST1:
32                         int sensor = open("/sys/devices/platform/tsc/ain1", O_RDONLY); 
33                         char buffer[128];       
34                         int read = read(sensor, buffer, sizeof(buffer);
35                         if (read != -1) {
36                                 buffer[read] = NULL;    
37                                 int value = atoi(buffer);
38                                 double convert = (value/4096) * 1800;   
39                 d->value = convert;
40                                 lseek(sensor, 0, 0);    
41             }
42                         else {
43                                 perror("Failed to get value from sensor");
44                         }
45                         close(sensor);
46                         break;
47                         break;
48                 }
49                 //TODO: think about a better way to address individual pins
50                 //              i.e. pass an int to a separate function that builds the correct filename
51                 //              doesn't really matter if the pins we're using are fixed though
52                 case DIGITAL_TEST0:
53                         int fd = open("/sys/class/gpio/gpio17/value", O_RDONLY)         //again need the right addresses for each pin
54                         char ch;                                                                                                        //just one character for binary info
55                         lseek(fd, 0, SEEK_SET);
56                         int read = read(fd, &ch, sizeof(ch);
57                         if (read != -1) {
58                                 if (ch != '0') {
59                                         d->value = 1;
60                                 }
61                                 else {
62                                         d->value = 0;
63                                 }
64                         }
65                         else {
66                                 perror("Failed to get value from pin"); 
67                         }
68                         break;
69                 case DIGITAL_TEST1:
70                         int fd = open("/sys/class/gpio/gpio23/value", O_RDONLY)
71                         char ch;
72                         lseek(fd, 0, SEEK_SET);
73                         int read = read(fd, &ch, sizeof(ch);
74                         if (read != -1) {
75                                 if (ch != '0') {
76                                         d->value = 1;
77                                 }
78                                 else {
79                                         d->value = 0;
80                                 }
81                         }
82                         else {
83                                 perror("Failed to get value from pin"); 
84                         }
85                         break;
86                 default:
87                         Fatal("Unknown sensor id: %d", sensor_id);
88                         break;
89         }       
90         usleep(100000); // simulate delay in sensor polling
91
92         return d;
93 }

UCC git Repository :: git.ucc.asn.au