Compare commits
16 Commits
unfinished
...
V2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f29dc80ce | ||
|
|
60a9c97758 | ||
|
|
5974a3a1d5 | ||
|
|
c265e4c1fb | ||
|
|
8b0c083d5d | ||
|
|
8281c38db8 | ||
|
|
fc25e2f506 | ||
|
|
71c04f77f3 | ||
|
|
bcad9de814 | ||
|
|
a2baf654b6 | ||
|
|
b15d09216e | ||
|
|
f230a08868 | ||
|
|
414c1a95ac | ||
|
|
966611815f | ||
|
|
398a259050 | ||
|
|
92b78db992 |
@@ -1,2 +1,2 @@
|
|||||||
# resy-ss17-template
|
# resy-ss17-grp1
|
||||||
Template for grp repositories
|
Template for grp repositories
|
||||||
|
|||||||
120
V1/switch.c
Normal file
120
V1/switch.c
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* switch.c
|
||||||
|
* How to register an button and count this
|
||||||
|
* author: Simon Wörner, Manuel Vögele,
|
||||||
|
* Siegfried Kienzle
|
||||||
|
* 24-March-2017
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
|
||||||
|
#define BUTTON "17"
|
||||||
|
#define IN "in\n"
|
||||||
|
#define OUT "out\n"
|
||||||
|
#define HIGH "1\n"
|
||||||
|
#define LOW "0\n"
|
||||||
|
#define MAXBYTES 1
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
int writeIntoFile(char* value, char* path)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char buffer[3];
|
||||||
|
strcpy(buffer, value);
|
||||||
|
if((fd=open(path, O_RDWR | O_CREAT | O_TRUNC)) == -1) {
|
||||||
|
perror(NULL);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((write(fd, &buffer, sizeof(buffer))) == -1) {
|
||||||
|
perror("Cannot write into file");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
printf("Success");
|
||||||
|
close(fd);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int reserve(char* gpioPort)
|
||||||
|
{
|
||||||
|
char str[3];
|
||||||
|
strcpy(str, gpioPort);
|
||||||
|
strcat(str, "\n");
|
||||||
|
return writeIntoFile(str,"/sys/class/gpio/export");
|
||||||
|
}
|
||||||
|
|
||||||
|
int setDirection(char* gpioPort, char* inOut)
|
||||||
|
{
|
||||||
|
char *direc = "/direction";
|
||||||
|
char *gpioPath = "/sys/class/gpio/gpio";
|
||||||
|
char path[32];
|
||||||
|
strcpy(path, gpioPath);
|
||||||
|
strcat(path, gpioPort);
|
||||||
|
strcat(path, direc);
|
||||||
|
return writeIntoFile(inOut, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
int setOutput(char* gpioPort, char* volt)
|
||||||
|
{
|
||||||
|
char *val = "/value";
|
||||||
|
char *gpioPath = "/sys/class/gpio/gpio";
|
||||||
|
char path[28];
|
||||||
|
strcpy(path, gpioPath);
|
||||||
|
strcat(path, gpioPort);
|
||||||
|
strcat(path, val);
|
||||||
|
return writeIntoFile(volt, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mainLoop(char* gpioPort)
|
||||||
|
{
|
||||||
|
int fid, portValue, count, countButton;
|
||||||
|
char *val = "/value";
|
||||||
|
char *gpioPath = "/sys/class/gpio/gpio";
|
||||||
|
char path[28];
|
||||||
|
strcpy(path, gpioPath);
|
||||||
|
strcat(path, gpioPort);
|
||||||
|
strcat(path, val);
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
if( (fid=open(path, O_RDONLY)) == -1)
|
||||||
|
perror("Fehler ");
|
||||||
|
if(readInput(fid) == 1)
|
||||||
|
{
|
||||||
|
countButton++;
|
||||||
|
printf("%d", countButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
close(fid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char[] readInput(int fid)
|
||||||
|
{
|
||||||
|
char buffer[MAXBYTES];
|
||||||
|
read(fid, &buffer, MAXBYTES);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int unexport(char* gpioport)
|
||||||
|
{
|
||||||
|
char str[3];
|
||||||
|
strcpy(str, gpioPort);
|
||||||
|
strcat(str, "\n");
|
||||||
|
return writeIntoFile(str,"/sys/class/gpio/unexport");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
reserve(BUTTON);
|
||||||
|
setDirection(BUTTON, IN);
|
||||||
|
mainLoop(BUTTON);
|
||||||
|
unexport(BUTTON);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
31
V2/diff.c
Normal file
31
V2/diff.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<time.h>
|
||||||
|
#include<pthread.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
struct timespec time1, start, ende;
|
||||||
|
time1.tv_sec = 5;
|
||||||
|
time1.tv_nsec = 0;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
clock_nanosleep(CLOCK_MONOTONIC, 0, &time1, NULL);
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ende);
|
||||||
|
|
||||||
|
int nsec_difference = (int) ((ende.tv_nsec - start.tv_nsec)/1000);
|
||||||
|
|
||||||
|
|
||||||
|
//microseconds
|
||||||
|
|
||||||
|
int sec_difference = (int) (ende.tv_sec - start.tv_sec)*1000*1000;
|
||||||
|
int real_time_slept = sec_difference + nsec_difference;
|
||||||
|
|
||||||
|
int difference_real_set = real_time_slept - time1.tv_sec * 1000 * 1000;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
printf("Real time slept: %d\n", real_time_slept);
|
||||||
|
printf("Difference Real time slept and actually sleep time: %d usec\n", difference_real_set);
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user