-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotentiometer.c
59 lines (51 loc) · 1.3 KB
/
Potentiometer.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
/*
* Potentiometer.c
*
* Created on: Oct 4, 2017
* Author: jamie
*/
#include "Potentiometer.h"
#include <stdio.h>
#include <stdlib.h>
#define ENABLE_A2D_FILE "/sys/devices/platform/bone_capemgr/slots"
#define ENABLE_A2D_VALUE "BB-ADC"
#define POTENTIOMETER_VALUE "/sys/bus/iio/devices/iio:device0/in_voltage0_raw"
static void enableA2D();
static FILE* openFile(const char* fileName, const char* permissions);
void Potentiometer_init(void){
enableA2D();
FILE* potFile = NULL;
while(potFile == NULL){
potFile = fopen((POTENTIOMETER_VALUE), "r");
}
fclose(potFile);
return;
}
void Potentiometer_cleanup(void){
return;
}
int Potentiometer_getValue(void){
FILE* potFile= openFile((POTENTIOMETER_VALUE), "r");
int potValue = 0;
int readSuccess = fscanf(potFile, "%d", &potValue);
if(readSuccess < 1){
printf("ERROR: Unable to read potentiometer value.\n");
exit(-1);
}
fclose(potFile);
return potValue;
}
static void enableA2D(){
FILE* A2DFile = openFile((ENABLE_A2D_FILE), "w");
fprintf(A2DFile, "%s", (ENABLE_A2D_VALUE));
fclose(A2DFile);
return;
}
static FILE* openFile(const char* fileName, const char* permissions){
FILE* filePointer = fopen(fileName, permissions);
if (filePointer == NULL){
printf("ERROR: Unable to open slots or pot file.\n");
exit(-1);
}
return filePointer;
}