![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
striscia led
Dependencies: X_NUCLEO_LED61A1 mbed
Fork of HelloWorld_LED61A1 by
main.cpp@2:dbd596540d46, 2015-12-17 (annotated)
- Committer:
- Davidroid
- Date:
- Thu Dec 17 18:13:57 2015 +0000
- Revision:
- 2:dbd596540d46
- Parent:
- 1:1c01b873ad45
- Child:
- 3:ed347127fc38
+ Implementation completed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:f5a41692e1fb | 1 | /** |
Davidroid | 0:f5a41692e1fb | 2 | ****************************************************************************** |
Davidroid | 0:f5a41692e1fb | 3 | * @file main.cpp |
Davidroid | 0:f5a41692e1fb | 4 | * @author Davide Aliprandi, STMicrolectronics |
Davidroid | 0:f5a41692e1fb | 5 | * @version V1.0.0 |
Davidroid | 0:f5a41692e1fb | 6 | * @date December 9th, 2015 |
Davidroid | 0:f5a41692e1fb | 7 | * @brief mbed test application for the STMicrolectronics X-NUCLEO-LED61A1 |
Davidroid | 0:f5a41692e1fb | 8 | * LED expansion board. |
Davidroid | 0:f5a41692e1fb | 9 | ****************************************************************************** |
Davidroid | 0:f5a41692e1fb | 10 | * @attention |
Davidroid | 0:f5a41692e1fb | 11 | * |
Davidroid | 0:f5a41692e1fb | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
Davidroid | 0:f5a41692e1fb | 13 | * |
Davidroid | 0:f5a41692e1fb | 14 | * Redistribution and use in source and binary forms, with or without modification, |
Davidroid | 0:f5a41692e1fb | 15 | * are permitted provided that the following conditions are met: |
Davidroid | 0:f5a41692e1fb | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:f5a41692e1fb | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:f5a41692e1fb | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Davidroid | 0:f5a41692e1fb | 19 | * this list of conditions and the following disclaimer in the documentation |
Davidroid | 0:f5a41692e1fb | 20 | * and/or other materials provided with the distribution. |
Davidroid | 0:f5a41692e1fb | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Davidroid | 0:f5a41692e1fb | 22 | * may be used to endorse or promote products derived from this software |
Davidroid | 0:f5a41692e1fb | 23 | * without specific prior written permission. |
Davidroid | 0:f5a41692e1fb | 24 | * |
Davidroid | 0:f5a41692e1fb | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:f5a41692e1fb | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:f5a41692e1fb | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Davidroid | 0:f5a41692e1fb | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Davidroid | 0:f5a41692e1fb | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Davidroid | 0:f5a41692e1fb | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Davidroid | 0:f5a41692e1fb | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Davidroid | 0:f5a41692e1fb | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Davidroid | 0:f5a41692e1fb | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Davidroid | 0:f5a41692e1fb | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:f5a41692e1fb | 35 | * |
Davidroid | 0:f5a41692e1fb | 36 | ****************************************************************************** |
Davidroid | 0:f5a41692e1fb | 37 | */ |
Davidroid | 0:f5a41692e1fb | 38 | |
Davidroid | 0:f5a41692e1fb | 39 | |
Davidroid | 0:f5a41692e1fb | 40 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 41 | |
Davidroid | 0:f5a41692e1fb | 42 | /* mbed specific header files. */ |
Davidroid | 0:f5a41692e1fb | 43 | #include "mbed.h" |
Davidroid | 0:f5a41692e1fb | 44 | |
Davidroid | 1:1c01b873ad45 | 45 | /* Component specific header files. */ |
Davidroid | 1:1c01b873ad45 | 46 | #include "led6001_class.h" |
Davidroid | 1:1c01b873ad45 | 47 | |
Davidroid | 1:1c01b873ad45 | 48 | |
Davidroid | 1:1c01b873ad45 | 49 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 50 | |
Davidroid | 2:dbd596540d46 | 51 | /* Loop period in micro-seconds. */ |
Davidroid | 2:dbd596540d46 | 52 | #define LOOP_PERIOD_us 1E6 |
Davidroid | 2:dbd596540d46 | 53 | |
Davidroid | 2:dbd596540d46 | 54 | /* Duration of button press in milli-seconds. */ |
Davidroid | 2:dbd596540d46 | 55 | #define SWITCH_DEMO_BUTTON_PRESS_ms 500 |
Davidroid | 2:dbd596540d46 | 56 | #define SWITCH_POWER_BUTTON_PRESS_ms 2000 |
Davidroid | 2:dbd596540d46 | 57 | |
Davidroid | 2:dbd596540d46 | 58 | /* Dimming Step. */ |
Davidroid | 2:dbd596540d46 | 59 | #define DIMMING_STEP 0.1f |
Davidroid | 2:dbd596540d46 | 60 | |
Davidroid | 2:dbd596540d46 | 61 | |
Davidroid | 2:dbd596540d46 | 62 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 2:dbd596540d46 | 63 | |
Davidroid | 2:dbd596540d46 | 64 | /* Demos. */ |
Davidroid | 2:dbd596540d46 | 65 | typedef enum |
Davidroid | 2:dbd596540d46 | 66 | { |
Davidroid | 2:dbd596540d46 | 67 | PWM_DIMMING_STEPS = 0, |
Davidroid | 2:dbd596540d46 | 68 | ANALOG_DIMMING_STEPS, |
Davidroid | 2:dbd596540d46 | 69 | PWM_DIMMING_VARY, |
Davidroid | 2:dbd596540d46 | 70 | ANALOG_DIMMING_VARY, |
Davidroid | 2:dbd596540d46 | 71 | ANALOG_DIMMING_PHOTO, |
Davidroid | 2:dbd596540d46 | 72 | LED_DEMO_SIZE |
Davidroid | 2:dbd596540d46 | 73 | } LED_Demo_t; |
Davidroid | 2:dbd596540d46 | 74 | |
Davidroid | 2:dbd596540d46 | 75 | /* Actions. */ |
Davidroid | 2:dbd596540d46 | 76 | typedef enum |
Davidroid | 2:dbd596540d46 | 77 | { |
Davidroid | 2:dbd596540d46 | 78 | SWITCH_POWER = 0, |
Davidroid | 2:dbd596540d46 | 79 | SWITCH_DEMO, |
Davidroid | 2:dbd596540d46 | 80 | SWITCH_STATE, |
Davidroid | 2:dbd596540d46 | 81 | NO_ACTION |
Davidroid | 2:dbd596540d46 | 82 | } LED_Action_t; |
Davidroid | 1:1c01b873ad45 | 83 | |
Davidroid | 1:1c01b873ad45 | 84 | |
Davidroid | 1:1c01b873ad45 | 85 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 86 | |
Davidroid | 2:dbd596540d46 | 87 | /* Main loop's ticker. */ |
Davidroid | 2:dbd596540d46 | 88 | static Ticker ticker; |
Davidroid | 1:1c01b873ad45 | 89 | |
Davidroid | 2:dbd596540d46 | 90 | /* User Button's interrupt. */ |
Davidroid | 2:dbd596540d46 | 91 | static InterruptIn button(USER_BUTTON); |
Davidroid | 1:1c01b873ad45 | 92 | |
Davidroid | 1:1c01b873ad45 | 93 | /* MCU Board's Led which provides the user with a visual feedback on |
Davidroid | 1:1c01b873ad45 | 94 | the user button's status (ON = pressed, OFF = released). */ |
Davidroid | 1:1c01b873ad45 | 95 | DigitalOut button_pressed_led(LED1); |
Davidroid | 1:1c01b873ad45 | 96 | |
Davidroid | 2:dbd596540d46 | 97 | /* LED Control Component. */ |
Davidroid | 2:dbd596540d46 | 98 | LED6001 *led; |
Davidroid | 2:dbd596540d46 | 99 | |
Davidroid | 2:dbd596540d46 | 100 | /* Interrupt flags. */ |
Davidroid | 2:dbd596540d46 | 101 | static volatile bool ticker_irq_triggered; |
Davidroid | 2:dbd596540d46 | 102 | static volatile bool button_irq_triggered; |
Davidroid | 2:dbd596540d46 | 103 | static volatile bool xfault_irq_triggered; |
Davidroid | 2:dbd596540d46 | 104 | |
Davidroid | 2:dbd596540d46 | 105 | /* Demo State. */ |
Davidroid | 2:dbd596540d46 | 106 | static volatile LED_Demo_t demo; |
Davidroid | 2:dbd596540d46 | 107 | static volatile LED_Action_t action; |
Davidroid | 2:dbd596540d46 | 108 | static volatile bool power_on; |
Davidroid | 2:dbd596540d46 | 109 | |
Davidroid | 2:dbd596540d46 | 110 | /* PWM and Analog dimming values. */ |
Davidroid | 2:dbd596540d46 | 111 | static float pwm_dimming; |
Davidroid | 2:dbd596540d46 | 112 | static float analog_dimming; |
Davidroid | 2:dbd596540d46 | 113 | |
Davidroid | 2:dbd596540d46 | 114 | /* Demos' Names. */ |
Davidroid | 2:dbd596540d46 | 115 | static char* demos[] = |
Davidroid | 2:dbd596540d46 | 116 | { |
Davidroid | 2:dbd596540d46 | 117 | "Manual PWM Dimming", |
Davidroid | 2:dbd596540d46 | 118 | "Manual Analog Dimming", |
Davidroid | 2:dbd596540d46 | 119 | "Cyclic PWM Dimming", |
Davidroid | 2:dbd596540d46 | 120 | "Cyclic Analog Dimming", |
Davidroid | 2:dbd596540d46 | 121 | "Photo-based PWM Dimming" |
Davidroid | 2:dbd596540d46 | 122 | }; |
Davidroid | 1:1c01b873ad45 | 123 | |
Davidroid | 1:1c01b873ad45 | 124 | |
Davidroid | 1:1c01b873ad45 | 125 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 126 | |
Davidroid | 1:1c01b873ad45 | 127 | /** |
Davidroid | 2:dbd596540d46 | 128 | * @brief Initilizing the demo. |
Davidroid | 2:dbd596540d46 | 129 | * @param None. |
Davidroid | 2:dbd596540d46 | 130 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 131 | */ |
Davidroid | 2:dbd596540d46 | 132 | void LEDInit(void) |
Davidroid | 1:1c01b873ad45 | 133 | { |
Davidroid | 2:dbd596540d46 | 134 | /* Initializing Interrupt flags. */ |
Davidroid | 2:dbd596540d46 | 135 | button_irq_triggered = false; |
Davidroid | 2:dbd596540d46 | 136 | xfault_irq_triggered = false; |
Davidroid | 2:dbd596540d46 | 137 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 138 | |
Davidroid | 2:dbd596540d46 | 139 | /* Initializing Demo. */ |
Davidroid | 2:dbd596540d46 | 140 | power_on = true; |
Davidroid | 2:dbd596540d46 | 141 | action = SWITCH_DEMO; |
Davidroid | 2:dbd596540d46 | 142 | demo = (LED_Demo_t) (LED_DEMO_SIZE - 1); |
Davidroid | 2:dbd596540d46 | 143 | |
Davidroid | 2:dbd596540d46 | 144 | /* Initializing PWM and Analog dimming to maximum values. */ |
Davidroid | 2:dbd596540d46 | 145 | pwm_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 146 | analog_dimming = 1.0f; |
Davidroid | 1:1c01b873ad45 | 147 | } |
Davidroid | 1:1c01b873ad45 | 148 | |
Davidroid | 1:1c01b873ad45 | 149 | /** |
Davidroid | 2:dbd596540d46 | 150 | * @brief Handling the LED capabilities and executing several demos. |
Davidroid | 2:dbd596540d46 | 151 | * @param None. |
Davidroid | 2:dbd596540d46 | 152 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 153 | */ |
Davidroid | 2:dbd596540d46 | 154 | void LEDHandler(void) |
Davidroid | 1:1c01b873ad45 | 155 | { |
Davidroid | 2:dbd596540d46 | 156 | /* Switching LED power ON/OFF. */ |
Davidroid | 2:dbd596540d46 | 157 | if (action == SWITCH_POWER) |
Davidroid | 2:dbd596540d46 | 158 | power_on = !power_on; |
Davidroid | 2:dbd596540d46 | 159 | |
Davidroid | 2:dbd596540d46 | 160 | /* Handling the LED when powered ON. */ |
Davidroid | 2:dbd596540d46 | 161 | if (power_on) |
Davidroid | 2:dbd596540d46 | 162 | { |
Davidroid | 2:dbd596540d46 | 163 | /* Switching to the next demo. */ |
Davidroid | 2:dbd596540d46 | 164 | if (action == SWITCH_DEMO) |
Davidroid | 2:dbd596540d46 | 165 | { |
Davidroid | 2:dbd596540d46 | 166 | pwm_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 167 | analog_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 168 | demo = (LED_Demo_t) ((demo + 1) % LED_DEMO_SIZE); |
Davidroid | 2:dbd596540d46 | 169 | } |
Davidroid | 2:dbd596540d46 | 170 | |
Davidroid | 2:dbd596540d46 | 171 | /* Setting the LED depending on the selected demo. */ |
Davidroid | 2:dbd596540d46 | 172 | switch (demo) |
Davidroid | 2:dbd596540d46 | 173 | { |
Davidroid | 2:dbd596540d46 | 174 | /* Changing PWM dimming according to the user button. */ |
Davidroid | 2:dbd596540d46 | 175 | case PWM_DIMMING_STEPS: |
Davidroid | 2:dbd596540d46 | 176 | if (action == SWITCH_STATE) |
Davidroid | 2:dbd596540d46 | 177 | { |
Davidroid | 2:dbd596540d46 | 178 | pwm_dimming -= DIMMING_STEP; |
Davidroid | 2:dbd596540d46 | 179 | pwm_dimming = (pwm_dimming <= 0.0f ? 1.0f : pwm_dimming); |
Davidroid | 2:dbd596540d46 | 180 | } |
Davidroid | 2:dbd596540d46 | 181 | break; |
Davidroid | 2:dbd596540d46 | 182 | |
Davidroid | 2:dbd596540d46 | 183 | /* Changing Analog dimming according to the user button. */ |
Davidroid | 2:dbd596540d46 | 184 | case ANALOG_DIMMING_STEPS: |
Davidroid | 2:dbd596540d46 | 185 | if (action == SWITCH_STATE) |
Davidroid | 2:dbd596540d46 | 186 | { |
Davidroid | 2:dbd596540d46 | 187 | analog_dimming -= DIMMING_STEP; |
Davidroid | 2:dbd596540d46 | 188 | analog_dimming = (analog_dimming <= 0.0f ? 1.0f : analog_dimming); |
Davidroid | 2:dbd596540d46 | 189 | } |
Davidroid | 2:dbd596540d46 | 190 | break; |
Davidroid | 2:dbd596540d46 | 191 | |
Davidroid | 2:dbd596540d46 | 192 | /* Changing PWM dimming continuously. */ |
Davidroid | 2:dbd596540d46 | 193 | case PWM_DIMMING_VARY: |
Davidroid | 2:dbd596540d46 | 194 | pwm_dimming -= DIMMING_STEP; |
Davidroid | 2:dbd596540d46 | 195 | pwm_dimming = (pwm_dimming <= 0.0f ? 1.0f : pwm_dimming); |
Davidroid | 2:dbd596540d46 | 196 | //wait_ms(50); |
Davidroid | 2:dbd596540d46 | 197 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 198 | break; |
Davidroid | 2:dbd596540d46 | 199 | |
Davidroid | 2:dbd596540d46 | 200 | /* Changing Analog dimming continuously. */ |
Davidroid | 2:dbd596540d46 | 201 | case ANALOG_DIMMING_VARY: |
Davidroid | 2:dbd596540d46 | 202 | analog_dimming -= DIMMING_STEP; |
Davidroid | 2:dbd596540d46 | 203 | analog_dimming = (analog_dimming <= 0.0f ? 1.0f : analog_dimming); |
Davidroid | 2:dbd596540d46 | 204 | //wait_ms(500); |
Davidroid | 2:dbd596540d46 | 205 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 206 | break; |
Davidroid | 2:dbd596540d46 | 207 | |
Davidroid | 2:dbd596540d46 | 208 | /* Setting Analog dimming according to the photo sensor. */ |
Davidroid | 2:dbd596540d46 | 209 | case ANALOG_DIMMING_PHOTO: |
Davidroid | 2:dbd596540d46 | 210 | //analog_dimming = (uint8_t) (led->GetCurrent() * ADC_RANGE / 72 + 8); |
Davidroid | 2:dbd596540d46 | 211 | analog_dimming = ((1.0f - led->GetCurrent()) * (1.0f - DIMMING_STEP) + DIMMING_STEP); |
Davidroid | 2:dbd596540d46 | 212 | //wait_ms(100); |
Davidroid | 2:dbd596540d46 | 213 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 214 | break; |
Davidroid | 2:dbd596540d46 | 215 | } |
Davidroid | 2:dbd596540d46 | 216 | } |
Davidroid | 2:dbd596540d46 | 217 | |
Davidroid | 2:dbd596540d46 | 218 | /* Writing PWM and Analog dimming values to the LED and printing to the console. */ |
Davidroid | 2:dbd596540d46 | 219 | if (action != NO_ACTION) |
Davidroid | 2:dbd596540d46 | 220 | { |
Davidroid | 2:dbd596540d46 | 221 | if (action == SWITCH_POWER) |
Davidroid | 2:dbd596540d46 | 222 | if (power_on) |
Davidroid | 2:dbd596540d46 | 223 | { |
Davidroid | 2:dbd596540d46 | 224 | /* Initializing PWM and Analog dimming to maximum values. */ |
Davidroid | 2:dbd596540d46 | 225 | pwm_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 226 | analog_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 227 | |
Davidroid | 2:dbd596540d46 | 228 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 229 | printf("Power ON\r\n"); |
Davidroid | 2:dbd596540d46 | 230 | } |
Davidroid | 2:dbd596540d46 | 231 | else |
Davidroid | 2:dbd596540d46 | 232 | { |
Davidroid | 2:dbd596540d46 | 233 | /* Powering OFF the LED. */ |
Davidroid | 2:dbd596540d46 | 234 | led->PowerOFF(); |
Davidroid | 2:dbd596540d46 | 235 | |
Davidroid | 2:dbd596540d46 | 236 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 237 | printf("Power OFF\r\n"); |
Davidroid | 2:dbd596540d46 | 238 | } |
Davidroid | 2:dbd596540d46 | 239 | |
Davidroid | 2:dbd596540d46 | 240 | if (power_on) |
Davidroid | 2:dbd596540d46 | 241 | { |
Davidroid | 2:dbd596540d46 | 242 | /* Writing PWM and Analog dimming values to the LED. */ |
Davidroid | 2:dbd596540d46 | 243 | led->SetPWMDimming(pwm_dimming); |
Davidroid | 2:dbd596540d46 | 244 | led->SetAnalogDimming(analog_dimming); |
Davidroid | 2:dbd596540d46 | 245 | |
Davidroid | 2:dbd596540d46 | 246 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 247 | printf("%-24s PWM/Analog: %.3f %.3f\r\n", demos[demo], pwm_dimming, analog_dimming); |
Davidroid | 2:dbd596540d46 | 248 | } |
Davidroid | 2:dbd596540d46 | 249 | |
Davidroid | 2:dbd596540d46 | 250 | /* Resetting action. */ |
Davidroid | 2:dbd596540d46 | 251 | action = NO_ACTION; |
Davidroid | 2:dbd596540d46 | 252 | } |
Davidroid | 2:dbd596540d46 | 253 | } |
Davidroid | 2:dbd596540d46 | 254 | |
Davidroid | 2:dbd596540d46 | 255 | /** |
Davidroid | 2:dbd596540d46 | 256 | * @brief Interrupt Request for the main loop's ticker related interrupt. |
Davidroid | 2:dbd596540d46 | 257 | * @param None. |
Davidroid | 2:dbd596540d46 | 258 | * @retval None. |
Davidroid | 2:dbd596540d46 | 259 | */ |
Davidroid | 2:dbd596540d46 | 260 | void TickerIRQ(void) |
Davidroid | 2:dbd596540d46 | 261 | { |
Davidroid | 2:dbd596540d46 | 262 | ticker_irq_triggered = true; |
Davidroid | 2:dbd596540d46 | 263 | } |
Davidroid | 2:dbd596540d46 | 264 | |
Davidroid | 2:dbd596540d46 | 265 | /** |
Davidroid | 2:dbd596540d46 | 266 | * @brief Interrupt Request for the user button's interrupt. |
Davidroid | 2:dbd596540d46 | 267 | * @param None. |
Davidroid | 2:dbd596540d46 | 268 | * @retval None. |
Davidroid | 2:dbd596540d46 | 269 | */ |
Davidroid | 2:dbd596540d46 | 270 | void ButtonIRQ(void) |
Davidroid | 2:dbd596540d46 | 271 | { |
Davidroid | 2:dbd596540d46 | 272 | button_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 273 | button.disable_irq(); |
Davidroid | 1:1c01b873ad45 | 274 | } |
Davidroid | 1:1c01b873ad45 | 275 | |
Davidroid | 1:1c01b873ad45 | 276 | /** |
Davidroid | 2:dbd596540d46 | 277 | * @brief Interrupt Request for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 278 | * @param None. |
Davidroid | 2:dbd596540d46 | 279 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 280 | */ |
Davidroid | 1:1c01b873ad45 | 281 | void XFaultIRQ(void) |
Davidroid | 1:1c01b873ad45 | 282 | { |
Davidroid | 1:1c01b873ad45 | 283 | xfault_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 284 | led->DisableXFaultIRQ(); |
Davidroid | 1:1c01b873ad45 | 285 | } |
Davidroid | 1:1c01b873ad45 | 286 | |
Davidroid | 1:1c01b873ad45 | 287 | /** |
Davidroid | 2:dbd596540d46 | 288 | * @brief Interrupt Handler for the user button's interrupt. |
Davidroid | 2:dbd596540d46 | 289 | * @param None. |
Davidroid | 2:dbd596540d46 | 290 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 291 | */ |
Davidroid | 1:1c01b873ad45 | 292 | void ButtonHandler(void) |
Davidroid | 1:1c01b873ad45 | 293 | { |
Davidroid | 2:dbd596540d46 | 294 | /* User Button's timer to measure the time the button remains pressed. */ |
Davidroid | 2:dbd596540d46 | 295 | static Timer button_pressed_timer; |
Davidroid | 2:dbd596540d46 | 296 | |
Davidroid | 1:1c01b873ad45 | 297 | bool button_pressed_flag = (button.read() == 0 ? true : false); |
Davidroid | 1:1c01b873ad45 | 298 | |
Davidroid | 1:1c01b873ad45 | 299 | if (button_pressed_flag) |
Davidroid | 1:1c01b873ad45 | 300 | { |
Davidroid | 1:1c01b873ad45 | 301 | button_pressed_led = 1; |
Davidroid | 1:1c01b873ad45 | 302 | button_pressed_timer.start(); |
Davidroid | 1:1c01b873ad45 | 303 | } |
Davidroid | 1:1c01b873ad45 | 304 | else |
Davidroid | 1:1c01b873ad45 | 305 | { |
Davidroid | 1:1c01b873ad45 | 306 | button_pressed_led = 0; |
Davidroid | 1:1c01b873ad45 | 307 | button_pressed_timer.stop(); |
Davidroid | 1:1c01b873ad45 | 308 | |
Davidroid | 1:1c01b873ad45 | 309 | /* Either changing current demo's state or switching to the next demo. */ |
Davidroid | 2:dbd596540d46 | 310 | int time_pressed = button_pressed_timer.read_ms(); |
Davidroid | 2:dbd596540d46 | 311 | if (time_pressed < SWITCH_DEMO_BUTTON_PRESS_ms) |
Davidroid | 2:dbd596540d46 | 312 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 313 | else if (time_pressed < SWITCH_POWER_BUTTON_PRESS_ms) |
Davidroid | 2:dbd596540d46 | 314 | action = SWITCH_DEMO; |
Davidroid | 1:1c01b873ad45 | 315 | else |
Davidroid | 2:dbd596540d46 | 316 | action = SWITCH_POWER; |
Davidroid | 1:1c01b873ad45 | 317 | |
Davidroid | 1:1c01b873ad45 | 318 | button_pressed_timer.reset(); |
Davidroid | 1:1c01b873ad45 | 319 | } |
Davidroid | 1:1c01b873ad45 | 320 | |
Davidroid | 1:1c01b873ad45 | 321 | button.enable_irq(); |
Davidroid | 1:1c01b873ad45 | 322 | } |
Davidroid | 0:f5a41692e1fb | 323 | |
Davidroid | 2:dbd596540d46 | 324 | /** |
Davidroid | 2:dbd596540d46 | 325 | * @brief Interrupt Handler for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 326 | * @param None. |
Davidroid | 2:dbd596540d46 | 327 | * @retval None. |
Davidroid | 2:dbd596540d46 | 328 | */ |
Davidroid | 2:dbd596540d46 | 329 | void XFaultHandler(void) |
Davidroid | 2:dbd596540d46 | 330 | { |
Davidroid | 2:dbd596540d46 | 331 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 332 | printf("XFAULT Interrupt detected! Re-initializing LED driver...\r\n"); |
Davidroid | 2:dbd596540d46 | 333 | |
Davidroid | 2:dbd596540d46 | 334 | /* Re-initializing the demo. */ |
Davidroid | 2:dbd596540d46 | 335 | LEDInit(); |
Davidroid | 2:dbd596540d46 | 336 | |
Davidroid | 2:dbd596540d46 | 337 | led->EnableXFaultIRQ(); |
Davidroid | 2:dbd596540d46 | 338 | } |
Davidroid | 2:dbd596540d46 | 339 | |
Davidroid | 0:f5a41692e1fb | 340 | |
Davidroid | 0:f5a41692e1fb | 341 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 342 | |
Davidroid | 0:f5a41692e1fb | 343 | int main() |
Davidroid | 0:f5a41692e1fb | 344 | { |
Davidroid | 0:f5a41692e1fb | 345 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 346 | |
Davidroid | 2:dbd596540d46 | 347 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 348 | printf("LED Control Application Example\r\n\n"); |
Davidroid | 2:dbd596540d46 | 349 | |
Davidroid | 1:1c01b873ad45 | 350 | /* Initializing LED Control Component. */ |
Davidroid | 2:dbd596540d46 | 351 | led = new LED6001(D4, A3, D6, D5); |
Davidroid | 2:dbd596540d46 | 352 | if (led->Init() != COMPONENT_OK) |
Davidroid | 1:1c01b873ad45 | 353 | exit(EXIT_FAILURE); |
Davidroid | 1:1c01b873ad45 | 354 | |
Davidroid | 1:1c01b873ad45 | 355 | /* Attaching interrupt request functions. */ |
Davidroid | 1:1c01b873ad45 | 356 | button.fall(ButtonIRQ); |
Davidroid | 1:1c01b873ad45 | 357 | button.rise(ButtonIRQ); |
Davidroid | 1:1c01b873ad45 | 358 | led->AttachXFaultIRQ(&XFaultIRQ); |
Davidroid | 2:dbd596540d46 | 359 | ticker.attach_us(TickerIRQ, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 360 | |
Davidroid | 2:dbd596540d46 | 361 | /* Initializing the demo. */ |
Davidroid | 2:dbd596540d46 | 362 | LEDInit(); |
Davidroid | 0:f5a41692e1fb | 363 | |
Davidroid | 0:f5a41692e1fb | 364 | |
Davidroid | 0:f5a41692e1fb | 365 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 366 | |
Davidroid | 2:dbd596540d46 | 367 | /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
Davidroid | 1:1c01b873ad45 | 368 | while (true) |
Davidroid | 1:1c01b873ad45 | 369 | { |
Davidroid | 2:dbd596540d46 | 370 | if (ticker_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 371 | { |
Davidroid | 2:dbd596540d46 | 372 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 373 | LEDHandler(); |
Davidroid | 1:1c01b873ad45 | 374 | } else if (button_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 375 | { |
Davidroid | 1:1c01b873ad45 | 376 | button_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 377 | ButtonHandler(); |
Davidroid | 1:1c01b873ad45 | 378 | } else if(xfault_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 379 | { |
Davidroid | 1:1c01b873ad45 | 380 | xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 381 | XFaultHandler(); |
Davidroid | 1:1c01b873ad45 | 382 | } else |
Davidroid | 1:1c01b873ad45 | 383 | { |
Davidroid | 1:1c01b873ad45 | 384 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 385 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 386 | } |
Davidroid | 1:1c01b873ad45 | 387 | } |
Davidroid | 1:1c01b873ad45 | 388 | } |