![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
Test application for the STMicroelectronics X-NUCLEO-LED61A1 LED Control Expansion Board showing several LED control modes.
Dependencies: X_NUCLEO_LED61A1 mbed
Fork of LedDimming_LED61A1 by
LED Control with the X-NUCLEO-LED61A1 Expansion Board
This application provides an example of usage of the X-NUCLEO-LED61A1 LED Control Expansion Board. It shows how to control a LED stripe load connected to the board through the following control modes:
- Manual PWM Dimming;
- Manual Analog Dimming;
- Sinusoidal PWM Dimming;
- Sinusoidal Analog Dimming;
- Photo-based Analog Dimming.
The button of the MCU board, when available, can be used in the following ways:
- Short Button Press [<0.5s] for Manual Dimming;
- Medium Button Press to Switch Demo;
- Long Button Press [>2s] to Switch Power ON/OFF.
The program starts in mode 1.
main.cpp@16:2497344ba436, 2017-07-13 (annotated)
- Committer:
- Davidroid
- Date:
- Thu Jul 13 16:23:20 2017 +0000
- Revision:
- 16:2497344ba436
- Parent:
- 15:ea1e402ce919
Adapting to ARM mbed coding style.
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 | 6:899828fd9d9d | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:f5a41692e1fb | 5 | * @version V1.0.0 |
Davidroid | 7:5763474c7551 | 6 | * @date February 4h, 2016 |
Davidroid | 6:899828fd9d9d | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-LED61A1 |
Davidroid | 7:5763474c7551 | 8 | * LED Control Expansion Board showing several LED control modes. |
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 | 16:2497344ba436 | 46 | #include "Led6001.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 | 14:f2ba2ca417d3 | 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 | 14:f2ba2ca417d3 | 57 | #define LOOP_PERIOD_us (1E5) |
Davidroid | 3:ed347127fc38 | 58 | |
Davidroid | 3:ed347127fc38 | 59 | /* Sin period in micro-seconds. */ |
Davidroid | 14:f2ba2ca417d3 | 60 | #define PWM_SIN_PERIOD_us (1E7) |
Davidroid | 14:f2ba2ca417d3 | 61 | #define ANALOG_SIN_PERIOD_us (1E7) |
Davidroid | 2:dbd596540d46 | 62 | |
Davidroid | 2:dbd596540d46 | 63 | /* Duration of button press in milli-seconds. */ |
Davidroid | 14:f2ba2ca417d3 | 64 | #define SWITCH_DEMO_BUTTON_PRESS_ms (500) |
Davidroid | 14:f2ba2ca417d3 | 65 | #define SWITCH_POWER_BUTTON_PRESS_ms (2000) |
Davidroid | 2:dbd596540d46 | 66 | |
Davidroid | 2:dbd596540d46 | 67 | /* Dimming Step. */ |
Davidroid | 14:f2ba2ca417d3 | 68 | #define DIMMING_STEP (0.05f) |
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 | 5:b00bbf1edfa9 | 76 | MANUAL_PWM_DIMMING = 0, |
Davidroid | 5:b00bbf1edfa9 | 77 | MANUAL_ANALOG_DIMMING, |
Davidroid | 5:b00bbf1edfa9 | 78 | AUTOMATIC_PWM_DIMMING, |
Davidroid | 5:b00bbf1edfa9 | 79 | AUTOMATIC_ANALOG_DIMMING, |
Davidroid | 5:b00bbf1edfa9 | 80 | PHOTO_BASED_ANALOG_DIMMING, |
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 | 16:2497344ba436 | 107 | Led6001 *led; |
Davidroid | 2:dbd596540d46 | 108 | |
Davidroid | 2:dbd596540d46 | 109 | /* Interrupt flags. */ |
Davidroid | 14:f2ba2ca417d3 | 110 | static volatile bool ticker_irq_triggered = false; |
Davidroid | 14:f2ba2ca417d3 | 111 | static volatile bool button_irq_triggered = false; |
Davidroid | 14:f2ba2ca417d3 | 112 | static volatile bool xfault_irq_triggered = false; |
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 | /* Demos' Names. */ |
Davidroid | 2:dbd596540d46 | 120 | static char* demos[] = |
Davidroid | 2:dbd596540d46 | 121 | { |
Davidroid | 2:dbd596540d46 | 122 | "Manual PWM Dimming", |
Davidroid | 2:dbd596540d46 | 123 | "Manual Analog Dimming", |
Davidroid | 3:ed347127fc38 | 124 | "Sinusoidal PWM Dimming", |
Davidroid | 3:ed347127fc38 | 125 | "Sinusoidal Analog Dimming", |
Davidroid | 5:b00bbf1edfa9 | 126 | "Photo-based Analog Dimming" |
Davidroid | 2:dbd596540d46 | 127 | }; |
Davidroid | 1:1c01b873ad45 | 128 | |
Davidroid | 1:1c01b873ad45 | 129 | |
Davidroid | 1:1c01b873ad45 | 130 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 131 | |
Davidroid | 1:1c01b873ad45 | 132 | /** |
Davidroid | 14:f2ba2ca417d3 | 133 | * @brief Initializing the demo. |
Davidroid | 2:dbd596540d46 | 134 | * @param None. |
Davidroid | 2:dbd596540d46 | 135 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 136 | */ |
Davidroid | 16:2497344ba436 | 137 | void init_demo(void) |
Davidroid | 1:1c01b873ad45 | 138 | { |
Davidroid | 2:dbd596540d46 | 139 | power_on = true; |
Davidroid | 2:dbd596540d46 | 140 | action = SWITCH_DEMO; |
Davidroid | 2:dbd596540d46 | 141 | demo = (LED_Demo_t) (LED_DEMO_SIZE - 1); |
Davidroid | 1:1c01b873ad45 | 142 | } |
Davidroid | 1:1c01b873ad45 | 143 | |
Davidroid | 1:1c01b873ad45 | 144 | /** |
Davidroid | 2:dbd596540d46 | 145 | * @brief Handling the LED capabilities and executing several demos. |
Davidroid | 2:dbd596540d46 | 146 | * @param None. |
Davidroid | 2:dbd596540d46 | 147 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 148 | */ |
Davidroid | 16:2497344ba436 | 149 | void led_handler(void) |
Davidroid | 1:1c01b873ad45 | 150 | { |
Davidroid | 3:ed347127fc38 | 151 | static int tick = 0; |
Davidroid | 14:f2ba2ca417d3 | 152 | static float pwm_dimming; |
Davidroid | 14:f2ba2ca417d3 | 153 | static float analog_dimming; |
Davidroid | 3:ed347127fc38 | 154 | |
Davidroid | 3:ed347127fc38 | 155 | /* Handling the power switch. */ |
Davidroid | 16:2497344ba436 | 156 | if (action == SWITCH_POWER) { |
Davidroid | 3:ed347127fc38 | 157 | /* Switching the LED power ON/OFF. */ |
Davidroid | 2:dbd596540d46 | 158 | power_on = !power_on; |
Davidroid | 2:dbd596540d46 | 159 | |
Davidroid | 16:2497344ba436 | 160 | if (power_on) { |
Davidroid | 3:ed347127fc38 | 161 | /* Initializing PWM and Analog dimming to maximum values. */ |
Davidroid | 3:ed347127fc38 | 162 | pwm_dimming = 1.0f; |
Davidroid | 3:ed347127fc38 | 163 | analog_dimming = 1.0f; |
Davidroid | 16:2497344ba436 | 164 | } else { |
Davidroid | 3:ed347127fc38 | 165 | /* Printing to the console. */ |
Davidroid | 3:ed347127fc38 | 166 | printf("%-56s\r", "Power OFF"); |
Davidroid | 3:ed347127fc38 | 167 | |
Davidroid | 3:ed347127fc38 | 168 | /* Powering OFF the LED. */ |
Davidroid | 16:2497344ba436 | 169 | led->power_off(); |
Davidroid | 3:ed347127fc38 | 170 | } |
Davidroid | 3:ed347127fc38 | 171 | } |
Davidroid | 3:ed347127fc38 | 172 | |
Davidroid | 3:ed347127fc38 | 173 | /* Handling the LED dimming when powered ON. */ |
Davidroid | 16:2497344ba436 | 174 | if (power_on) { |
Davidroid | 2:dbd596540d46 | 175 | /* Switching to the next demo. */ |
Davidroid | 16:2497344ba436 | 176 | if (action == SWITCH_DEMO) { |
Davidroid | 2:dbd596540d46 | 177 | pwm_dimming = 1.0f; |
Davidroid | 2:dbd596540d46 | 178 | analog_dimming = 1.0f; |
Davidroid | 3:ed347127fc38 | 179 | tick = 0; |
Davidroid | 2:dbd596540d46 | 180 | demo = (LED_Demo_t) ((demo + 1) % LED_DEMO_SIZE); |
Davidroid | 2:dbd596540d46 | 181 | } |
Davidroid | 2:dbd596540d46 | 182 | |
Davidroid | 3:ed347127fc38 | 183 | /* Setting the LED dimming depending on the selected demo. */ |
Davidroid | 16:2497344ba436 | 184 | switch (demo) { |
Davidroid | 2:dbd596540d46 | 185 | /* Changing PWM dimming according to the user button. */ |
Davidroid | 5:b00bbf1edfa9 | 186 | case MANUAL_PWM_DIMMING: |
Davidroid | 16:2497344ba436 | 187 | if (action == SWITCH_STATE) { |
Davidroid | 2:dbd596540d46 | 188 | pwm_dimming -= DIMMING_STEP; |
Davidroid | 5:b00bbf1edfa9 | 189 | pwm_dimming = (pwm_dimming < 0.0f ? 1.0f : pwm_dimming); |
Davidroid | 2:dbd596540d46 | 190 | } |
Davidroid | 2:dbd596540d46 | 191 | break; |
Davidroid | 3:ed347127fc38 | 192 | |
Davidroid | 2:dbd596540d46 | 193 | /* Changing Analog dimming according to the user button. */ |
Davidroid | 5:b00bbf1edfa9 | 194 | case MANUAL_ANALOG_DIMMING: |
Davidroid | 16:2497344ba436 | 195 | if (action == SWITCH_STATE) { |
Davidroid | 2:dbd596540d46 | 196 | analog_dimming -= DIMMING_STEP; |
Davidroid | 5:b00bbf1edfa9 | 197 | analog_dimming = (analog_dimming < 0.0f ? 1.0f : analog_dimming); |
Davidroid | 2:dbd596540d46 | 198 | } |
Davidroid | 2:dbd596540d46 | 199 | break; |
Davidroid | 3:ed347127fc38 | 200 | |
Davidroid | 2:dbd596540d46 | 201 | /* Changing PWM dimming continuously. */ |
Davidroid | 5:b00bbf1edfa9 | 202 | case AUTOMATIC_PWM_DIMMING: |
Davidroid | 3:ed347127fc38 | 203 | pwm_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / PWM_SIN_PERIOD_us) + 0.5f; |
Davidroid | 7:5763474c7551 | 204 | tick %= (int) (PWM_SIN_PERIOD_us / LOOP_PERIOD_us); |
Davidroid | 2:dbd596540d46 | 205 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 206 | break; |
Davidroid | 3:ed347127fc38 | 207 | |
Davidroid | 2:dbd596540d46 | 208 | /* Changing Analog dimming continuously. */ |
Davidroid | 5:b00bbf1edfa9 | 209 | case AUTOMATIC_ANALOG_DIMMING: |
Davidroid | 3:ed347127fc38 | 210 | analog_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / ANALOG_SIN_PERIOD_us) + 0.5f; |
Davidroid | 7:5763474c7551 | 211 | tick %= (int) (ANALOG_SIN_PERIOD_us / LOOP_PERIOD_us); |
Davidroid | 2:dbd596540d46 | 212 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 213 | break; |
Davidroid | 3:ed347127fc38 | 214 | |
Davidroid | 2:dbd596540d46 | 215 | /* Setting Analog dimming according to the photo sensor. */ |
Davidroid | 5:b00bbf1edfa9 | 216 | case PHOTO_BASED_ANALOG_DIMMING: |
Davidroid | 16:2497344ba436 | 217 | analog_dimming = 1.0f - led->get_current(); |
Davidroid | 2:dbd596540d46 | 218 | action = SWITCH_STATE; |
Davidroid | 2:dbd596540d46 | 219 | break; |
Davidroid | 2:dbd596540d46 | 220 | } |
Davidroid | 2:dbd596540d46 | 221 | |
Davidroid | 3:ed347127fc38 | 222 | /* Writing PWM and Analog dimming values to the LED. */ |
Davidroid | 16:2497344ba436 | 223 | if (action != NO_ACTION) { |
Davidroid | 3:ed347127fc38 | 224 | /* Printing to the console. */ |
Davidroid | 5:b00bbf1edfa9 | 225 | printf("%d) %-26s --> Dimming: %0.2f\r", demo + 1, demos[demo], demo == MANUAL_PWM_DIMMING || demo == AUTOMATIC_PWM_DIMMING ? pwm_dimming : analog_dimming); |
Davidroid | 2:dbd596540d46 | 226 | |
Davidroid | 2:dbd596540d46 | 227 | /* Writing PWM and Analog dimming values to the LED. */ |
Davidroid | 16:2497344ba436 | 228 | led->set_pwm_dimming(pwm_dimming); |
Davidroid | 16:2497344ba436 | 229 | led->set_analog_dimming(analog_dimming); |
Davidroid | 2:dbd596540d46 | 230 | } |
Davidroid | 3:ed347127fc38 | 231 | } |
Davidroid | 2:dbd596540d46 | 232 | |
Davidroid | 3:ed347127fc38 | 233 | /* Resetting the action. */ |
Davidroid | 3:ed347127fc38 | 234 | action = NO_ACTION; |
Davidroid | 2:dbd596540d46 | 235 | } |
Davidroid | 2:dbd596540d46 | 236 | |
Davidroid | 2:dbd596540d46 | 237 | /** |
Davidroid | 2:dbd596540d46 | 238 | * @brief Interrupt Request for the main loop's ticker related interrupt. |
Davidroid | 2:dbd596540d46 | 239 | * @param None. |
Davidroid | 2:dbd596540d46 | 240 | * @retval None. |
Davidroid | 2:dbd596540d46 | 241 | */ |
Davidroid | 16:2497344ba436 | 242 | void ticker_irq(void) |
Davidroid | 2:dbd596540d46 | 243 | { |
Davidroid | 2:dbd596540d46 | 244 | ticker_irq_triggered = true; |
Davidroid | 2:dbd596540d46 | 245 | } |
Davidroid | 2:dbd596540d46 | 246 | |
Davidroid | 2:dbd596540d46 | 247 | /** |
Davidroid | 2:dbd596540d46 | 248 | * @brief Interrupt Request for the user button's interrupt. |
Davidroid | 2:dbd596540d46 | 249 | * @param None. |
Davidroid | 2:dbd596540d46 | 250 | * @retval None. |
Davidroid | 2:dbd596540d46 | 251 | */ |
Davidroid | 16:2497344ba436 | 252 | void button_irq(void) |
Davidroid | 2:dbd596540d46 | 253 | { |
Davidroid | 2:dbd596540d46 | 254 | button_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 255 | button.disable_irq(); |
Davidroid | 1:1c01b873ad45 | 256 | } |
Davidroid | 1:1c01b873ad45 | 257 | |
Davidroid | 1:1c01b873ad45 | 258 | /** |
Davidroid | 2:dbd596540d46 | 259 | * @brief Interrupt Request for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 260 | * @param None. |
Davidroid | 2:dbd596540d46 | 261 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 262 | */ |
Davidroid | 16:2497344ba436 | 263 | void xfault_irq(void) |
Davidroid | 1:1c01b873ad45 | 264 | { |
Davidroid | 1:1c01b873ad45 | 265 | xfault_irq_triggered = true; |
Davidroid | 16:2497344ba436 | 266 | led->disable_xfault_irq(); |
Davidroid | 1:1c01b873ad45 | 267 | } |
Davidroid | 1:1c01b873ad45 | 268 | |
Davidroid | 1:1c01b873ad45 | 269 | /** |
Davidroid | 2:dbd596540d46 | 270 | * @brief Interrupt Handler for the user button's interrupt. |
Davidroid | 2:dbd596540d46 | 271 | * @param None. |
Davidroid | 2:dbd596540d46 | 272 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 273 | */ |
Davidroid | 16:2497344ba436 | 274 | void button_handler(void) |
Davidroid | 1:1c01b873ad45 | 275 | { |
Davidroid | 2:dbd596540d46 | 276 | /* User Button's timer to measure the time the button remains pressed. */ |
Davidroid | 2:dbd596540d46 | 277 | static Timer button_pressed_timer; |
Davidroid | 2:dbd596540d46 | 278 | |
Davidroid | 1:1c01b873ad45 | 279 | bool button_pressed_flag = (button.read() == 0 ? true : false); |
Davidroid | 1:1c01b873ad45 | 280 | |
Davidroid | 16:2497344ba436 | 281 | if (button_pressed_flag) { |
Davidroid | 1:1c01b873ad45 | 282 | button_pressed_led = 1; |
Davidroid | 1:1c01b873ad45 | 283 | button_pressed_timer.start(); |
Davidroid | 16:2497344ba436 | 284 | } else { |
Davidroid | 1:1c01b873ad45 | 285 | button_pressed_led = 0; |
Davidroid | 1:1c01b873ad45 | 286 | button_pressed_timer.stop(); |
Davidroid | 1:1c01b873ad45 | 287 | |
Davidroid | 1:1c01b873ad45 | 288 | /* Either changing current demo's state or switching to the next demo. */ |
Davidroid | 2:dbd596540d46 | 289 | int time_pressed = button_pressed_timer.read_ms(); |
Davidroid | 16:2497344ba436 | 290 | if (time_pressed < SWITCH_DEMO_BUTTON_PRESS_ms) { |
Davidroid | 2:dbd596540d46 | 291 | action = SWITCH_STATE; |
Davidroid | 16:2497344ba436 | 292 | } else if (time_pressed < SWITCH_POWER_BUTTON_PRESS_ms) { |
Davidroid | 2:dbd596540d46 | 293 | action = SWITCH_DEMO; |
Davidroid | 16:2497344ba436 | 294 | } else { |
Davidroid | 2:dbd596540d46 | 295 | action = SWITCH_POWER; |
Davidroid | 16:2497344ba436 | 296 | } |
Davidroid | 1:1c01b873ad45 | 297 | |
Davidroid | 1:1c01b873ad45 | 298 | button_pressed_timer.reset(); |
Davidroid | 1:1c01b873ad45 | 299 | } |
Davidroid | 1:1c01b873ad45 | 300 | |
Davidroid | 1:1c01b873ad45 | 301 | button.enable_irq(); |
Davidroid | 1:1c01b873ad45 | 302 | } |
Davidroid | 0:f5a41692e1fb | 303 | |
Davidroid | 2:dbd596540d46 | 304 | /** |
Davidroid | 2:dbd596540d46 | 305 | * @brief Interrupt Handler for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 306 | * @param None. |
Davidroid | 2:dbd596540d46 | 307 | * @retval None. |
Davidroid | 2:dbd596540d46 | 308 | */ |
Davidroid | 16:2497344ba436 | 309 | void xfault_handler(void) |
Davidroid | 2:dbd596540d46 | 310 | { |
Davidroid | 2:dbd596540d46 | 311 | /* Printing to the console. */ |
Davidroid | 14:f2ba2ca417d3 | 312 | printf("XFAULT Interrupt detected! Re-initializing LED driver..."); |
Davidroid | 14:f2ba2ca417d3 | 313 | |
Davidroid | 14:f2ba2ca417d3 | 314 | /* Re-starting-up LED Control Component. */ |
Davidroid | 16:2497344ba436 | 315 | led->start_up(); |
Davidroid | 14:f2ba2ca417d3 | 316 | |
Davidroid | 14:f2ba2ca417d3 | 317 | /* Printing to the console. */ |
Davidroid | 14:f2ba2ca417d3 | 318 | printf("Done.\r\n\n"); |
Davidroid | 2:dbd596540d46 | 319 | |
Davidroid | 2:dbd596540d46 | 320 | /* Re-initializing the demo. */ |
Davidroid | 16:2497344ba436 | 321 | init_demo(); |
Davidroid | 2:dbd596540d46 | 322 | |
Davidroid | 16:2497344ba436 | 323 | led->enable_xfault_irq(); |
Davidroid | 2:dbd596540d46 | 324 | } |
Davidroid | 2:dbd596540d46 | 325 | |
Davidroid | 0:f5a41692e1fb | 326 | |
Davidroid | 0:f5a41692e1fb | 327 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 328 | |
Davidroid | 0:f5a41692e1fb | 329 | int main() |
Davidroid | 0:f5a41692e1fb | 330 | { |
Davidroid | 0:f5a41692e1fb | 331 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 332 | |
Davidroid | 2:dbd596540d46 | 333 | /* Printing to the console. */ |
Davidroid | 5:b00bbf1edfa9 | 334 | printf("LED Control Application Example\r\n\n" \ |
Davidroid | 5:b00bbf1edfa9 | 335 | "Demos:\r\n"); |
Davidroid | 5:b00bbf1edfa9 | 336 | int demo = 0; |
Davidroid | 16:2497344ba436 | 337 | for (demo = 0; demo < LED_DEMO_SIZE; demo++) { |
Davidroid | 7:5763474c7551 | 338 | printf("%d) %-26s\r\n", demo + 1, demos[demo]); |
Davidroid | 16:2497344ba436 | 339 | } |
Davidroid | 5:b00bbf1edfa9 | 340 | printf("\r\nActions:\r\n" \ |
Davidroid | 5:b00bbf1edfa9 | 341 | "+ Short Button Press [<%.1fs] --> Manual Dimming\r\n" \ |
Davidroid | 5:b00bbf1edfa9 | 342 | "+ Medium Button Press --> Switch Demo\r\n" \ |
Davidroid | 5:b00bbf1edfa9 | 343 | "+ Long Button Press [>%.1fs] --> Switch Power ON/OFF\r\n\n" \ |
Davidroid | 4:aeeb361d0227 | 344 | , SWITCH_DEMO_BUTTON_PRESS_ms / 1000.0f, SWITCH_POWER_BUTTON_PRESS_ms / 1000.0f); |
Davidroid | 2:dbd596540d46 | 345 | |
Davidroid | 1:1c01b873ad45 | 346 | /* Initializing LED Control Component. */ |
Davidroid | 16:2497344ba436 | 347 | led = new Led6001(D4, A3, D6, D5); |
Davidroid | 16:2497344ba436 | 348 | if (led->init() != COMPONENT_OK) { |
Davidroid | 1:1c01b873ad45 | 349 | exit(EXIT_FAILURE); |
Davidroid | 16:2497344ba436 | 350 | } |
Davidroid | 1:1c01b873ad45 | 351 | |
Davidroid | 1:1c01b873ad45 | 352 | /* Attaching interrupt request functions. */ |
Davidroid | 16:2497344ba436 | 353 | button.fall(button_irq); |
Davidroid | 16:2497344ba436 | 354 | button.rise(button_irq); |
Davidroid | 16:2497344ba436 | 355 | led->attach_xfault_irq(&xfault_irq); |
Davidroid | 16:2497344ba436 | 356 | led->enable_xfault_irq(); |
Davidroid | 16:2497344ba436 | 357 | ticker.attach_us(ticker_irq, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 358 | |
Davidroid | 14:f2ba2ca417d3 | 359 | /* Starting-up LED Control Component. */ |
Davidroid | 16:2497344ba436 | 360 | led->start_up(); |
Davidroid | 14:f2ba2ca417d3 | 361 | |
Davidroid | 2:dbd596540d46 | 362 | /* Initializing the demo. */ |
Davidroid | 16:2497344ba436 | 363 | init_demo(); |
Davidroid | 0:f5a41692e1fb | 364 | |
Davidroid | 0:f5a41692e1fb | 365 | |
Davidroid | 0:f5a41692e1fb | 366 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 367 | |
Davidroid | 2:dbd596540d46 | 368 | /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
Davidroid | 16:2497344ba436 | 369 | while (true) { |
Davidroid | 16:2497344ba436 | 370 | if (ticker_irq_triggered) { |
Davidroid | 2:dbd596540d46 | 371 | ticker_irq_triggered = false; |
Davidroid | 16:2497344ba436 | 372 | led_handler(); |
Davidroid | 16:2497344ba436 | 373 | } else if (button_irq_triggered) { |
Davidroid | 1:1c01b873ad45 | 374 | button_irq_triggered = false; |
Davidroid | 16:2497344ba436 | 375 | button_handler(); |
Davidroid | 16:2497344ba436 | 376 | } else if (xfault_irq_triggered) { |
Davidroid | 1:1c01b873ad45 | 377 | xfault_irq_triggered = false; |
Davidroid | 16:2497344ba436 | 378 | xfault_handler(); |
Davidroid | 5:b00bbf1edfa9 | 379 | } else { |
Davidroid | 1:1c01b873ad45 | 380 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 381 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 382 | } |
Davidroid | 1:1c01b873ad45 | 383 | } |
Davidroid | 1:1c01b873ad45 | 384 | } |