diff options
Diffstat (limited to 'recipes-demo/de10-nano-adxl-apps/files/tap_detect.c')
-rw-r--r-- | recipes-demo/de10-nano-adxl-apps/files/tap_detect.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/recipes-demo/de10-nano-adxl-apps/files/tap_detect.c b/recipes-demo/de10-nano-adxl-apps/files/tap_detect.c new file mode 100644 index 0000000..bbd19d1 --- /dev/null +++ b/recipes-demo/de10-nano-adxl-apps/files/tap_detect.c @@ -0,0 +1,103 @@ +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <error.h> +#include <errno.h> +#include <limits.h> +#include <unistd.h> +#include <string.h> +#include "linux/input.h" + +#define INPUT_DEV_NODE "/dev/input/by-path/platform-ffc04000.i2c-event" +#define SYSFS_DEVICE_DIR "/sys/devices/platform/soc/ffc04000.i2c/i2c-0/0-0053/" + +#define EV_CODE_X (0) +#define EV_CODE_Y (1) +#define EV_CODE_Z (2) + +#define LOOP_COUNT (100) + +void write_sysfs_cntl_file(const char *dir_name, const char *file_name, + const char *write_str) { + + char path[PATH_MAX]; + int path_length; + int file_fd; + int result; + + // create the path to the file we need to open + path_length = snprintf(path, PATH_MAX, "%s/%s", dir_name, file_name); + if(path_length < 0) + error(1, 0, "path output error"); + if(path_length >= PATH_MAX) + error(1, 0, "path length overflow"); + + // open the file + file_fd = open(path, O_WRONLY | O_SYNC); + if(file_fd < 0) + error(1, errno, "could not open file '%s'", path); + + // write the string to the file + result = write(file_fd, write_str, strlen(write_str)); + if(result < 0) + error(1, errno, "writing to '%s'", path); + if((size_t)(result) != strlen(write_str)) + error(1, errno, "buffer underflow writing '%s'", path); + + // close the file + result = close(file_fd); + if(result < 0) + error(1, errno, "could not close file '%s'", path); +} + +int main(void) { + int event_dev_fd; + const char *input_dev_node = INPUT_DEV_NODE; + int result; + struct input_event the_event; + + // enable adxl + write_sysfs_cntl_file(SYSFS_DEVICE_DIR, "disable", "0"); + + // set the sample rate to maximum + write_sysfs_cntl_file(SYSFS_DEVICE_DIR, "rate", "15"); + + // do not auto sleep + write_sysfs_cntl_file(SYSFS_DEVICE_DIR, "autosleep", "0"); + + // open the event device node + event_dev_fd = open(input_dev_node, O_RDONLY | O_SYNC); + if(event_dev_fd < 0) + error(1, errno, "could not open file '%s'", input_dev_node); + + printf("Please tap the DE10 Nano board.\n"); + + while(1) { + // read the next event + result = read(event_dev_fd, &the_event, + sizeof(struct input_event)); + if(result < 0) + error(1, errno, "reading %d from '%s'", + sizeof(struct input_event), + input_dev_node); + if(result != sizeof(struct input_event)) + error(1, 0, "did not read %d bytes from '%s'", + sizeof(struct input_event), + input_dev_node); + + if((the_event.type == EV_KEY) && (the_event.code == BTN_TOUCH)) + break; + } + + printf("Tap detected.\n"); + + // close the device node + result = close(event_dev_fd); + if(result < 0) + error(1, errno, "could not close file '%s'", input_dev_node); + + // disable adxl + write_sysfs_cntl_file(SYSFS_DEVICE_DIR, "disable", "1"); +} + |