![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
striscia led
Dependencies: X_NUCLEO_LED61A1 mbed
Fork of HelloWorld_LED61A1 by
main.cpp@4:aeeb361d0227, 2015-12-18 (annotated)
- Committer:
- Davidroid
- Date:
- Fri Dec 18 17:05:26 2015 +0000
- Revision:
- 4:aeeb361d0227
- Parent:
- 3:ed347127fc38
- Child:
- 5:b00bbf1edfa9
+ Demo Usage Added.
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 | 3:ed347127fc38 | 51 | /* PI. */ |
Davidroid | 3:ed347127fc38 | 52 | #ifndef M_PI |
Davidroid | 3:ed347127fc38 | 53 | #define M_PI 3.14159265358979323846f |
Davidroid | 3:ed347127fc38 | 54 | #endif |
Davidroid | 3:ed347127fc38 | 55 | |
Davidroid | 2:dbd596540d46 | 56 | /* Loop period in micro-seconds. */ |
Davidroid | 3:ed347127fc38 | 57 | #define LOOP_PERIOD_us 1E5 |
Davidroid | 3:ed347127fc38 | 58 | |
Davidroid | 3:ed347127fc38 | 59 | /* Sin period in micro-seconds. */ |
Davidroid | 3:ed347127fc38 | 60 | #define PWM_SIN_PERIOD_us 1E7 |
Davidroid | 3:ed347127fc38 | 61 | #define ANALOG_SIN_PERIOD_us 1E7 |
Davidroid | 2:dbd596540d46 | 62 | |
Davidroid | 2:dbd596540d46 | 63 | /* Duration of button press in milli-seconds. */ |
Davidroid | 2:dbd596540d46 | 64 | #define SWITCH_DEMO_BUTTON_PRESS_ms 500 |
Davidroid | 2:dbd596540d46 | 65 | #define SWITCH_POWER_BUTTON_PRESS_ms 2000 |
Davidroid | 2:dbd596540d46 | 66 | |
Davidroid | 2:dbd596540d46 | 67 | /* Dimming Step. */ |
Davidroid | 2:dbd596540d46 | 68 | #define DIMMING_STEP 0.1f |
Davidroid | 2:dbd596540d46 | 69 | |
Davidroid | 2:dbd596540d46 | 70 | |
Davidroid | 2:dbd596540d46 | 71 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 2:dbd596540d46 | 72 | |
Davidroid | 2:dbd596540d46 | 73 | /* Demos. */ |
Davidroid | 2:dbd596540d46 | 74 | typedef enum |
Davidroid | 2:dbd596540d46 | 75 | { |
Davidroid | 2:dbd596540d46 | 76 | PWM_DIMMING_STEPS = 0, |
Davidroid | 2:dbd596540d46 | 77 | ANALOG_DIMMING_STEPS, |
Davidroid | 2:dbd596540d46 | 78 | PWM_DIMMING_VARY, |
Davidroid | 2:dbd596540d46 | 79 | ANALOG_DIMMING_VARY, |
Davidroid | 2:dbd596540d46 | 80 | ANALOG_DIMMING_PHOTO, |
Davidroid | 2:dbd596540d46 | 81 | LED_DEMO_SIZE |
Davidroid | 2:dbd596540d46 | 82 | } LED_Demo_t; |
Davidroid | 2:dbd596540d46 | 83 | |
Davidroid | 2:dbd596540d46 | 84 | /* Actions. */ |
Davidroid | 2:dbd596540d46 | 85 | typedef enum |
Davidroid | 2:dbd596540d46 | 86 | { |
Davidroid | 2:dbd596540d46 | 87 | SWITCH_POWER = 0, |
Davidroid | 2:dbd596540d46 | 88 | SWITCH_DEMO, |
Davidroid | 2:dbd596540d46 | 89 | SWITCH_STATE, |
Davidroid | 2:dbd596540d46 | 90 | NO_ACTION |
Davidroid | 2:dbd596540d46 | 91 | } LED_Action_t; |
Davidroid | 1:1c01b873ad45 | 92 | |
Davidroid | 1:1c01b873ad45 | 93 | |
Davidroid | 1:1c01b873ad45 | 94 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 95 | |
Davidroid | 2:dbd596540d46 | 96 | /* Main loop's ticker. */ |
Davidroid | 2:dbd596540d46 | 97 | static Ticker ticker; |
Davidroid | 1:1c01b873ad45 | 98 | |
Davidroid | 2:dbd596540d46 | 99 | /* User Button's interrupt. */ |
Davidroid | 2:dbd596540d46 | 100 | static InterruptIn button(USER_BUTTON); |
Davidroid | 1:1c01b873ad45 | 101 | |
Davidroid | 1:1c01b873ad45 | 102 | /* MCU Board's Led which provides the user with a visual feedback on |
Davidroid | 1:1c01b873ad45 | 103 | the user button's status (ON = pressed, OFF = released). */ |
Davidroid | 1:1c01b873ad45 | 104 | DigitalOut button_pressed_led(LED1); |
Davidroid | 1:1c01b873ad45 | 105 | |
Davidroid | 2:dbd596540d46 | 106 | /* LED Control Component. */ |
Davidroid | 2:dbd596540d46 | 107 | LED6001 *led; |
Davidroid | 2:dbd596540d46 | 108 | |
Davidroid | 2:dbd596540d46 | 109 | /* Interrupt flags. */ |
Davidroid | 2:dbd596540d46 | 110 | static volatile bool ticker_irq_triggered; |
Davidroid | 2:dbd596540d46 | 111 | static volatile bool button_irq_triggered; |
Davidroid | 2:dbd596540d46 | 112 | static volatile bool xfault_irq_triggered; |
Davidroid | 2:dbd596540d46 | 113 | |
Davidroid | 2:dbd596540d46 | 114 | /* Demo State. */ |
Davidroid | 2:dbd596540d46 | 115 | static volatile LED_Demo_t demo; |
Davidroid | 2:dbd596540d46 | 116 | static volatile LED_Action_t action; |
Davidroid | 2:dbd596540d46 | 117 | static volatile bool power_on; |
Davidroid | 2:dbd596540d46 | 118 | |
Davidroid | 2:dbd596540d46 | 119 | /* PWM and Analog dimming values. */ |
Davidroid | 2:dbd596540d46 | 120 | static float pwm_dimming; |
Davidroid | 2:dbd596540d46 | 121 | static float analog_dimming; |
Davidroid | 2:dbd596540d46 | 122 | |
Davidroid | 2:dbd596540d46 | 123 | /* Demos' Names. */ |
Davidroid | 2:dbd596540d46 | 124 | static char* demos[] = |
Davidroid | 2:dbd596540d46 | 125 | { |
Davidroid | 2:dbd596540d46 | 126 | "Manual PWM Dimming", |
Davidroid | 2:dbd596540d46 | 127 | "Manual Analog Dimming", |
Davidroid | 3:ed347127fc38 | 128 | "Sinusoidal PWM Dimming", |
Davidroid | 3:ed347127fc38 | 129 | "Sinusoidal Analog Dimming", |
Davidroid | 2:dbd596540d46 | 130 | "Photo-based PWM Dimming" |
Davidroid | 2:dbd596540d46 | 131 | }; |
Davidroid | 1:1c01b873ad45 | 132 | |
Davidroid | 1:1c01b873ad45 | 133 | |
Davidroid | 1:1c01b873ad45 | 134 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 135 | |
Davidroid | 1:1c01b873ad45 | 136 | /** |
Davidroid | 2:dbd596540d46 | 137 | * @brief Initilizing the demo. |
Davidroid | 2:dbd596540d46 | 138 | * @param None. |
Davidroid | 2:dbd596540d46 | 139 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 140 | */ |
Davidroid | 2:dbd596540d46 | 141 | void LEDInit(void) |
Davidroid | 1:1c01b873ad45 | 142 | { |
Davidroid | 2:dbd596540d46 | 143 | /* Initializing Interrupt flags. */ |
Davidroid | 2:dbd596540d46 | 144 | button_irq_triggered = false; |
Davidroid | 2:dbd596540d46 | 145 | xfault_irq_triggered = false; |
Davidroid | 2:dbd596540d46 | 146 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 147 | |
Davidroid | 2:dbd596540d46 | 148 | /* Initializing Demo. */ |
Davidroid | 2:dbd596540d46 | 149 | power_on = true; |
Davidroid | 2:dbd596540d46 | 150 | action = SWITCH_DEMO; |
Davidroid | 2:dbd596540d46 | 151 | demo = (LED_Demo_t) (LED_DEMO_SIZE - 1); |
Davidroid | 2:dbd596540d46 | 152 | |
Davidroid | 2:dbd596540d46 | 153 | /* Initializing PWM and Analog dimming to maximum values. */ |
Davidroid | 2:dbd596540d46 | 154 | pwm_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 155 | analog_dimming = 1.0f; |
Davidroid | 1:1c01b873ad45 | 156 | } |
Davidroid | 1:1c01b873ad45 | 157 | |
Davidroid | 1:1c01b873ad45 | 158 | /** |
Davidroid | 2:dbd596540d46 | 159 | * @brief Handling the LED capabilities and executing several demos. |
Davidroid | 2:dbd596540d46 | 160 | * @param None. |
Davidroid | 2:dbd596540d46 | 161 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 162 | */ |
Davidroid | 2:dbd596540d46 | 163 | void LEDHandler(void) |
Davidroid | 1:1c01b873ad45 | 164 | { |
Davidroid | 3:ed347127fc38 | 165 | static int tick = 0; |
Davidroid | 3:ed347127fc38 | 166 | |
Davidroid | 3:ed347127fc38 | 167 | /* Handling the power switch. */ |
Davidroid | 2:dbd596540d46 | 168 | if (action == SWITCH_POWER) |
Davidroid | 3:ed347127fc38 | 169 | { |
Davidroid | 3:ed347127fc38 | 170 | /* Switching the LED power ON/OFF. */ |
Davidroid | 2:dbd596540d46 | 171 | power_on = !power_on; |
Davidroid | 2:dbd596540d46 | 172 | |
Davidroid | 3:ed347127fc38 | 173 | if (power_on) |
Davidroid | 3:ed347127fc38 | 174 | { |
Davidroid | 3:ed347127fc38 | 175 | /* Initializing PWM and Analog dimming to maximum values. */ |
Davidroid | 3:ed347127fc38 | 176 | pwm_dimming = 1.0f; |
Davidroid | 3:ed347127fc38 | 177 | analog_dimming = 1.0f; |
Davidroid | 3:ed347127fc38 | 178 | } |
Davidroid | 3:ed347127fc38 | 179 | else |
Davidroid | 3:ed347127fc38 | 180 | { |
Davidroid | 3:ed347127fc38 | 181 | /* Printing to the console. */ |
Davidroid | 3:ed347127fc38 | 182 | printf("%-56s\r", "Power OFF"); |
Davidroid | 3:ed347127fc38 | 183 | |
Davidroid | 3:ed347127fc38 | 184 | /* Powering OFF the LED. */ |
Davidroid | 3:ed347127fc38 | 185 | led->PowerOFF(); |
Davidroid | 3:ed347127fc38 | 186 | } |
Davidroid | 3:ed347127fc38 | 187 | } |
Davidroid | 3:ed347127fc38 | 188 | |
Davidroid | 3:ed347127fc38 | 189 | /* Handling the LED dimming when powered ON. */ |
Davidroid | 2:dbd596540d46 | 190 | if (power_on) |
Davidroid | 2:dbd596540d46 | 191 | { |
Davidroid | 2:dbd596540d46 | 192 | /* Switching to the next demo. */ |
Davidroid | 2:dbd596540d46 | 193 | if (action == SWITCH_DEMO) |
Davidroid | 2:dbd596540d46 | 194 | { |
Davidroid | 2:dbd596540d46 | 195 | pwm_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 196 | analog_dimming = 1.0f; |
Davidroid | 3:ed347127fc38 | 197 | tick = 0; |
Davidroid | 2:dbd596540d46 | 198 | demo = (LED_Demo_t) ((demo + 1) % LED_DEMO_SIZE); |
Davidroid | 2:dbd596540d46 | 199 | } |
Davidroid | 2:dbd596540d46 | 200 | |
Davidroid | 3:ed347127fc38 | 201 | /* Setting the LED dimming depending on the selected demo. */ |
Davidroid | 2:dbd596540d46 | 202 | switch (demo) |
Davidroid | 2:dbd596540d46 | 203 | { |
Davidroid | 2:dbd596540d46 | 204 | /* Changing PWM dimming according to the user button. */ |
Davidroid | 2:dbd596540d46 | 205 | case PWM_DIMMING_STEPS: |
Davidroid | 2:dbd596540d46 | 206 | if (action == SWITCH_STATE) |
Davidroid | 2:dbd596540d46 | 207 | { |
Davidroid | 2:dbd596540d46 | 208 | pwm_dimming -= DIMMING_STEP; |
Davidroid | 2:dbd596540d46 | 209 | pwm_dimming = (pwm_dimming <= 0.0f ? 1.0f : pwm_dimming); |
Davidroid | 2:dbd596540d46 | 210 | } |
Davidroid | 2:dbd596540d46 | 211 | break; |
Davidroid | 3:ed347127fc38 | 212 | |
Davidroid | 2:dbd596540d46 | 213 | /* Changing Analog dimming according to the user button. */ |
Davidroid | 2:dbd596540d46 | 214 | case ANALOG_DIMMING_STEPS: |
Davidroid | 2:dbd596540d46 | 215 | if (action == SWITCH_STATE) |
Davidroid | 2:dbd596540d46 | 216 | { |
Davidroid | 2:dbd596540d46 | 217 | analog_dimming -= DIMMING_STEP; |
Davidroid | 2:dbd596540d46 | 218 | analog_dimming = (analog_dimming <= 0.0f ? 1.0f : analog_dimming); |
Davidroid | 2:dbd596540d46 | 219 | } |
Davidroid | 2:dbd596540d46 | 220 | break; |
Davidroid | 3:ed347127fc38 | 221 | |
Davidroid | 2:dbd596540d46 | 222 | /* Changing PWM dimming continuously. */ |
Davidroid | 2:dbd596540d46 | 223 | case PWM_DIMMING_VARY: |
Davidroid | 3:ed347127fc38 | 224 | pwm_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / PWM_SIN_PERIOD_us) + 0.5f; |
Davidroid | 3:ed347127fc38 | 225 | tick %= (int) (PWM_SIN_PERIOD_us / LOOP_PERIOD_us) + 1; |
Davidroid | 2:dbd596540d46 | 226 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 227 | break; |
Davidroid | 3:ed347127fc38 | 228 | |
Davidroid | 2:dbd596540d46 | 229 | /* Changing Analog dimming continuously. */ |
Davidroid | 2:dbd596540d46 | 230 | case ANALOG_DIMMING_VARY: |
Davidroid | 3:ed347127fc38 | 231 | analog_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / ANALOG_SIN_PERIOD_us) + 0.5f; |
Davidroid | 3:ed347127fc38 | 232 | tick %= (int) (ANALOG_SIN_PERIOD_us / LOOP_PERIOD_us) + 1; |
Davidroid | 2:dbd596540d46 | 233 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 234 | break; |
Davidroid | 3:ed347127fc38 | 235 | |
Davidroid | 2:dbd596540d46 | 236 | /* Setting Analog dimming according to the photo sensor. */ |
Davidroid | 2:dbd596540d46 | 237 | case ANALOG_DIMMING_PHOTO: |
Davidroid | 2:dbd596540d46 | 238 | //analog_dimming = (uint8_t) (led->GetCurrent() * ADC_RANGE / 72 + 8); |
Davidroid | 3:ed347127fc38 | 239 | analog_dimming = 1.0f - led->GetCurrent(); |
Davidroid | 2:dbd596540d46 | 240 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 241 | break; |
Davidroid | 2:dbd596540d46 | 242 | } |
Davidroid | 2:dbd596540d46 | 243 | |
Davidroid | 3:ed347127fc38 | 244 | /* Writing PWM and Analog dimming values to the LED. */ |
Davidroid | 3:ed347127fc38 | 245 | if (action != NO_ACTION) |
Davidroid | 3:ed347127fc38 | 246 | { |
Davidroid | 3:ed347127fc38 | 247 | /* Printing to the console. */ |
Davidroid | 3:ed347127fc38 | 248 | printf("%d) %-26s PWM/Analog: %.3f %.3f\r", demo + 1, demos[demo], pwm_dimming, analog_dimming); |
Davidroid | 2:dbd596540d46 | 249 | |
Davidroid | 2:dbd596540d46 | 250 | /* Writing PWM and Analog dimming values to the LED. */ |
Davidroid | 2:dbd596540d46 | 251 | led->SetPWMDimming(pwm_dimming); |
Davidroid | 2:dbd596540d46 | 252 | led->SetAnalogDimming(analog_dimming); |
Davidroid | 2:dbd596540d46 | 253 | } |
Davidroid | 3:ed347127fc38 | 254 | } |
Davidroid | 2:dbd596540d46 | 255 | |
Davidroid | 3:ed347127fc38 | 256 | /* Resetting the action. */ |
Davidroid | 3:ed347127fc38 | 257 | action = NO_ACTION; |
Davidroid | 2:dbd596540d46 | 258 | } |
Davidroid | 2:dbd596540d46 | 259 | |
Davidroid | 2:dbd596540d46 | 260 | /** |
Davidroid | 2:dbd596540d46 | 261 | * @brief Interrupt Request for the main loop's ticker related interrupt. |
Davidroid | 2:dbd596540d46 | 262 | * @param None. |
Davidroid | 2:dbd596540d46 | 263 | * @retval None. |
Davidroid | 2:dbd596540d46 | 264 | */ |
Davidroid | 2:dbd596540d46 | 265 | void TickerIRQ(void) |
Davidroid | 2:dbd596540d46 | 266 | { |
Davidroid | 2:dbd596540d46 | 267 | ticker_irq_triggered = true; |
Davidroid | 2:dbd596540d46 | 268 | } |
Davidroid | 2:dbd596540d46 | 269 | |
Davidroid | 2:dbd596540d46 | 270 | /** |
Davidroid | 2:dbd596540d46 | 271 | * @brief Interrupt Request for the user button's interrupt. |
Davidroid | 2:dbd596540d46 | 272 | * @param None. |
Davidroid | 2:dbd596540d46 | 273 | * @retval None. |
Davidroid | 2:dbd596540d46 | 274 | */ |
Davidroid | 2:dbd596540d46 | 275 | void ButtonIRQ(void) |
Davidroid | 2:dbd596540d46 | 276 | { |
Davidroid | 2:dbd596540d46 | 277 | button_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 278 | button.disable_irq(); |
Davidroid | 1:1c01b873ad45 | 279 | } |
Davidroid | 1:1c01b873ad45 | 280 | |
Davidroid | 1:1c01b873ad45 | 281 | /** |
Davidroid | 2:dbd596540d46 | 282 | * @brief Interrupt Request for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 283 | * @param None. |
Davidroid | 2:dbd596540d46 | 284 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 285 | */ |
Davidroid | 1:1c01b873ad45 | 286 | void XFaultIRQ(void) |
Davidroid | 1:1c01b873ad45 | 287 | { |
Davidroid | 1:1c01b873ad45 | 288 | xfault_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 289 | led->DisableXFaultIRQ(); |
Davidroid | 1:1c01b873ad45 | 290 | } |
Davidroid | 1:1c01b873ad45 | 291 | |
Davidroid | 1:1c01b873ad45 | 292 | /** |
Davidroid | 2:dbd596540d46 | 293 | * @brief Interrupt Handler for the user button's interrupt. |
Davidroid | 2:dbd596540d46 | 294 | * @param None. |
Davidroid | 2:dbd596540d46 | 295 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 296 | */ |
Davidroid | 1:1c01b873ad45 | 297 | void ButtonHandler(void) |
Davidroid | 1:1c01b873ad45 | 298 | { |
Davidroid | 2:dbd596540d46 | 299 | /* User Button's timer to measure the time the button remains pressed. */ |
Davidroid | 2:dbd596540d46 | 300 | static Timer button_pressed_timer; |
Davidroid | 2:dbd596540d46 | 301 | |
Davidroid | 1:1c01b873ad45 | 302 | bool button_pressed_flag = (button.read() == 0 ? true : false); |
Davidroid | 1:1c01b873ad45 | 303 | |
Davidroid | 1:1c01b873ad45 | 304 | if (button_pressed_flag) |
Davidroid | 1:1c01b873ad45 | 305 | { |
Davidroid | 1:1c01b873ad45 | 306 | button_pressed_led = 1; |
Davidroid | 1:1c01b873ad45 | 307 | button_pressed_timer.start(); |
Davidroid | 1:1c01b873ad45 | 308 | } |
Davidroid | 1:1c01b873ad45 | 309 | else |
Davidroid | 1:1c01b873ad45 | 310 | { |
Davidroid | 1:1c01b873ad45 | 311 | button_pressed_led = 0; |
Davidroid | 1:1c01b873ad45 | 312 | button_pressed_timer.stop(); |
Davidroid | 1:1c01b873ad45 | 313 | |
Davidroid | 1:1c01b873ad45 | 314 | /* Either changing current demo's state or switching to the next demo. */ |
Davidroid | 2:dbd596540d46 | 315 | int time_pressed = button_pressed_timer.read_ms(); |
Davidroid | 2:dbd596540d46 | 316 | if (time_pressed < SWITCH_DEMO_BUTTON_PRESS_ms) |
Davidroid | 2:dbd596540d46 | 317 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 318 | else if (time_pressed < SWITCH_POWER_BUTTON_PRESS_ms) |
Davidroid | 2:dbd596540d46 | 319 | action = SWITCH_DEMO; |
Davidroid | 1:1c01b873ad45 | 320 | else |
Davidroid | 2:dbd596540d46 | 321 | action = SWITCH_POWER; |
Davidroid | 1:1c01b873ad45 | 322 | |
Davidroid | 1:1c01b873ad45 | 323 | button_pressed_timer.reset(); |
Davidroid | 1:1c01b873ad45 | 324 | } |
Davidroid | 1:1c01b873ad45 | 325 | |
Davidroid | 1:1c01b873ad45 | 326 | button.enable_irq(); |
Davidroid | 1:1c01b873ad45 | 327 | } |
Davidroid | 0:f5a41692e1fb | 328 | |
Davidroid | 2:dbd596540d46 | 329 | /** |
Davidroid | 2:dbd596540d46 | 330 | * @brief Interrupt Handler for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 331 | * @param None. |
Davidroid | 2:dbd596540d46 | 332 | * @retval None. |
Davidroid | 2:dbd596540d46 | 333 | */ |
Davidroid | 2:dbd596540d46 | 334 | void XFaultHandler(void) |
Davidroid | 2:dbd596540d46 | 335 | { |
Davidroid | 2:dbd596540d46 | 336 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 337 | printf("XFAULT Interrupt detected! Re-initializing LED driver...\r\n"); |
Davidroid | 2:dbd596540d46 | 338 | |
Davidroid | 2:dbd596540d46 | 339 | /* Re-initializing the demo. */ |
Davidroid | 2:dbd596540d46 | 340 | LEDInit(); |
Davidroid | 2:dbd596540d46 | 341 | |
Davidroid | 2:dbd596540d46 | 342 | led->EnableXFaultIRQ(); |
Davidroid | 2:dbd596540d46 | 343 | } |
Davidroid | 2:dbd596540d46 | 344 | |
Davidroid | 0:f5a41692e1fb | 345 | |
Davidroid | 0:f5a41692e1fb | 346 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 347 | |
Davidroid | 0:f5a41692e1fb | 348 | int main() |
Davidroid | 0:f5a41692e1fb | 349 | { |
Davidroid | 0:f5a41692e1fb | 350 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 351 | |
Davidroid | 2:dbd596540d46 | 352 | /* Printing to the console. */ |
Davidroid | 4:aeeb361d0227 | 353 | printf("LED Control Application Example\r\n\n"\ |
Davidroid | 4:aeeb361d0227 | 354 | "+ Short Button Press [<%.1fs] --> Manual Dimming\r\n"\ |
Davidroid | 4:aeeb361d0227 | 355 | "+ Medium Button Press --> Switch Demo\r\n"\ |
Davidroid | 4:aeeb361d0227 | 356 | "+ Long Button Press [>%.1fs] --> Switch Power ON/OFF\r\n\n"\ |
Davidroid | 4:aeeb361d0227 | 357 | , SWITCH_DEMO_BUTTON_PRESS_ms / 1000.0f, SWITCH_POWER_BUTTON_PRESS_ms / 1000.0f); |
Davidroid | 2:dbd596540d46 | 358 | |
Davidroid | 1:1c01b873ad45 | 359 | /* Initializing LED Control Component. */ |
Davidroid | 2:dbd596540d46 | 360 | led = new LED6001(D4, A3, D6, D5); |
Davidroid | 2:dbd596540d46 | 361 | if (led->Init() != COMPONENT_OK) |
Davidroid | 1:1c01b873ad45 | 362 | exit(EXIT_FAILURE); |
Davidroid | 1:1c01b873ad45 | 363 | |
Davidroid | 1:1c01b873ad45 | 364 | /* Attaching interrupt request functions. */ |
Davidroid | 1:1c01b873ad45 | 365 | button.fall(ButtonIRQ); |
Davidroid | 1:1c01b873ad45 | 366 | button.rise(ButtonIRQ); |
Davidroid | 1:1c01b873ad45 | 367 | led->AttachXFaultIRQ(&XFaultIRQ); |
Davidroid | 2:dbd596540d46 | 368 | ticker.attach_us(TickerIRQ, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 369 | |
Davidroid | 2:dbd596540d46 | 370 | /* Initializing the demo. */ |
Davidroid | 2:dbd596540d46 | 371 | LEDInit(); |
Davidroid | 0:f5a41692e1fb | 372 | |
Davidroid | 0:f5a41692e1fb | 373 | |
Davidroid | 0:f5a41692e1fb | 374 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 375 | |
Davidroid | 2:dbd596540d46 | 376 | /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
Davidroid | 1:1c01b873ad45 | 377 | while (true) |
Davidroid | 1:1c01b873ad45 | 378 | { |
Davidroid | 2:dbd596540d46 | 379 | if (ticker_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 380 | { |
Davidroid | 2:dbd596540d46 | 381 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 382 | LEDHandler(); |
Davidroid | 1:1c01b873ad45 | 383 | } else if (button_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 384 | { |
Davidroid | 1:1c01b873ad45 | 385 | button_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 386 | ButtonHandler(); |
Davidroid | 1:1c01b873ad45 | 387 | } else if(xfault_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 388 | { |
Davidroid | 1:1c01b873ad45 | 389 | xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 390 | XFaultHandler(); |
Davidroid | 1:1c01b873ad45 | 391 | } else |
Davidroid | 1:1c01b873ad45 | 392 | { |
Davidroid | 1:1c01b873ad45 | 393 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 394 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 395 | } |
Davidroid | 1:1c01b873ad45 | 396 | } |
Davidroid | 1:1c01b873ad45 | 397 | } |