Subversion Repositories HomeAutomation

Rev

Rev 1296 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * Touch gesture driver
  3.  *
  4.  * @date    2009-07-19
  5.  *
  6.  * @author  Linus Lundin, Jonas Andersson, Anders Runeson
  7.  *  
  8.  */
  9.  
  10. #include "gesture.h"
  11.  
  12. /**
  13.  * Touch gesture parser
  14.  * Implementation of 'A new gesture recognition algorithm and segmentation
  15.  * method of Korean scripts for gesture-allowed ink editor' by Mi Gyung Cho
  16.  *
  17.  * Call function parseBuffer with a buffer containing x/y coordinates, the buffer start and end.
  18.  * Returns function results that describe the gesture in 24bits
  19.  *
  20.  * @param buffer
  21.  *      Pointer to buffer to where to data to parse is stored
  22.  * @param startIndex
  23.  *      First value in buffer
  24.  * @param endIndex
  25.  *      Last value in buffer
  26.  * @return
  27.  *      Struct with parsed function results
  28.  */
  29. gesture parseBuffer(point *buffer, uint8_t startIndex, uint8_t endIndex)
  30. {
  31.     int16_t f1_k = 0, f3_tmp = 0, f4_tmp = 0, f5_tmp = 0;
  32.     uint8_t f7_tmp = 0, f7_sign = 0, f1_sign = 0, f3_yref = 0, xMin, xMax, yMin, yMax, f9_tmp;
  33.     gesture functionData;
  34.     functionData.f1=0;
  35.     functionData.f7=0;
  36.    
  37.     /* Function 6 indicates the sign value of the y coordinates of the last point minus y coordinates of the first point */
  38.     functionData.f6 = 0;
  39.     if ((int16_t)buffer[endIndex].y - (int16_t)buffer[startIndex].y > 0)
  40.     {
  41.         functionData.f6 = 1;
  42.     }
  43.    
  44.     xMin = buffer[startIndex].x;
  45.     xMax = buffer[startIndex].x;
  46.     yMin = buffer[startIndex].y;
  47.     yMax = buffer[startIndex].y;
  48.     f3_yref = (buffer[startIndex].y + buffer[endIndex].y)/2;
  49.     f1_k = ((buffer[endIndex].y-buffer[startIndex].y)<<4)/(buffer[endIndex].x-buffer[startIndex].x);
  50.     f1_sign = 2;
  51.  
  52.     for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  53.     {
  54.         /* Function 1 indicates the number of intersection points between an input gesture g(x) and the straight line f(x)
  55.         which connects the first and last point of the gesture */
  56.         if (((f1_k*(buffer[i].x-buffer[0].x))>>4) + buffer[0].y < buffer[i].y)
  57.         {
  58.             if (f1_sign != 1)
  59.             {
  60.                 functionData.f1++;
  61.             }
  62.             f1_sign = 1;
  63.         }
  64.         else
  65.         {
  66.             if (f1_sign != 0)
  67.             {
  68.                 functionData.f1++;
  69.             }
  70.             f1_sign = 0;
  71.         }
  72.        
  73.         /* Function 3 indicates the sign value of area gap between g(x) and f(x) */
  74.         if (buffer[i].x > min(buffer[startIndex].x, buffer[endIndex].x) && buffer[i].x < max(buffer[startIndex].x, buffer[endIndex].x))
  75.         {
  76.             f3_tmp += f3_yref - (buffer[i].y + buffer[i-1].y)/2;
  77.         }
  78.        
  79.         /* find min and max of x and y */
  80.         if (xMin > buffer[i].x)
  81.         {
  82.             xMin = buffer[i].x;
  83.         }
  84.         if (xMax < buffer[i].x)
  85.         {
  86.             xMax = buffer[i].x;
  87.         }
  88.         if (yMin > buffer[i].y)
  89.         {
  90.             yMin = buffer[i].y;
  91.         }
  92.         if (yMax < buffer[i].y)
  93.         {
  94.             yMax = buffer[i].y;
  95.         }
  96.        
  97.         /* Function 4 represents the sign value of the sum of the x coordinates of all the points that constitute strokes
  98.         minus the x coordinates of the last point */
  99.         f4_tmp += (int16_t)buffer[i].x - (int16_t)buffer[endIndex].x;
  100.        
  101.         /* Function 5 checks the sign value of the sum of the x coordinates of all the points that constitute strokes
  102.         minus x coordinates of the first point */
  103.         f5_tmp += (int16_t)buffer[i].x - (int16_t)buffer[startIndex].x;
  104.     }
  105.    
  106.     /* Function 7 indicates the number of intersection points between an input gesture g(x) and all horizontal lines yi,
  107.     where i is between 0 and n, that constitute a gesture*/
  108.     for (uint8_t j = yMin; j < yMax-1; j++)
  109.     {
  110.         f7_tmp = 0xff;
  111.         f7_sign = 0;
  112.         for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  113.         {
  114.             if (j > buffer[i].y)
  115.             {
  116.                 if (f7_sign != '+')
  117.                 {
  118.                     f7_tmp++;
  119.                 }
  120.                 f7_sign = '+';
  121.             }
  122.             else {
  123.                 if (f7_sign != '-')
  124.                 {
  125.                     f7_tmp++;
  126.                 }
  127.                 f7_sign = '-';
  128.             }
  129.         }
  130.         if (f7_tmp > functionData.f7)
  131.         {
  132.             functionData.f7 = f7_tmp;
  133.         }
  134.     }
  135.    
  136.     functionData.f1--;
  137.     /* Function 2 checks if the y coordinates of the points which exist between the intersection point and the last point are
  138.     greater than the y coordinates of the intersection point */
  139.     functionData.f2 = f1_sign;
  140.     if (functionData.f1 == 0)
  141.     {
  142.         functionData.f2 = 2;
  143.     }
  144.    
  145.     /* Function 3 indicates the sign value of area gap between g(x) and f(x) */
  146.     functionData.f3 = 1;
  147.     if (f3_tmp > 0)
  148.     {
  149.         functionData.f3 = 0;
  150.     }
  151.        
  152.     /* Function 9 represents the sign value of the multiplication of x coordinates of the first point and the last point
  153.     minus the mediate value of x coordinates of all the points */
  154.     //f9_tmp = ((buffer[startIndex].x - (xMax+xMin)/2)*(buffer[endIndex].x - (xMax+xMin)/2));
  155.     f9_tmp = ((buffer[startIndex].x - (xMax+xMin)/2 > 0) ^ (buffer[endIndex].x - (xMax+xMin)/2 > 0));
  156.     functionData.f9 = 0;
  157.     if (!f9_tmp)
  158.     {
  159.         functionData.f9 = 1;
  160.     }
  161.    
  162.     /* Function 5 checks the sign value of the sum of the x coordinates of all the points that constitute strokes
  163.     minus x coordinates of the first point */
  164.     functionData.f5 = 0;
  165.     if (f5_tmp > 0)
  166.     {
  167.         functionData.f5 = 1;
  168.     }
  169.    
  170.     /* Function 4 represents the sign value of the sum of the x coordinates of all the points that constitute strokes
  171.     minus the x coordinates of the last point */
  172.     functionData.f4 = 0;
  173.     if (f4_tmp > 0)
  174.     {
  175.         functionData.f4 = 1;
  176.     }
  177.    
  178.     /* Function 8 checks whether or not x coordinates of all the points except for the first point and the last point is between x0 and xn */
  179.     functionData.f8 = 1;
  180.     if (xMin+F8_OFFSET < min(buffer[startIndex].x,buffer[endIndex].x) || xMax > max(buffer[startIndex].x,buffer[endIndex].x)+F8_OFFSET)
  181.     {
  182.         functionData.f8 = 0;
  183.     }
  184.        
  185.     //printf("1%u2%c3%c4%c5%c6%c7%u8%c9%c\n", f1, f2, f3, f4, f5, f6, f7, f8, f9);
  186.  
  187.     return functionData;
  188. }
  189.  
  190.  
  191.