slider_sample.c 6.0 KB

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