Subversion Repositories HomeAutomation

Rev

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