0a973bacff08fea2450ffa5e95ff4ffbc9058988
[matches/MCTX3420.git] / BBB code / old / simple code examples.c
1 //Code to blink an LED - just to illustrate that it's pretty easy
2 //Only important thing is which name to address the LED by
3
4 #include <stdio.h>
5 #include <unistd.h>
6  
7 using namespace std;
8  
9 int main(int argc, char** argv) {
10   FILE *LEDHandle = NULL;
11   char *LEDBrightness = "/sys/class/leds/beaglebone:green:usr0/brightness";
12   printf("\nStarting LED blink program wooo!\n");
13   while(1){
14     if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
15       fwrite("1", sizeof(char), 1, LEDHandle);
16       fclose(LEDHandle);
17     }
18     sleep(1);
19     if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
20       fwrite("0", sizeof(char), 1, LEDHandle);
21       fclose(LEDHandle);
22     }
23     sleep(1);
24   }
25   return 0;
26 }
27
28 //Sample code that should read a pressure sensor pin (conversion factors
29 //are just random numbers). Again pretty simple.
30
31 #include STUFF
32  
33 double pressure(char *string) {
34         int value = atoi(string);
35         double millivolts = (value / 4096.0) * 1800; //convert input to volts
36         double pressure = (millivolts - 500.0) / 10.0; //convert volts to pressure
37         return pressure;
38 }
39  
40 void main() {
41         int fd = open("/sys/devices/platform/tsc/ain2", O_RDONLY); //open pin signal
42         while (1) {
43                 char buffer[1024];
44                 int ret = read(fd, buffer, sizeof(buffer)); //get data
45                 if (ret != -1) {
46                         buffer[ret] = NULL;
47                         double kpa = pressure(buffer);
48                         printf("digital value: %s  kilopascals: %f\n", buffer, kpa);
49                         lseek(fd, 0, 0);
50                 }
51                 sleep(1);
52         }
53         close(fd);
54 }

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