![](/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@2:02954bc28fc5, 2019-10-17 (annotated)
- Committer:
- reedas
- Date:
- Thu Oct 17 15:55:03 2019 +0000
- Revision:
- 2:02954bc28fc5
- Parent:
- 1:f8f4e789361f
Version working with External RGB LEDs. Demonstrates Capsense buttons, emwin tft display driver, PinDetect key debouncing callbacks
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 | 2:02954bc28fc5 | 13 | DigitalOut userLed(LED4); /* Amber LED */ |
reedas | 0:4ad2c16b6e43 | 14 | DigitalOut ledBlue(LED_BLUE); |
reedas | 1:f8f4e789361f | 15 | DigitalOut ledBlue2(P13_4); |
reedas | 1:f8f4e789361f | 16 | DigitalOut ledGreen2(P13_3); |
reedas | 1:f8f4e789361f | 17 | DigitalOut ledRed2(P13_2); |
reedas | 2:02954bc28fc5 | 18 | PwmOut myPwm(LED5); /* Red Ststus LED */ |
reedas | 0:4ad2c16b6e43 | 19 | |
reedas | 1:f8f4e789361f | 20 | PinDetect pb2(P0_4); |
reedas | 0:4ad2c16b6e43 | 21 | |
reedas | 0:4ad2c16b6e43 | 22 | /* Macros for switch press status */ |
reedas | 0:4ad2c16b6e43 | 23 | #define BTN_PRESSED (0u) |
reedas | 0:4ad2c16b6e43 | 24 | #define BTN_RELEASED (1u) |
reedas | 0:4ad2c16b6e43 | 25 | |
reedas | 1:f8f4e789361f | 26 | /*************************************************************************** |
reedas | 1:f8f4e789361f | 27 | * Global constants |
reedas | 1:f8f4e789361f | 28 | ***************************************************************************/ |
reedas | 1:f8f4e789361f | 29 | #define SLIDER_NUM_TOUCH (1u) /* Number of touches on the slider */ |
reedas | 2:02954bc28fc5 | 30 | #define nLED_OFF (1u) |
reedas | 2:02954bc28fc5 | 31 | #define nLED_ON (0u) |
reedas | 2:02954bc28fc5 | 32 | #define LED_OFF (0u) |
reedas | 2:02954bc28fc5 | 33 | #define LED_ON (1u) |
reedas | 1:f8f4e789361f | 34 | #define CAPSENSE_SCAN_PERIOD_MS (20u) /* milliseconds */ |
reedas | 1:f8f4e789361f | 35 | |
reedas | 1:f8f4e789361f | 36 | |
reedas | 1:f8f4e789361f | 37 | /*************************************** |
reedas | 1:f8f4e789361f | 38 | * Function Prototypes |
reedas | 1:f8f4e789361f | 39 | **************************************/ |
reedas | 1:f8f4e789361f | 40 | void RunCapSenseScan(void); |
reedas | 1:f8f4e789361f | 41 | void InitTunerCommunication(void); |
reedas | 1:f8f4e789361f | 42 | void ProcessTouchStatus(void); |
reedas | 1:f8f4e789361f | 43 | void EZI2C_InterruptHandler(void); |
reedas | 1:f8f4e789361f | 44 | void CapSense_InterruptHandler(void); |
reedas | 1:f8f4e789361f | 45 | void CapSenseEndOfScanCallback(cy_stc_active_scan_sns_t * ptrActiveScan); |
reedas | 1:f8f4e789361f | 46 | void InitCapSenseClock(void); |
reedas | 2:02954bc28fc5 | 47 | void LED_Init(void); |
reedas | 2:02954bc28fc5 | 48 | void Display_Init(void); |
reedas | 2:02954bc28fc5 | 49 | void CapSense_Init(void); |
reedas | 2:02954bc28fc5 | 50 | void Button_Init(void); |
reedas | 0:4ad2c16b6e43 | 51 | |
reedas | 1:f8f4e789361f | 52 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 53 | * Interrupt configuration |
reedas | 1:f8f4e789361f | 54 | *******************************************************************************/ |
reedas | 2:02954bc28fc5 | 55 | const cy_stc_sysint_t CapSense_ISR_cfg = { |
reedas | 1:f8f4e789361f | 56 | .intrSrc = CYBSP_CSD_IRQ, |
reedas | 1:f8f4e789361f | 57 | .intrPriority = 4u |
reedas | 1:f8f4e789361f | 58 | }; |
reedas | 1:f8f4e789361f | 59 | |
reedas | 1:f8f4e789361f | 60 | const cy_stc_sysint_t EZI2C_ISR_cfg = { |
reedas | 1:f8f4e789361f | 61 | .intrSrc = CYBSP_CSD_COMM_IRQ, |
reedas | 1:f8f4e789361f | 62 | .intrPriority = 3u |
reedas | 1:f8f4e789361f | 63 | }; |
reedas | 1:f8f4e789361f | 64 | |
reedas | 1:f8f4e789361f | 65 | |
reedas | 1:f8f4e789361f | 66 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 67 | * Global variables |
reedas | 1:f8f4e789361f | 68 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 69 | //DigitalOut ledRed(LED_RED); |
reedas | 1:f8f4e789361f | 70 | Semaphore capsense_sem; |
reedas | 1:f8f4e789361f | 71 | EventQueue queue; |
reedas | 1:f8f4e789361f | 72 | cy_stc_scb_ezi2c_context_t EZI2C_context; |
reedas | 2:02954bc28fc5 | 73 | uint32_t prevBtn0Status = 0u; |
reedas | 1:f8f4e789361f | 74 | uint32_t prevBtn1Status = 0u; |
reedas | 1:f8f4e789361f | 75 | uint32_t prevSliderPos = 0u; |
reedas | 1:f8f4e789361f | 76 | bool sw2Pressed = false; |
reedas | 1:f8f4e789361f | 77 | bool prevSw2Pressed = false; |
reedas | 2:02954bc28fc5 | 78 | bool sw2ToggleStatus = false; |
reedas | 2:02954bc28fc5 | 79 | bool btn0ToggleStatus = false; |
reedas | 2:02954bc28fc5 | 80 | bool btn1ToggleStatus = false; |
reedas | 0:4ad2c16b6e43 | 81 | |
reedas | 0:4ad2c16b6e43 | 82 | |
reedas | 0:4ad2c16b6e43 | 83 | /* External global references */ |
reedas | 0:4ad2c16b6e43 | 84 | //extern GUI_CONST_STORAGE GUI_BITMAP bmCypressLogo_1bpp; |
reedas | 0:4ad2c16b6e43 | 85 | |
reedas | 2:02954bc28fc5 | 86 | extern GUI_CONST_STORAGE GUI_BITMAP bmExampleImage; |
reedas | 0:4ad2c16b6e43 | 87 | extern GUI_CONST_STORAGE GUI_BITMAP bmCypressLogo; |
reedas | 0:4ad2c16b6e43 | 88 | |
reedas | 1:f8f4e789361f | 89 | // Callback routine is interrupt activated by a debounced pb1 hit |
reedas | 2:02954bc28fc5 | 90 | void pb2_hit_callback (void) |
reedas | 2:02954bc28fc5 | 91 | { |
reedas | 1:f8f4e789361f | 92 | // printf("Count is %d\n", ++countit); |
reedas | 1:f8f4e789361f | 93 | sw2Pressed = true; |
reedas | 2:02954bc28fc5 | 94 | } |
reedas | 1:f8f4e789361f | 95 | // Callback routine is interrupt activated by a debounced pb1 hit |
reedas | 2:02954bc28fc5 | 96 | void pb2_released_callback (void) |
reedas | 2:02954bc28fc5 | 97 | { |
reedas | 1:f8f4e789361f | 98 | // printf("Count is %d\n", ++countit); |
reedas | 1:f8f4e789361f | 99 | sw2Pressed = false; |
reedas | 2:02954bc28fc5 | 100 | } |
reedas | 0:4ad2c16b6e43 | 101 | /******************************************************************************* |
reedas | 0:4ad2c16b6e43 | 102 | * Function Name: bool IsBtnClicked |
reedas | 0:4ad2c16b6e43 | 103 | ******************************************************************************** |
reedas | 0:4ad2c16b6e43 | 104 | * |
reedas | 0:4ad2c16b6e43 | 105 | * Summary: This non-blocking function implements SW2 button click check. |
reedas | 0:4ad2c16b6e43 | 106 | * |
reedas | 0:4ad2c16b6e43 | 107 | * Parameters: |
reedas | 0:4ad2c16b6e43 | 108 | * None |
reedas | 0:4ad2c16b6e43 | 109 | * |
reedas | 0:4ad2c16b6e43 | 110 | * Return: |
reedas | 0:4ad2c16b6e43 | 111 | * Status of the SW2 button: |
reedas | 2:02954bc28fc5 | 112 | * true when button was pressed and then released and |
reedas | 0:4ad2c16b6e43 | 113 | * false in other cases |
reedas | 0:4ad2c16b6e43 | 114 | * |
reedas | 2:02954bc28fc5 | 115 | * This code is not used in this version of the program as PinDetect Library |
reedas | 2:02954bc28fc5 | 116 | * is employed to debounce the switch. |
reedas | 2:02954bc28fc5 | 117 | * |
reedas | 0:4ad2c16b6e43 | 118 | *******************************************************************************/ |
reedas | 2:02954bc28fc5 | 119 | /* |
reedas | 2:02954bc28fc5 | 120 | * bool IsBtnClicked(void) |
reedas | 2:02954bc28fc5 | 121 | * { |
reedas | 2:02954bc28fc5 | 122 | * int currBtnState; |
reedas | 2:02954bc28fc5 | 123 | * static int prevBtnState = BTN_RELEASED; |
reedas | 2:02954bc28fc5 | 124 | * |
reedas | 2:02954bc28fc5 | 125 | * bool result = false; |
reedas | 2:02954bc28fc5 | 126 | * |
reedas | 2:02954bc28fc5 | 127 | * currBtnState = sw2; |
reedas | 2:02954bc28fc5 | 128 | * |
reedas | 2:02954bc28fc5 | 129 | * if((prevBtnState == BTN_RELEASED) && (currBtnState == BTN_PRESSED)) { |
reedas | 2:02954bc28fc5 | 130 | * result = true; |
reedas | 2:02954bc28fc5 | 131 | * } |
reedas | 2:02954bc28fc5 | 132 | * |
reedas | 2:02954bc28fc5 | 133 | * prevBtnState = currBtnState; |
reedas | 2:02954bc28fc5 | 134 | * |
reedas | 2:02954bc28fc5 | 135 | * wait_ms(5); |
reedas | 2:02954bc28fc5 | 136 | * |
reedas | 2:02954bc28fc5 | 137 | * return result; |
reedas | 2:02954bc28fc5 | 138 | * } |
reedas | 2:02954bc28fc5 | 139 | */ |
reedas | 1:f8f4e789361f | 140 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 141 | * Function Name: RunCapSenseScan() |
reedas | 1:f8f4e789361f | 142 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 143 | * Summary: |
reedas | 1:f8f4e789361f | 144 | * This function starts the scan, and processes the touch status. It is |
reedas | 2:02954bc28fc5 | 145 | * periodically called by an event dispatcher. |
reedas | 1:f8f4e789361f | 146 | * |
reedas | 1:f8f4e789361f | 147 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 148 | void RunCapSenseScan(void) |
reedas | 1:f8f4e789361f | 149 | { |
reedas | 1:f8f4e789361f | 150 | Cy_CapSense_ScanAllWidgets(&cy_capsense_context); |
reedas | 2:02954bc28fc5 | 151 | capsense_sem.acquire(); |
reedas | 1:f8f4e789361f | 152 | Cy_CapSense_ProcessAllWidgets(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 153 | Cy_CapSense_RunTuner(&cy_capsense_context); |
reedas | 2:02954bc28fc5 | 154 | ProcessTouchStatus(); |
reedas | 1:f8f4e789361f | 155 | } |
reedas | 1:f8f4e789361f | 156 | |
reedas | 0:4ad2c16b6e43 | 157 | |
reedas | 0:4ad2c16b6e43 | 158 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 159 | * Function Name: InitTunerCommunication |
reedas | 0:4ad2c16b6e43 | 160 | ******************************************************************************** |
reedas | 0:4ad2c16b6e43 | 161 | * |
reedas | 1:f8f4e789361f | 162 | * Summary: |
reedas | 1:f8f4e789361f | 163 | * This function performs the following functions: |
reedas | 1:f8f4e789361f | 164 | * - Initializes SCB block for operation in EZI2C mode |
reedas | 1:f8f4e789361f | 165 | * - Configures EZI2C pins |
reedas | 1:f8f4e789361f | 166 | * - Configures EZI2C clock |
reedas | 1:f8f4e789361f | 167 | * - Sets communication data buffer to CapSense data structure |
reedas | 0:4ad2c16b6e43 | 168 | * |
reedas | 0:4ad2c16b6e43 | 169 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 170 | void InitTunerCommunication(void) |
reedas | 0:4ad2c16b6e43 | 171 | { |
reedas | 1:f8f4e789361f | 172 | /* Initialize EZI2C pins */ |
reedas | 1:f8f4e789361f | 173 | Cy_GPIO_Pin_Init(CYBSP_EZI2C_SCL_PORT, CYBSP_EZI2C_SCL_PIN, &CYBSP_EZI2C_SCL_config); |
reedas | 1:f8f4e789361f | 174 | Cy_GPIO_Pin_Init(CYBSP_EZI2C_SDA_PORT, CYBSP_EZI2C_SDA_PIN, &CYBSP_EZI2C_SDA_config); |
reedas | 2:02954bc28fc5 | 175 | |
reedas | 1:f8f4e789361f | 176 | /* Configure the peripheral clock for EZI2C */ |
reedas | 1:f8f4e789361f | 177 | Cy_SysClk_PeriphAssignDivider(PCLK_SCB3_CLOCK, CY_SYSCLK_DIV_8_BIT, 1U); |
reedas | 1:f8f4e789361f | 178 | Cy_SysClk_PeriphDisableDivider(CY_SYSCLK_DIV_8_BIT, 1U); |
reedas | 1:f8f4e789361f | 179 | Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT, 1U, 7U); |
reedas | 1:f8f4e789361f | 180 | Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, 1U); |
reedas | 2:02954bc28fc5 | 181 | |
reedas | 1:f8f4e789361f | 182 | Cy_SCB_EZI2C_Init(CYBSP_CSD_COMM_HW, &CYBSP_CSD_COMM_config, &EZI2C_context); |
reedas | 1:f8f4e789361f | 183 | |
reedas | 1:f8f4e789361f | 184 | /* Initialize and enable EZI2C interrupts */ |
reedas | 1:f8f4e789361f | 185 | Cy_SysInt_Init(&EZI2C_ISR_cfg, &EZI2C_InterruptHandler); |
reedas | 1:f8f4e789361f | 186 | NVIC_EnableIRQ(EZI2C_ISR_cfg.intrSrc); |
reedas | 1:f8f4e789361f | 187 | |
reedas | 1:f8f4e789361f | 188 | /* Set up communication data buffer to CapSense data structure to be exposed |
reedas | 1:f8f4e789361f | 189 | * to I2C master at primary slave address request. |
reedas | 1:f8f4e789361f | 190 | */ |
reedas | 1:f8f4e789361f | 191 | Cy_SCB_EZI2C_SetBuffer1(CYBSP_CSD_COMM_HW, (uint8 *)&cy_capsense_tuner, |
reedas | 2:02954bc28fc5 | 192 | sizeof(cy_capsense_tuner), sizeof(cy_capsense_tuner), &EZI2C_context); |
reedas | 1:f8f4e789361f | 193 | |
reedas | 1:f8f4e789361f | 194 | /* Enable EZI2C block */ |
reedas | 1:f8f4e789361f | 195 | Cy_SCB_EZI2C_Enable(CYBSP_CSD_COMM_HW); |
reedas | 1:f8f4e789361f | 196 | } |
reedas | 1:f8f4e789361f | 197 | |
reedas | 0:4ad2c16b6e43 | 198 | |
reedas | 1:f8f4e789361f | 199 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 200 | * Function Name: ProcessTouchStatus |
reedas | 1:f8f4e789361f | 201 | ******************************************************************************** |
reedas | 1:f8f4e789361f | 202 | * |
reedas | 1:f8f4e789361f | 203 | * Summary: |
reedas | 1:f8f4e789361f | 204 | * Controls the LED status according to the status of CapSense widgets and |
reedas | 1:f8f4e789361f | 205 | * prints the status to serial terminal. |
reedas | 1:f8f4e789361f | 206 | * |
reedas | 1:f8f4e789361f | 207 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 208 | void ProcessTouchStatus(void) |
reedas | 1:f8f4e789361f | 209 | { |
reedas | 2:02954bc28fc5 | 210 | uint32_t currSliderPos; |
reedas | 2:02954bc28fc5 | 211 | uint32_t currBtn0Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON0_WDGT_ID, CY_CAPSENSE_BUTTON0_SNS0_ID, &cy_capsense_context); |
reedas | 2:02954bc28fc5 | 212 | uint32_t currBtn1Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON1_WDGT_ID, CY_CAPSENSE_BUTTON1_SNS0_ID, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 213 | cy_stc_capsense_touch_t *sldrTouch = Cy_CapSense_GetTouchInfo(CY_CAPSENSE_LINEARSLIDER0_WDGT_ID, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 214 | char outputString[80]; |
reedas | 2:02954bc28fc5 | 215 | if(currBtn0Status != prevBtn0Status) { |
reedas | 1:f8f4e789361f | 216 | printf("Button_0 status: %u\r\n", currBtn0Status); |
reedas | 1:f8f4e789361f | 217 | sprintf(outputString," Button_0 status: %u ", currBtn0Status); |
reedas | 1:f8f4e789361f | 218 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 219 | GUI_DispStringAt(outputString, 160, 60); |
reedas | 2:02954bc28fc5 | 220 | if (currBtn0Status) { |
reedas | 2:02954bc28fc5 | 221 | if(btn0ToggleStatus) { |
reedas | 2:02954bc28fc5 | 222 | btn0ToggleStatus = false; |
reedas | 2:02954bc28fc5 | 223 | /* Turn The Blue RGB LED off */ |
reedas | 2:02954bc28fc5 | 224 | ledBlue2 = LED_OFF; |
reedas | 2:02954bc28fc5 | 225 | /* turn virtual Blue led off */ |
reedas | 2:02954bc28fc5 | 226 | GUI_SetColor(50); |
reedas | 2:02954bc28fc5 | 227 | GUI_FillCircle(240,70,8); |
reedas | 2:02954bc28fc5 | 228 | } else { |
reedas | 2:02954bc28fc5 | 229 | btn0ToggleStatus = true; |
reedas | 2:02954bc28fc5 | 230 | /* Turn The Blue RGB LED on */ |
reedas | 2:02954bc28fc5 | 231 | ledBlue2 = LED_ON; |
reedas | 2:02954bc28fc5 | 232 | /* turn virtual led on */ |
reedas | 2:02954bc28fc5 | 233 | GUI_SetColor(255); |
reedas | 2:02954bc28fc5 | 234 | GUI_FillCircle(240,70,8); |
reedas | 2:02954bc28fc5 | 235 | } |
reedas | 2:02954bc28fc5 | 236 | } |
reedas | 2:02954bc28fc5 | 237 | GUI_SetColor(GUI_WHITE); |
reedas | 2:02954bc28fc5 | 238 | |
reedas | 1:f8f4e789361f | 239 | prevBtn0Status = currBtn0Status; |
reedas | 1:f8f4e789361f | 240 | } |
reedas | 2:02954bc28fc5 | 241 | |
reedas | 2:02954bc28fc5 | 242 | if(currBtn1Status != prevBtn1Status) { |
reedas | 1:f8f4e789361f | 243 | printf("Button_1 status: %u\r\n", currBtn1Status); |
reedas | 1:f8f4e789361f | 244 | sprintf(outputString," Button_1 status: %u ", currBtn1Status); |
reedas | 1:f8f4e789361f | 245 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 246 | GUI_DispStringAt(outputString, 160, 80); |
reedas | 2:02954bc28fc5 | 247 | btn1ToggleStatus = !btn1ToggleStatus; |
reedas | 2:02954bc28fc5 | 248 | if (currBtn1Status) { |
reedas | 2:02954bc28fc5 | 249 | if(btn1ToggleStatus) { |
reedas | 2:02954bc28fc5 | 250 | btn1ToggleStatus = false; |
reedas | 2:02954bc28fc5 | 251 | /* Turn The Red RGB LED off */ |
reedas | 2:02954bc28fc5 | 252 | ledRed2 = LED_OFF; |
reedas | 2:02954bc28fc5 | 253 | /* turn virtual led off */ |
reedas | 2:02954bc28fc5 | 254 | GUI_SetColor(0x320000); |
reedas | 2:02954bc28fc5 | 255 | GUI_FillCircle(240,90,8); |
reedas | 2:02954bc28fc5 | 256 | } else { |
reedas | 2:02954bc28fc5 | 257 | btn1ToggleStatus = true; |
reedas | 2:02954bc28fc5 | 258 | /* Turn The Red RGB LED on */ |
reedas | 2:02954bc28fc5 | 259 | ledRed2 = LED_ON; |
reedas | 2:02954bc28fc5 | 260 | /* turn virtual led on */ |
reedas | 2:02954bc28fc5 | 261 | GUI_SetColor(0xff0000); |
reedas | 2:02954bc28fc5 | 262 | GUI_FillCircle(240,90,8); |
reedas | 2:02954bc28fc5 | 263 | } |
reedas | 2:02954bc28fc5 | 264 | } |
reedas | 2:02954bc28fc5 | 265 | GUI_SetColor(GUI_WHITE); |
reedas | 1:f8f4e789361f | 266 | prevBtn1Status = currBtn1Status; |
reedas | 2:02954bc28fc5 | 267 | } |
reedas | 1:f8f4e789361f | 268 | if(sw2Pressed != prevSw2Pressed) { |
reedas | 2:02954bc28fc5 | 269 | printf("Button sw2 status: %u\r\n", sw2Pressed); |
reedas | 1:f8f4e789361f | 270 | sprintf(outputString," Button_2 status: %u ", sw2Pressed); |
reedas | 1:f8f4e789361f | 271 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 272 | GUI_DispStringAt(outputString, 160, 100); |
reedas | 2:02954bc28fc5 | 273 | sw2ToggleStatus = !sw2ToggleStatus; |
reedas | 2:02954bc28fc5 | 274 | if (sw2Pressed) { |
reedas | 2:02954bc28fc5 | 275 | if(sw2ToggleStatus) { |
reedas | 2:02954bc28fc5 | 276 | sw2ToggleStatus = false; |
reedas | 2:02954bc28fc5 | 277 | /* Turn The Green RGB LED off */ |
reedas | 2:02954bc28fc5 | 278 | ledGreen2 = LED_OFF; |
reedas | 2:02954bc28fc5 | 279 | /* turn virtual Green led off */ |
reedas | 2:02954bc28fc5 | 280 | GUI_SetColor(0x3200); |
reedas | 2:02954bc28fc5 | 281 | GUI_FillCircle(240,110,8); |
reedas | 2:02954bc28fc5 | 282 | } else { |
reedas | 2:02954bc28fc5 | 283 | sw2ToggleStatus = true; |
reedas | 2:02954bc28fc5 | 284 | /* Turn The Green RGB LED on */ |
reedas | 2:02954bc28fc5 | 285 | ledGreen2 = LED_ON; |
reedas | 2:02954bc28fc5 | 286 | /* turn virtual Green led on */ |
reedas | 2:02954bc28fc5 | 287 | GUI_SetColor(0xff00); |
reedas | 2:02954bc28fc5 | 288 | GUI_FillCircle(240,110,8); |
reedas | 2:02954bc28fc5 | 289 | } |
reedas | 2:02954bc28fc5 | 290 | } |
reedas | 2:02954bc28fc5 | 291 | GUI_SetColor(GUI_WHITE); |
reedas | 1:f8f4e789361f | 292 | prevSw2Pressed = sw2Pressed; |
reedas | 1:f8f4e789361f | 293 | } |
reedas | 2:02954bc28fc5 | 294 | if (sldrTouch->numPosition == SLIDER_NUM_TOUCH) { |
reedas | 1:f8f4e789361f | 295 | currSliderPos = sldrTouch->ptrPosition->x; |
reedas | 0:4ad2c16b6e43 | 296 | |
reedas | 2:02954bc28fc5 | 297 | if(currSliderPos != prevSliderPos) { |
reedas | 1:f8f4e789361f | 298 | printf("Slider position: %u\r\n", currSliderPos); |
reedas | 1:f8f4e789361f | 299 | sprintf(outputString," Slider position: %u ", currSliderPos); |
reedas | 1:f8f4e789361f | 300 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 301 | GUI_DispStringAt(outputString, 160, 120); |
reedas | 2:02954bc28fc5 | 302 | myPwm.pulsewidth_ms((100-currSliderPos)/10); |
reedas | 2:02954bc28fc5 | 303 | GUI_SetColor(GUI_BLACK); |
reedas | 2:02954bc28fc5 | 304 | GUI_SetPenSize(10); |
reedas | 2:02954bc28fc5 | 305 | GUI_DrawLine((61 + (currSliderPos * 2)), 150, 260, 150); |
reedas | 2:02954bc28fc5 | 306 | GUI_SetColor(GUI_MAGENTA); |
reedas | 2:02954bc28fc5 | 307 | GUI_SetPenSize(10); |
reedas | 2:02954bc28fc5 | 308 | GUI_DrawLine(60, 150, (60 + (currSliderPos * 2)), 150); |
reedas | 2:02954bc28fc5 | 309 | GUI_SetColor(GUI_WHITE); |
reedas | 1:f8f4e789361f | 310 | prevSliderPos = currSliderPos; |
reedas | 0:4ad2c16b6e43 | 311 | } |
reedas | 0:4ad2c16b6e43 | 312 | } |
reedas | 2:02954bc28fc5 | 313 | /* flash a LED when a key press is detected */ |
reedas | 2:02954bc28fc5 | 314 | userLed = (sw2Pressed || currBtn0Status || currBtn1Status || (sldrTouch->numPosition == SLIDER_NUM_TOUCH)) ? nLED_ON : nLED_OFF; |
reedas | 0:4ad2c16b6e43 | 315 | } |
reedas | 0:4ad2c16b6e43 | 316 | |
reedas | 0:4ad2c16b6e43 | 317 | |
reedas | 1:f8f4e789361f | 318 | /******************************************************************************* |
reedas | 1:f8f4e789361f | 319 | * Function Name: EZI2C_InterruptHandler |
reedas | 1:f8f4e789361f | 320 | ******************************************************************************** |
reedas | 1:f8f4e789361f | 321 | * Summary: |
reedas | 2:02954bc28fc5 | 322 | * Wrapper function for handling interrupts from EZI2C block. |
reedas | 1:f8f4e789361f | 323 | * |
reedas | 1:f8f4e789361f | 324 | *******************************************************************************/ |
reedas | 1:f8f4e789361f | 325 | void EZI2C_InterruptHandler(void) |
reedas | 1:f8f4e789361f | 326 | { |
reedas | 1:f8f4e789361f | 327 | Cy_SCB_EZI2C_Interrupt(CYBSP_CSD_COMM_HW, &EZI2C_context); |
reedas | 1:f8f4e789361f | 328 | } |
reedas | 1:f8f4e789361f | 329 | |
reedas | 1:f8f4e789361f | 330 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 331 | * Function Name: CapSense_InterruptHandler() |
reedas | 1:f8f4e789361f | 332 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 333 | * Summary: |
reedas | 1:f8f4e789361f | 334 | * Wrapper function for handling interrupts from CSD block. |
reedas | 1:f8f4e789361f | 335 | * |
reedas | 1:f8f4e789361f | 336 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 337 | void CapSense_InterruptHandler(void) |
reedas | 1:f8f4e789361f | 338 | { |
reedas | 1:f8f4e789361f | 339 | Cy_CapSense_InterruptHandler(CYBSP_CSD_HW, &cy_capsense_context); |
reedas | 1:f8f4e789361f | 340 | } |
reedas | 1:f8f4e789361f | 341 | |
reedas | 1:f8f4e789361f | 342 | |
reedas | 1:f8f4e789361f | 343 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 344 | * Function Name: CapSenseEndOfScanCallback() |
reedas | 1:f8f4e789361f | 345 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 346 | * Summary: |
reedas | 1:f8f4e789361f | 347 | * This function releases a semaphore to indicate end of a CapSense scan. |
reedas | 1:f8f4e789361f | 348 | * |
reedas | 1:f8f4e789361f | 349 | * Parameters: |
reedas | 1:f8f4e789361f | 350 | * cy_stc_active_scan_sns_t* : pointer to active sensor details. |
reedas | 1:f8f4e789361f | 351 | * |
reedas | 1:f8f4e789361f | 352 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 353 | void CapSenseEndOfScanCallback(cy_stc_active_scan_sns_t * ptrActiveScan) |
reedas | 1:f8f4e789361f | 354 | { |
reedas | 1:f8f4e789361f | 355 | capsense_sem.release(); |
reedas | 1:f8f4e789361f | 356 | } |
reedas | 1:f8f4e789361f | 357 | |
reedas | 1:f8f4e789361f | 358 | |
reedas | 1:f8f4e789361f | 359 | /***************************************************************************** |
reedas | 1:f8f4e789361f | 360 | * Function Name: InitCapSenseClock() |
reedas | 1:f8f4e789361f | 361 | ****************************************************************************** |
reedas | 1:f8f4e789361f | 362 | * Summary: |
reedas | 2:02954bc28fc5 | 363 | * This function configures the peripheral clock for CapSense. |
reedas | 1:f8f4e789361f | 364 | * |
reedas | 1:f8f4e789361f | 365 | *****************************************************************************/ |
reedas | 1:f8f4e789361f | 366 | void InitCapSenseClock(void) |
reedas | 1:f8f4e789361f | 367 | { |
reedas | 1:f8f4e789361f | 368 | Cy_SysClk_PeriphAssignDivider(PCLK_CSD_CLOCK, CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM); |
reedas | 1:f8f4e789361f | 369 | Cy_SysClk_PeriphDisableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM); |
reedas | 1:f8f4e789361f | 370 | Cy_SysClk_PeriphSetDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM, 0u); |
reedas | 1:f8f4e789361f | 371 | Cy_SysClk_PeriphEnableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM); |
reedas | 1:f8f4e789361f | 372 | } |
reedas | 2:02954bc28fc5 | 373 | void LED_Init(void) |
reedas | 1:f8f4e789361f | 374 | { |
reedas | 2:02954bc28fc5 | 375 | /* Turn off all LEDs */ |
reedas | 2:02954bc28fc5 | 376 | ledGreen = nLED_OFF; |
reedas | 2:02954bc28fc5 | 377 | ledRed = nLED_OFF; |
reedas | 2:02954bc28fc5 | 378 | ledBlue = nLED_OFF; |
reedas | 2:02954bc28fc5 | 379 | userLed = nLED_OFF; |
reedas | 2:02954bc28fc5 | 380 | ledGreen2 = LED_OFF; /* 1 for on 0 for off */ |
reedas | 2:02954bc28fc5 | 381 | ledRed2 = LED_OFF; |
reedas | 2:02954bc28fc5 | 382 | ledBlue2 = LED_OFF; |
reedas | 2:02954bc28fc5 | 383 | |
reedas | 2:02954bc28fc5 | 384 | /* Setup pwm output initially at 50% */ |
reedas | 2:02954bc28fc5 | 385 | myPwm.period_ms(10); |
reedas | 2:02954bc28fc5 | 386 | myPwm.pulsewidth_ms(5); |
reedas | 2:02954bc28fc5 | 387 | |
reedas | 2:02954bc28fc5 | 388 | } |
reedas | 2:02954bc28fc5 | 389 | |
reedas | 2:02954bc28fc5 | 390 | void Display_Init(void) |
reedas | 2:02954bc28fc5 | 391 | { |
reedas | 2:02954bc28fc5 | 392 | |
reedas | 2:02954bc28fc5 | 393 | /* memory buffer for sprintf */ |
reedas | 1:f8f4e789361f | 394 | char outputString[80]; |
reedas | 2:02954bc28fc5 | 395 | |
reedas | 1:f8f4e789361f | 396 | /* Set font size, foreground and background Colours */ |
reedas | 1:f8f4e789361f | 397 | GUI_SetFont(GUI_FONT_16B_1); |
reedas | 1:f8f4e789361f | 398 | GUI_SetColor(GUI_WHITE); |
reedas | 1:f8f4e789361f | 399 | GUI_SetBkColor(GUI_BLACK); |
reedas | 1:f8f4e789361f | 400 | |
reedas | 2:02954bc28fc5 | 401 | /* Clear screen and print splash screen */ |
reedas | 1:f8f4e789361f | 402 | GUI_Clear(); |
reedas | 1:f8f4e789361f | 403 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 404 | GUI_DispStringAt("Capsense Demo", 160, 20); |
reedas | 1:f8f4e789361f | 405 | printf("\r\nApplication has started. Touch any CapSense button or slider.\r\n"); |
reedas | 1:f8f4e789361f | 406 | sprintf(outputString,"\r\nApplication has started.\r\n Touch any CapSense button or slider."); |
reedas | 1:f8f4e789361f | 407 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 408 | GUI_DispStringAt(outputString, 160, 180); |
reedas | 1:f8f4e789361f | 409 | wait(0.10); |
reedas | 2:02954bc28fc5 | 410 | } |
reedas | 2:02954bc28fc5 | 411 | |
reedas | 2:02954bc28fc5 | 412 | void CapSense_Init(void) |
reedas | 2:02954bc28fc5 | 413 | { |
reedas | 2:02954bc28fc5 | 414 | |
reedas | 2:02954bc28fc5 | 415 | /* memory buffer for sprintf */ |
reedas | 2:02954bc28fc5 | 416 | char outputString[80]; |
reedas | 2:02954bc28fc5 | 417 | |
reedas | 2:02954bc28fc5 | 418 | /* Configure AMUX bus for CapSense */ |
reedas | 1:f8f4e789361f | 419 | init_cycfg_routing(); |
reedas | 2:02954bc28fc5 | 420 | |
reedas | 1:f8f4e789361f | 421 | /* Configure PERI clocks for CapSense */ |
reedas | 2:02954bc28fc5 | 422 | InitCapSenseClock(); |
reedas | 2:02954bc28fc5 | 423 | |
reedas | 1:f8f4e789361f | 424 | InitTunerCommunication(); |
reedas | 2:02954bc28fc5 | 425 | |
reedas | 1:f8f4e789361f | 426 | /* Initialize the CSD HW block to the default state. */ |
reedas | 1:f8f4e789361f | 427 | cy_status status = Cy_CapSense_Init(&cy_capsense_context); |
reedas | 2:02954bc28fc5 | 428 | if(CY_RET_SUCCESS != status) { |
reedas | 1:f8f4e789361f | 429 | printf("CapSense initialization failed. Status code: %u\r\n", status); |
reedas | 1:f8f4e789361f | 430 | sprintf(outputString,"CapSense initialization failed. Status code: %u", status); |
reedas | 1:f8f4e789361f | 431 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 1:f8f4e789361f | 432 | GUI_DispStringAt(outputString, 160, 40); |
reedas | 1:f8f4e789361f | 433 | wait(osWaitForever); |
reedas | 1:f8f4e789361f | 434 | } |
reedas | 1:f8f4e789361f | 435 | /* Initialize CapSense interrupt */ |
reedas | 1:f8f4e789361f | 436 | Cy_SysInt_Init(&CapSense_ISR_cfg, &CapSense_InterruptHandler); |
reedas | 1:f8f4e789361f | 437 | NVIC_ClearPendingIRQ(CapSense_ISR_cfg.intrSrc); |
reedas | 1:f8f4e789361f | 438 | NVIC_EnableIRQ(CapSense_ISR_cfg.intrSrc); |
reedas | 1:f8f4e789361f | 439 | |
reedas | 1:f8f4e789361f | 440 | /* Initialize the CapSense firmware modules. */ |
reedas | 1:f8f4e789361f | 441 | Cy_CapSense_Enable(&cy_capsense_context); |
reedas | 1:f8f4e789361f | 442 | Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E, CapSenseEndOfScanCallback, &cy_capsense_context); |
reedas | 2:02954bc28fc5 | 443 | |
reedas | 2:02954bc28fc5 | 444 | } |
reedas | 1:f8f4e789361f | 445 | |
reedas | 2:02954bc28fc5 | 446 | /* Setup debounced mechanical buttons and callbacks*/ |
reedas | 2:02954bc28fc5 | 447 | void Button_Init(void) |
reedas | 2:02954bc28fc5 | 448 | { |
reedas | 1:f8f4e789361f | 449 | |
reedas | 2:02954bc28fc5 | 450 | /* Use internal pullups for pushbutton */ |
reedas | 2:02954bc28fc5 | 451 | pb2.mode(PullUp); |
reedas | 1:f8f4e789361f | 452 | // pb2.mode(PullUp); |
reedas | 1:f8f4e789361f | 453 | // Delay for initial pullup to take effect |
reedas | 1:f8f4e789361f | 454 | wait(.01); |
reedas | 1:f8f4e789361f | 455 | // Setup Interrupt callback functions for a pb hit |
reedas | 1:f8f4e789361f | 456 | pb2.attach_deasserted(&pb2_hit_callback); |
reedas | 1:f8f4e789361f | 457 | // pb2.attach_deasserted(&pb2_hit_callback); |
reedas | 1:f8f4e789361f | 458 | // Start sampling pb inputs using interrupts |
reedas | 1:f8f4e789361f | 459 | pb2.setSampleFrequency(); |
reedas | 1:f8f4e789361f | 460 | |
reedas | 1:f8f4e789361f | 461 | pb2.attach_asserted( &pb2_released_callback ); |
reedas | 1:f8f4e789361f | 462 | } |
reedas | 1:f8f4e789361f | 463 | |
reedas | 2:02954bc28fc5 | 464 | /***************************************************************************** |
reedas | 2:02954bc28fc5 | 465 | * Function Name: main() |
reedas | 2:02954bc28fc5 | 466 | ****************************************************************************** |
reedas | 2:02954bc28fc5 | 467 | * Summary: |
reedas | 2:02954bc28fc5 | 468 | * Main function that starts a thread for CapSense scan, initialises the |
reedas | 2:02954bc28fc5 | 469 | * tft screen, debounce buttons, turns off LEDs and enters a forever |
reedas | 2:02954bc28fc5 | 470 | * wait state. |
reedas | 2:02954bc28fc5 | 471 | * |
reedas | 2:02954bc28fc5 | 472 | *****************************************************************************/ |
reedas | 2:02954bc28fc5 | 473 | int main(void) |
reedas | 2:02954bc28fc5 | 474 | { |
reedas | 2:02954bc28fc5 | 475 | |
reedas | 2:02954bc28fc5 | 476 | /* Initialise EmWin driver*/ |
reedas | 2:02954bc28fc5 | 477 | GUI_Init(); |
reedas | 2:02954bc28fc5 | 478 | |
reedas | 2:02954bc28fc5 | 479 | /* Initialise display */ |
reedas | 2:02954bc28fc5 | 480 | Display_Init(); |
reedas | 2:02954bc28fc5 | 481 | |
reedas | 2:02954bc28fc5 | 482 | /* Set up CapSense paramteters */ |
reedas | 2:02954bc28fc5 | 483 | CapSense_Init(); |
reedas | 2:02954bc28fc5 | 484 | |
reedas | 2:02954bc28fc5 | 485 | /* Create a thread to run CapSense scan periodically using an event queue |
reedas | 2:02954bc28fc5 | 486 | * dispatcher. |
reedas | 2:02954bc28fc5 | 487 | */ |
reedas | 2:02954bc28fc5 | 488 | Thread thread(osPriorityNormal, OS_STACK_SIZE, NULL, "CapSense Scan Thread"); |
reedas | 2:02954bc28fc5 | 489 | thread.start(callback(&queue, &EventQueue::dispatch_forever)); |
reedas | 2:02954bc28fc5 | 490 | queue.call_every(CAPSENSE_SCAN_PERIOD_MS, RunCapSenseScan); |
reedas | 2:02954bc28fc5 | 491 | |
reedas | 2:02954bc28fc5 | 492 | /* Initiate scan immediately since the first call of RunCapSenseScan() |
reedas | 2:02954bc28fc5 | 493 | * happens CAPSENSE_SCAN_PERIOD_MS after the event queue dispatcher has |
reedas | 2:02954bc28fc5 | 494 | * started. |
reedas | 2:02954bc28fc5 | 495 | */ |
reedas | 2:02954bc28fc5 | 496 | Cy_CapSense_ScanAllWidgets(&cy_capsense_context); |
reedas | 2:02954bc28fc5 | 497 | |
reedas | 2:02954bc28fc5 | 498 | /* Setup debounced mechanical buttons and callbacks*/ |
reedas | 2:02954bc28fc5 | 499 | Button_Init(); |
reedas | 2:02954bc28fc5 | 500 | |
reedas | 2:02954bc28fc5 | 501 | /* Turn off all LEDs, set up pwm drive */ |
reedas | 2:02954bc28fc5 | 502 | LED_Init(); |
reedas | 2:02954bc28fc5 | 503 | |
reedas | 2:02954bc28fc5 | 504 | /* everything is started so goto sleep forever while events control the hardware */ |
reedas | 2:02954bc28fc5 | 505 | wait(osWaitForever); |
reedas | 2:02954bc28fc5 | 506 | |
reedas | 2:02954bc28fc5 | 507 | } |
reedas | 2:02954bc28fc5 | 508 | |
reedas | 2:02954bc28fc5 | 509 |