SHARE
TWEET

app_cy_touch_button.c

tranthudo Jun 26th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * app_cy_touch_button.c
  3.  *
  4.  *  Created on: Apr 7, 2019
  5.  *      Author: Thinh Nguyen
  6.  */
  7.  
  8. #ifndef APP_CY_TOUCH_BUTTON_C_
  9. #define APP_CY_TOUCH_BUTTON_C_
  10. #include "app_cy_touch_button.h"
  11. #include "stdlib.h"
  12. #include "zps_gen.h"
  13. #include "drv_led.h"
  14. #include "app_motor_ctrl.h"
  15. #include "app_router_dimmer.h"
  16. #include "app_reporting.h"
  17. #ifndef TRACE_APP_CY
  18. #define TRACE_APP_CY TRUE
  19. #endif
  20. #define TRAN_SLIDER(target_) g_pSlider->lastState = g_pSlider->state; g_pSlider->state = (uint8_t)(target_)
  21. #define TRAN_BUTTON(target_) (g_pButton->state = (uint8_t)(target_))
  22. Slider g_Slider;
  23. Slider* g_pSlider = (Slider*)&g_Slider;
  24. Button* g_pButton;
  25. uint32  g_u32ZeroDetectFalseCount = 0;
  26. bool bI2CInitedOK = FALSE;
  27.  
  28. /****************************************************************************
  29.  *
  30.  * NAME: APP_bI2CInitalise
  31.  *
  32.  * DESCRIPTION:
  33.  * I2C Initialization
  34.  *
  35.  * PARAMETER: void
  36.  *
  37.  * RETURNS: bool
  38.  *
  39.  ****************************************************************************/
  40. PUBLIC bool_t APP_bI2CInitalise(void) //
  41. {
  42.     DBG_vPrintf(TRUE,"APP_bI2CInitalise thinhnt13 \n");
  43.     //Slider_Init(g_pSlider);
  44.     Host_Init();;
  45.     APP_cbTimerI2C(NULL);
  46.  
  47.     return TRUE;
  48. }
  49.  
  50. PUBLIC void Button_Init(Button *but)
  51. {
  52.     but->state = BUTTON_NONTOUCH_STATE;
  53.     but->last_state = BUTTON_NONTOUCH_STATE;
  54.     but->timeout = APP_CY_BUTTON_TIMEOUT_MS;
  55. }
  56.  
  57. /**
  58.  * Turn on/off slider because pressing down button
  59.  * @param slider slider itself
  60.  * @param bOn Turn on if TRUE, turn off if FALSE
  61.  */
  62. PRIVATE void APP_Slider_TurnOnOff(Slider* slider, bool_t bOn)
  63. {
  64.     //  TODO: Continue to develope & test
  65.     // TODO: Update the LED according to cur_slide position
  66.     if (bOn) {
  67.         // Change slider position to
  68.         slider->cur_pos = slider->default_pos;
  69.         slider->u8OnState = 1;
  70.         TRAN_SLIDER(SLIDING_STOP_STATE);
  71. //      APP_SET_DIM_PULSE_ON;
  72. //      APP_vGetDimmerPeriod(slider);
  73.         // Enable Zerodetect interrupt
  74.         g_ZeroDetectEnable = TRUE;
  75.         vAHI_DioInterruptEnable(APP_ZERO_DETECT_MASK, 0);
  76.     } else {
  77.         g_ZeroDetectEnable = FALSE;
  78.         slider->state = SLIDING_OFF_STATE;
  79.         slider->u8OnState = 0;
  80.         slider->cur_pos = slider->min_pos;
  81.         vAHI_DioInterruptEnable(0, APP_ZERO_DETECT_MASK);
  82.         vAHI_TimerDisable(E_AHI_TIMER_2);
  83.         APP_SET_DIM_PULSE_OFF;
  84. //      APP_vGetDimmerPeriod(slider);
  85.     }
  86. }
  87.  
  88.  
  89. PUBLIC void Slider_Init(Slider *slider)
  90. {
  91.  
  92.     slider->state = SLIDING_OFF_STATE;
  93.     slider->u8OnState = 0;
  94.     slider->u8LastOnState = 0;
  95.     slider->timeout = APP_CY_SLIDER_TIMEOUT_MS;
  96.     slider->min_change = APP_CY_SLIDER_MIN_CHANGE;
  97.  
  98.     slider->max_change = APP_CY_SLIDER_MAX_CHANGE;
  99.     slider->max_pos = APP_CY_SLIDER_MAX_VAL;
  100.     slider->min_pos = APP_CY_SLIDER_MIN_VAL;
  101.     slider->default_pos = APP_CY_SLIDER_DEFAULT_VALUE;
  102.     slider->cur_pos = slider->min_pos; // off state
  103.     slider->last_pos = slider->max_pos;
  104.     vAHI_DioInterruptEnable(0, APP_ZERO_DETECT_MASK);
  105.     vAHI_TimerDisable(E_AHI_TIMER_2);
  106.     APP_SET_DIM_PULSE_OFF;
  107.     APP_vGetDimmerPeriod(slider);
  108. //  APP_Slider_TurnOnOff(slider, FALSE);
  109.  
  110. }
  111.  
  112. PRIVATE void App_vPrintSensorStatus(CY8CMBR3xxx_SENSORSTATUS sensorStatus)
  113. {
  114.     DBG_vPrintf(TRACE_APP_CY, "=======CY8CMBR3xxx_SENSORSTATUS====== \r\n");
  115.     DBG_vPrintf(TRACE_APP_CY, "buttonStatus = 0x%x\r\n", sensorStatus.buttonStatus);
  116.     DBG_vPrintf(TRACE_APP_CY, "latchedButtonStatus = 0x%x\r\n", sensorStatus.latchedButtonStatus);
  117.  
  118.     DBG_vPrintf(TRACE_APP_CY, "proxStatus = 0x%x\r\n", sensorStatus.proxStatus);
  119.  
  120.     DBG_vPrintf(TRACE_APP_CY, "latchedProxStatus = 0x%x\r\n", sensorStatus.latchedProxStatus);
  121.     DBG_vPrintf(TRACE_APP_CY, "slider1Position = 0x%x\r\n", sensorStatus.slider1Position);
  122.     DBG_vPrintf(TRACE_APP_CY, "liftoffSlider1Position = 0x%x\r\n", sensorStatus.liftoffSlider1Position);
  123.     DBG_vPrintf(TRACE_APP_CY, "slider2Position = 0x%x\r\n", sensorStatus.slider2Position);
  124.     DBG_vPrintf(TRACE_APP_CY, "liftoffSlider2Position = 0x%x\r\n", sensorStatus.liftoffSlider2Position);
  125. }
  126. PRIVATE void TurnOnDimmerLED(void* param) {
  127.     APP_LED_vTurnOn(BOARD_LED_CS01_PIN);
  128.     APP_LED_vTurnOff(BOARD_LED_CS02_PIN);
  129.     //APP_vSliderDisplay((uint16)sDimmerLight.sLevelControlServerCluster.u8CurrentLevel*100/254);
  130. }
  131. PRIVATE void TurnOffDimmerLED(void* param) {
  132.     APP_LED_vTurnOn(BOARD_LED_CS02_PIN);
  133.     APP_LED_vTurnOff(BOARD_LED_CS01_PIN);
  134.     //APP_vSliderDisplay((uint16)sDimmerLight.sLevelControlServerCluster.u8CurrentLevel*100/254);
  135. }
  136. PUBLIC void APP_vGetDimmerPeriod(Slider* pSlider)
  137.  
  138. {
  139. #if 1
  140. #define A1 -0.4333
  141. #define B1 93
  142. #define A22 -0.0005
  143. #define A21 -0.0594
  144. #define A20 81.885
  145. #define A3 -1.5
  146. #define B3 379.5
  147. #else
  148. #define A4 6e-8
  149. #define A3 -2e-05
  150. #define A2 -0.0007
  151. #define A1 -0.274
  152. #define A0 90.931
  153.  
  154. #endif
  155.     static uint8_t u8Pers[11] = {100, 80, 70,  65, 60, 55, 50, 45, 30, 10, 0};
  156.     bool bReported;
  157.     uint16_t u16Idx= (pSlider->cur_pos*100/pSlider->max_pos)/10;
  158.     pSlider->u8Per = u8Pers[u16Idx];
  159.     float X = (float)pSlider->cur_pos;
  160.     float X2 = X*X;
  161.     float X3 = X2*X;
  162.     float X4 = X3*X;
  163.     float fPer;
  164. //  fPer = A4*X4+ A3*X3 + A2*X2+ A1*X + A0;
  165.     if (pSlider->cur_pos < 30) {
  166.         fPer = A1*X+B1;
  167.     } else if (pSlider->cur_pos > 220) {
  168.         fPer = A3*X+B3;
  169.     } else {
  170.         fPer = A22*X2+A21*X+A20;
  171.     }
  172.     if (fPer > 100)
  173.         fPer = 100;
  174.     if (fPer < 0)
  175.         fPer = 0;
  176.     g_u16Per = 6*(fPer);
  177. //  DBG_vPrintf(TRACE_APP_CY, "pSlider->cur_pos = %d, fPer = %d, g_u16Per = %d\r\n", pSlider->cur_pos, (int)fPer, g_u16Per);
  178.  
  179.     if ( (pSlider->u8OnState != pSlider->u8LastOnState) ||
  180.             (pSlider->u8OnState && pSlider->cur_pos != pSlider->last_pos))
  181.     {
  182.         if (pSlider->u8OnState != pSlider->u8LastOnState)
  183.         {
  184.             g_u16RealPer = g_u16Per;
  185.             // something change
  186.             sDimmerLight.sOnOffServerCluster.bOnOff = pSlider->u8OnState;
  187.             pSlider->u8LastOnState = pSlider->u8OnState;
  188.             bReported = bSendImmediateReportOnOff(ROUTER_DIMMER_SW1_ENDPOINT);
  189.             if (pSlider->u8OnState)
  190.             {
  191.                 if (pSlider->state == SLIDING_STOP_STATE) {
  192.                     bReported = bSendImmediateReportLevel(ROUTER_DIMMER_SW1_ENDPOINT);
  193.                 }
  194.             }
  195.         }
  196.         sDimmerLight.sLevelControlServerCluster.u8CurrentLevel = pSlider->cur_pos;
  197.         DBG_vPrintf(TRACE_APP_CY, "APP_vGetDimmerPeriod: sDimmerLight bOnOff =  %d.\r\n", sDimmerLight.sOnOffServerCluster.bOnOff);
  198.         DBG_vPrintf(TRACE_APP_CY, "APP_vGetDimmerPeriod: sDimmerLight u8CurrentLevel =  %d.\r\n", sDimmerLight.sLevelControlServerCluster.u8CurrentLevel);
  199.         pSlider->last_pos = pSlider->cur_pos;
  200.  
  201.         // Display Leds Here
  202.         if (!App_bIsEnodeStateRunning())
  203.         {
  204.             DBG_vPrintf(TRACE_APP_CY, "\r\nAPP_vGetDimmerPeriod: App_bIsEnodeState is not running\r\n");
  205.             if (pSlider->u8LastOnState) {
  206.                     APP_LED_vTurnOff(BOARD_LED_CS02_PIN);
  207.                     APP_LED_vBlink(BOARD_LED_CS01_PIN,3000, 500, TurnOnDimmerLED,NULL);
  208.             } else {
  209.  
  210.                 APP_LED_vTurnOff(BOARD_LED_CS01_PIN);
  211.                 APP_LED_vBlink(BOARD_LED_CS02_PIN,3000, 500, TurnOffDimmerLED,NULL);
  212.             }
  213.         } else {
  214.             if (pSlider->u8LastOnState) {
  215.                 TurnOnDimmerLED(NULL);
  216.             } else {
  217.                 TurnOffDimmerLED(NULL);
  218.             }
  219.         }
  220.     }
  221.     else if (pSlider->u8OnState)
  222.     {
  223.         if (pSlider->state == SLIDING_STOP_STATE) {
  224.             bReported = bSendImmediateReportLevel(ROUTER_DIMMER_SW1_ENDPOINT);
  225.         }
  226.     }
  227.     APP_vSliderDisplay((uint16)sDimmerLight.sLevelControlServerCluster.u8CurrentLevel*100/254);
  228. //  DBG_vPrintf(TRACE_APP_CY, "g_u16Per = %d\r\n", g_u16Per);
  229. }
  230.  
  231. PUBLIC void APP_Slider_Dispatch(Slider* slider, APP_tsEvent const evt)
  232. {
  233.     DBG_vPrintf(TRACE_APP_CY, "APP_Slider_Dispatch slider->state = %d evt.eType = %d\r\n",
  234.             slider->state, evt.eType);
  235.     uint16 u16Diff = abs(evt.uEvent.sSlider.u16SliderValue - slider->cur_pos);
  236.     static uint8 u8DevChangingDirection = 1;
  237.     switch (slider->state) {
  238.     case SLIDING_STOP_STATE:
  239.         // if CS0 button is down
  240.         if (evt.eType == APP_E_EVENT_BUTTON_DOWN)
  241.         {
  242.             if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_CS0 ||
  243.                     evt.uEvent.sButton.u8Button == APP_E_BUTTONS_BUTTON_HI_BUZ_N // for test only
  244.             )
  245.             {
  246.                 // Change state of button from ON --> OFF
  247.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] should change from ON --> OFF because CS0 \r\n");
  248.                 APP_Slider_TurnOnOff(slider, FALSE);
  249.             }
  250.             /* For testing only*/
  251.             else if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_DEV1)
  252.             {
  253.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] DEV1 BUTTON pressed\r\n");
  254.                 if (u8DevChangingDirection == 1 && slider->cur_pos == slider->max_pos) {
  255.                     u8DevChangingDirection = 0;
  256.                 } else if (u8DevChangingDirection == 0 && slider->cur_pos == 0) {
  257.                     u8DevChangingDirection = 1;
  258.                 }
  259.                 if (u8DevChangingDirection == 1) {
  260.                     slider->cur_pos += slider->max_change;
  261.                     if (slider->cur_pos > slider->max_pos)
  262.                         slider->cur_pos = slider->max_pos;
  263.                 } else {
  264.                     slider->cur_pos -= slider->max_change;
  265.                     if (slider->cur_pos < slider->min_pos)
  266.                         slider->cur_pos = 0;
  267.                 }
  268.             }
  269.             /* Pressing CS1 (+) Button*/
  270.             else if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_CS1)
  271.             {
  272.                 app_motor_start();
  273.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] CS1 BUTTON pressed\r\n");
  274.                 APP_LED_vBlink(BOARD_LED_CS11_PIN, 500, 500, NULL, NULL);//(LED_CS11, 1, 0, 500,500, NULL, NULL);
  275.                 slider->cur_pos += slider->max_change;
  276.                 if (slider->cur_pos > slider->max_pos)
  277.                     slider->cur_pos = slider->max_pos;
  278.             }
  279.             /* Pressing CS2 (-) Button*/
  280.             else if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_CS2)
  281.             {
  282.                 app_motor_start();
  283.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] CS2 BUTTON pressed\r\n");
  284. //              DRV_LEDS_vBlinkLed(LED_CS22, 1);
  285.                 APP_LED_vBlink(BOARD_LED_CS11_PIN, 500, 500, NULL, NULL);
  286.                 slider->cur_pos -= slider->max_change;
  287.                 if (slider->cur_pos <= slider->min_pos)
  288.                     slider->cur_pos = slider->min_pos;
  289.             }
  290.             else if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_BUTTON_INFO)
  291.             {
  292.                 vSendImmediateReport(ROUTER_DIMMER_SW1_ENDPOINT);
  293.             }
  294.         }
  295.         else if (evt.eType == APP_E_EVENT_SLIDER_UPDATE) {
  296.             // added: 2019.04.20: Khong chuyen thanh sliding state vi touch dau thi cho
  297.             // chuyen trang thai slide den do
  298.             // TODO: Do something, change the direction slider's led & dimmer value
  299.             if ( evt.uEvent.sSlider.u16SliderValue != 255) {
  300. //              slider->cur_pos = CentroidJitterFilter1(evt.uEvent.sSlider.u16SliderValue);
  301.                 TRAN_SLIDER(SLIDING_STATE);
  302.                 slider->cur_pos = evt.uEvent.sSlider.u16SliderValue;
  303.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] Update  sliding pos: %d\r\n", evt.uEvent.sSlider.u16SliderValue);
  304. #if 0
  305.                 // if not button press
  306.                 if (u16Diff <= slider->min_change)
  307.                 {
  308.                     // Change state to SLIDING_STATE
  309.                     TRAN_SLIDER(SLIDING_STATE);
  310.                     // TODO: Do something, change the direction slider's led & dimmer value
  311.                     slider->cur_pos = evt.uEvent.sSlider.u16SliderValue;
  312.                     DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] Update sliding pos: %d\r\n", evt.uEvent.sSlider.u16SliderValue);
  313.                 } else {
  314.                     DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] Sliding remain STOP, diff = %d\r\n", u16Diff);
  315.                 }
  316. #endif
  317.             } else {
  318.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] Slider released pos: %d\r\n", evt.uEvent.sSlider.u16SliderValue);
  319.             }
  320.         }
  321.         else if (evt.eType == APP_E_EVENT_SLIDER_APP_OFF)
  322.         {
  323.             // Change state of button from ON --> OFF
  324.             DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] should change from ON --> OFF because APP_OFF \r\n");
  325.             APP_Slider_TurnOnOff(slider, FALSE);
  326.         }
  327.         else if (evt.eType == APP_E_EVENT_SLIDER_APP_UPDATE_LEVEL)
  328.         {
  329.             // Change state of button from ON --> OFF
  330.             DBG_vPrintf(TRACE_APP_CY, "[Slider][STOP] changing level of dimmer because APP_UPDATE_LEVEL\r\n");
  331.             slider->cur_pos = evt.uEvent.sSlider.u16SliderValue;
  332.         }
  333.         break;
  334.     case SLIDING_STATE:
  335.         // if CS0/DEV1 button is down
  336.         if (evt.eType == APP_E_EVENT_BUTTON_DOWN)
  337.         {
  338.             if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_CS0 ||
  339.                     evt.uEvent.sButton.u8Button == APP_E_BUTTONS_BUTTON_HI_BUZ_N // for test only
  340.             ) {
  341.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][SLIDING] should change from ON --> OFF \r\n");
  342.                 APP_Slider_TurnOnOff(slider, FALSE);
  343.             }
  344.             /* For testing only*/
  345.             else if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_DEV1) {
  346.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][SLIDING] DEV1 BUTTON pressed\r\n");
  347.             }
  348.         }
  349.         else if (evt.eType == APP_E_EVENT_SLIDER_UPDATE) {
  350.             if ( evt.uEvent.sSlider.u16SliderValue != 255) {
  351.                 // Change state to SLIDING_STATE
  352.                 TRAN_SLIDER(SLIDING_STATE);
  353.                 // TODO: Do something, change the direction slider's led & dimmer value
  354.                 slider->cur_pos = evt.uEvent.sSlider.u16SliderValue;
  355. //              slider->cur_pos = CentroidJitterFilter1( evt.uEvent.sSlider.u16SliderValue);
  356.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][SLIDING] Update sliding pos: %d\r\n", evt.uEvent.sSlider.u16SliderValue);
  357.             } else {
  358.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][SLIDING] Changing to STOP, diff = %d\r\n", u16Diff);
  359.                 TRAN_SLIDER(SLIDING_STOP_STATE);
  360.             }
  361.         }
  362.         else if (evt.eType == APP_E_EVENT_SLIDER_APP_OFF)
  363.         {
  364.             // Change state of button from ON --> OFF
  365.             DBG_vPrintf(TRACE_APP_CY, "[Slider][SLIDING] should change from ON --> OFF because APP_OFF \r\n");
  366.             APP_Slider_TurnOnOff(slider, FALSE);
  367.         }
  368.         break;
  369.     case SLIDING_OFF_STATE:
  370.         // if CS0 button is down
  371.         if (evt.eType == APP_E_EVENT_BUTTON_DOWN)
  372.         {
  373.             if (evt.uEvent.sButton.u8Button == APP_E_BUTTONS_CY_BUTTON_CS0
  374.                     || evt.uEvent.sButton.u8Button == APP_E_BUTTONS_BUTTON_HI_BUZ_N // for test only
  375.             ) {
  376.                 DBG_vPrintf(TRACE_APP_CY, "[Slider][OFF] should change from OFF --> ON \r\n");
  377.                 APP_Slider_TurnOnOff(slider, TRUE);
  378.             }
  379.         }
  380.         else if (evt.eType == APP_E_EVENT_SLIDER_APP_ON )
  381.         {
  382.             //Change state of button from ON --> OFF
  383.             DBG_vPrintf(TRACE_APP_CY, "[Slider][OFF] should change from OFF --> ON Because APP_ON \r\n");
  384.             APP_Slider_TurnOnOff(slider, TRUE);
  385.         }
  386.         break;
  387.     default:
  388.         DBG_vPrintf(TRACE_APP_CY, "[Slider]Nothing change, diff = %d\r\n", u16Diff);
  389.         break;
  390.     }
  391.     APP_vGetDimmerPeriod(slider);
  392.  
  393.     DBG_vPrintf(TRACE_APP_CY, "Slider->cur_pos =  %d, g_u16Per=%d\r\n", slider->cur_pos, g_u16Per);
  394. }
  395. /**
  396.  * This scanning I2C port to get button status and slider's position.
  397.  */
  398.  
  399.  
  400. PUBLIC void APP_cbTimerI2C(void *pvParam)
  401. {
  402. #define REAL_AC_PLUG TRUE // khi cam vao AC220 thi g_ZerodetectEnable moi co tac dung
  403.     if (g_ZeroDetectEnable && REAL_AC_PLUG && g_u32ZeroDetectFalseCount++ < 5)
  404.     {
  405.         ZTIMER_eStop(u8TimerI2C);
  406.         ZTIMER_eStart(u8TimerI2C, ZTIMER_TIME_MSEC(APP_CY_I2C_UPDATE_PERIOD_MS));
  407.         return;
  408.     }
  409.     ZTIMER_eStop(u8TimerI2C);
  410.     APP_SliderQuery();
  411.  
  412.     ZTIMER_eStart(u8TimerI2C, ZTIMER_TIME_MSEC(APP_CY_I2C_UPDATE_PERIOD_MS));
  413. //#endif
  414. }
  415.  
  416. uint16 CentroidJitterFilter1(uint16 wSliderCentroid)
  417. {
  418.     static uint16 wPrevSliderCentroid = 0;
  419.     int wDiffSliderCentroid;
  420.     //calculate the difference between the previous and present centroid positions.
  421.     wDiffSliderCentroid = wSliderCentroid - wPrevSliderCentroid;
  422.  
  423.     //if the present Centroid position is incremented by Jitter threshold from previous then decrement the
  424.     //centroid position by Jitter Threshold and viceversa.
  425.     if((1 < wDiffSliderCentroid) || (1 == wDiffSliderCentroid))
  426.     {//slider centroid position is incremented by Jitter Threshold from previous value so decrement the centroid by Jitter Threshold.
  427.         wSliderCentroid = wSliderCentroid - 1;
  428.     }
  429.     else if((-1 > wDiffSliderCentroid) || (-1 == wDiffSliderCentroid))
  430.     {//current slider position is decreased by Jitter Threshold from previous value so increment the centroid by Jitter Threshold
  431.         wSliderCentroid = wSliderCentroid + 1;
  432.     }
  433.  
  434.     //update the previous centroid position with current centroid position.
  435.     wPrevSliderCentroid = wSliderCentroid;
  436.     return wSliderCentroid;
  437. }
  438.  
  439. uint16 CentroidJitterFilter(uint16 wSliderCentroid)
  440. {
  441. #define NSAMPLE 8
  442.     static uint16 i = 0;
  443.     int j;
  444.     static uint16 u16Sliders[NSAMPLE]  ={0};
  445.     static uint16 u16Ret = 0;
  446.     u16Sliders[i] = wSliderCentroid;
  447. //  DBG_vPrintf(TRACE_APP_CY, "====> wSliderCentroid=%d\n", wSliderCentroid);
  448.     if (wSliderCentroid == 255)
  449.     {
  450.  
  451.         if (i == 0) {
  452.             u16Ret = 255;
  453.             return u16Ret;
  454.         } else {
  455.             u16Ret = 0;
  456.             for (j= 0; j < i; j++)
  457.             {
  458.                 u16Ret += u16Sliders[j];
  459.             }
  460.             u16Ret /= i;
  461.             i = 0;
  462.             return u16Ret;
  463.         }
  464.     }
  465.     if (i < NSAMPLE) {
  466.         i++;
  467.     } else {
  468.         u16Ret = 0;
  469.         for (j= 0; j < i; j++)
  470.         {
  471. //          if (u16Sliders[i] > u16Ret)
  472.             u16Ret += u16Sliders[j];
  473.         }
  474.         u16Ret /= i;
  475.         i = 0;
  476.     }
  477. //  DBG_vPrintf(TRACE_APP_CY, "====> u16Ret=%d\n", u16Ret);
  478.     return u16Ret;
  479. }
  480.  
  481. PUBLIC void APP_SliderQuery(void)
  482. {
  483. #if JN5169_CY_ENABLE
  484.     uint16_t u16CS0State = 0, u16Dev1State = 0, u16SliderState = 0, u16CS1State = 0, u16CS2State = 0;
  485.     static uint16_t u16CS0LastState = 0, u16Dev1LastState = 0, u16SliderLastState = 0, u16CS1LastState = 0, u16CS2LastState = 0;
  486. //  static bool bFirstTime = TRUE;
  487.  
  488.     if (!bI2CInitedOK) {
  489.         return;
  490.     }
  491.  
  492.     CY8CMBR3xxx_SENSORSTATUS sensorStatus;
  493. //  CY8CMBR3xxx_SENSORDATA sensorDbgData;
  494.     if (TRUE == CY8CMBR3xxx_ReadSensorStatus(SLAVE_ADDRESS, &sensorStatus))
  495.     {
  496. //      App_vPrintSensorStatus(sensorStatus);
  497.         u16CS0State = (sensorStatus.buttonStatus>>APP_CY_BUTTON_CS0_BIT) & 0x01;
  498.         u16CS1State = (sensorStatus.buttonStatus>>APP_CY_BUTTON_CS1_BIT) & 0x01;
  499.         u16CS2State = (sensorStatus.buttonStatus>>APP_CY_BUTTON_CS2_BIT) & 0x01;
  500.         u16Dev1State = (sensorStatus.buttonStatus>>APP_CY_BUTTON_DEV1_BIT) & 0x01;
  501. //      u16SliderState = sensorStatus.slider1Position;
  502.         u16SliderState = CentroidJitterFilter(sensorStatus.slider1Position);
  503.         if (u16CS0State != u16CS0LastState)
  504.         {
  505.             app_motor_start();
  506.             APP_tsEvent sButtonEvent;
  507.             if (u16CS0State)
  508.             {
  509.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_DOWN;
  510.             } else {
  511.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_UP;
  512.             }
  513.             sButtonEvent.uEvent.sButton.u8Button = APP_E_BUTTONS_CY_BUTTON_CS0;
  514.             DBG_vPrintf(TRACE_APP_CY, "Button  CS0 DN=%d\n", APP_E_BUTTONS_CY_BUTTON_CS0);
  515.             if(ZQ_bQueueSend(&APP_msgAppEvents, &sButtonEvent) == FALSE)
  516.             {
  517.                 DBG_vPrintf(TRACE_APP_CY, "Button CS0: Failed to post Event %d  \n", sButtonEvent.eType);
  518.             }
  519.             u16CS0LastState = u16CS0State;
  520.         }
  521.         if (u16CS1State != u16CS1LastState)
  522.         {
  523.             APP_tsEvent sButtonEvent;
  524.             if (u16CS1State)
  525.             {
  526.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_DOWN;
  527.             } else {
  528.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_UP;
  529.             }
  530.             sButtonEvent.uEvent.sButton.u8Button = APP_E_BUTTONS_CY_BUTTON_CS1;
  531.             DBG_vPrintf(TRACE_APP_CY, "Button CS1 DN=%d\n", APP_E_BUTTONS_CY_BUTTON_CS1);
  532.             if(ZQ_bQueueSend(&APP_msgAppEvents, &sButtonEvent) == FALSE)
  533.             {
  534.                 DBG_vPrintf(TRACE_APP_CY, "Button CS1: Failed to post Event %d \n", sButtonEvent.eType);
  535.             }
  536.             u16CS1LastState = u16CS1State;
  537.         }
  538.  
  539.         if (u16CS2State != u16CS2LastState)
  540.         {
  541.             APP_tsEvent sButtonEvent;
  542.             if (u16CS2State)
  543.             {
  544.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_DOWN;
  545.             } else {
  546.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_UP;
  547.             }
  548.             sButtonEvent.uEvent.sButton.u8Button = APP_E_BUTTONS_CY_BUTTON_CS2;
  549.             DBG_vPrintf(TRACE_APP_CY, "Button CS2 DN=%d\n", APP_E_BUTTONS_CY_BUTTON_CS2);
  550.             if(ZQ_bQueueSend(&APP_msgAppEvents, &sButtonEvent) == FALSE)
  551.             {
  552.                 DBG_vPrintf(TRACE_APP_CY, "Button CS2: Failed to post Event %d \n", sButtonEvent.eType);
  553.             }
  554.             u16CS2LastState = u16CS2State;
  555.         }
  556.  
  557.         if (u16Dev1State != u16Dev1LastState)
  558.         {
  559.             APP_tsEvent sButtonEvent;
  560.             if (u16Dev1State)
  561.             {
  562.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_DOWN;
  563.             } else {
  564.                 sButtonEvent.eType = APP_E_EVENT_BUTTON_UP;
  565.             }
  566.             sButtonEvent.uEvent.sButton.u8Button = APP_E_BUTTONS_CY_BUTTON_DEV1;
  567.             DBG_vPrintf(TRACE_APP_CY, "Button DEV1 DN=%d\n", APP_E_BUTTONS_CY_BUTTON_CS0);
  568.             if(ZQ_bQueueSend(&APP_msgAppEvents, &sButtonEvent) == FALSE)
  569.             {
  570.                 DBG_vPrintf(TRACE_APP_CY, "Button DEV1: Failed to post Event %d \n", sButtonEvent.eType);
  571.             }
  572.             u16Dev1LastState = u16Dev1State;
  573.         }
  574.         if (u16SliderState != u16SliderLastState)
  575.         {
  576.             APP_tsEvent sSliderEvent;
  577.             sSliderEvent.eType = APP_E_EVENT_SLIDER_UPDATE;
  578.             sSliderEvent.uEvent.sSlider.u16SliderValue = u16SliderState;
  579.             DBG_vPrintf(TRACE_APP_CY, "Slider Update VAL=%d\n", u16SliderState);
  580.             if(ZQ_bQueueSend(&APP_msgAppEvents, &sSliderEvent) == FALSE)
  581.             {
  582.                 DBG_vPrintf(TRACE_APP_CY, "Slider: Failed to post Event %d \n", sSliderEvent.eType);
  583.             }
  584.             u16SliderLastState = u16SliderState;
  585.         }
  586.     }
  587. #else
  588.     return;
  589. #endif
  590. }
  591.  
  592.  
  593. #endif /* APP_CY_TOUCH_BUTTON_C_ */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top