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