1 char adc_dir = "/sys/devices/platform/tsc/ain";
2 char pin_dir = "/sys/class/gpio/gpio";
4 /** Open an ADC and return the voltage value from it
5 * @param adc_num - ADC number, ranges from 0 to 7 on the Beaglebone
6 @return the converted voltage value if successful
9 //TODO: create a function to lookup the ADC or pin number instead of manually
10 // specifying it here (so we can keep all the numbers in one place)
12 int OpenAnalog(int adc_num)
15 snprintf(adc_path, sizeof(adc_path), "%s%d", adc_dir, adc_num); //construct ADC path
16 int sensor = open(adc_path, O_RDONLY);
17 char buffer[128]; //I think ADCs are only 12 bits (0-4096)
18 int read = read(sensor, buffer, sizeof(buffer); //buffer can probably be smaller
21 int value = atoi(buffer);
22 double convert = (value/4096) * 1000; //random conversion factor, will be different for each sensor
23 //lseek(sensor, 0, 0);
28 perror("Failed to get value from sensor");
34 /** Open a digital pin and return the value from it
35 * @param pin_num - pin number, specified by electronics team
36 @return 1 or 0 if reading was successful
39 int OpenDigital(int pin_num)
42 snprintf(pin_path, sizeof(pin_path), "%s%d%s", pin_dir, pin_num, "/value"); //construct pin path
43 int pin = open(pin_path, O_RDONLY);
45 lseek(fd, 0, SEEK_SET);
46 int read = read(pin, &ch, sizeof(ch);
57 perror("Failed to get value from pin");
64 * Read a DataPoint from a Sensor; block until value is read
65 * @param id - The ID of the sensor
66 * @param d - DataPoint to set
67 * @returns True if the DataPoint was different from the most recently recorded.
69 bool Sensor_Read(Sensor * s, DataPoint * d)
74 gettimeofday(&t, NULL);
75 d->time_stamp = TIMEVAL_DIFF(t, g_options.start_time);
77 // Read value based on Sensor Id
81 d->value = OpenAnalog(0); //ADC #0 on the Beaglebone
85 d->value = OpenAnalog(1);
89 d->value = (double)(rand() % 6) * -( rand() % 2) / ( rand() % 100 + 1);
90 //Gives a value between -5 and 5
93 d->value = openDigital(0); //replace 0 with correct pin number
96 d->value = openDigital(1); //replace 1 with correct pin number
99 if( rand() % 100 > 98)
101 d->value = rand() % 2;
102 //Gives 0 or 1 or a 2 every 1/100 times
105 Fatal("Unknown sensor id: %d", s->id);
108 usleep(100000); // simulate delay in sensor polling
110 // Perform sanity check based on Sensor's ID and the DataPoint
111 Sensor_CheckData(s->id, d->value);
113 // Update latest DataPoint if necessary
114 bool result = (d->value != s->newest_data.value);
117 s->newest_data.time_stamp = d->time_stamp;
118 s->newest_data.value = d->value;