Skip to content

Commit

Permalink
3state toggle (swipe, swipetouch, disable)
Browse files Browse the repository at this point in the history
swipe enabled by default
calculate swipe velocity (ignore slow swipes)
  • Loading branch information
ddvk committed Nov 8, 2019
1 parent 847192c commit d269e3e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 28 deletions.
5 changes: 3 additions & 2 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
#define WACOM "/dev/input/event0"

#define MAX_SLOTS 7 //max touch points to track
#define TWOTAP_DISTANCE 700 //distance between the fingers to enable disable
#define JITTER 20 //finger displacement to be consideded a swipe
#define TWOTAP_DISTANCE 900 //distance between the fingers to enable disable
#define JITTER 10 //finger displacement to be consideded a swipe
#define SWIPE_VELOCITY 4 //swipe speed threshold
5 changes: 4 additions & 1 deletion eventreader.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
Expand Down Expand Up @@ -91,8 +92,10 @@ void process_touch(void(*process)(struct TouchEvent *)){
f = &fingers[i];
if (f->status) {

struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
struct TouchEvent event;
event.time = evt.time.tv_sec;
event.time = tv.tv_sec*1000+ tv.tv_nsec /1000000;
event.slot = i; //enumerating slots
event.x = f->x;
event.y = f->y;
Expand Down
2 changes: 1 addition & 1 deletion eventreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct TouchEvent{
struct Point position;
struct Point raw_position;

long time;
unsigned long time;
enum FingerStatus status;
};

Expand Down
22 changes: 15 additions & 7 deletions gestures.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void recognize_gestures(struct TouchEvent *f) {
if(keys_down < 0) keys_down = 0; //todo: fixit
segments[slot].start.x = f->x;
segments[slot].start.y = f->y;
segments[slot].start.time = f->time;

keys_down++;
segment_count++;
Expand All @@ -39,31 +40,35 @@ void recognize_gestures(struct TouchEvent *f) {
keys_down--;
segments[slot].end.x = x;
segments[slot].end.y = y;
segments[slot].end.time = f->time;

//todo: extract gesture recognition

if(keys_down == 0){
struct Gesture gesture;
struct Segment *p = segments;
int dx,dy,distance=0;
unsigned long dt;
switch(segment_count){

//single tap
case 1:
printf("Tap x:%d, y:%d raw_x:%d, raw_y:%d\n", f->x, f->y, f->raw_position.x, f->raw_position.y);
dx = p->end.x - p->start.x;
dy = p->end.y - p->start.y;
dt = p->end.time - p->start.time;
printf("delta dt %lu\n", dt);
distance = (int)sqrt(dx*dx+dy*dy);

if (abs(dx) < JITTER &&
abs(dy) < JITTER){
if (distance < JITTER) {

int nav_stripe = SCREEN_WIDTH /3;
if (y > 100){//disable upper stripe
if (y > 100 && x > 100){//disable upper stripe and left menus
if (x < nav_stripe) {
if (y > SCREEN_HEIGHT - 150 && x < 150) {
printf("TOC\n");
}
else {

gesture.type = TapLeft;
interpret_gesture(&gesture);
}
Expand All @@ -75,14 +80,17 @@ void recognize_gestures(struct TouchEvent *f) {
}
}
else {
unsigned int velo = (10*distance) / (dt == 0 ? 1 : dt);
printf("velocity %d\n", velo);
if (velo < SWIPE_VELOCITY) //ignore slow swipes
break;
//swipe
if (abs(dx) > abs(dy)) {
//horizontal
if (dx < 0) {
printf("swipe left\n");
//todo: output gestures, extract executer
gesture.type = SwipeLeft;
interpret_gesture(&gesture);
gesture.type = SwipeLeft;
interpret_gesture(&gesture);
}
else {
printf("swipe right\n");
Expand Down
36 changes: 21 additions & 15 deletions keyinjector.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "config.h"

enum Key {Left=105, Right=106, Home=102,Power=116};

static bool touch_enabled = false;
enum TouchStatus {SwipeOnly, SwipeAndTouch,Disabled};
static enum TouchStatus touch_status = SwipeOnly;
int f;
int w;
void injector_init() {
Expand All @@ -37,18 +37,18 @@ void move_pen(int x, int y, long time);

void interpret_gesture(struct Gesture *g){
if (g->type == TwoTapWide) {
if (touch_enabled){
show("touch navigation disabled");
if (verbose)
printf("disabling\n");
touch_enabled = false;
}
else {
show("touch navigation enabled");
if(verbose)
printf("enabling\n");
touch_enabled = true;
}
touch_status = ((int)touch_status + 1) % 3; //cycle the states
switch(touch_status) {
case SwipeOnly:
show("swipe only");
break;
case Disabled:
show("touch disabled");
break;
case SwipeAndTouch:
show("swipe and touch");
break;
}
return;
}

Expand All @@ -69,16 +69,22 @@ void interpret_gesture(struct Gesture *g){

}

if (!touch_enabled)
if (touch_status == Disabled)
return;

//require touch enabled
switch(g->type){
case TapLeft:
if(touch_status != SwipeAndTouch)
break;
// fall through
case SwipeRight:
press_button(Left);
break;
case TapRight:
if(touch_status != SwipeAndTouch)
break;
// fall through
case SwipeLeft:
press_button(Right);
break;
Expand Down
1 change: 1 addition & 0 deletions point.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
struct Point {
int x,y;
int time;
};
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ fi

make || exit 1
ssh $DEVICE killall touchinjector
arm-poky-linux-gnueabi-strip bin/touchinjector
arm-oe-linux-gnueabi-strip bin/touchinjector
scp bin/touchinjector $DEVICE:~/ &&
ssh -tt $DEVICE '/home/root/touchinjector'
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define VERSION "0.0.2-19-g55385c9"
#define VERSION "0.0.3-9-g847192c"

0 comments on commit d269e3e

Please sign in to comment.