Subversion Repositories HomeAutomation

Rev

Rev 1910 | 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. point lastValidPoint;
  7. uint8_t timeCnt;
  8.  
  9.  
  10. #if sns_Touch_USEEEPROM==1
  11. #include "sns_Touch_eeprom.h"
  12. struct eeprom_sns_Touch EEMEM eeprom_sns_Touch =
  13. {
  14.     {
  15.         ///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.
  16.         0xAB,   // x
  17.         0x1234  // y
  18.     },
  19.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  20. };
  21. #endif
  22.  
  23. void sns_Touch_Init(void)
  24. {
  25. #if sns_Touch_USEEEPROM==1
  26.     if (EEDATA_OK)
  27.     {
  28.       ///TODO: Use stored data to set initial values for the module
  29.       blablaX = eeprom_read_byte(EEDATA.x);
  30.       blablaY = eeprom_read_word(EEDATA.y);
  31.     } else
  32.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  33.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  34.       eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC);
  35.       EEDATA_UPDATE_CRC;
  36.     }
  37. #endif  
  38.     ///TODO: Initialize hardware etc here
  39.  
  40.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  41.     // Pcint_SetCallbackPin(sns_Touch_PCINT, EXP_C , &sns_Touch_pcint_callback);
  42.  
  43.     ADC_Init();
  44.     Timer_SetTimeout(sns_Touch_POLL_TIMER, sns_Touch_POLL_PERIOD , TimerTypeFreeRunning, 0);
  45.    
  46.     rxbufidx = 0;
  47.     pushStatus = 0;
  48.     lastValidPoint.x=0;
  49.     lastValidPoint.y=0;
  50.     timeCnt=0;
  51. }
  52.  
  53. void sns_Touch_Process(void)
  54. {
  55.    
  56.     if (Timer_Expired(sns_Touch_POLL_TIMER)) {
  57.         timeCnt++;
  58.         //StdCan_Msg_t txMsg;
  59.         gpio_set_in(sns_Touch_XPLUS);
  60.         gpio_set_pullup(sns_Touch_XPLUS);   //turn on pullup for x+
  61.         gpio_set_out(sns_Touch_YPLUS);
  62.         gpio_set_pin(sns_Touch_YPLUS);      //turn on 1 on y+
  63.         gpio_set_out(sns_Touch_YMINUS);
  64.         gpio_clr_pin(sns_Touch_YMINUS);     //turn on 0 on y-
  65. #ifdef sns_Touch_SWITCH_XY
  66.             uint8_t adyval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
  67. #else
  68.             uint8_t adxval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
  69. #endif
  70.        
  71.         gpio_set_in(sns_Touch_YPLUS);
  72.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  73.         gpio_set_in(sns_Touch_YMINUS);
  74.         gpio_clr_pullup(sns_Touch_YMINUS);  //turn off pullup for y-
  75.         gpio_set_in(sns_Touch_XPLUS);
  76.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  77.        
  78.         gpio_set_in(sns_Touch_YPLUS);
  79.         gpio_set_pullup(sns_Touch_YPLUS);   //turn on pullup for y+
  80.         gpio_set_out(sns_Touch_XPLUS);
  81.         gpio_set_pin(sns_Touch_XPLUS);      //turn on 1 on x+
  82.         gpio_set_out(sns_Touch_XMINUS);
  83.         gpio_clr_pin(sns_Touch_XMINUS);     //turn on 0 on x-
  84. #ifdef sns_Touch_SWITCH_XY
  85.         uint8_t adxval = ADC_Get(sns_Touch_TOUCHYAD)>>2;    //read y-
  86. #else
  87.         uint8_t adyval = ADC_Get(sns_Touch_TOUCHYAD)>>2;    //read y-
  88. #endif
  89.         gpio_set_in(sns_Touch_XPLUS);
  90.         gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
  91.         gpio_set_in(sns_Touch_XMINUS);
  92.         gpio_clr_pullup(sns_Touch_XMINUS);  //turn off pullup for x-
  93.         gpio_set_in(sns_Touch_YPLUS);
  94.         gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
  95.        
  96.        
  97.  
  98.         if (adyval < 0xf0 && adxval < 0xf0 )
  99.         {
  100. #ifdef sns_Touch_INVERT_Y
  101.             adyval = 255-adyval;
  102. #endif
  103. #ifdef sns_Touch_INVERT_X
  104.             adxval = 255-adxval;
  105. #endif
  106.             lastValidPoint.x=adxval;
  107.             lastValidPoint.y=adyval;
  108.             pushStatus = 1;
  109.             uint8_t xdiff=0;
  110.             uint8_t ydiff=0;
  111.             if (rxbufidx > 0)
  112.             {
  113.                 xdiff = touchBuffer[rxbufidx-1].x - adxval;
  114.                 if (touchBuffer[rxbufidx-1].x < adxval)
  115.                 {
  116.                     xdiff = adxval - touchBuffer[rxbufidx-1].x;
  117.                 }
  118.                 ydiff = touchBuffer[rxbufidx-1].y - adyval;
  119.                 if (touchBuffer[rxbufidx-1].y < adyval)
  120.                 {
  121.                     ydiff = adyval - touchBuffer[rxbufidx-1].y;
  122.                 }
  123.             }
  124.             if (rxbufidx == 0 || (rxbufidx > 0 && xdiff+ydiff > 10))
  125.             {
  126.                 touchBuffer[rxbufidx].x = adxval;
  127.                 touchBuffer[rxbufidx].y = adyval;
  128.                 rxbufidx++;
  129.                 if (rxbufidx==sns_Touch_BUFFERSIZE)
  130.                 {
  131.                     rxbufidx=0;
  132.                 }
  133.             }
  134.             if (rxbufidx == 0 || (rxbufidx > 0 && xdiff+ydiff > 10) || timeCnt%sns_Touch_POLL_SEND_RESOLUTION == 0)
  135.             {
  136.                 StdCan_Msg_t txMsg;
  137.                 StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  138.                 StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  139.                 txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  140.                 txMsg.Header.ModuleId = sns_Touch_ID;
  141.                 txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_RAW;
  142.                 txMsg.Length = 3;
  143.                 txMsg.Data[0] = CAN_MODULE_ENUM_TOUCH_RAW_STATUS_PRESSED;
  144.                 txMsg.Data[1] = adxval;
  145.                 txMsg.Data[2] = adyval;
  146.    
  147.                 StdCan_Put(&txMsg);
  148.             }
  149.             //printf("Y: %d X: %d\n", adyval, adxval);
  150.         }
  151.         else if (rxbufidx > 3)
  152.         {
  153.             if (pushStatus==1)
  154.             {
  155.                 pushStatus = 2;
  156.             }
  157.             //printf("Released %d\n", rxbufidx);
  158.             gesture functionData = parseBuffer(touchBuffer, 0, rxbufidx-1);
  159.            
  160.             StdCan_Msg_t txMsg;
  161.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  162.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  163.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  164.             txMsg.Header.ModuleId = sns_Touch_ID;
  165.             txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_GESTURE;
  166.             txMsg.Length = 3;
  167.             txMsg.Data[0] = functionData.f1;
  168.             txMsg.Data[1] = functionData.f7;
  169.             txMsg.Data[2] = functionData.f3<<7|functionData.f4<<6|functionData.f5<<5|functionData.f6<<4|functionData.f8<<3|functionData.f9<<2|functionData.f2;
  170.    
  171.             StdCan_Put(&txMsg);
  172.             /*for (uint8_t i = 0; i < rxbufidx; i++)
  173.             {
  174.                 printf("%3d ", touchBuffer[i].x);
  175.             }
  176.             printf("\n");
  177.             for (uint8_t i = 0; i < rxbufidx; i++)
  178.             {
  179.                 printf("%3d ", touchBuffer[i].y);
  180.             }
  181.             printf("\n");
  182.             */
  183.            
  184.             rxbufidx = 0;
  185.         }
  186.         else if (pushStatus==1)
  187.         {
  188.             rxbufidx = 0;
  189.             pushStatus = 2;
  190.         }
  191.        
  192.         if (pushStatus == 2)
  193.         {
  194.             pushStatus = 0;
  195.            
  196.             StdCan_Msg_t txMsg;
  197.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  198.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  199.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  200.             txMsg.Header.ModuleId = sns_Touch_ID;
  201.             txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_RAW;
  202.             txMsg.Length = 3;
  203.             txMsg.Data[0] = CAN_MODULE_ENUM_TOUCH_RAW_STATUS_RELEASED;
  204.             txMsg.Data[1] = lastValidPoint.x;
  205.             txMsg.Data[2] = lastValidPoint.y;
  206.  
  207.             StdCan_Put(&txMsg);
  208.         }
  209.     }
  210. }
  211.  
  212.  
  213. void sns_Touch_HandleMessage(StdCan_Msg_t *rxMsg)
  214. {
  215.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  216.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  217.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  218.         rxMsg->Header.ModuleId == sns_Touch_ID)
  219.     {
  220.         switch (rxMsg->Header.Command)
  221.         {
  222.         case CAN_CMD_MODULE_DUMMY:
  223.         ///TODO: Do something dummy
  224.         break;
  225.         }
  226.     }*/
  227. }
  228.  
  229. void sns_Touch_List(uint8_t ModuleSequenceNumber)
  230. {
  231.     StdCan_Msg_t txMsg;
  232.    
  233.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  234.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  235.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
  236.     txMsg.Header.ModuleId = sns_Touch_ID;
  237.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  238.     txMsg.Length = 6;
  239.  
  240.     uint32_t HwId=BIOS_GetHwId();
  241.     txMsg.Data[0] = HwId&0xff;
  242.     txMsg.Data[1] = (HwId>>8)&0xff;
  243.     txMsg.Data[2] = (HwId>>16)&0xff;
  244.     txMsg.Data[3] = (HwId>>24)&0xff;
  245.    
  246.     txMsg.Data[4] = NUMBER_OF_MODULES;
  247.     txMsg.Data[5] = ModuleSequenceNumber;
  248.    
  249.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  250. }
  251.