![](/media/cache/profiles/20200518_163046.jpg.50x50_q85.jpg)
PSoC 6 pioneer kit multi library code for emwin driven tft display, capsense and mechanical button input.
main.cpp@1:f8f4e789361f, 2019-10-11 (annotated)
- Committer:
- reedas
- Date:
- Fri Oct 11 12:43:11 2019 +0000
- Revision:
- 1:f8f4e789361f
- Parent:
- 0:4ad2c16b6e43
- Child:
- 2:02954bc28fc5
Version working for Cypress psoc 6 Pioneer kit wifi board with tft display sw2 debounced mechanical switch, capsense buttons and capsense slider.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reedas | 0:4ad2c16b6e43 | 1 | #include "mbed.h" |
reedas | 0:4ad2c16b6e43 | 2 | #include "GUI.h" |
reedas | 0:4ad2c16b6e43 | 3 | #include "cy8ckit_028_tft.h" |
reedas | 0:4ad2c16b6e43 | 4 | #define Cy_SysLib_Delay wait_ms |
reedas | 1:f8f4e789361f | 5 | #include "cy_pdl.h" |
reedas | 1:f8f4e789361f | 6 | #include "cycfg_capsense.h" |
reedas | 1:f8f4e789361f | 7 | #include "cycfg.h" |
reedas | 1:f8f4e789361f | 8 | #include "PinDetect.h" |
reedas | 0:4ad2c16b6e43 | 9 | |
reedas | 0:4ad2c16b6e43 | 10 | DigitalOut ledGreen(LED_GREEN); |
reedas | 0:4ad2c16b6e43 | 11 | DigitalIn sw2(SWITCH2, PullUp); |
reedas | 0:4ad2c16b6e43 | 12 | DigitalOut ledRed(LED_RED); |
reedas | 0:4ad2c16b6e43 | 13 | DigitalOut ledBlue(LED_BLUE); |
reedas | 1:f8f4e789361f | 14 | DigitalOut ledBlue2(P13_4); |
reedas | 1:f8f4e789361f | 15 | DigitalOut ledGreen2(P13_3); |
reedas | 1:f8f4e789361f | 16 | DigitalOut ledRed2(P13_2); |
reedas | 0:4ad2c16b6e43 | 17 | |
reedas | 1:f8f4e789361f | 18 | PinDetect pb2(P0_4); |
reedas | 0:4ad2c16b6e43 | 19 | |
reedas | 0:4ad2c16b6e43 | 20 | /* Macros for switch press status */ |
reedas | 0:4ad2c16b6e43 | 21 | #define BTN_PRESSED (0u) |
reedas | 0:4ad2c16b6e43 | 22 | #define BTN_RELEASED (1u) |
reedas | 0:4ad2c16b6e43 | 23 | |
reedas | 1:f8f4e789361f | 24 | /*************************************************************************** |
reedas | 1:f8f4e789361f | 25 | * Global constants |
reedas | 1:f8f4e789361f | 26 | ***************************************************************************/ |
reedas | 1:f8f4e789361f | 27 | #define SLIDER_NUM_TOUCH (1u) /* Number of touches on the slider */ |
reedas | 1:f8f4e789361f | 28 | #define LED_OFF (1u) |
reedas | 1:f8f4e789361f | 29 | #define LED_ON (0u) |
reedas | 1:f8f4e789361f | 30 | #define CAPSENSE_SCAN_PERIOD_MS (20u) /* milliseconds */ |
reedas | 1:f8f4e789361f | 31 | |
reedas | 1:f8f4e789361f | 32 | |
reedas | 1:f8f4e789361f | 33 | /*************************************** |
reedas | 1:f8f4e789361f | 34 | * Function Prototypes |
reedas | 1:f8f4e789361f | 35 | **************************************/ |
reedas | 1:f8f4e789361f | 36 | void RunCapSenseScan(void); |
reedas | 1:f8f4e789361f | 37 | void InitTunerCommunication(void); |
reedas | 1:f8f4e789361f | 38 | void ProcessTouchStatus(void); |
reedas | 1:f8f4e789361f | 39 | void EZI2C_InterruptHandler(void); |
reedas | 1:f8f4e789361f | 40 | void CapSense_InterruptHandler(void); |
reedas | 1:f8f4e789361f | 41 | void CapSenseEndOfScanCallback(cy_stc_active_scan_sns_t * ptrActiveScan); |
reedas | 1:f8f4e789361f | 42 | void InitCapSenseClock(void); |
reedas | 1:f8f4e789361f | 43 | |
reedas | 0:4ad2c16b6e43 | 44 | |
reedas | 1:f8f4e789361f | 45 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 46 | * Interrupt configuration |
reedas | 1:f8f4e789361f | 47 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 48 | const cy_stc_sysint_t CapSense_ISR_cfg = |
reedas | 1:f8f4e789361f | 49 | { |
reedas | 1:f8f4e789361f | 50 | .intrSrc = CYBSP_CSD_IRQ, |
reedas | 1:f8f4e789361f | 51 | .intrPriority = 4u |
reedas | 1:f8f4e789361f | 52 | }; |
reedas | 1:f8f4e789361f | 53 | |
reedas | 1:f8f4e789361f | 54 | const cy_stc_sysint_t EZI2C_ISR_cfg = { |
reedas | 1:f8f4e789361f | 55 | .intrSrc = CYBSP_CSD_COMM_IRQ, |
reedas | 1:f8f4e789361f | 56 | .intrPriority = 3u |
reedas | 1:f8f4e789361f | 57 | }; |
reedas | 1:f8f4e789361f | 58 | |
reedas | 1:f8f4e789361f | 59 | |
reedas | 1:f8f4e789361f | 60 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 61 | * Global variables |
reedas | 1:f8f4e789361f | 62 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 63 | //DigitalOut ledRed(LED_RED); |
reedas | 1:f8f4e789361f | 64 | Semaphore capsense_sem; |
reedas | 1:f8f4e789361f | 65 | EventQueue queue; |
reedas | 1:f8f4e789361f | 66 | cy_stc_scb_ezi2c_context_t EZI2C_context; |
reedas | 1:f8f4e789361f | 67 | uint32_t prevBtn0Status = 0u; |
reedas | 1:f8f4e789361f | 68 | uint32_t prevBtn1Status = 0u; |
reedas | 1:f8f4e789361f | 69 | uint32_t prevSliderPos = 0u; |
reedas | 1:f8f4e789361f | 70 | bool sw2Pressed = false; |
reedas | 1:f8f4e789361f | 71 | bool prevSw2Pressed = false; |
reedas | 0:4ad2c16b6e43 | 72 | |
reedas | 0:4ad2c16b6e43 | 73 | |
reedas | 0:4ad2c16b6e43 | 74 | /* External global references */ |
reedas | 0:4ad2c16b6e43 | 75 | //extern GUI_CONST_STORAGE GUI_BITMAP bmCypressLogo_1bpp; |
reedas | 0:4ad2c16b6e43 | 76 | |
reedas | 0:4ad2c16b6e43 | 77 | extern GUI_CONST_STORAGE GUI_BITMAP bmExampleImage; |
reedas | 0:4ad2c16b6e43 | 78 | extern GUI_CONST_STORAGE GUI_BITMAP bmCypressLogo; |
reedas | 0:4ad2c16b6e43 | 79 | |
reedas | 1:f8f4e789361f | 80 | // Callback routine is interrupt activated by a debounced pb1 hit |
reedas | 1:f8f4e789361f | 81 | void pb2_hit_callback (void) { |
reedas | 1:f8f4e789361f | 82 | // printf("Count is %d\n", ++countit); |
reedas | 1:f8f4e789361f | 83 | sw2Pressed = true; |
reedas | 0:4ad2c16b6e43 | 84 | } |
reedas | 1:f8f4e789361f | 85 | // Callback routine is interrupt activated by a debounced pb1 hit |
reedas | 1:f8f4e789361f | 86 | void pb2_released_callback (void) { |
reedas | 1:f8f4e789361f | 87 | // printf("Count is %d\n", ++countit); |
reedas | 1:f8f4e789361f | 88 | sw2Pressed = false; |
reedas | 0:4ad2c16b6e43 | 89 | } |
reedas | 0:4ad2c16b6e43 | 90 | /******************************************************************************* |
reedas | 0:4ad2c16b6e43 | 91 | * Function Name: bool IsBtnClicked |
reedas | 0:4ad2c16b6e43 | 92 | ******************************************************************************** |
reedas | 0:4ad2c16b6e43 | 93 | * |
reedas | 0:4ad2c16b6e43 | 94 | * Summary: This non-blocking function implements SW2 button click check. |
reedas | 0:4ad2c16b6e43 | 95 | * |
reedas | 0:4ad2c16b6e43 | 96 | * Parameters: |
reedas | 0:4ad2c16b6e43 | 97 | * None |
reedas | 0:4ad2c16b6e43 | 98 | * |
reedas | 0:4ad2c16b6e43 | 99 | * Return: |
reedas | 0:4ad2c16b6e43 | 100 | * Status of the SW2 button: |
reedas | 0:4ad2c16b6e43 | 101 | * true when button was pressed and then released and |
reedas | 0:4ad2c16b6e43 | 102 | * false in other cases |
reedas | 0:4ad2c16b6e43 | 103 | * |
reedas | 0:4ad2c16b6e43 | 104 | *******************************************************************************/ |
reedas | 0:4ad2c16b6e43 | 105 | bool IsBtnClicked(void) |
reedas | 0:4ad2c16b6e43 | 106 | { |
reedas | 0:4ad2c16b6e43 | 107 | int currBtnState; |
reedas | 0:4ad2c16b6e43 | 108 | static int prevBtnState = BTN_RELEASED; |
reedas | 0:4ad2c16b6e43 | 109 | |
reedas | 0:4ad2c16b6e43 | 110 | bool result = false; |
reedas | 0:4ad2c16b6e43 | 111 | |
reedas | 0:4ad2c16b6e43 | 112 | currBtnState = sw2; |
reedas | 0:4ad2c16b6e43 | 113 | |
reedas | 0:4ad2c16b6e43 | 114 | if((prevBtnState == BTN_RELEASED) && (currBtnState == BTN_PRESSED)) |
reedas | 0:4ad2c16b6e43 | 115 | { |
reedas | 0:4ad2c16b6e43 | 116 | result = true; |
reedas | 0:4ad2c16b6e43 | 117 | } |
reedas | 0:4ad2c16b6e43 | 118 | |
reedas | 0:4ad2c16b6e43 | 119 | prevBtnState = currBtnState; |
reedas | 0:4ad2c16b6e43 | 120 | |
reedas | 0:4ad2c16b6e43 | 121 | wait_ms(5); |
reedas | 0:4ad2c16b6e43 | 122 | |
reedas | 0:4ad2c16b6e43 | 123 | return result; |
reedas | 0:4ad2c16b6e43 | 124 | } |
reedas | 0:4ad2c16b6e43 | 125 | |
reedas | 1:f8f4e789361f | 126 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 127 | * Function Name: RunCapSenseScan() |
reedas | 1:f8f4e789361f | 128 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 129 | * Summary: |
reedas | 1:f8f4e789361f | 130 | * This function starts the scan, and processes the touch status. It is |
reedas | 1:f8f4e789361f | 131 | * periodically called by an event dispatcher. |
reedas | 1:f8f4e789361f | 132 | * |
reedas | 1:f8f4e789361f | 133 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 134 | void RunCapSenseScan(void) |
reedas | 1:f8f4e789361f | 135 | { |
reedas | 1:f8f4e789361f | 136 | Cy_CapSense_ScanAllWidgets(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 137 | capsense_sem.acquire(); |
reedas | 1:f8f4e789361f | 138 | Cy_CapSense_ProcessAllWidgets(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 139 | Cy_CapSense_RunTuner(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 140 | ProcessTouchStatus(); |
reedas | 1:f8f4e789361f | 141 | } |
reedas | 1:f8f4e789361f | 142 | |
reedas | 0:4ad2c16b6e43 | 143 | |
reedas | 0:4ad2c16b6e43 | 144 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 145 | * Function Name: InitTunerCommunication |
reedas | 0:4ad2c16b6e43 | 146 | ******************************************************************************** |
reedas | 0:4ad2c16b6e43 | 147 | * |
reedas | 1:f8f4e789361f | 148 | * Summary: |
reedas | 1:f8f4e789361f | 149 | * This function performs the following functions: |
reedas | 1:f8f4e789361f | 150 | * - Initializes SCB block for operation in EZI2C mode |
reedas | 1:f8f4e789361f | 151 | * - Configures EZI2C pins |
reedas | 1:f8f4e789361f | 152 | * - Configures EZI2C clock |
reedas | 1:f8f4e789361f | 153 | * - Sets communication data buffer to CapSense data structure |
reedas | 0:4ad2c16b6e43 | 154 | * |
reedas | 0:4ad2c16b6e43 | 155 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 156 | void InitTunerCommunication(void) |
reedas | 0:4ad2c16b6e43 | 157 | { |
reedas | 1:f8f4e789361f | 158 | /* Initialize EZI2C pins */ |
reedas | 1:f8f4e789361f | 159 | Cy_GPIO_Pin_Init(CYBSP_EZI2C_SCL_PORT, CYBSP_EZI2C_SCL_PIN, &CYBSP_EZI2C_SCL_config); |
reedas | 1:f8f4e789361f | 160 | Cy_GPIO_Pin_Init(CYBSP_EZI2C_SDA_PORT, CYBSP_EZI2C_SDA_PIN, &CYBSP_EZI2C_SDA_config); |
reedas | 1:f8f4e789361f | 161 | |
reedas | 1:f8f4e789361f | 162 | /* Configure the peripheral clock for EZI2C */ |
reedas | 1:f8f4e789361f | 163 | Cy_SysClk_PeriphAssignDivider(PCLK_SCB3_CLOCK, CY_SYSCLK_DIV_8_BIT, 1U); |
reedas | 1:f8f4e789361f | 164 | Cy_SysClk_PeriphDisableDivider(CY_SYSCLK_DIV_8_BIT, 1U); |
reedas | 1:f8f4e789361f | 165 | Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT, 1U, 7U); |
reedas | 1:f8f4e789361f | 166 | Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, 1U); |
reedas | 0:4ad2c16b6e43 | 167 | |
reedas | 1:f8f4e789361f | 168 | Cy_SCB_EZI2C_Init(CYBSP_CSD_COMM_HW, &CYBSP_CSD_COMM_config, &EZI2C_context); |
reedas | 1:f8f4e789361f | 169 | |
reedas | 1:f8f4e789361f | 170 | /* Initialize and enable EZI2C interrupts */ |
reedas | 1:f8f4e789361f | 171 | Cy_SysInt_Init(&EZI2C_ISR_cfg, &EZI2C_InterruptHandler); |
reedas | 1:f8f4e789361f | 172 | NVIC_EnableIRQ(EZI2C_ISR_cfg.intrSrc); |
reedas | 1:f8f4e789361f | 173 | |
reedas | 1:f8f4e789361f | 174 | /* Set up communication data buffer to CapSense data structure to be exposed |
reedas | 1:f8f4e789361f | 175 | * to I2C master at primary slave address request. |
reedas | 1:f8f4e789361f | 176 | */ |
reedas | 1:f8f4e789361f | 177 | Cy_SCB_EZI2C_SetBuffer1(CYBSP_CSD_COMM_HW, (uint8 *)&cy_capsense_tuner, |
reedas | 1:f8f4e789361f | 178 | sizeof(cy_capsense_tuner), sizeof(cy_capsense_tuner), &EZI2C_context); |
reedas | 1:f8f4e789361f | 179 | |
reedas | 1:f8f4e789361f | 180 | /* Enable EZI2C block */ |
reedas | 1:f8f4e789361f | 181 | Cy_SCB_EZI2C_Enable(CYBSP_CSD_COMM_HW); |
reedas | 1:f8f4e789361f | 182 | } |
reedas | 1:f8f4e789361f | 183 | |
reedas | 0:4ad2c16b6e43 | 184 | |
reedas | 1:f8f4e789361f | 185 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 186 | * Function Name: ProcessTouchStatus |
reedas | 1:f8f4e789361f | 187 | ******************************************************************************** |
reedas | 1:f8f4e789361f | 188 | * |
reedas | 1:f8f4e789361f | 189 | * Summary: |
reedas | 1:f8f4e789361f | 190 | * Controls the LED status according to the status of CapSense widgets and |
reedas | 1:f8f4e789361f | 191 | * prints the status to serial terminal. |
reedas | 1:f8f4e789361f | 192 | * |
reedas | 1:f8f4e789361f | 193 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 194 | void ProcessTouchStatus(void) |
reedas | 1:f8f4e789361f | 195 | { |
reedas | 1:f8f4e789361f | 196 | uint32_t currSliderPos; |
reedas | 1:f8f4e789361f | 197 | uint32_t currBtn0Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON0_WDGT_ID, CY_CAPSENSE_BUTTON0_SNS0_ID, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 198 | uint32_t currBtn1Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON1_WDGT_ID, CY_CAPSENSE_BUTTON1_SNS0_ID, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 199 | cy_stc_capsense_touch_t *sldrTouch = Cy_CapSense_GetTouchInfo(CY_CAPSENSE_LINEARSLIDER0_WDGT_ID, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 200 | char outputString[80]; |
reedas | 1:f8f4e789361f | 201 | if(currBtn0Status != prevBtn0Status) |
reedas | 1:f8f4e789361f | 202 | { |
reedas | 1:f8f4e789361f | 203 | printf("Button_0 status: %u\r\n", currBtn0Status); |
reedas | 1:f8f4e789361f | 204 | sprintf(outputString," Button_0 status: %u ", currBtn0Status); |
reedas | 1:f8f4e789361f | 205 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 206 | GUI_DispStringAt(outputString, 160, 60); |
reedas | 1:f8f4e789361f | 207 | prevBtn0Status = currBtn0Status; |
reedas | 1:f8f4e789361f | 208 | } |
reedas | 1:f8f4e789361f | 209 | |
reedas | 1:f8f4e789361f | 210 | if(currBtn1Status != prevBtn1Status) |
reedas | 1:f8f4e789361f | 211 | { |
reedas | 1:f8f4e789361f | 212 | printf("Button_1 status: %u\r\n", currBtn1Status); |
reedas | 1:f8f4e789361f | 213 | sprintf(outputString," Button_1 status: %u ", currBtn1Status); |
reedas | 1:f8f4e789361f | 214 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 215 | GUI_DispStringAt(outputString, 160, 80); |
reedas | 1:f8f4e789361f | 216 | prevBtn1Status = currBtn1Status; |
reedas | 1:f8f4e789361f | 217 | } |
reedas | 1:f8f4e789361f | 218 | if(sw2Pressed != prevSw2Pressed) { |
reedas | 1:f8f4e789361f | 219 | sprintf(outputString," Button_2 status: %u ", sw2Pressed); |
reedas | 1:f8f4e789361f | 220 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 221 | GUI_DispStringAt(outputString, 160, 100); |
reedas | 1:f8f4e789361f | 222 | prevSw2Pressed = sw2Pressed; |
reedas | 1:f8f4e789361f | 223 | } |
reedas | 1:f8f4e789361f | 224 | if (sldrTouch->numPosition == SLIDER_NUM_TOUCH) |
reedas | 1:f8f4e789361f | 225 | { |
reedas | 1:f8f4e789361f | 226 | currSliderPos = sldrTouch->ptrPosition->x; |
reedas | 0:4ad2c16b6e43 | 227 | |
reedas | 1:f8f4e789361f | 228 | if(currSliderPos != prevSliderPos) |
reedas | 0:4ad2c16b6e43 | 229 | { |
reedas | 1:f8f4e789361f | 230 | printf("Slider position: %u\r\n", currSliderPos); |
reedas | 1:f8f4e789361f | 231 | sprintf(outputString," Slider position: %u ", currSliderPos); |
reedas | 1:f8f4e789361f | 232 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 233 | GUI_DispStringAt(outputString, 160, 120); |
reedas | 1:f8f4e789361f | 234 | prevSliderPos = currSliderPos; |
reedas | 0:4ad2c16b6e43 | 235 | } |
reedas | 0:4ad2c16b6e43 | 236 | } |
reedas | 1:f8f4e789361f | 237 | |
reedas | 1:f8f4e789361f | 238 | ledRed = (sw2Pressed || currBtn0Status || currBtn1Status || (sldrTouch->numPosition == SLIDER_NUM_TOUCH)) ? LED_ON : LED_OFF; |
reedas | 0:4ad2c16b6e43 | 239 | } |
reedas | 0:4ad2c16b6e43 | 240 | |
reedas | 0:4ad2c16b6e43 | 241 | |
reedas | 1:f8f4e789361f | 242 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 243 | * Function Name: EZI2C_InterruptHandler |
reedas | 1:f8f4e789361f | 244 | ******************************************************************************** |
reedas | 1:f8f4e789361f | 245 | * Summary: |
reedas | 1:f8f4e789361f | 246 | * Wrapper function for handling interrupts from EZI2C block. |
reedas | 1:f8f4e789361f | 247 | * |
reedas | 1:f8f4e789361f | 248 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 249 | void EZI2C_InterruptHandler(void) |
reedas | 1:f8f4e789361f | 250 | { |
reedas | 1:f8f4e789361f | 251 | Cy_SCB_EZI2C_Interrupt(CYBSP_CSD_COMM_HW, &EZI2C_context); |
reedas | 1:f8f4e789361f | 252 | } |
reedas | 1:f8f4e789361f | 253 | |
reedas | 1:f8f4e789361f | 254 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 255 | * Function Name: CapSense_InterruptHandler() |
reedas | 1:f8f4e789361f | 256 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 257 | * Summary: |
reedas | 1:f8f4e789361f | 258 | * Wrapper function for handling interrupts from CSD block. |
reedas | 1:f8f4e789361f | 259 | * |
reedas | 1:f8f4e789361f | 260 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 261 | void CapSense_InterruptHandler(void) |
reedas | 1:f8f4e789361f | 262 | { |
reedas | 1:f8f4e789361f | 263 | Cy_CapSense_InterruptHandler(CYBSP_CSD_HW, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 264 | } |
reedas | 1:f8f4e789361f | 265 | |
reedas | 1:f8f4e789361f | 266 | |
reedas | 1:f8f4e789361f | 267 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 268 | * Function Name: CapSenseEndOfScanCallback() |
reedas | 1:f8f4e789361f | 269 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 270 | * Summary: |
reedas | 1:f8f4e789361f | 271 | * This function releases a semaphore to indicate end of a CapSense scan. |
reedas | 1:f8f4e789361f | 272 | * |
reedas | 1:f8f4e789361f | 273 | * Parameters: |
reedas | 1:f8f4e789361f | 274 | * cy_stc_active_scan_sns_t* : pointer to active sensor details. |
reedas | 1:f8f4e789361f | 275 | * |
reedas | 1:f8f4e789361f | 276 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 277 | void CapSenseEndOfScanCallback(cy_stc_active_scan_sns_t * ptrActiveScan) |
reedas | 1:f8f4e789361f | 278 | { |
reedas | 1:f8f4e789361f | 279 | capsense_sem.release(); |
reedas | 1:f8f4e789361f | 280 | } |
reedas | 1:f8f4e789361f | 281 | |
reedas | 1:f8f4e789361f | 282 | |
reedas | 1:f8f4e789361f | 283 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 284 | * Function Name: InitCapSenseClock() |
reedas | 1:f8f4e789361f | 285 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 286 | * Summary: |
reedas | 1:f8f4e789361f | 287 | * This function configures the peripheral clock for CapSense. |
reedas | 1:f8f4e789361f | 288 | * |
reedas | 1:f8f4e789361f | 289 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 290 | void InitCapSenseClock(void) |
reedas | 1:f8f4e789361f | 291 | { |
reedas | 1:f8f4e789361f | 292 | Cy_SysClk_PeriphAssignDivider(PCLK_CSD_CLOCK, CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM); |
reedas | 1:f8f4e789361f | 293 | Cy_SysClk_PeriphDisableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM); |
reedas | 1:f8f4e789361f | 294 | Cy_SysClk_PeriphSetDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM, 0u); |
reedas | 1:f8f4e789361f | 295 | Cy_SysClk_PeriphEnableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM); |
reedas | 1:f8f4e789361f | 296 | } |
reedas | 1:f8f4e789361f | 297 | |
reedas | 1:f8f4e789361f | 298 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 299 | * Function Name: main() |
reedas | 1:f8f4e789361f | 300 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 301 | * Summary: |
reedas | 1:f8f4e789361f | 302 | * Main function that starts a thread for CapSense scan and enters a forever |
reedas | 1:f8f4e789361f | 303 | * wait state. |
reedas | 1:f8f4e789361f | 304 | * |
reedas | 1:f8f4e789361f | 305 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 306 | int main(void) |
reedas | 1:f8f4e789361f | 307 | { |
reedas | 1:f8f4e789361f | 308 | /* Turn off both red and green LEDs */ |
reedas | 1:f8f4e789361f | 309 | ledGreen = LED_OFF; |
reedas | 1:f8f4e789361f | 310 | ledRed = LED_OFF; |
reedas | 1:f8f4e789361f | 311 | ledBlue = LED_OFF; |
reedas | 1:f8f4e789361f | 312 | ledGreen2 = 0; /* 1 for on 0 for off */ |
reedas | 1:f8f4e789361f | 313 | ledRed2 = 0; |
reedas | 1:f8f4e789361f | 314 | ledBlue2 = 0; |
reedas | 1:f8f4e789361f | 315 | char outputString[80]; |
reedas | 1:f8f4e789361f | 316 | |
reedas | 1:f8f4e789361f | 317 | /* Initialize EmWin driver*/ |
reedas | 1:f8f4e789361f | 318 | GUI_Init(); |
reedas | 1:f8f4e789361f | 319 | /* Set font size, foreground and background Colours */ |
reedas | 1:f8f4e789361f | 320 | GUI_SetFont(GUI_FONT_16B_1); |
reedas | 1:f8f4e789361f | 321 | GUI_SetColor(GUI_WHITE); |
reedas | 1:f8f4e789361f | 322 | GUI_SetBkColor(GUI_BLACK); |
reedas | 1:f8f4e789361f | 323 | |
reedas | 1:f8f4e789361f | 324 | GUI_Clear(); |
reedas | 1:f8f4e789361f | 325 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 326 | GUI_DispStringAt("Capsense Demo", 160, 20); |
reedas | 1:f8f4e789361f | 327 | printf("\r\nApplication has started. Touch any CapSense button or slider.\r\n"); |
reedas | 1:f8f4e789361f | 328 | sprintf(outputString,"\r\nApplication has started.\r\n Touch any CapSense button or slider."); |
reedas | 1:f8f4e789361f | 329 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 330 | GUI_DispStringAt(outputString, 160, 180); |
reedas | 1:f8f4e789361f | 331 | wait(0.10); |
reedas | 1:f8f4e789361f | 332 | /* Configure AMUX bus for CapSense */ |
reedas | 1:f8f4e789361f | 333 | init_cycfg_routing(); |
reedas | 1:f8f4e789361f | 334 | |
reedas | 1:f8f4e789361f | 335 | /* Configure PERI clocks for CapSense */ |
reedas | 1:f8f4e789361f | 336 | InitCapSenseClock(); |
reedas | 1:f8f4e789361f | 337 | |
reedas | 1:f8f4e789361f | 338 | InitTunerCommunication(); |
reedas | 1:f8f4e789361f | 339 | |
reedas | 1:f8f4e789361f | 340 | /* Initialize the CSD HW block to the default state. */ |
reedas | 1:f8f4e789361f | 341 | cy_status status = Cy_CapSense_Init(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 342 | if(CY_RET_SUCCESS != status) |
reedas | 1:f8f4e789361f | 343 | { |
reedas | 1:f8f4e789361f | 344 | printf("CapSense initialization failed. Status code: %u\r\n", status); |
reedas | 1:f8f4e789361f | 345 | sprintf(outputString,"CapSense initialization failed. Status code: %u", status); |
reedas | 1:f8f4e789361f | 346 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 347 | GUI_DispStringAt(outputString, 160, 40); |
reedas | 1:f8f4e789361f | 348 | wait(osWaitForever); |
reedas | 1:f8f4e789361f | 349 | } |
reedas | 1:f8f4e789361f | 350 | |
reedas | 1:f8f4e789361f | 351 | /* Initialize CapSense interrupt */ |
reedas | 1:f8f4e789361f | 352 | Cy_SysInt_Init(&CapSense_ISR_cfg, &CapSense_InterruptHandler); |
reedas | 1:f8f4e789361f | 353 | NVIC_ClearPendingIRQ(CapSense_ISR_cfg.intrSrc); |
reedas | 1:f8f4e789361f | 354 | NVIC_EnableIRQ(CapSense_ISR_cfg.intrSrc); |
reedas | 1:f8f4e789361f | 355 | |
reedas | 1:f8f4e789361f | 356 | /* Initialize the CapSense firmware modules. */ |
reedas | 1:f8f4e789361f | 357 | Cy_CapSense_Enable(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 358 | Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E, CapSenseEndOfScanCallback, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 359 | |
reedas | 1:f8f4e789361f | 360 | /* Create a thread to run CapSense scan periodically using an event queue |
reedas | 1:f8f4e789361f | 361 | * dispatcher. |
reedas | 1:f8f4e789361f | 362 | */ |
reedas | 1:f8f4e789361f | 363 | Thread thread(osPriorityNormal, OS_STACK_SIZE, NULL, "CapSense Scan Thread"); |
reedas | 1:f8f4e789361f | 364 | thread.start(callback(&queue, &EventQueue::dispatch_forever)); |
reedas | 1:f8f4e789361f | 365 | queue.call_every(CAPSENSE_SCAN_PERIOD_MS, RunCapSenseScan); |
reedas | 1:f8f4e789361f | 366 | |
reedas | 1:f8f4e789361f | 367 | /* Initiate scan immediately since the first call of RunCapSenseScan() |
reedas | 1:f8f4e789361f | 368 | * happens CAPSENSE_SCAN_PERIOD_MS after the event queue dispatcher has |
reedas | 1:f8f4e789361f | 369 | * started. |
reedas | 1:f8f4e789361f | 370 | */ |
reedas | 1:f8f4e789361f | 371 | Cy_CapSense_ScanAllWidgets(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 372 | |
reedas | 1:f8f4e789361f | 373 | |
reedas | 1:f8f4e789361f | 374 | // Use internal pullups for pushbutton |
reedas | 1:f8f4e789361f | 375 | pb2.mode(PullUp); |
reedas | 1:f8f4e789361f | 376 | // pb2.mode(PullUp); |
reedas | 1:f8f4e789361f | 377 | // Delay for initial pullup to take effect |
reedas | 1:f8f4e789361f | 378 | wait(.01); |
reedas | 1:f8f4e789361f | 379 | // Setup Interrupt callback functions for a pb hit |
reedas | 1:f8f4e789361f | 380 | pb2.attach_deasserted(&pb2_hit_callback); |
reedas | 1:f8f4e789361f | 381 | // pb2.attach_deasserted(&pb2_hit_callback); |
reedas | 1:f8f4e789361f | 382 | // Start sampling pb inputs using interrupts |
reedas | 1:f8f4e789361f | 383 | pb2.setSampleFrequency(); |
reedas | 1:f8f4e789361f | 384 | |
reedas | 1:f8f4e789361f | 385 | pb2.attach_asserted( &pb2_released_callback ); |
reedas | 1:f8f4e789361f | 386 | wait(osWaitForever); |
reedas | 1:f8f4e789361f | 387 | |
reedas | 1:f8f4e789361f | 388 | } |
reedas | 1:f8f4e789361f | 389 |