Subversion Repositories HomeAutomation

Rev

Rev 1279 | Rev 1284 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_Touch.h"
  3. point touchBuffer[sns_Touch_BUFFERSIZE];
  4. uint8_t rxbufidx;
  5.  
  6.  
  7. #ifdef sns_Touch_USEEEPROM
  8. #include "sns_Touch_eeprom.h"
  9. struct eeprom_sns_Touch EEMEM eeprom_sns_Touch =
  10. {
  11.     {
  12.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  13.         0xAB,   // x
  14.         0x1234  // y
  15.     },
  16.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  17. };
  18. #endif
  19.  
  20. void sns_Touch_Init(void)
  21. {
  22. #ifdef sns_Touch_USEEEPROM
  23.     if (EEDATA_OK)
  24.     {
  25.       ///TODO: Use stored data to set initial values for the module
  26.       blablaX = eeprom_read_byte(EEDATA.x);
  27.       blablaY = eeprom_read_word(EEDATA.y);
  28.     } else
  29.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  30.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  31.       eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC);
  32.       EEDATA_UPDATE_CRC;
  33.     }
  34. #endif  
  35.     ///TODO: Initialize hardware etc here
  36.  
  37.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  38.     // Pcint_SetCallbackPin(sns_Touch_PCINT, EXP_C , &sns_Touch_pcint_callback);
  39.  
  40.     ADC_Init();
  41.     Timer_SetTimeout(sns_Touch_POLL_TIMER, sns_Touch_POLL_PERIOD , TimerTypeFreeRunning, 0);
  42.    
  43.     rxbufidx = 0;
  44. }
  45.  
  46.  
  47. /*
  48. F1: Klar
  49. F2: Klar
  50. F3: Klar
  51. F4: Klar
  52. F5: Klar
  53. F6: Klar
  54. F7: Klar
  55. F8: Klar
  56. F9:
  57.  
  58. */
  59.  
  60.  
  61. /*
  62. To fix:
  63. Input parameter for buffer
  64. Output returnvalues
  65. Break out to a driver file
  66. */
  67. uint8_t parseBuffer(uint8_t startIndex, uint8_t endIndex)
  68. {
  69.     int16_t f1_k = 0, f3_tmp = 0, f4_tmp = 0, f5_tmp = 0, f9_tmp = 0;
  70.     uint8_t f7_tmp = 0, f7_sign = 0, f1_sign = 0, f3_yref = 0, f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0, f7 = 0, f8 = 0, f9 = 0, xMin, xMax, yMin, yMax;
  71.    
  72.     /* Function 6 indicates the sign value of the y coordinates of the last point minus y coordinates of the first point */
  73.     if ((int16_t)touchBuffer[endIndex].y - (int16_t)touchBuffer[startIndex].y > 0)
  74.     {
  75.         f6 = CAN_MODULE_ENUM_TOUCH_GESTURE_F6_P;
  76.     }
  77.     else
  78.     {
  79.         f6 = CAN_MODULE_ENUM_TOUCH_GESTURE_F6_M;
  80.     }
  81.    
  82.     xMin = touchBuffer[startIndex].x;
  83.     xMax = touchBuffer[startIndex].x;
  84.     yMin = touchBuffer[startIndex].y;
  85.     yMax = touchBuffer[startIndex].y;
  86.     f3_yref = (touchBuffer[startIndex].y + touchBuffer[endIndex].y)/2;
  87.     f1_k = ((touchBuffer[endIndex].y-touchBuffer[startIndex].y)<<4)/(touchBuffer[endIndex].x-touchBuffer[startIndex].x);
  88.     f1_sign = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_U;
  89.  
  90.     for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  91.     {
  92.         /* Function 1 indicates the number of intersection points between an input gesture g(x) and the straight line f(x)
  93.         which connects the first and last point of the gesture */
  94.         if (((f1_k*(touchBuffer[i].x-touchBuffer[0].x))>>4) + touchBuffer[0].y < touchBuffer[i].y)
  95.         {
  96.             if (f1_sign != CAN_MODULE_ENUM_TOUCH_GESTURE_F2_P)
  97.             {
  98.                 f1++;
  99.             }
  100.             f1_sign = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_P;
  101.         }
  102.         else
  103.         {
  104.             if (f1_sign != CAN_MODULE_ENUM_TOUCH_GESTURE_F2_M)
  105.             {
  106.                 f1++;
  107.             }
  108.             f1_sign = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_M;
  109.         }
  110.        
  111.         /* Function 3 indicates the sign value of area gap between g(x) and f(x) */
  112.         if (touchBuffer[i].x > min(touchBuffer[startIndex].x, touchBuffer[endIndex].x) && touchBuffer[i].x < max(touchBuffer[startIndex].x, touchBuffer[endIndex].x))
  113.         {
  114.             f3_tmp += f3_yref - (touchBuffer[i].y + touchBuffer[i-1].y)/2;
  115.         }
  116.        
  117.         /* find min an max of x and y */
  118.         if (xMin > touchBuffer[i].x)
  119.         {
  120.             xMin = touchBuffer[i].x;
  121.         }
  122.         if (xMax < touchBuffer[i].x)
  123.         {
  124.             xMax = touchBuffer[i].x;
  125.         }
  126.         if (yMin > touchBuffer[i].y)
  127.         {
  128.             yMin = touchBuffer[i].y;
  129.         }
  130.         if (yMax < touchBuffer[i].y)
  131.         {
  132.             yMax = touchBuffer[i].y;
  133.         }
  134.        
  135.         /* Function 4 represents the sign value of the sum of the x coordinates of all the points that constitute strokes
  136.         minus the x coordinates of the last point */
  137.         f4_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[endIndex].x;
  138.        
  139.         /* Function 5 checks the sign value of the sum of the x coordinates of all the points that constitute strokes
  140.         minus x coordinates of the first point */
  141.         f5_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[startIndex].x;
  142.     }
  143.    
  144.     /* Function 7 indicates the number of intersection points between an input gesture g(x) and all horizontal lines yi,
  145.     where i is between 0 and n, that constitute a gesture*/
  146.     for (uint8_t j = yMin; j < yMax-1; j++)
  147.     {
  148.         f7_tmp = 0xff;
  149.         f7_sign = 0;
  150.         for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  151.         {
  152.             if (j > touchBuffer[i].y)
  153.             {
  154.                 if (f7_sign != '+')
  155.                 {
  156.                     f7_tmp++;
  157.                 }
  158.                 f7_sign = '+';
  159.             }
  160.             else {
  161.                 if (f7_sign != '-')
  162.                 {
  163.                     f7_tmp++;
  164.                 }
  165.                 f7_sign = '-';
  166.             }
  167.         }
  168.         if (f7_tmp > f7)
  169.         {
  170.             f7 = f7_tmp;
  171.         }
  172.     }
  173.    
  174.     f1--;
  175.     /* Function 2 checks if the y coordinates of the points which exist between the intersection point and the last point are
  176.     greater than the y coordinates of the intersection point */
  177.     f2 = f1_sign;
  178.     if (f1 == 0)
  179.     {
  180.         f2 = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_U;
  181.     }
  182.    
  183.     /* Function 3 indicates the sign value of area gap between g(x) and f(x) */
  184.     f3 = CAN_MODULE_ENUM_TOUCH_GESTURE_F3_P;
  185.     if (f3_tmp > 0)
  186.     {
  187.         f3 = CAN_MODULE_ENUM_TOUCH_GESTURE_F3_M;
  188.     }
  189.        
  190.     /* Function 9 represents the sign value of the multiplication of x coordinates of the first point and the last point
  191.     minus the mediate value of x coordinates of all the points */
  192.     f9_tmp = ((touchBuffer[startIndex].x - (xMax+xMin)/2)*(touchBuffer[endIndex].x - (xMax+xMin)/2));
  193.     f9 = CAN_MODULE_ENUM_TOUCH_GESTURE_F9_M;
  194.     if (f9_tmp > 0)
  195.     {
  196.         f9 = CAN_MODULE_ENUM_TOUCH_GESTURE_F9_P;
  197.     }
  198.    
  199.     /* Function 5 checks the sign value of the sum of the x coordinates of all the points that constitute strokes
  200.     minus x coordinates of the first point */
  201.     f5 = CAN_MODULE_ENUM_TOUCH_GESTURE_F5_M;
  202.     if (f5_tmp > 0)
  203.     {
  204.         f5 = CAN_MODULE_ENUM_TOUCH_GESTURE_F5_P;
  205.     }
  206.    
  207.     /* Function 4 represents the sign value of the sum of the x coordinates of all the points that constitute strokes
  208.     minus the x coordinates of the last point */
  209.     f4 = CAN_MODULE_ENUM_TOUCH_GESTURE_F4_M;
  210.     if (f4_tmp > 0)
  211.     {
  212.         f4 = CAN_MODULE_ENUM_TOUCH_GESTURE_F4_P;
  213.     }
  214.    
  215.     /* 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 */
  216.     f8 = CAN_MODULE_ENUM_TOUCH_GESTURE_F8_M;
  217.     if (xMin < min(touchBuffer[startIndex].x,touchBuffer[endIndex].x) || xMax > max(touchBuffer[startIndex].x,touchBuffer[endIndex].x))
  218.     {
  219.         f8 = CAN_MODULE_ENUM_TOUCH_GESTURE_F8_P;
  220.     }
  221.        
  222.     //printf("1%u2%c3%c4%c5%c6%c7%u8%c9%c\n", f1, f2, f3, f4, f5, f6, f7, f8, f9);
  223.  
  224.     StdCan_Msg_t txMsg;
  225.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  226.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  227.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  228.     txMsg.Header.ModuleId = sns_Touch_ID;
  229.     txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_GESTURE;
  230.     txMsg.Length = 3;
  231.     txMsg.Data[0] = f1;
  232.     txMsg.Data[1] = f7;
  233.     txMsg.Data[2] = f3<<7|f4<<6|f5<<5|f6<<4|f8<<3|f9<<2|f2;
  234.    
  235.     StdCan_Put(&txMsg);
  236.     return 0;
  237. }
  238.  
  239.  
  240. void sns_Touch_Process(void)
  241. {
  242.    
  243.     if (Timer_Expired(sns_Touch_POLL_TIMER)) {
  244.         //StdCan_Msg_t txMsg;
  245.         gpio_set_in(sns_Touch_XPLUS);
  246.         gpio_set_pullup(sns_Touch_XPLUS);   //turn on pullup for x+
  247.         gpio_set_out(sns_Touch_YPLUS);
  248.         gpio_set_pin(sns_Touch_YPLUS);      //turn on 1 on y+
  249.         gpio_set_out(sns_Touch_YMINUS);
  250.         gpio_clr_pin(sns_Touch_YMINUS);     //turn on 0 on y-
  251.         uint8_t adxval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
  252.         gpio_set_in(sns_Touch_YPLUS);
  253.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  254.         gpio_set_in(sns_Touch_YMINUS);
  255.         gpio_clr_pullup(sns_Touch_YMINUS);  //turn off pullup for y-
  256.         gpio_set_in(sns_Touch_XPLUS);
  257.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  258.        
  259.         gpio_set_in(sns_Touch_YPLUS);
  260.         gpio_set_pullup(sns_Touch_YPLUS);   //turn on pullup for y+
  261.         gpio_set_out(sns_Touch_XPLUS);
  262.         gpio_set_pin(sns_Touch_XPLUS);      //turn on 1 on x+
  263.         gpio_set_out(sns_Touch_XMINUS);
  264.         gpio_clr_pin(sns_Touch_XMINUS);     //turn on 0 on x-
  265.         uint8_t adyval = ADC_Get(sns_Touch_TOUCHYAD)>>2;    //read y-
  266.         gpio_set_in(sns_Touch_XPLUS);
  267.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  268.         gpio_set_in(sns_Touch_XMINUS);
  269.         gpio_clr_pullup(sns_Touch_XMINUS);  //turn off pullup for x-
  270.         gpio_set_in(sns_Touch_YPLUS);
  271.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  272.        
  273.         if (adyval < 0xf0 && adxval < 0xf0 )
  274.         {
  275.             uint8_t xdiff=0;
  276.             uint8_t ydiff=0;
  277.             if (rxbufidx > 0)
  278.             {
  279.                 xdiff = touchBuffer[rxbufidx-1].x - adxval;
  280.                 if (touchBuffer[rxbufidx-1].x < adxval)
  281.                 {
  282.                     xdiff = adxval - touchBuffer[rxbufidx-1].x;
  283.                 }
  284.                 ydiff = touchBuffer[rxbufidx-1].y - adyval;
  285.                 if (touchBuffer[rxbufidx-1].y < adyval)
  286.                 {
  287.                     ydiff = adyval - touchBuffer[rxbufidx-1].y;
  288.                 }
  289.             }
  290.             if (rxbufidx == 0 || (rxbufidx > 0 && xdiff+ydiff > 10))
  291.             {
  292.                 touchBuffer[rxbufidx].x = adxval;
  293.                 touchBuffer[rxbufidx].y = adyval;
  294.                 rxbufidx++;
  295.                 if (rxbufidx==sns_Touch_BUFFERSIZE)
  296.                 {
  297.                     rxbufidx=0;
  298.                 }
  299.             }
  300.             //printf("Y: %d X: %d\n", adyval, adxval);
  301.         }
  302.         else if (rxbufidx > 3)
  303.         {
  304.             //printf("Released %d\n", rxbufidx);
  305.             parseBuffer(0, rxbufidx-1);
  306.             /*for (uint8_t i = 0; i < rxbufidx; i++)
  307.             {
  308.                 printf("%3d ", touchBuffer[i].x);
  309.             }
  310.             printf("\n");
  311.             for (uint8_t i = 0; i < rxbufidx; i++)
  312.             {
  313.                 printf("%3d ", touchBuffer[i].y);
  314.             }
  315.             printf("\n");
  316.             */
  317.            
  318.             rxbufidx = 0;
  319.         }
  320.     }
  321. }
  322.  
  323.  
  324. void sns_Touch_HandleMessage(StdCan_Msg_t *rxMsg)
  325. {
  326.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  327.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  328.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  329.         rxMsg->Header.ModuleId == sns_Touch_ID)
  330.     {
  331.         switch (rxMsg->Header.Command)
  332.         {
  333.         case CAN_CMD_MODULE_DUMMY:
  334.         ///TODO: Do something dummy
  335.         break;
  336.         }
  337.     }*/
  338. }
  339.  
  340. void sns_Touch_List(uint8_t ModuleSequenceNumber)
  341. {
  342.     StdCan_Msg_t txMsg;
  343.    
  344.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  345.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  346.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  347.     txMsg.Header.ModuleId = sns_Touch_ID;
  348.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  349.     txMsg.Length = 6;
  350.  
  351.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  352.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  353.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  354.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  355.    
  356.     txMsg.Data[4] = NUMBER_OF_MODULES;
  357.     txMsg.Data[5] = ModuleSequenceNumber;
  358.    
  359.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  360. }
  361.