/
miniProj2.c
70 lines (56 loc) · 1.41 KB
/
miniProj2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Stephen Shinn
* 9/25/12
* MiniProject 2: Force Sensitive Resistor example
Input: Force-sensitive resistor on Pin 36 (analog input)
Output: LED on Pin 12 (GPIO)
Result: LED lights-up when a strong force is placed on the resistor.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SYSFS_GPIO_DIR "/sys/class/gpio"
// Read analog value and print to console
int readAnalog() {
FILE* file = fopen("/sys/devices/platform/omap/tsc/ain6", "r");
int num = 0;
fscanf (file, "%d", &num);
fclose (file);
printf("\rAnalog num: %d ", num);
fflush(stdout);
return num;
}
// Light the LED if the analog value is low (ie, strong force)
void lightLedIfStrong(analogValue) {
FILE *file;
file = fopen("/sys/class/gpio/gpio60/value","w+");
if (analogValue < 100) {
fprintf(file, "1");
} else {
fprintf(file, "0");
}
fclose(file);
}
// Main
void main(int argc, char **argv, char **envp) {
// Export gpio60
FILE *fp;
char set_value[5];
fp = fopen(SYSFS_GPIO_DIR "/export", "ab");
rewind(fp);
strcpy(set_value, "60");
fwrite(&set_value, sizeof(char), 2, fp);
fclose(fp);
// Set gpio60 direction
FILE *file;
file = fopen("/sys/class/gpio/gpio60/direction","w+");
fprintf(file,"%s","out");
fclose(file);
// Keep going; CTRL+C to exit
while (1) {
int analogValue = readAnalog();
// Do anything based on analog value
lightLedIfStrong(analogValue);
sleep(0.5);
}
}