Subversion Repositories HomeAutomation

Rev

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