Subversion Repositories HomeAutomation

Rev

Rev 1274 | Rev 1279 | 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. uint8_t parseBuffer(uint8_t startIndex, uint8_t endIndex)
  63. {
  64.     int16_t f1_k = 0, f1_tmp = 0,f2_tmp = 0,f3_tmp = 0,f4_tmp = 0,f5_tmp = 0,f8_tmp = 0,f9_tmp = 0,int16temp;
  65.     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;
  66.    
  67.     if ((int16_t)touchBuffer[endIndex].y - (int16_t)touchBuffer[startIndex].y > 0)
  68.         f6 = 1;
  69.     else
  70.         f6 = 0;
  71.    
  72.     xMin = touchBuffer[startIndex].x;
  73.     xMax = touchBuffer[startIndex].x;
  74.     yMin = touchBuffer[startIndex].y;
  75.     yMax = touchBuffer[startIndex].y;
  76.     f3_yref = (touchBuffer[startIndex].y + touchBuffer[endIndex].y)/2;
  77.     f1_k = ((touchBuffer[endIndex].y-touchBuffer[startIndex].y)<<4)/(touchBuffer[endIndex].x-touchBuffer[startIndex].x);
  78.     f1_sign = 2;
  79.  
  80.     for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  81.     {
  82.         if (((f1_k*(touchBuffer[i].x-touchBuffer[0].x))>>4) + touchBuffer[0].y < touchBuffer[i].y) {
  83.             if (f1_sign != 1)
  84.                 f1++;
  85.             f1_sign = 1;
  86.         }
  87.         else {
  88.             if (f1_sign != 0)
  89.                 f1++;
  90.             f1_sign = 0;
  91.         }
  92.        
  93.         if (touchBuffer[i].x > min(touchBuffer[startIndex].x, touchBuffer[endIndex].x) && touchBuffer[i].x < max(touchBuffer[startIndex].x, touchBuffer[endIndex].x))
  94.         {
  95.             f3_tmp += f3_yref - (touchBuffer[i].y + touchBuffer[i-1].y)/2;
  96.         }
  97.         if (xMin > touchBuffer[i].x)
  98.             xMin = touchBuffer[i].x;
  99.         if (xMax < touchBuffer[i].x)
  100.             xMax = touchBuffer[i].x;
  101.         if (yMin > touchBuffer[i].y)
  102.             yMin = touchBuffer[i].y;
  103.         if (yMax < touchBuffer[i].y)
  104.             yMax = touchBuffer[i].y;
  105.         f4_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[endIndex].x;
  106.         f5_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[startIndex].x;
  107.     }
  108.    
  109.     for (uint8_t j = yMin; j < yMax-1; j++)
  110.     {
  111.         f7_tmp = 0xff;
  112.         f7_sign = 0;
  113.         for (uint8_t i = startIndex+1; i < endIndex-1; i++)
  114.         {
  115.             if (j > touchBuffer[i].y) {
  116.                 if (f7_sign != '+')
  117.                     f7_tmp++;
  118.                 f7_sign = '+';
  119.             }
  120.             else {
  121.                 if (f7_sign != '-')
  122.                     f7_tmp++;
  123.                 f7_sign = '-';
  124.             }
  125.         }
  126.         if (f7_tmp > f7)
  127.             f7 = f7_tmp;
  128.     }
  129.    
  130.     f1--;
  131.     f2 = f1_sign;
  132.     if (f1 == 0)
  133.         f2 = 2;
  134.    
  135.     if (f3_tmp > 0)
  136.         f3 = 0;
  137.     else
  138.         f3 = 1;
  139.        
  140.     //int16temp = (((int16_t)touchBuffer[startIndex].x - (int16_t)(xMax+xMin)/2)*((int16_t)touchBuffer[endIndex].x - (int16_t)(xMax+xMin)/2));
  141.     //int16temp = ((touchBuffer[startIndex].x - (xMax+xMin)/2)*(touchBuffer[endIndex].x - (xMax+xMin)/2));
  142.     int16temp = (touchBuffer[startIndex].x*touchBuffer[endIndex].x - (xMax+xMin)/2);
  143.     if (int16temp > 0)
  144.         f9 = 1;
  145.     else
  146.         f9 = 0;
  147.     if (f5_tmp > 0)
  148.         f5 = 1;
  149.     else
  150.         f5 = 0;
  151.     if (f4_tmp > 0)
  152.         f4 = 1;
  153.     else
  154.         f4 = 0;
  155.     if (xMin < min(touchBuffer[startIndex].x,touchBuffer[endIndex].x) || xMax > max(touchBuffer[startIndex].x,touchBuffer[endIndex].x))
  156.         f8 = 0;
  157.     else
  158.         f8 = 1;
  159.        
  160.     //printf("1%u2%c3%c4%c5%c6%c7%u8%c9%c\n", f1, f2, f3, f4, f5, f6, f7, f8, f9);
  161.  
  162.     StdCan_Msg_t txMsg;
  163.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  164.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  165.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  166.     txMsg.Header.ModuleId = sns_Touch_ID;
  167.     txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_GESTURE;
  168.     txMsg.Length = 3;
  169.     txMsg.Data[0] = f1;
  170.     txMsg.Data[1] = f7;
  171.     txMsg.Data[2] = f3<<7|f4<<6|f5<<5|f6<<4|f8<<3|f9<<2|f2;
  172.    
  173.     StdCan_Put(&txMsg);
  174.     return 0;
  175. }
  176.  
  177.  
  178. void sns_Touch_Process(void)
  179. {
  180.    
  181.     if (Timer_Expired(sns_Touch_POLL_TIMER)) {
  182.         //StdCan_Msg_t txMsg;
  183.         gpio_set_in(sns_Touch_XPLUS);
  184.         gpio_set_pullup(sns_Touch_XPLUS);   //turn on pullup for x+
  185.         gpio_set_out(sns_Touch_YPLUS);
  186.         gpio_set_pin(sns_Touch_YPLUS);      //turn on 1 on y+
  187.         gpio_set_out(sns_Touch_YMINUS);
  188.         gpio_clr_pin(sns_Touch_YMINUS);     //turn on 0 on y-
  189.         uint8_t adxval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
  190.         gpio_set_in(sns_Touch_YPLUS);
  191.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  192.         gpio_set_in(sns_Touch_YMINUS);
  193.         gpio_clr_pullup(sns_Touch_YMINUS);  //turn off pullup for y-
  194.         gpio_set_in(sns_Touch_XPLUS);
  195.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  196.        
  197.         gpio_set_in(sns_Touch_YPLUS);
  198.         gpio_set_pullup(sns_Touch_YPLUS);   //turn on pullup for y+
  199.         gpio_set_out(sns_Touch_XPLUS);
  200.         gpio_set_pin(sns_Touch_XPLUS);      //turn on 1 on x+
  201.         gpio_set_out(sns_Touch_XMINUS);
  202.         gpio_clr_pin(sns_Touch_XMINUS);     //turn on 0 on x-
  203.         uint8_t adyval = ADC_Get(sns_Touch_TOUCHYAD)>>2;    //read y-
  204.         gpio_set_in(sns_Touch_XPLUS);
  205.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  206.         gpio_set_in(sns_Touch_XMINUS);
  207.         gpio_clr_pullup(sns_Touch_XMINUS);  //turn off pullup for x-
  208.         gpio_set_in(sns_Touch_YPLUS);
  209.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  210.        
  211.         if (adyval < 0xf0 && adxval < 0xf0 )
  212.         {
  213.             uint8_t xdiff=0;
  214.             uint8_t ydiff=0;
  215.             if (rxbufidx > 0)
  216.             {
  217.                 xdiff = touchBuffer[rxbufidx-1].x - adxval;
  218.                 if (touchBuffer[rxbufidx-1].x < adxval)
  219.                 {
  220.                     xdiff = adxval - touchBuffer[rxbufidx-1].x;
  221.                 }
  222.                 ydiff = touchBuffer[rxbufidx-1].y - adyval;
  223.                 if (touchBuffer[rxbufidx-1].y < adyval)
  224.                 {
  225.                     ydiff = adyval - touchBuffer[rxbufidx-1].y;
  226.                 }
  227.             }
  228.             if (rxbufidx == 0 || (rxbufidx > 0 && xdiff+ydiff > 10))
  229.             {
  230.                 touchBuffer[rxbufidx].x = adxval;
  231.                 touchBuffer[rxbufidx].y = adyval;
  232.                 rxbufidx++;
  233.                 if (rxbufidx==sns_Touch_BUFFERSIZE)
  234.                 {
  235.                     rxbufidx=0;
  236.                 }
  237.             }
  238.             //printf("Y: %d X: %d\n", adyval, adxval);
  239.         }
  240.         else if (rxbufidx > 2)
  241.         {
  242.             //printf("Released %d\n", rxbufidx);
  243.             uint8_t temp = parseBuffer(0, rxbufidx-1);
  244.             /*for (uint8_t i = 0; i < rxbufidx; i++)
  245.             {
  246.                 printf("%3d ", touchBuffer[i].x);
  247.             }
  248.             printf("\n");
  249.             for (uint8_t i = 0; i < rxbufidx; i++)
  250.             {
  251.                 printf("%3d ", touchBuffer[i].y);
  252.             }
  253.             printf("\n");
  254.             */
  255.            
  256.             rxbufidx = 0;
  257.         }
  258.     }
  259. }
  260.  
  261.  
  262. void sns_Touch_HandleMessage(StdCan_Msg_t *rxMsg)
  263. {
  264.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  265.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  266.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  267.         rxMsg->Header.ModuleId == sns_Touch_ID)
  268.     {
  269.         switch (rxMsg->Header.Command)
  270.         {
  271.         case CAN_CMD_MODULE_DUMMY:
  272.         ///TODO: Do something dummy
  273.         break;
  274.         }
  275.     }*/
  276. }
  277.  
  278. void sns_Touch_List(uint8_t ModuleSequenceNumber)
  279. {
  280.     StdCan_Msg_t txMsg;
  281.    
  282.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  283.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  284.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  285.     txMsg.Header.ModuleId = sns_Touch_ID;
  286.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  287.     txMsg.Length = 6;
  288.  
  289.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  290.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  291.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  292.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  293.    
  294.     txMsg.Data[4] = NUMBER_OF_MODULES;
  295.     txMsg.Data[5] = ModuleSequenceNumber;
  296.    
  297.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  298. }
  299.