project for a (works on my devices) demo
This commit is contained in:
303
sources/input/input_events.c
Normal file
303
sources/input/input_events.c
Normal file
@@ -0,0 +1,303 @@
|
||||
#include "../../headers/input/input_events.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
/* capturing all the keys known by linux */
|
||||
unsigned char buffered_keys[KEY_CNT];
|
||||
|
||||
/*
|
||||
glut exposes
|
||||
keypress
|
||||
until a key is pressed, will keep invoking the keypress callback
|
||||
mousemoved
|
||||
absolute x and y of the mouse
|
||||
*/
|
||||
|
||||
void dispatch_input_event( device_event event){
|
||||
switch( event.type){
|
||||
case EV_SYN:
|
||||
//printf("separator for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_KEY:
|
||||
dispatch_input_key_event( event);
|
||||
break;
|
||||
case EV_REL:
|
||||
//printf("delta event %d : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_ABS:
|
||||
dispatch_input_absolute_event( event);
|
||||
break;
|
||||
case EV_MSC:
|
||||
//printf("miscellaneous event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_SW:
|
||||
//printf("binary switch event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_LED:
|
||||
//printf("led toggle event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_SND:
|
||||
//printf("sound event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_REP:
|
||||
//printf("autorepeating event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_FF:
|
||||
//printf("force-feedback event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_PWR:
|
||||
//printf("power toggle event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
case EV_FF_STATUS:
|
||||
//printf("force-feedback status event for %u : %d\n", event.code, event.value);
|
||||
break;
|
||||
default:
|
||||
printf("unknown event for %u : %d\n", event.code, event.value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dispatch_input_key_event( device_event event){
|
||||
/* goes from KEY_RESERVED = 0 to KEY_MAX = 0x2ff */
|
||||
|
||||
printf("this is dispatch_input_key_event\n");
|
||||
|
||||
/* common keyboard codes from 0 up to KEY_MACRO, before KEY_MUTE */
|
||||
if( event.code < KEY_MUTE){
|
||||
dispatch_keyboard_keys( event);
|
||||
}
|
||||
/* INTERESTING KEYS BEGIN */
|
||||
/* interesting keys from KEY_MIN_INTERESTING = KEY_MUTE up to BTN_DEAD, before BTN_GAMEPAD */
|
||||
else if( event.code >= KEY_MUTE && event.code <= BTN_DEAD){
|
||||
dispatch_interesting_keys_1( event);
|
||||
}
|
||||
/* NO GAP */
|
||||
/* joypad keys, from BTN_GAMEPAD up to BTN_THUMBR, gap of one before BTN_DIGI */
|
||||
else if( event.code >= BTN_GAMEPAD && event.code <= BTN_THUMBR){
|
||||
dispatch_interesting_keys_2( event);
|
||||
}
|
||||
/* GAP OF ONE between this and the previous.. */
|
||||
/* ignored keys */
|
||||
/* interesting keys, from BTN_DIGI up to the MAX */
|
||||
else if( event.code >= BTN_DIGI && event.code <= KEY_MAX){
|
||||
dispatch_interesting_keys_3( event);
|
||||
}
|
||||
else{
|
||||
printf("UNKNOWN KEY EVENT CODE %d with value %d\n", event.code, event.value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dispatch_input_absolute_event( device_event event){
|
||||
if(event.code < ABS_RESERVED){
|
||||
dispatch_regular_absolute_event( event);
|
||||
}
|
||||
/* a gap for ABS_RESERVED */
|
||||
else if( event.code >= ABS_MT_SLOT && event.code <= ABS_MT_TOOL_Y){
|
||||
dispatch_multitouch_event( event);
|
||||
}
|
||||
else{
|
||||
printf("UNKNOWN ABSOLUTE EVENT CODE %d with value %d\n", event.code, event.value);
|
||||
}
|
||||
}
|
||||
|
||||
void dispatch_regular_absolute_event( device_event event){
|
||||
switch( event.code){
|
||||
case ABS_X:
|
||||
//cursor_x = event.value;
|
||||
//printf("WARN !!! ABS_X untracked %d\n", event.value);
|
||||
break;
|
||||
case ABS_Y:
|
||||
//cursor_y = event.value;
|
||||
//printf("WARN !!! ABS_Y untracked %d\n", event.value);
|
||||
break;
|
||||
default:
|
||||
printf("UNKNOWN REGULAR ABSOLUTE EVENT CODE %d with value %d\n", event.code, event.value);
|
||||
}
|
||||
}
|
||||
|
||||
void dispatch_multitouch_event( device_event event){
|
||||
switch( event.code){
|
||||
case ABS_MT_SLOT: /* MT slot being modified */
|
||||
//printf("ABS_MT_SLOT [MULTITOUCH SLOT MODIFIED] %d\n", event.value);
|
||||
TC_L1_builder_set_slot_number( event.value);
|
||||
break;
|
||||
case ABS_MT_TOUCH_MAJOR: /* Major axis of touching ellipse */
|
||||
// printf("ABS_MT_TOUCH_MAJOR [MAJOR AXIS OF TOUCH ELLIPSE] %d\n", event.value);
|
||||
TC_L1_builder_set_major_axis( event.value);
|
||||
break;
|
||||
case ABS_MT_TOUCH_MINOR: /* Minor axis (omit if circular) */
|
||||
// printf("ABS_MT_TOUCH_MINOR [MINOR AXIS OF TOUCH ELLIPSE] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_WIDTH_MAJOR: /* Major axis of approaching ellipse */
|
||||
// printf("ABS_MT_WIDTH_MAJOR [MAJOR AXIS OF APPROACHING ELLIPSE] %d\n", event.value);
|
||||
TC_L1_builder_set_approaching_major_axis( event.value);
|
||||
break;
|
||||
case ABS_MT_WIDTH_MINOR: /* Minor axis (omit if circular) */
|
||||
// printf("ABS_MT_WIDTH_MINOR [MINOR AXIS OF APPROACHING ELLIPSE] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_ORIENTATION: /* Ellipse orientation */
|
||||
// printf("ABS_MT_ORIENTATION [ELLIPSE ORIENTATION] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_POSITION_X: /* Center X touch position */
|
||||
// printf("ABS_MT_POSITION_X [CENTER X TOUCH POSITION] %d\n", event.value);
|
||||
TC_L1_builder_set_position_x( event.value);
|
||||
break;
|
||||
case ABS_MT_POSITION_Y: /* Center Y touch position */
|
||||
//printf("ABS_MT_POSITION_Y [CENTER Y TOUCH POSITION] %d\n", event.value);
|
||||
TC_L1_builder_set_position_y( event.value);
|
||||
break;
|
||||
case ABS_MT_TOOL_TYPE: /* Type of touching device */
|
||||
// printf("ABS_MT_TOOL_TYPE [TYPE OF TOUCHING DEVICE] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_BLOB_ID: /* Group a set of packets as a blob */
|
||||
// printf("ABS_MT_BLOB_ID [GROUP A SET OF PACKETS AS A BLOB] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_TRACKING_ID: /* Unique ID of initiated contact */
|
||||
//printf("ABS_MT_TRACKING_ID [UNIQUE ID OF INITIATED CONTACT] %d\n", event.value);
|
||||
TC_L1_builder_set_tracking_id( event.value);
|
||||
break;
|
||||
case ABS_MT_PRESSURE: /* Pressure on contact area */
|
||||
// printf("ABS_MT_PRESSURE [PRESSURE ON CONTACT AREA] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_DISTANCE: /* Contact hover distance */
|
||||
// printf("ABS_MT_DISTANCE [CONTACT HOVER DISTANCE] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_TOOL_X: /* Center X tool position */
|
||||
// printf("ABS_MT_TOOL_X [CENTER X TOOL POSITION] %d\n", event.value);
|
||||
break;
|
||||
case ABS_MT_TOOL_Y: /* Center Y tool position */
|
||||
// printf("ABS_MT_TOOL_Y [CENTER Y TOOL POSITION] %d\n", event.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* common keyboard codes from 0 up to KEY_MACRO, before KEY_MUTE */
|
||||
void dispatch_keyboard_keys( device_event event){
|
||||
printf("IGNORED KEY EVENT CODE %d with value %d [KEYBOARD GROUP]\n", event.code, event.value);
|
||||
}
|
||||
|
||||
/* interesting keys from KEY_MIN_INTERESTING = KEY_MUTE up to BTN_DEAD, before BTN_GAMEPAD */
|
||||
void dispatch_interesting_keys_1( device_event event){
|
||||
printf("UNKNOWN KEY EVENT CODE %d with value %d [INTERESTING GROUP]\n", event.code, event.value);
|
||||
}
|
||||
|
||||
/* joypad keys, from BTN_GAMEPAD up to BTN_THUMBR, gap of one before BTN_DIGI */
|
||||
void dispatch_interesting_keys_2( device_event event){
|
||||
|
||||
/* NO OFFSET NEEDED, THEY START FROM 0 */
|
||||
|
||||
/* could also store the transitions
|
||||
unsigned char buffered_keys_transition[KEY_CNT];
|
||||
*/
|
||||
printf("interesting key [JOYPAD] %x %x\n", event.code, event.value);
|
||||
buffered_keys[event.code] = (char) event.value; /* they only have 3 states : press, released, retriggered */
|
||||
}
|
||||
|
||||
|
||||
/* interesting keys, from BTN_DIGI up to the MAX */
|
||||
void dispatch_interesting_keys_3( device_event event){
|
||||
if( event.code == BTN_TOUCH){
|
||||
printf("TOUCH EVENT %s\n", (event.value) ? "BEGIN" : "END");
|
||||
}
|
||||
else{
|
||||
printf("UNKNOWN KEY EVENT CODE %d with value %d [INTERESTING GROUP]\n", event.code, event.value);
|
||||
buffered_keys[event.code] = (char) event.value; /* they only have 3 states : press, released, retriggered */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* reserving the NULL character code for unsupported keys
|
||||
maps from linux input.h key codes to ascii
|
||||
*/
|
||||
/*
|
||||
UPPERCASE REQUIRES QUERYING THE STATE OF THE SHIFT KEY
|
||||
*/
|
||||
char get_ascii_code( unsigned short linux_code){
|
||||
/* yanderedev would be proud of me... :'( */
|
||||
switch( linux_code){
|
||||
case KEY_DELETE:
|
||||
return 0x18; /* refer to ascii CANCEL (non printable) instead of DELETE (printable)*/
|
||||
case KEY_ESC:
|
||||
return 0x1b;
|
||||
/* KEY_1 is 0x2 KEY_9 is 0xA KEY_0 is 0xB */
|
||||
/* ASCII 0 is 0x30 ASCII 9 is 0x39 */
|
||||
case KEY_1:
|
||||
case KEY_2:
|
||||
case KEY_3:
|
||||
case KEY_4:
|
||||
case KEY_5:
|
||||
case KEY_6:
|
||||
case KEY_7:
|
||||
case KEY_8:
|
||||
case KEY_9:
|
||||
/* ASCII 1 - (lin_1 - lin_code) */
|
||||
return ( 0x31 + ( 0x2 - linux_code));
|
||||
case KEY_0:
|
||||
return 0x30;
|
||||
|
||||
case KEY_BACKSPACE:
|
||||
return 0x8;
|
||||
case KEY_TAB:
|
||||
return 0x9;
|
||||
case KEY_SPACE:
|
||||
return 0x20;
|
||||
|
||||
case KEY_A:
|
||||
return 0x41;
|
||||
case KEY_B:
|
||||
return 0x42;
|
||||
case KEY_C:
|
||||
return 0x43;
|
||||
case KEY_D:
|
||||
return 0x44;
|
||||
case KEY_E:
|
||||
return 0x45;
|
||||
case KEY_F:
|
||||
return 0x46;
|
||||
case KEY_G:
|
||||
return 0x47;
|
||||
case KEY_H:
|
||||
return 0x48;
|
||||
case KEY_I:
|
||||
return 0x49;
|
||||
case KEY_J:
|
||||
return 0x4a;
|
||||
case KEY_K:
|
||||
return 0x4b;
|
||||
case KEY_L:
|
||||
return 0x4c;
|
||||
case KEY_M:
|
||||
return 0x4d;
|
||||
case KEY_N:
|
||||
return 0x4e;
|
||||
case KEY_O:
|
||||
return 0x4f;
|
||||
case KEY_P:
|
||||
return 0x50;
|
||||
case KEY_Q:
|
||||
return 0x51;
|
||||
case KEY_R:
|
||||
return 0x52;
|
||||
case KEY_S:
|
||||
return 0x53;
|
||||
case KEY_T:
|
||||
return 0x54;
|
||||
case KEY_U:
|
||||
return 0x55;
|
||||
case KEY_V:
|
||||
return 0x56;
|
||||
case KEY_W:
|
||||
return 0x57;
|
||||
case KEY_X:
|
||||
return 0x58;
|
||||
case KEY_Y:
|
||||
return 0x59;
|
||||
case KEY_Z:
|
||||
return 0x5a;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
162
sources/input/touch/touch_controller_L1_builder.c
Normal file
162
sources/input/touch/touch_controller_L1_builder.c
Normal file
@@ -0,0 +1,162 @@
|
||||
#include "../../../headers/input/touch/touch_controller_L1_builder.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* otherwise
|
||||
error: variably modified ‘tracked’ at file scope
|
||||
even if tracked_size gets declared as const
|
||||
*/
|
||||
#define tracked_size 255
|
||||
/* could hold 256 using tracked_size + 1 but don't want to mess the copy tracked loop */
|
||||
static touch_event *tracked[ tracked_size] = { NULL };
|
||||
static uint8_t tracked_count = 0;
|
||||
|
||||
static touch_event *last_event = NULL;
|
||||
static touch_event *current_event = NULL;
|
||||
|
||||
|
||||
touch_event* tracked_find( uint64_t slot_number){
|
||||
return tracked[slot_number];
|
||||
}
|
||||
|
||||
void tracked_remove( touch_event *toremove){
|
||||
tracked[toremove->slot_number] = NULL;
|
||||
tracked_count--;
|
||||
}
|
||||
|
||||
void tracked_add( touch_event *toadd){
|
||||
tracked[toadd->slot_number] = toadd;
|
||||
tracked_count++;
|
||||
}
|
||||
|
||||
uint8_t tracked_get_first_free_slot_number(){
|
||||
int first_free_slot_number = 0;
|
||||
while(
|
||||
tracked[first_free_slot_number]
|
||||
&&
|
||||
! ( tracked_size == first_free_slot_number)
|
||||
){
|
||||
first_free_slot_number++;
|
||||
}
|
||||
if( tracked_size == first_free_slot_number){
|
||||
fprintf( stderr, "no more slot_numbers for tracked touches\n");
|
||||
exit(1);
|
||||
}
|
||||
return first_free_slot_number;
|
||||
}
|
||||
|
||||
/* yes int instead of uint */
|
||||
/**
|
||||
SCHIFO : il primo tocco non ha mai ABS_MT_SLOT
|
||||
ma parte direttamente da ABS_MT_TRACKING_ID
|
||||
|
||||
QUINDI se viene settato senza ABS_MT_SLOT
|
||||
|
||||
|
||||
*/
|
||||
void TC_L1_builder_set_tracking_id( int64_t tracking_id){
|
||||
/*
|
||||
fix brutta
|
||||
*/
|
||||
if( ! current_event){
|
||||
if( last_event){
|
||||
printf("current was unset : setting as the last, which had slot number %ld\n", last_event->slot_number);
|
||||
current_event = last_event;
|
||||
}
|
||||
else{
|
||||
printf("current was unset : setting as NEW\n");
|
||||
TC_L1_builder_set_slot_number( tracked_get_first_free_slot_number());
|
||||
}
|
||||
}
|
||||
|
||||
/* unsigned aghhh */
|
||||
if( -1u == tracking_id){
|
||||
printf("REMOVE\n");
|
||||
tracked_remove( current_event);
|
||||
free( current_event);
|
||||
current_event = NULL;
|
||||
}
|
||||
else{
|
||||
current_event->tracking_id = tracking_id;
|
||||
}
|
||||
}
|
||||
|
||||
void TC_L1_builder_set_position_x( uint64_t x_coord){
|
||||
current_event->position.x = x_coord;
|
||||
}
|
||||
|
||||
void TC_L1_builder_set_position_y( uint64_t y_coord){
|
||||
current_event->position.y = y_coord;
|
||||
}
|
||||
|
||||
void TC_L1_builder_set_major_axis( uint64_t width){
|
||||
|
||||
}
|
||||
|
||||
void TC_L1_builder_set_approaching_major_axis( uint64_t width){
|
||||
|
||||
}
|
||||
|
||||
void TC_L1_builder_set_slot_number( uint64_t number){
|
||||
touch_event* target = tracked_find( number);
|
||||
if( ! target){
|
||||
printf("FETCHING DID NOT SUCCEED, allocating\n");
|
||||
target = malloc( sizeof( touch_event));
|
||||
target->slot_number = number;
|
||||
tracked_add( target);
|
||||
}
|
||||
else{
|
||||
printf("FETCHING SUCCEEDED\n");
|
||||
target->slot_number = number;
|
||||
}
|
||||
last_event = current_event;
|
||||
current_event = target;
|
||||
}
|
||||
|
||||
void TC_L1_builder_copy_tracked_events( touch_event ***store, uint8_t *count){
|
||||
//printf("TC_L1_builder_copy_tracked_events begins with %p, %d\n", (void*) *store, *count);
|
||||
|
||||
if( ! tracked_count){
|
||||
//printf("TC_L1_b_c_t_e has nothing to copy");
|
||||
*store = NULL;
|
||||
*count = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
//printf("tracked\n");
|
||||
//for( uint8_t i = 0; i < tracked_count; i++){
|
||||
// printf( "%ld\n", tracked[i]->tracking_id);
|
||||
//}
|
||||
|
||||
*store = malloc( tracked_count * sizeof( touch_event* ));
|
||||
touch_event *clone;
|
||||
touch_event *current;
|
||||
int index = 0;
|
||||
|
||||
for( int i = 0; i < tracked_size; i++){
|
||||
current = tracked[i];
|
||||
if( current){
|
||||
clone = malloc( sizeof( touch_event));
|
||||
touch_event_copy( clone, current);
|
||||
(*store)[index++] = clone;
|
||||
}
|
||||
}
|
||||
*count = index;
|
||||
|
||||
//printf("copied\n");
|
||||
//for( uint8_t i = 0; i < *count; i++){
|
||||
// printf( "%ld\n", (*store)[i]->tracking_id);
|
||||
//}
|
||||
|
||||
//printf("TC_L1_builder_copy_tracked_events ends with %p, %d\n", (void*) *store, *count);
|
||||
}
|
||||
|
||||
void TC_L1_builder_destroy_copy( touch_event **store, uint8_t count){
|
||||
for( int i = 0; i < count; i++){
|
||||
free(store[i]);
|
||||
}
|
||||
if( count){
|
||||
free( store);
|
||||
}
|
||||
}
|
||||
81
sources/input/touch/touch_controller_L2.c
Normal file
81
sources/input/touch/touch_controller_L2.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "../../../headers/input/touch/touch_controller_L2.h"
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/* ...but the compiler throws an error, so math.h does not really define it */
|
||||
/* aarch64-linux-gnu/include/math.h already defines it */
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846264338327950288
|
||||
#endif
|
||||
|
||||
touch_coordinates* TC_L2_getAABB( touch_event **events, uint8_t count){
|
||||
touch_coordinates* AABB = malloc( 2 * sizeof(touch_coordinates));
|
||||
uint8_t minX = UINT8_MAX;
|
||||
uint8_t maxX = 0;
|
||||
uint8_t minY = UINT8_MAX;
|
||||
uint8_t maxY = 0;
|
||||
|
||||
touch_event *current;
|
||||
for( uint8_t i = 0; i < count; i++){
|
||||
current = events[i];
|
||||
if( minX > current->position.x)
|
||||
minX = current->position.x;
|
||||
|
||||
if( maxX < current->position.x)
|
||||
maxX = current->position.x;
|
||||
|
||||
if( minY > current->position.y)
|
||||
minY = current->position.y;
|
||||
|
||||
if( maxY < current->position.y)
|
||||
maxY = current->position.y;
|
||||
}
|
||||
|
||||
AABB[0].x = minX;
|
||||
AABB[0].y = minY;
|
||||
AABB[1].x = maxX;
|
||||
AABB[1].y = maxY;
|
||||
return AABB;
|
||||
}
|
||||
|
||||
float TC_L2_get_width( touch_event **events, uint8_t count){
|
||||
touch_coordinates* AABB = TC_L2_getAABB( events, count);
|
||||
|
||||
uint16_t dx = AABB[0].x - AABB[1].x;
|
||||
uint16_t dy = AABB[0].y - AABB[1].y;
|
||||
float width = sqrtf( powf( dx, 2) + powf( dy, 2));
|
||||
|
||||
free( AABB);
|
||||
return width;
|
||||
}
|
||||
|
||||
float TC_L2_get_angle( touch_event **events, uint8_t count){
|
||||
touch_coordinates* AABB = TC_L2_getAABB( events, count);
|
||||
|
||||
uint16_t dx = AABB[0].x - AABB[1].x;
|
||||
uint16_t dy = AABB[0].y - AABB[1].y;
|
||||
|
||||
free( AABB);
|
||||
return atan2f(dx, dy) * 180 / M_PI;
|
||||
|
||||
}
|
||||
|
||||
touch_coordinates* TC_L2_get_midpoint( touch_event **events, uint8_t count){
|
||||
uint32_t sum_x;
|
||||
uint32_t sum_y;
|
||||
|
||||
if( !count){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for( uint8_t i = 0; i < count; i++){
|
||||
sum_x += events[i]->position.x;
|
||||
sum_y += events[i]->position.y;
|
||||
}
|
||||
|
||||
touch_coordinates *mid = malloc( sizeof( touch_coordinates));
|
||||
mid->x = sum_x / count;
|
||||
mid->y = sum_y / count;
|
||||
|
||||
return mid;
|
||||
}
|
||||
145
sources/input/touch/touch_controller_L3.c
Normal file
145
sources/input/touch/touch_controller_L3.c
Normal file
@@ -0,0 +1,145 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdlib.h>
|
||||
|
||||
/* a single printf */
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../../headers/input/touch/touch_controller_L2.h"
|
||||
#include "../../../headers/input/touch/touch_controller_L3.h"
|
||||
#include "../../../headers/input/touch/touch_controller_L3_deltas_compute.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
WHAT POINTER IS THE THIRD???
|
||||
|
||||
*/
|
||||
static int cmp_uint64(const void *p1, const void *p2)
|
||||
{
|
||||
uint64_t a = *(const uint64_t*) p1;
|
||||
uint64_t b = *(const uint64_t*) p2;
|
||||
//printf("comparison between %ld and %ld\n", a, b);
|
||||
if( a < b ){
|
||||
return 1;
|
||||
}
|
||||
else if( a > b){
|
||||
return -1;
|
||||
}
|
||||
else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* returns the sorted tracking_ids of a touch_coordinates array */
|
||||
void touch_events_to_sorted_ids( uint64_t **sortedIds, touch_event **full, uint8_t full_count){
|
||||
//printf("sorting an array of ids long %d\n", full_count);
|
||||
*sortedIds = malloc( full_count * sizeof( uint64_t));
|
||||
//printf("populate\n");
|
||||
for( uint8_t i = 0; i < full_count; i++){
|
||||
// could crash here...
|
||||
( *sortedIds)[i] = full[i]->tracking_id;
|
||||
}
|
||||
//printf("begin of actual sorting\n");
|
||||
/* this NULL arg could bite */
|
||||
/* qsort_r */
|
||||
qsort( sortedIds, full_count, sizeof( uint64_t), cmp_uint64);
|
||||
}
|
||||
|
||||
void TC_L3_get_position_deltas(
|
||||
touch_event **previous, uint8_t previous_count,
|
||||
touch_event **current, uint8_t current_count,
|
||||
touch_coordinates **deltas, uint8_t *count
|
||||
){
|
||||
if( previous && current){
|
||||
uint64_t *sorted_previous_ids = NULL;
|
||||
uint64_t *sorted_current_ids = NULL;
|
||||
tracked_delta *id_deltas;
|
||||
/* the number of total id_deltas, being a sum of sets, can be greater than the single sorted_ids list
|
||||
but the output count, being their intersections, cannot
|
||||
*/
|
||||
uint16_t id_deltas_count;
|
||||
|
||||
printf("mannaggia all'abaco mostrami sti id\n");
|
||||
printf("previous\n");
|
||||
for( uint8_t i = 0; i < previous_count; i++){
|
||||
printf( "%ld\n", previous[i]->tracking_id);
|
||||
}
|
||||
|
||||
printf("current\n");
|
||||
for( uint8_t i = 0; i < current_count; i++){
|
||||
printf( "%ld\n", current[i]->tracking_id);
|
||||
}
|
||||
|
||||
printf("sort ids 1\n");
|
||||
|
||||
touch_events_to_sorted_ids( &sorted_previous_ids, previous, previous_count);
|
||||
|
||||
printf("sort ids 2\n");
|
||||
|
||||
touch_events_to_sorted_ids( &sorted_current_ids, current, current_count);
|
||||
|
||||
printf("sorted ids\n");
|
||||
|
||||
printf("get id_deltas\n");
|
||||
touch_events_sorted_ids_deltas(
|
||||
&id_deltas, &id_deltas_count,
|
||||
sorted_previous_ids, previous_count,
|
||||
sorted_current_ids, current_count
|
||||
);
|
||||
printf("got id_deltas\n");
|
||||
|
||||
free( sorted_previous_ids);
|
||||
free( sorted_current_ids);
|
||||
|
||||
uint8_t endcount = 0;
|
||||
|
||||
for( uint16_t i = 0; i < id_deltas_count; i++){
|
||||
if( TRACKED_DELTA_UNCHANGED == id_deltas[i].state){
|
||||
endcount++;
|
||||
}
|
||||
}
|
||||
|
||||
*count = endcount;
|
||||
|
||||
*deltas = new_touch_coordinates_buffer( endcount);
|
||||
|
||||
for( uint16_t i = 0; i < id_deltas_count; i++){
|
||||
tracked_delta current_id_delta = id_deltas[i];
|
||||
if( TRACKED_DELTA_UNCHANGED == current_id_delta.state){
|
||||
touch_event *prev_evt = previous[ current_id_delta.old_index];
|
||||
touch_event *next_evt = current[ current_id_delta.new_index];
|
||||
deltas[i]->x = next_evt->position.x - prev_evt->position.x;
|
||||
deltas[i]->y = next_evt->position.y - prev_evt->position.y;
|
||||
}
|
||||
}
|
||||
free( id_deltas);
|
||||
}
|
||||
else{
|
||||
printf( "previous or current are empty!\n");
|
||||
*deltas = NULL;
|
||||
*count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
float TC_L3_get_angle_delta( float previous_angle, float current_angle){
|
||||
float dAngle = 0;
|
||||
dAngle = current_angle - previous_angle;
|
||||
if( dAngle > 180){
|
||||
dAngle -= 360;
|
||||
}
|
||||
else if( dAngle < -180){
|
||||
dAngle += 360;
|
||||
}
|
||||
return dAngle;
|
||||
}
|
||||
|
||||
touch_coordinates* TC_L3_get_midpoint_delta( touch_coordinates previous, touch_coordinates current){
|
||||
touch_coordinates* delta = malloc( sizeof( touch_coordinates));
|
||||
|
||||
delta->x = current.x - previous.x;
|
||||
delta->y = current.y - previous.y;
|
||||
|
||||
return delta;
|
||||
}
|
||||
110
sources/input/touch/touch_controller_L3_deltas_compute.c
Normal file
110
sources/input/touch/touch_controller_L3_deltas_compute.c
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "../../../headers/input/touch/touch_controller_L3_deltas_compute.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void delta_store(
|
||||
tracked_delta *delta, uint8_t state_flag, uint8_t cursor, uint64_t *ids_buffer
|
||||
){
|
||||
(*delta).state = state_flag;
|
||||
(*delta).tracking_id = ids_buffer[cursor];
|
||||
}
|
||||
|
||||
void increase_and_flag(
|
||||
uint8_t *cursor, uint8_t buffer_length, uint8_t *buffer_completed
|
||||
){
|
||||
(*cursor)++;
|
||||
|
||||
if( (*cursor) >= buffer_length){
|
||||
*buffer_completed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* a lot of params since sizeof(arr) does not work with heap arrays */
|
||||
/* takes two sorted arrays of ids : previous and current
|
||||
returns an array of tracked_delta(s)
|
||||
*/
|
||||
void touch_events_sorted_ids_deltas(
|
||||
tracked_delta **endbuffer, uint16_t *endcount,
|
||||
uint64_t *previous_ids, uint8_t previous_count,
|
||||
uint64_t *current_ids, uint8_t current_count
|
||||
){
|
||||
uint16_t accumulatedCounter = 0;
|
||||
|
||||
uint8_t previous_completed = 0;
|
||||
uint8_t current_completed = 0;
|
||||
|
||||
uint8_t previous_cursor = 0;
|
||||
uint8_t current_cursor = 0;
|
||||
|
||||
//until at least one of the lists has not been fully traversed, checks must be done to avoid collisions
|
||||
while( ! previous_completed && ! current_completed){
|
||||
|
||||
accumulatedCounter++;
|
||||
|
||||
if( previous_ids[previous_cursor] < current_ids[current_cursor]){
|
||||
increase_and_flag( &previous_cursor, previous_count, &previous_completed);
|
||||
}
|
||||
else if( previous_ids[previous_cursor] == current_ids[current_cursor]){
|
||||
increase_and_flag( &previous_cursor, previous_count, &previous_completed);
|
||||
increase_and_flag( ¤t_cursor, current_count, ¤t_completed);
|
||||
}
|
||||
else{
|
||||
increase_and_flag( ¤t_cursor, current_count, ¤t_completed);
|
||||
}
|
||||
}
|
||||
|
||||
accumulatedCounter += ( previous_count - previous_cursor);
|
||||
accumulatedCounter += ( current_count - current_cursor);
|
||||
|
||||
*endcount = accumulatedCounter;
|
||||
|
||||
*endbuffer = malloc( *endcount * sizeof( tracked_delta));
|
||||
|
||||
/* since the scan has already been done once, previous_completed and current_completed could be substituted by a counter_threshold variable... */
|
||||
accumulatedCounter = (uint16_t) -1u;
|
||||
|
||||
previous_completed = 0;
|
||||
current_completed = 0;
|
||||
|
||||
previous_cursor = 0;
|
||||
current_cursor = 0;
|
||||
|
||||
while( ! previous_completed && ! current_completed){
|
||||
|
||||
accumulatedCounter++;
|
||||
|
||||
if( previous_ids[previous_cursor] < current_ids[current_cursor]){
|
||||
endbuffer[accumulatedCounter]->old_index = previous_cursor;
|
||||
delta_store( endbuffer[accumulatedCounter], TRACKED_DELTA_EXITED, previous_cursor, previous_ids);
|
||||
increase_and_flag( &previous_cursor, previous_count, &previous_completed);
|
||||
}
|
||||
else if( previous_ids[previous_cursor] == current_ids[current_cursor]){
|
||||
endbuffer[accumulatedCounter]->old_index = previous_cursor;
|
||||
endbuffer[accumulatedCounter]->new_index = current_cursor;
|
||||
// both the combinations of cursor and ids_buffer will work
|
||||
delta_store( endbuffer[accumulatedCounter], TRACKED_DELTA_UNCHANGED, previous_cursor, previous_ids);
|
||||
increase_and_flag( &previous_cursor, previous_count, &previous_completed);
|
||||
increase_and_flag( ¤t_cursor, current_count, ¤t_completed);
|
||||
}
|
||||
else{
|
||||
endbuffer[accumulatedCounter]->new_index = current_cursor;
|
||||
delta_store( endbuffer[accumulatedCounter], TRACKED_DELTA_ENTERED, current_cursor, current_ids );
|
||||
increase_and_flag( ¤t_cursor, current_count, ¤t_completed);
|
||||
}
|
||||
}
|
||||
|
||||
while( ! previous_completed){
|
||||
accumulatedCounter++;
|
||||
endbuffer[accumulatedCounter]->old_index = previous_cursor;
|
||||
delta_store( endbuffer[accumulatedCounter], TRACKED_DELTA_EXITED, previous_cursor, previous_ids);
|
||||
increase_and_flag( &previous_cursor, previous_count, &previous_completed);
|
||||
}
|
||||
|
||||
while( ! current_completed){
|
||||
accumulatedCounter++;
|
||||
endbuffer[accumulatedCounter]->new_index = current_cursor;
|
||||
delta_store( endbuffer[accumulatedCounter], TRACKED_DELTA_ENTERED, current_cursor, current_ids );
|
||||
increase_and_flag( ¤t_cursor, current_count, ¤t_completed);
|
||||
}
|
||||
}
|
||||
73
sources/input/touch/touch_data.c
Normal file
73
sources/input/touch/touch_data.c
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../../headers/input/touch/touch_data.h"
|
||||
|
||||
touch_event* touch_event_new(){
|
||||
touch_event* te = malloc( sizeof( touch_event));
|
||||
te->slot_number = 0;
|
||||
te->tracking_id = 0;
|
||||
te->position.x = 0;
|
||||
te->position.y = 0;
|
||||
return te;
|
||||
}
|
||||
|
||||
/* could use memcpy...*/
|
||||
void touch_event_copy( touch_event *dst, touch_event *src){
|
||||
dst->slot_number = src->slot_number;
|
||||
dst->tracking_id = src->tracking_id;
|
||||
dst->position.x = src->position.x;
|
||||
dst->position.y = src->position.y;
|
||||
}
|
||||
|
||||
touch_coordinates* new_touch_coordinates_buffer( uint8_t count){
|
||||
if( ! count){
|
||||
return NULL;
|
||||
}
|
||||
touch_coordinates* buffer = malloc( count * sizeof( touch_coordinates));
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void free_touch_coordinates_buffer( touch_coordinates* buffer){
|
||||
if( buffer){
|
||||
free( buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void print_touch_event( touch_event *event){
|
||||
printf("touch_event info : %p\n", (void*) event);
|
||||
printf("slot_number : %ld\n", event->slot_number);
|
||||
printf("tracking_id : %ld\n", event->tracking_id);
|
||||
printf("position_x : %ld\n", event->position.x);
|
||||
printf("position_y : %ld\n", event->position.y);
|
||||
printf("touch_event info end\n");
|
||||
}
|
||||
|
||||
void touch_events_array_copy( touch_event ***dst, touch_event **src, uint8_t count){
|
||||
//printf("begin copy\n");
|
||||
if( ! count){
|
||||
//printf("nothing to copy\n");
|
||||
*dst = NULL;
|
||||
return;
|
||||
}
|
||||
*dst = malloc( count * sizeof( touch_event*));
|
||||
//printf("will copy %d items, into %p, taken from %p\n", count, ( void*) (*dst), ( void*) src);
|
||||
for( uint8_t i ; i < count; i++){
|
||||
//print_touch_event( src[i]);
|
||||
( *dst)[i] = touch_event_new();
|
||||
|
||||
//print_touch_event( ( *dst)[i]);
|
||||
|
||||
touch_event_copy( ( *dst)[i], src[i]);
|
||||
//print_touch_event( ( *dst)[i]);
|
||||
}
|
||||
//printf("end copy\n");
|
||||
}
|
||||
|
||||
void free_touch_events_array( touch_event **buffer, uint8_t count){
|
||||
for( uint8_t i; i < count; i++){
|
||||
free( buffer[i]);
|
||||
}
|
||||
free( buffer);
|
||||
}
|
||||
Reference in New Issue
Block a user