1 DataPoint * GetData(int sensor_id, DataPoint * d)
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);
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
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);
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'
23 lseek(sensor, 0, 0); //after read string must be rewound to start of file using lseek
26 perror("Failed to get value from sensor");
32 int sensor = open("/sys/devices/platform/tsc/ain1", O_RDONLY);
34 int read = read(sensor, buffer, sizeof(buffer);
37 int value = atoi(buffer);
38 double convert = (value/4096) * 1800;
43 perror("Failed to get value from sensor");
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
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);
66 perror("Failed to get value from pin");
70 int fd = open("/sys/class/gpio/gpio23/value", O_RDONLY)
72 lseek(fd, 0, SEEK_SET);
73 int read = read(fd, &ch, sizeof(ch);
83 perror("Failed to get value from pin");
87 Fatal("Unknown sensor id: %d", sensor_id);
90 usleep(100000); // simulate delay in sensor polling