slider_sample.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-28 Rbb666 first version
  9. */
  10. #include <rtthread.h>
  11. #include "drv_common.h"
  12. #ifdef BSP_USING_SLIDER
  13. #include "cycfg_capsense.h"
  14. #define CAPSENSE_INTR_PRIORITY (7u)
  15. #define EZI2C_INTR_PRIORITY (6u)
  16. /* Allowed duty cycle for maximum brightness */
  17. #define LED_MAX_BRIGHTNESS (100u)
  18. /* Allowed duty cycle for minimum brightness*/
  19. #define LED_MIN_BRIGHTNESS (0u)
  20. #define GET_DUTY_CYCLE(x) (1 * 1000 * 1000 - x * 10 * 1000)
  21. typedef enum
  22. {
  23. LED_OFF,
  24. LED_ON
  25. } led_state_t;
  26. typedef struct
  27. {
  28. led_state_t state;
  29. uint32_t brightness;
  30. } led_data_t;
  31. static rt_sem_t trans_done_semphr = RT_NULL;
  32. static rt_thread_t sld_thread = RT_NULL;
  33. #ifndef RT_USING_PWM
  34. #error You need enable PWM to use this sample
  35. #else
  36. #define PWM_DEV_NAME "pwm0"
  37. #define PWM_DEV_CHANNEL 3
  38. static struct rt_device_pwm *pwm_dev;
  39. #endif
  40. static void capsense_isr(void)
  41. {
  42. /* enter interrupt */
  43. rt_interrupt_enter();
  44. Cy_CapSense_InterruptHandler(CYBSP_CSD_HW, &cy_capsense_context);
  45. /* leave interrupt */
  46. rt_interrupt_leave();
  47. }
  48. void capsense_callback(cy_stc_active_scan_sns_t *ptrActiveScan)
  49. {
  50. rt_sem_release(trans_done_semphr);
  51. }
  52. static uint32_t initialize_capsense(void)
  53. {
  54. uint32_t status = CYRET_SUCCESS;
  55. /* CapSense interrupt configuration parameters */
  56. static const cy_stc_sysint_t capSense_intr_config =
  57. {
  58. .intrSrc = csd_interrupt_IRQn,
  59. .intrPriority = CAPSENSE_INTR_PRIORITY,
  60. };
  61. /* Capture the CSD HW block and initialize it to the default state. */
  62. status = Cy_CapSense_Init(&cy_capsense_context);
  63. if (CYRET_SUCCESS != status)
  64. {
  65. return status;
  66. }
  67. /* Initialize CapSense interrupt */
  68. cyhal_system_set_isr(csd_interrupt_IRQn, csd_interrupt_IRQn, CAPSENSE_INTR_PRIORITY, &capsense_isr);
  69. NVIC_ClearPendingIRQ(capSense_intr_config.intrSrc);
  70. NVIC_EnableIRQ(capSense_intr_config.intrSrc);
  71. /* Initialize the CapSense firmware modules. */
  72. status = Cy_CapSense_Enable(&cy_capsense_context);
  73. if (CYRET_SUCCESS != status)
  74. {
  75. return status;
  76. }
  77. /* Assign a callback function to indicate end of CapSense scan. */
  78. status = Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E,
  79. capsense_callback, &cy_capsense_context);
  80. if (CYRET_SUCCESS != status)
  81. {
  82. return status;
  83. }
  84. return status;
  85. }
  86. void Slider_Init(void)
  87. {
  88. cy_rslt_t result;
  89. result = initialize_capsense();
  90. if (CYRET_SUCCESS != result)
  91. {
  92. /* Halt the CPU if CapSense initialization failed */
  93. RT_ASSERT(0);
  94. }
  95. /* Initiate first scan */
  96. Cy_CapSense_ScanAllWidgets(&cy_capsense_context);
  97. trans_done_semphr = rt_sem_create("slider_sem", 1, RT_IPC_FLAG_PRIO);
  98. if (trans_done_semphr == RT_NULL)
  99. {
  100. rt_kprintf("create transform done semphr failed.\n");
  101. RT_ASSERT(0);
  102. return;
  103. }
  104. #ifdef BSP_USING_PWM0_PORT13
  105. /* Initiate PWM*/
  106. pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
  107. if (pwm_dev == RT_NULL)
  108. {
  109. rt_kprintf("PWM init failed! can't find %s device!\n", PWM_DEV_NAME);
  110. RT_ASSERT(0);
  111. }
  112. /*default period:1ms pulse:0*/
  113. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, 1 * 1000 * 1000, 1 * 1000 * 1000);
  114. rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
  115. #endif
  116. }
  117. void update_led_state(led_data_t *ledData)
  118. {
  119. if (ledData->brightness >= 0)
  120. {
  121. uint32_t brightness = (ledData->brightness < LED_MIN_BRIGHTNESS) ? LED_MIN_BRIGHTNESS : ledData->brightness;
  122. /* Drive the LED with brightness */
  123. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, 1 * 1000 * 1000, GET_DUTY_CYCLE(brightness));
  124. }
  125. }
  126. static void process_touch(void)
  127. {
  128. cy_stc_capsense_touch_t *slider_touch_info;
  129. uint16_t slider_pos;
  130. uint8_t slider_touch_status;
  131. bool led_update_req = false;
  132. static uint16_t slider_pos_prev;
  133. static led_data_t led_data = {LED_ON, LED_MAX_BRIGHTNESS};
  134. /* Get slider status */
  135. slider_touch_info = Cy_CapSense_GetTouchInfo(
  136. CY_CAPSENSE_LINEARSLIDER0_WDGT_ID, &cy_capsense_context);
  137. slider_touch_status = slider_touch_info->numPosition;
  138. slider_pos = slider_touch_info->ptrPosition->x;
  139. /* Detect the new touch on slider */
  140. if ((RT_NULL != slider_touch_status) &&
  141. (slider_pos != slider_pos_prev))
  142. {
  143. led_data.brightness = (slider_pos * 100)
  144. / cy_capsense_context.ptrWdConfig[CY_CAPSENSE_LINEARSLIDER0_WDGT_ID].xResolution;
  145. led_update_req = true;
  146. }
  147. #ifndef RT_USING_PWM
  148. #error You need enable PWM to use this sample
  149. #else
  150. /* Update the LED state if requested */
  151. if (led_update_req)
  152. {
  153. update_led_state(&led_data);
  154. }
  155. #endif
  156. slider_pos_prev = slider_pos;
  157. }
  158. static void Slider_thread_entry(void *parameter)
  159. {
  160. Slider_Init();
  161. for (;;)
  162. {
  163. rt_sem_take(trans_done_semphr, RT_WAITING_FOREVER);
  164. /* Process all widgets */
  165. Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);
  166. /* Process touch input */
  167. process_touch();
  168. /* Establishes synchronized operation between the CapSense
  169. * middleware and the CapSense Tuner tool.
  170. */
  171. Cy_CapSense_RunTuner(&cy_capsense_context);
  172. /* Initiate next scan */
  173. Cy_CapSense_ScanAllWidgets(&cy_capsense_context);
  174. rt_thread_mdelay(50);
  175. }
  176. }
  177. int Slider_ctrl_sample(void)
  178. {
  179. rt_err_t ret = RT_EOK;
  180. sld_thread = rt_thread_create("slider_th",
  181. Slider_thread_entry,
  182. RT_NULL,
  183. 1024,
  184. 25,
  185. 10);
  186. if (sld_thread != RT_NULL)
  187. {
  188. rt_thread_startup(sld_thread);
  189. }
  190. else
  191. {
  192. ret = -RT_ERROR;
  193. }
  194. return ret;
  195. }
  196. MSH_CMD_EXPORT(Slider_ctrl_sample, Slider sample to ctrl led);
  197. #endif