project for a (works on my devices) demo
This commit is contained in:
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