![](/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, built against mbed OS.
Dependencies: X_NUCLEO_LED61A1
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@1:1c01b873ad45, 2015-12-14 (annotated)
- Committer:
- Davidroid
- Date:
- Mon Dec 14 17:43:47 2015 +0000
- Revision:
- 1:1c01b873ad45
- Parent:
- 0:f5a41692e1fb
- Child:
- 2:dbd596540d46
+ Added interrupts handling.
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 | 1:1c01b873ad45 | 51 | #define LOOP_PERIOD_us 1E6 |
Davidroid | 1:1c01b873ad45 | 52 | |
Davidroid | 1:1c01b873ad45 | 53 | |
Davidroid | 1:1c01b873ad45 | 54 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 55 | |
Davidroid | 1:1c01b873ad45 | 56 | /* LED Control Component. */ |
Davidroid | 1:1c01b873ad45 | 57 | LED6001 *led; |
Davidroid | 1:1c01b873ad45 | 58 | |
Davidroid | 1:1c01b873ad45 | 59 | /* User Button. */ |
Davidroid | 1:1c01b873ad45 | 60 | static InterruptIn button(USER_BUTTON); |
Davidroid | 1:1c01b873ad45 | 61 | |
Davidroid | 1:1c01b873ad45 | 62 | /* Interrupt flags. */ |
Davidroid | 1:1c01b873ad45 | 63 | static volatile boolean button_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 64 | static volatile boolean xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 65 | static volatile boolean timer_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 66 | |
Davidroid | 1:1c01b873ad45 | 67 | /* Timer. */ |
Davidroid | 1:1c01b873ad45 | 68 | static Ticker ticker; |
Davidroid | 1:1c01b873ad45 | 69 | |
Davidroid | 1:1c01b873ad45 | 70 | /* MCU Board's Led which provides the user with a visual feedback on |
Davidroid | 1:1c01b873ad45 | 71 | the user button's status (ON = pressed, OFF = released). */ |
Davidroid | 1:1c01b873ad45 | 72 | DigitalOut button_pressed_led(LED1); |
Davidroid | 1:1c01b873ad45 | 73 | |
Davidroid | 1:1c01b873ad45 | 74 | /* Flags to control the demo. */ |
Davidroid | 1:1c01b873ad45 | 75 | static volatile boolean next_demo_state = false; |
Davidroid | 1:1c01b873ad45 | 76 | static volatile boolean next_demo = false; |
Davidroid | 1:1c01b873ad45 | 77 | |
Davidroid | 1:1c01b873ad45 | 78 | |
Davidroid | 1:1c01b873ad45 | 79 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 80 | |
Davidroid | 1:1c01b873ad45 | 81 | /** |
Davidroid | 1:1c01b873ad45 | 82 | * @brief State machine initialization |
Davidroid | 1:1c01b873ad45 | 83 | * @param demo - selected demo |
Davidroid | 1:1c01b873ad45 | 84 | * p - reference to pwm dimming level |
Davidroid | 1:1c01b873ad45 | 85 | * a - reference to analog dimming level |
Davidroid | 1:1c01b873ad45 | 86 | * @retval None |
Davidroid | 1:1c01b873ad45 | 87 | */ |
Davidroid | 1:1c01b873ad45 | 88 | void SM_Init(LED_DRIVER_Demo* demo, uint8_t* p, uint8_t* a) |
Davidroid | 1:1c01b873ad45 | 89 | { |
Davidroid | 1:1c01b873ad45 | 90 | ChangeDemoState = 0; |
Davidroid | 1:1c01b873ad45 | 91 | NextDemo = 0; |
Davidroid | 1:1c01b873ad45 | 92 | ReInit_LED_Driver = 0; |
Davidroid | 1:1c01b873ad45 | 93 | |
Davidroid | 1:1c01b873ad45 | 94 | *p = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 95 | *a = MAX_BRIGHT_ADIM; |
Davidroid | 1:1c01b873ad45 | 96 | *demo = PWM_DIMMING_STEPS; |
Davidroid | 1:1c01b873ad45 | 97 | } |
Davidroid | 1:1c01b873ad45 | 98 | |
Davidroid | 1:1c01b873ad45 | 99 | /** |
Davidroid | 1:1c01b873ad45 | 100 | * @brief Interrupt Request for the timer-related interrupt. |
Davidroid | 1:1c01b873ad45 | 101 | * @param None |
Davidroid | 1:1c01b873ad45 | 102 | * @retval None |
Davidroid | 1:1c01b873ad45 | 103 | */ |
Davidroid | 1:1c01b873ad45 | 104 | void TimerIRQ(void) |
Davidroid | 1:1c01b873ad45 | 105 | { |
Davidroid | 1:1c01b873ad45 | 106 | timer_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 107 | button.disable_irq(); |
Davidroid | 1:1c01b873ad45 | 108 | } |
Davidroid | 1:1c01b873ad45 | 109 | |
Davidroid | 1:1c01b873ad45 | 110 | /** |
Davidroid | 1:1c01b873ad45 | 111 | * @brief Handling the LED capabilities and executing several demos. |
Davidroid | 1:1c01b873ad45 | 112 | * @param None |
Davidroid | 1:1c01b873ad45 | 113 | * @retval None |
Davidroid | 1:1c01b873ad45 | 114 | */ |
Davidroid | 1:1c01b873ad45 | 115 | void LEDHandler(void) |
Davidroid | 1:1c01b873ad45 | 116 | { |
Davidroid | 1:1c01b873ad45 | 117 | static LED_DRIVER_Demo SM_Demo; |
Davidroid | 1:1c01b873ad45 | 118 | static uint8_t pDim; |
Davidroid | 1:1c01b873ad45 | 119 | static uint8_t aDim; |
Davidroid | 1:1c01b873ad45 | 120 | uint8_t pDim1,pDim2,pDim3; |
Davidroid | 1:1c01b873ad45 | 121 | uint8_t aDim1,aDim2,aDim3; |
Davidroid | 1:1c01b873ad45 | 122 | |
Davidroid | 1:1c01b873ad45 | 123 | /* If LED driver is not to be reinitialized */ |
Davidroid | 1:1c01b873ad45 | 124 | if (!ReInit_LED_Driver) |
Davidroid | 1:1c01b873ad45 | 125 | { |
Davidroid | 1:1c01b873ad45 | 126 | /* Update state machine parameters */ |
Davidroid | 1:1c01b873ad45 | 127 | SM_Update(&SM_Demo,&pDim,&aDim); |
Davidroid | 1:1c01b873ad45 | 128 | |
Davidroid | 1:1c01b873ad45 | 129 | /* Change running demo and transmit serial information */ |
Davidroid | 1:1c01b873ad45 | 130 | if (NextDemo == 1) |
Davidroid | 1:1c01b873ad45 | 131 | { |
Davidroid | 1:1c01b873ad45 | 132 | pDim = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 133 | aDim = MAX_BRIGHT_ADIM; |
Davidroid | 1:1c01b873ad45 | 134 | |
Davidroid | 1:1c01b873ad45 | 135 | if (SM_Demo == PWM_DIMMING_STEPS) |
Davidroid | 1:1c01b873ad45 | 136 | { |
Davidroid | 1:1c01b873ad45 | 137 | SM_Demo = ANALOG_DIMMING_STEPS; |
Davidroid | 1:1c01b873ad45 | 138 | disp.Len = strlen((char*)Msg_Demo2_Line1); |
Davidroid | 1:1c01b873ad45 | 139 | memcpy(disp.Data,Msg_Demo2_Line1,disp.Len); |
Davidroid | 1:1c01b873ad45 | 140 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 141 | } |
Davidroid | 1:1c01b873ad45 | 142 | else if (SM_Demo == ANALOG_DIMMING_STEPS) |
Davidroid | 1:1c01b873ad45 | 143 | { |
Davidroid | 1:1c01b873ad45 | 144 | SM_Demo = PWM_DIMMING_VARY; |
Davidroid | 1:1c01b873ad45 | 145 | disp.Len = strlen((char*)Msg_Demo3_Line1); |
Davidroid | 1:1c01b873ad45 | 146 | memcpy(disp.Data,Msg_Demo3_Line1,disp.Len); |
Davidroid | 1:1c01b873ad45 | 147 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 148 | } |
Davidroid | 1:1c01b873ad45 | 149 | else if (SM_Demo == PWM_DIMMING_VARY) |
Davidroid | 1:1c01b873ad45 | 150 | { |
Davidroid | 1:1c01b873ad45 | 151 | SM_Demo = ANALOG_DIMMING_VARY; |
Davidroid | 1:1c01b873ad45 | 152 | disp.Len = strlen((char*)Msg_Demo4_Line1); |
Davidroid | 1:1c01b873ad45 | 153 | memcpy(disp.Data,Msg_Demo4_Line1,disp.Len); |
Davidroid | 1:1c01b873ad45 | 154 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 155 | } |
Davidroid | 1:1c01b873ad45 | 156 | else if (SM_Demo == ANALOG_DIMMING_VARY) |
Davidroid | 1:1c01b873ad45 | 157 | { |
Davidroid | 1:1c01b873ad45 | 158 | SM_Demo = ANALOG_DIMMING_PHOTO; |
Davidroid | 1:1c01b873ad45 | 159 | disp.Len = strlen((char*)Msg_Demo5_Line1); |
Davidroid | 1:1c01b873ad45 | 160 | memcpy(disp.Data,Msg_Demo5_Line1,disp.Len); |
Davidroid | 1:1c01b873ad45 | 161 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 162 | } |
Davidroid | 1:1c01b873ad45 | 163 | else if (SM_Demo == ANALOG_DIMMING_PHOTO) |
Davidroid | 1:1c01b873ad45 | 164 | { |
Davidroid | 1:1c01b873ad45 | 165 | SM_Demo = PWM_DIMMING_STEPS; |
Davidroid | 1:1c01b873ad45 | 166 | disp.Len = strlen((char*)Msg_Demo1_Line1); |
Davidroid | 1:1c01b873ad45 | 167 | memcpy(disp.Data,Msg_Demo1_Line1,disp.Len); |
Davidroid | 1:1c01b873ad45 | 168 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 169 | } |
Davidroid | 1:1c01b873ad45 | 170 | } |
Davidroid | 1:1c01b873ad45 | 171 | |
Davidroid | 1:1c01b873ad45 | 172 | switch (SM_Demo) |
Davidroid | 1:1c01b873ad45 | 173 | { |
Davidroid | 1:1c01b873ad45 | 174 | /* PWM changes with PB action */ |
Davidroid | 1:1c01b873ad45 | 175 | case PWM_DIMMING_STEPS: |
Davidroid | 1:1c01b873ad45 | 176 | aDim = MAX_BRIGHT_ADIM; |
Davidroid | 1:1c01b873ad45 | 177 | if (ChangeDemoState) |
Davidroid | 1:1c01b873ad45 | 178 | { |
Davidroid | 1:1c01b873ad45 | 179 | pDim = pDim - 10; |
Davidroid | 1:1c01b873ad45 | 180 | if (pDim < MIN_BRIGHT_PDIM) |
Davidroid | 1:1c01b873ad45 | 181 | pDim = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 182 | |
Davidroid | 1:1c01b873ad45 | 183 | disp.Len = strlen((char*)Msg_Demo1_Line2); |
Davidroid | 1:1c01b873ad45 | 184 | memcpy(disp.Data,Msg_Demo1_Line2 ,disp.Len); |
Davidroid | 1:1c01b873ad45 | 185 | pDim1 = pDim/100; |
Davidroid | 1:1c01b873ad45 | 186 | if (pDim1 == 1) |
Davidroid | 1:1c01b873ad45 | 187 | { |
Davidroid | 1:1c01b873ad45 | 188 | pDim2 = 0; pDim3 = 0; |
Davidroid | 1:1c01b873ad45 | 189 | disp.Data[disp.Len] = pDim1+0x30; |
Davidroid | 1:1c01b873ad45 | 190 | disp.Data[disp.Len+1] = pDim2+0x30; |
Davidroid | 1:1c01b873ad45 | 191 | disp.Data[disp.Len+2] = pDim3+0x30; |
Davidroid | 1:1c01b873ad45 | 192 | disp.Data[disp.Len+3] = '%'; |
Davidroid | 1:1c01b873ad45 | 193 | disp.Len+=4; |
Davidroid | 1:1c01b873ad45 | 194 | } |
Davidroid | 1:1c01b873ad45 | 195 | else |
Davidroid | 1:1c01b873ad45 | 196 | { |
Davidroid | 1:1c01b873ad45 | 197 | pDim2 = pDim/10; pDim3 = pDim%10; |
Davidroid | 1:1c01b873ad45 | 198 | disp.Data[disp.Len] = pDim2+0x30; |
Davidroid | 1:1c01b873ad45 | 199 | disp.Data[disp.Len+1] = pDim3+0x30; |
Davidroid | 1:1c01b873ad45 | 200 | disp.Data[disp.Len+2] = '%'; |
Davidroid | 1:1c01b873ad45 | 201 | disp.Len+=3; |
Davidroid | 1:1c01b873ad45 | 202 | } |
Davidroid | 1:1c01b873ad45 | 203 | disp.Data[disp.Len] = '\n'; |
Davidroid | 1:1c01b873ad45 | 204 | disp.Data[disp.Len+1] = '\r'; |
Davidroid | 1:1c01b873ad45 | 205 | disp.Len += 2; |
Davidroid | 1:1c01b873ad45 | 206 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 207 | } |
Davidroid | 1:1c01b873ad45 | 208 | break; |
Davidroid | 1:1c01b873ad45 | 209 | /* Analog dimming changes with PB action */ |
Davidroid | 1:1c01b873ad45 | 210 | case ANALOG_DIMMING_STEPS: |
Davidroid | 1:1c01b873ad45 | 211 | pDim = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 212 | if (ChangeDemoState) |
Davidroid | 1:1c01b873ad45 | 213 | { |
Davidroid | 1:1c01b873ad45 | 214 | aDim = aDim - 5; |
Davidroid | 1:1c01b873ad45 | 215 | if (aDim < MIN_BRIGHT_ADIM) |
Davidroid | 1:1c01b873ad45 | 216 | aDim = MAX_BRIGHT_ADIM; |
Davidroid | 1:1c01b873ad45 | 217 | |
Davidroid | 1:1c01b873ad45 | 218 | disp.Len = strlen((char*)Msg_Demo2_Line2); |
Davidroid | 1:1c01b873ad45 | 219 | memcpy(disp.Data,Msg_Demo2_Line2 ,disp.Len); |
Davidroid | 1:1c01b873ad45 | 220 | aDim1 = aDim/100; |
Davidroid | 1:1c01b873ad45 | 221 | if (aDim1 == 1) |
Davidroid | 1:1c01b873ad45 | 222 | { |
Davidroid | 1:1c01b873ad45 | 223 | aDim2 = 0; aDim3 = 0; |
Davidroid | 1:1c01b873ad45 | 224 | disp.Data[disp.Len] = aDim1+0x30; |
Davidroid | 1:1c01b873ad45 | 225 | disp.Data[disp.Len+1] = aDim2+0x30; |
Davidroid | 1:1c01b873ad45 | 226 | disp.Data[disp.Len+2] = aDim3+0x30; |
Davidroid | 1:1c01b873ad45 | 227 | disp.Data[disp.Len+3] = '%'; |
Davidroid | 1:1c01b873ad45 | 228 | disp.Len+=4; |
Davidroid | 1:1c01b873ad45 | 229 | } |
Davidroid | 1:1c01b873ad45 | 230 | else |
Davidroid | 1:1c01b873ad45 | 231 | { |
Davidroid | 1:1c01b873ad45 | 232 | aDim2 = aDim/10; aDim3 = aDim%10; |
Davidroid | 1:1c01b873ad45 | 233 | disp.Data[disp.Len] = aDim2+0x30; |
Davidroid | 1:1c01b873ad45 | 234 | disp.Data[disp.Len+1] = aDim3+0x30; |
Davidroid | 1:1c01b873ad45 | 235 | disp.Data[disp.Len+2] = '%'; |
Davidroid | 1:1c01b873ad45 | 236 | disp.Len+=3; |
Davidroid | 1:1c01b873ad45 | 237 | } |
Davidroid | 1:1c01b873ad45 | 238 | disp.Data[disp.Len] = '\n'; |
Davidroid | 1:1c01b873ad45 | 239 | disp.Data[disp.Len+1] = '\r'; |
Davidroid | 1:1c01b873ad45 | 240 | disp.Len += 2; |
Davidroid | 1:1c01b873ad45 | 241 | UART_SendMsg(&disp); |
Davidroid | 1:1c01b873ad45 | 242 | } |
Davidroid | 1:1c01b873ad45 | 243 | break; |
Davidroid | 1:1c01b873ad45 | 244 | /* PWM dimming changes continuously */ |
Davidroid | 1:1c01b873ad45 | 245 | case PWM_DIMMING_VARY: |
Davidroid | 1:1c01b873ad45 | 246 | aDim = MAX_BRIGHT_ADIM; |
Davidroid | 1:1c01b873ad45 | 247 | pDim--; |
Davidroid | 1:1c01b873ad45 | 248 | if (pDim < MIN_BRIGHT_PDIM) |
Davidroid | 1:1c01b873ad45 | 249 | pDim = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 250 | HAL_Delay(50); |
Davidroid | 1:1c01b873ad45 | 251 | break; |
Davidroid | 1:1c01b873ad45 | 252 | /* Analog dimming changes continuously */ |
Davidroid | 1:1c01b873ad45 | 253 | case ANALOG_DIMMING_VARY: |
Davidroid | 1:1c01b873ad45 | 254 | pDim = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 255 | aDim-=3; |
Davidroid | 1:1c01b873ad45 | 256 | if (aDim < MIN_BRIGHT_ADIM) |
Davidroid | 1:1c01b873ad45 | 257 | aDim = MAX_BRIGHT_ADIM; |
Davidroid | 1:1c01b873ad45 | 258 | HAL_Delay(500); |
Davidroid | 1:1c01b873ad45 | 259 | break; |
Davidroid | 1:1c01b873ad45 | 260 | /* Analog dimming changes based on photo sensor value */ |
Davidroid | 1:1c01b873ad45 | 261 | case ANALOG_DIMMING_PHOTO: |
Davidroid | 1:1c01b873ad45 | 262 | pDim = MAX_BRIGHT_PDIM; |
Davidroid | 1:1c01b873ad45 | 263 | aDim = (uint8_t)(((BSP_Photo_Sensor_GetVal()>>3)+72)/9); |
Davidroid | 1:1c01b873ad45 | 264 | HAL_Delay(100); |
Davidroid | 1:1c01b873ad45 | 265 | break; |
Davidroid | 1:1c01b873ad45 | 266 | } |
Davidroid | 1:1c01b873ad45 | 267 | |
Davidroid | 1:1c01b873ad45 | 268 | BSP_SetPwmDim(pDim); |
Davidroid | 1:1c01b873ad45 | 269 | BSP_SetAnaDim(aDim); |
Davidroid | 1:1c01b873ad45 | 270 | |
Davidroid | 1:1c01b873ad45 | 271 | ChangeDemoState = 0; |
Davidroid | 1:1c01b873ad45 | 272 | NextDemo = 0; |
Davidroid | 1:1c01b873ad45 | 273 | } |
Davidroid | 1:1c01b873ad45 | 274 | } |
Davidroid | 1:1c01b873ad45 | 275 | |
Davidroid | 1:1c01b873ad45 | 276 | /** |
Davidroid | 1:1c01b873ad45 | 277 | * @brief Interrupt Request for the XFAULT interrupt. |
Davidroid | 1:1c01b873ad45 | 278 | * @param None |
Davidroid | 1:1c01b873ad45 | 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 | 1:1c01b873ad45 | 288 | * @brief Interrupt Handler for the XFAULT interrupt. |
Davidroid | 1:1c01b873ad45 | 289 | * @param None |
Davidroid | 1:1c01b873ad45 | 290 | * @retval None |
Davidroid | 1:1c01b873ad45 | 291 | */ |
Davidroid | 1:1c01b873ad45 | 292 | void XFaultHandler(void) |
Davidroid | 1:1c01b873ad45 | 293 | { |
Davidroid | 1:1c01b873ad45 | 294 | printf("XFAULT Interrupt detected! Re-initializing LED driver...\r\n"); |
Davidroid | 1:1c01b873ad45 | 295 | |
Davidroid | 1:1c01b873ad45 | 296 | BSP_SetPwmDim(ZERO); |
Davidroid | 1:1c01b873ad45 | 297 | BSP_SetAnaDim(ZERO); |
Davidroid | 1:1c01b873ad45 | 298 | |
Davidroid | 1:1c01b873ad45 | 299 | SM_Init(&SM_Demo,&pDim,&aDim); |
Davidroid | 1:1c01b873ad45 | 300 | BSP_SetPwmDim(pDim); |
Davidroid | 1:1c01b873ad45 | 301 | BSP_SetAnaDim(aDim); |
Davidroid | 1:1c01b873ad45 | 302 | |
Davidroid | 1:1c01b873ad45 | 303 | led->EnableXFaultIRQ(); |
Davidroid | 1:1c01b873ad45 | 304 | } |
Davidroid | 1:1c01b873ad45 | 305 | |
Davidroid | 1:1c01b873ad45 | 306 | /** |
Davidroid | 1:1c01b873ad45 | 307 | * @brief Interrupt Request for the user button interrupt. |
Davidroid | 1:1c01b873ad45 | 308 | * @param None |
Davidroid | 1:1c01b873ad45 | 309 | * @retval None |
Davidroid | 1:1c01b873ad45 | 310 | */ |
Davidroid | 1:1c01b873ad45 | 311 | void ButtonIRQ(void) |
Davidroid | 1:1c01b873ad45 | 312 | { |
Davidroid | 1:1c01b873ad45 | 313 | button_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 314 | button.disable_irq(); |
Davidroid | 1:1c01b873ad45 | 315 | } |
Davidroid | 1:1c01b873ad45 | 316 | |
Davidroid | 1:1c01b873ad45 | 317 | /** |
Davidroid | 1:1c01b873ad45 | 318 | * @brief Interrupt Handler for the user button interrupt. |
Davidroid | 1:1c01b873ad45 | 319 | * @param None |
Davidroid | 1:1c01b873ad45 | 320 | * @retval None |
Davidroid | 1:1c01b873ad45 | 321 | */ |
Davidroid | 1:1c01b873ad45 | 322 | void ButtonHandler(void) |
Davidroid | 1:1c01b873ad45 | 323 | { |
Davidroid | 1:1c01b873ad45 | 324 | bool button_pressed_flag = (button.read() == 0 ? true : false); |
Davidroid | 1:1c01b873ad45 | 325 | |
Davidroid | 1:1c01b873ad45 | 326 | if (button_pressed_flag) |
Davidroid | 1:1c01b873ad45 | 327 | { |
Davidroid | 1:1c01b873ad45 | 328 | button_pressed_led = 1; |
Davidroid | 1:1c01b873ad45 | 329 | button_pressed_timer.start(); |
Davidroid | 1:1c01b873ad45 | 330 | } |
Davidroid | 1:1c01b873ad45 | 331 | else |
Davidroid | 1:1c01b873ad45 | 332 | { |
Davidroid | 1:1c01b873ad45 | 333 | button_pressed_led = 0; |
Davidroid | 1:1c01b873ad45 | 334 | button_pressed_timer.stop(); |
Davidroid | 1:1c01b873ad45 | 335 | |
Davidroid | 1:1c01b873ad45 | 336 | /* Either changing current demo's state or switching to the next demo. */ |
Davidroid | 1:1c01b873ad45 | 337 | if (button_pressed_timer.read_ms() < 1000) |
Davidroid | 1:1c01b873ad45 | 338 | next_demo_state = true; |
Davidroid | 1:1c01b873ad45 | 339 | else |
Davidroid | 1:1c01b873ad45 | 340 | next_demo = true; |
Davidroid | 1:1c01b873ad45 | 341 | |
Davidroid | 1:1c01b873ad45 | 342 | button_pressed_timer.reset(); |
Davidroid | 1:1c01b873ad45 | 343 | } |
Davidroid | 1:1c01b873ad45 | 344 | |
Davidroid | 1:1c01b873ad45 | 345 | button.enable_irq(); |
Davidroid | 1:1c01b873ad45 | 346 | } |
Davidroid | 0:f5a41692e1fb | 347 | |
Davidroid | 0:f5a41692e1fb | 348 | |
Davidroid | 0:f5a41692e1fb | 349 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 350 | |
Davidroid | 0:f5a41692e1fb | 351 | int main() |
Davidroid | 0:f5a41692e1fb | 352 | { |
Davidroid | 0:f5a41692e1fb | 353 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 354 | |
Davidroid | 1:1c01b873ad45 | 355 | /* Initializing LED Control Component. */ |
Davidroid | 1:1c01b873ad45 | 356 | led = new LED6001(D4, A3); |
Davidroid | 1:1c01b873ad45 | 357 | |
Davidroid | 1:1c01b873ad45 | 358 | /********** NOT USED, TBC *********/ |
Davidroid | 1:1c01b873ad45 | 359 | LED_DRIVER_InitTypeDef init = |
Davidroid | 1:1c01b873ad45 | 360 | { |
Davidroid | 1:1c01b873ad45 | 361 | LED6001_PDIM_FULL, |
Davidroid | 1:1c01b873ad45 | 362 | LED6001_ADIM_FULL |
Davidroid | 1:1c01b873ad45 | 363 | }; |
Davidroid | 1:1c01b873ad45 | 364 | |
Davidroid | 1:1c01b873ad45 | 365 | if (led->Init(&init) != COMPONENT_OK) |
Davidroid | 1:1c01b873ad45 | 366 | exit(EXIT_FAILURE); |
Davidroid | 1:1c01b873ad45 | 367 | |
Davidroid | 1:1c01b873ad45 | 368 | /* Attaching interrupt request functions. */ |
Davidroid | 1:1c01b873ad45 | 369 | button.fall(ButtonIRQ); |
Davidroid | 1:1c01b873ad45 | 370 | button.rise(ButtonIRQ); |
Davidroid | 1:1c01b873ad45 | 371 | led->AttachXFaultIRQ(&XFaultIRQ); |
Davidroid | 1:1c01b873ad45 | 372 | ticker.attach_us(TimerIRQ, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 373 | |
Davidroid | 1:1c01b873ad45 | 374 | SM_Init(&SM_Demo,&pDim,&aDim); |
Davidroid | 0:f5a41692e1fb | 375 | |
Davidroid | 0:f5a41692e1fb | 376 | /* Printing to the console. */ |
Davidroid | 0:f5a41692e1fb | 377 | printf("LED Control Application Example\r\n\n"); |
Davidroid | 0:f5a41692e1fb | 378 | |
Davidroid | 0:f5a41692e1fb | 379 | |
Davidroid | 0:f5a41692e1fb | 380 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 381 | |
Davidroid | 1:1c01b873ad45 | 382 | while (true) |
Davidroid | 1:1c01b873ad45 | 383 | { |
Davidroid | 1:1c01b873ad45 | 384 | if (timer_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 385 | { |
Davidroid | 1:1c01b873ad45 | 386 | timer_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 387 | LEDHandler(); |
Davidroid | 1:1c01b873ad45 | 388 | } else if (button_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 389 | { |
Davidroid | 1:1c01b873ad45 | 390 | button_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 391 | ButtonHandler(); |
Davidroid | 1:1c01b873ad45 | 392 | } else if(xfault_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 393 | { |
Davidroid | 1:1c01b873ad45 | 394 | xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 395 | XFaultHandler(); |
Davidroid | 1:1c01b873ad45 | 396 | } else |
Davidroid | 1:1c01b873ad45 | 397 | { |
Davidroid | 1:1c01b873ad45 | 398 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 399 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 400 | } |
Davidroid | 1:1c01b873ad45 | 401 | } |
Davidroid | 1:1c01b873ad45 | 402 | } |