Subversion Repositories HomeAutomation

Rev

Rev 1280 | Rev 1285 | 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. uint8_t pushStatus;
  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.     pushStatus = 0;
  45. }
  46.  
  47.  
  48. /*
  49. F1: Klar
  50. F2: Klar
  51. F3: Klar
  52. F4: Klar
  53. F5: Klar
  54. F6: Klar
  55. F7: Klar
  56. F8: Klar
  57. F9:
  58.  
  59. */
  60.  
  61.  
  62. /*
  63. To fix:
  64. Input parameter for buffer
  65. Output returnvalues
  66. Break out to a driver file
  67. */
  68. uint8_t parseBuffer(uint8_t startIndex, uint8_t endIndex)
  69. {
  70.     int16_t f1_k = 0, f3_tmp = 0, f4_tmp = 0, f5_tmp = 0, f9_tmp = 0;
  71.     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;
  72.    
  73.     /* Function 6 indicates the sign value of the y coordinates of the last point minus y coordinates of the first point */
  74.     if ((int16_t)touchBuffer[endIndex].y - (int16_t)touchBuffer[startIndex].y > 0)
  75.     {
  76.         f6 = CAN_MODULE_ENUM_TOUCH_GESTURE_F6_P;
  77.     }
  78.     else
  79.     {
  80.         f6 = CAN_MODULE_ENUM_TOUCH_GESTURE_F6_M;
  81.     }
  82.    
  83.     xMin = touchBuffer[startIndex].x;
  84.     xMax = touchBuffer[startIndex].x;
  85.     yMin = touchBuffer[startIndex].y;
  86.     yMax = touchBuffer[startIndex].y;
  87.     f3_yref = (touchBuffer[startIndex].y + touchBuffer[endIndex].y)/2;
  88.     f1_k = ((touchBuffer[endIndex].y-touchBuffer[startIndex].y)<<4)/(touchBuffer[endIndex].x-touchBuffer[startIndex].x);
  89.     f1_sign = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_U;
  90.  
  91.     for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  92.     {
  93.         /* Function 1 indicates the number of intersection points between an input gesture g(x) and the straight line f(x)
  94.         which connects the first and last point of the gesture */
  95.         if (((f1_k*(touchBuffer[i].x-touchBuffer[0].x))>>4) + touchBuffer[0].y < touchBuffer[i].y)
  96.         {
  97.             if (f1_sign != CAN_MODULE_ENUM_TOUCH_GESTURE_F2_P)
  98.             {
  99.                 f1++;
  100.             }
  101.             f1_sign = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_P;
  102.         }
  103.         else
  104.         {
  105.             if (f1_sign != CAN_MODULE_ENUM_TOUCH_GESTURE_F2_M)
  106.             {
  107.                 f1++;
  108.             }
  109.             f1_sign = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_M;
  110.         }
  111.        
  112.         /* Function 3 indicates the sign value of area gap between g(x) and f(x) */
  113.         if (touchBuffer[i].x > min(touchBuffer[startIndex].x, touchBuffer[endIndex].x) && touchBuffer[i].x < max(touchBuffer[startIndex].x, touchBuffer[endIndex].x))
  114.         {
  115.             f3_tmp += f3_yref - (touchBuffer[i].y + touchBuffer[i-1].y)/2;
  116.         }
  117.        
  118.         /* find min an max of x and y */
  119.         if (xMin > touchBuffer[i].x)
  120.         {
  121.             xMin = touchBuffer[i].x;
  122.         }
  123.         if (xMax < touchBuffer[i].x)
  124.         {
  125.             xMax = touchBuffer[i].x;
  126.         }
  127.         if (yMin > touchBuffer[i].y)
  128.         {
  129.             yMin = touchBuffer[i].y;
  130.         }
  131.         if (yMax < touchBuffer[i].y)
  132.         {
  133.             yMax = touchBuffer[i].y;
  134.         }
  135.        
  136.         /* Function 4 represents the sign value of the sum of the x coordinates of all the points that constitute strokes
  137.         minus the x coordinates of the last point */
  138.         f4_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[endIndex].x;
  139.        
  140.         /* Function 5 checks the sign value of the sum of the x coordinates of all the points that constitute strokes
  141.         minus x coordinates of the first point */
  142.         f5_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[startIndex].x;
  143.     }
  144.    
  145.     /* Function 7 indicates the number of intersection points between an input gesture g(x) and all horizontal lines yi,
  146.     where i is between 0 and n, that constitute a gesture*/
  147.     for (uint8_t j = yMin; j < yMax-1; j++)
  148.     {
  149.         f7_tmp = 0xff;
  150.         f7_sign = 0;
  151.         for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  152.         {
  153.             if (j > touchBuffer[i].y)
  154.             {
  155.                 if (f7_sign != '+')
  156.                 {
  157.                     f7_tmp++;
  158.                 }
  159.                 f7_sign = '+';
  160.             }
  161.             else {
  162.                 if (f7_sign != '-')
  163.                 {
  164.                     f7_tmp++;
  165.                 }
  166.                 f7_sign = '-';
  167.             }
  168.         }
  169.         if (f7_tmp > f7)
  170.         {
  171.             f7 = f7_tmp;
  172.         }
  173.     }
  174.    
  175.     f1--;
  176.     /* Function 2 checks if the y coordinates of the points which exist between the intersection point and the last point are
  177.     greater than the y coordinates of the intersection point */
  178.     f2 = f1_sign;
  179.     if (f1 == 0)
  180.     {
  181.         f2 = CAN_MODULE_ENUM_TOUCH_GESTURE_F2_U;
  182.     }
  183.    
  184.     /* Function 3 indicates the sign value of area gap between g(x) and f(x) */
  185.     f3 = CAN_MODULE_ENUM_TOUCH_GESTURE_F3_P;
  186.     if (f3_tmp > 0)
  187.     {
  188.         f3 = CAN_MODULE_ENUM_TOUCH_GESTURE_F3_M;
  189.     }
  190.        
  191.     /* Function 9 represents the sign value of the multiplication of x coordinates of the first point and the last point
  192.     minus the mediate value of x coordinates of all the points */
  193.     f9_tmp = ((touchBuffer[startIndex].x - (xMax+xMin)/2)*(touchBuffer[endIndex].x - (xMax+xMin)/2));
  194.     f9 = CAN_MODULE_ENUM_TOUCH_GESTURE_F9_M;
  195.     if (f9_tmp > 0)
  196.     {
  197.         f9 = CAN_MODULE_ENUM_TOUCH_GESTURE_F9_P;
  198.     }
  199.    
  200.     /* Function 5 checks the sign value of the sum of the x coordinates of all the points that constitute strokes
  201.     minus x coordinates of the first point */
  202.     f5 = CAN_MODULE_ENUM_TOUCH_GESTURE_F5_M;
  203.     if (f5_tmp > 0)
  204.     {
  205.         f5 = CAN_MODULE_ENUM_TOUCH_GESTURE_F5_P;
  206.     }
  207.    
  208.     /* Function 4 represents the sign value of the sum of the x coordinates of all the points that constitute strokes
  209.     minus the x coordinates of the last point */
  210.     f4 = CAN_MODULE_ENUM_TOUCH_GESTURE_F4_M;
  211.     if (f4_tmp > 0)
  212.     {
  213.         f4 = CAN_MODULE_ENUM_TOUCH_GESTURE_F4_P;
  214.     }
  215.    
  216.     /* 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 */
  217.     f8 = CAN_MODULE_ENUM_TOUCH_GESTURE_F8_M;
  218.     if (xMin < min(touchBuffer[startIndex].x,touchBuffer[endIndex].x) || xMax > max(touchBuffer[startIndex].x,touchBuffer[endIndex].x))
  219.     {
  220.         f8 = CAN_MODULE_ENUM_TOUCH_GESTURE_F8_P;
  221.     }
  222.        
  223.     //printf("1%u2%c3%c4%c5%c6%c7%u8%c9%c\n", f1, f2, f3, f4, f5, f6, f7, f8, f9);
  224.  
  225.     StdCan_Msg_t txMsg;
  226.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  227.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  228.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  229.     txMsg.Header.ModuleId = sns_Touch_ID;
  230.     txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_GESTURE;
  231.     txMsg.Length = 3;
  232.     txMsg.Data[0] = f1;
  233.     txMsg.Data[1] = f7;
  234.     txMsg.Data[2] = f3<<7|f4<<6|f5<<5|f6<<4|f8<<3|f9<<2|f2;
  235.    
  236.     StdCan_Put(&txMsg);
  237.     return 0;
  238. }
  239.  
  240.  
  241. void sns_Touch_Process(void)
  242. {
  243.    
  244.     if (Timer_Expired(sns_Touch_POLL_TIMER)) {
  245.         //StdCan_Msg_t txMsg;
  246.         gpio_set_in(sns_Touch_XPLUS);
  247.         gpio_set_pullup(sns_Touch_XPLUS);   //turn on pullup for x+
  248.         gpio_set_out(sns_Touch_YPLUS);
  249.         gpio_set_pin(sns_Touch_YPLUS);      //turn on 1 on y+
  250.         gpio_set_out(sns_Touch_YMINUS);
  251.         gpio_clr_pin(sns_Touch_YMINUS);     //turn on 0 on y-
  252.         uint8_t adxval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
  253.         gpio_set_in(sns_Touch_YPLUS);
  254.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  255.         gpio_set_in(sns_Touch_YMINUS);
  256.         gpio_clr_pullup(sns_Touch_YMINUS);  //turn off pullup for y-
  257.         gpio_set_in(sns_Touch_XPLUS);
  258.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  259.        
  260.         gpio_set_in(sns_Touch_YPLUS);
  261.         gpio_set_pullup(sns_Touch_YPLUS);   //turn on pullup for y+
  262.         gpio_set_out(sns_Touch_XPLUS);
  263.         gpio_set_pin(sns_Touch_XPLUS);      //turn on 1 on x+
  264.         gpio_set_out(sns_Touch_XMINUS);
  265.         gpio_clr_pin(sns_Touch_XMINUS);     //turn on 0 on x-
  266.         uint8_t adyval = ADC_Get(sns_Touch_TOUCHYAD)>>2;    //read y-
  267.         gpio_set_in(sns_Touch_XPLUS);
  268.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  269.         gpio_set_in(sns_Touch_XMINUS);
  270.         gpio_clr_pullup(sns_Touch_XMINUS);  //turn off pullup for x-
  271.         gpio_set_in(sns_Touch_YPLUS);
  272.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  273.        
  274.         if (adyval < 0xf0 && adxval < 0xf0 )
  275.         {
  276.             pushStatus = 1;
  277.             uint8_t xdiff=0;
  278.             uint8_t ydiff=0;
  279.             if (rxbufidx > 0)
  280.             {
  281.                 xdiff = touchBuffer[rxbufidx-1].x - adxval;
  282.                 if (touchBuffer[rxbufidx-1].x < adxval)
  283.                 {
  284.                     xdiff = adxval - touchBuffer[rxbufidx-1].x;
  285.                 }
  286.                 ydiff = touchBuffer[rxbufidx-1].y - adyval;
  287.                 if (touchBuffer[rxbufidx-1].y < adyval)
  288.                 {
  289.                     ydiff = adyval - touchBuffer[rxbufidx-1].y;
  290.                 }
  291.             }
  292.             if (rxbufidx == 0 || (rxbufidx > 0 && xdiff+ydiff > 10))
  293.             {
  294.                 touchBuffer[rxbufidx].x = adxval;
  295.                 touchBuffer[rxbufidx].y = adyval;
  296.                 rxbufidx++;
  297.                 if (rxbufidx==sns_Touch_BUFFERSIZE)
  298.                 {
  299.                     rxbufidx=0;
  300.                 }
  301.  
  302.                 StdCan_Msg_t txMsg;
  303.                 StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  304.                 StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  305.                 txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  306.                 txMsg.Header.ModuleId = sns_Touch_ID;
  307.                 txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_RAW;
  308.                 txMsg.Length = 3;
  309.                 txMsg.Data[0] = CAN_MODULE_ENUM_TOUCH_RAW_STATUS_PRESSED;
  310.                 txMsg.Data[1] = adxval;
  311.                 txMsg.Data[2] = adyval;
  312.    
  313.                 StdCan_Put(&txMsg);
  314.             }
  315.             //printf("Y: %d X: %d\n", adyval, adxval);
  316.         }
  317.         else if (rxbufidx > 3)
  318.         {
  319.             pushStatus = 2;
  320.             //printf("Released %d\n", rxbufidx);
  321.             parseBuffer(0, rxbufidx-1);
  322.             /*for (uint8_t i = 0; i < rxbufidx; i++)
  323.             {
  324.                 printf("%3d ", touchBuffer[i].x);
  325.             }
  326.             printf("\n");
  327.             for (uint8_t i = 0; i < rxbufidx; i++)
  328.             {
  329.                 printf("%3d ", touchBuffer[i].y);
  330.             }
  331.             printf("\n");
  332.             */
  333.            
  334.             rxbufidx = 0;
  335.         }
  336.         else
  337.         {
  338.             pushStatus = 2;
  339.         }
  340.        
  341.         if (pushStatus == 2)
  342.         {
  343.             pushStatus = 0;
  344.            
  345.             StdCan_Msg_t txMsg;
  346.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  347.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  348.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  349.             txMsg.Header.ModuleId = sns_Touch_ID;
  350.             txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_RAW;
  351.             txMsg.Length = 3;
  352.             txMsg.Data[0] = CAN_MODULE_ENUM_TOUCH_RAW_STATUS_RELEASED;
  353.             txMsg.Data[1] = adxval;
  354.             txMsg.Data[2] = adyval;
  355.  
  356.             StdCan_Put(&txMsg);
  357.         }
  358.     }
  359. }
  360.  
  361.  
  362. void sns_Touch_HandleMessage(StdCan_Msg_t *rxMsg)
  363. {
  364.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  365.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  366.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  367.         rxMsg->Header.ModuleId == sns_Touch_ID)
  368.     {
  369.         switch (rxMsg->Header.Command)
  370.         {
  371.         case CAN_CMD_MODULE_DUMMY:
  372.         ///TODO: Do something dummy
  373.         break;
  374.         }
  375.     }*/
  376. }
  377.  
  378. void sns_Touch_List(uint8_t ModuleSequenceNumber)
  379. {
  380.     StdCan_Msg_t txMsg;
  381.    
  382.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  383.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  384.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  385.     txMsg.Header.ModuleId = sns_Touch_ID;
  386.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  387.     txMsg.Length = 6;
  388.  
  389.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  390.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  391.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  392.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  393.    
  394.     txMsg.Data[4] = NUMBER_OF_MODULES;
  395.     txMsg.Data[5] = ModuleSequenceNumber;
  396.    
  397.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  398. }
  399.