|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file X_NUCLEO_LED61A1_HelloWorld.ino |
| 4 | + * @author Davide Aliprandi, STMicroelectronics |
| 5 | + * @version V1.0.0 |
| 6 | + * @date October 17h, 2017 |
| 7 | + * @brief Arduino test application for the STMicroelectronics X-NUCLEO-LED61A1 |
| 8 | + * LED expansion board. |
| 9 | + ****************************************************************************** |
| 10 | + * @attention |
| 11 | + * |
| 12 | + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
| 13 | + * |
| 14 | + * Redistribution and use in source and binary forms, with or without modification, |
| 15 | + * are permitted provided that the following conditions are met: |
| 16 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 17 | + * this list of conditions and the following disclaimer. |
| 18 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 | + * this list of conditions and the following disclaimer in the documentation |
| 20 | + * and/or other materials provided with the distribution. |
| 21 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 22 | + * may be used to endorse or promote products derived from this software |
| 23 | + * without specific prior written permission. |
| 24 | + * |
| 25 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 29 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 33 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + * |
| 36 | + ****************************************************************************** |
| 37 | + */ |
| 38 | + |
| 39 | + |
| 40 | +/* Includes ------------------------------------------------------------------*/ |
| 41 | + |
| 42 | +/* Arduino specific header files. */ |
| 43 | +#include "Arduino.h" |
| 44 | + |
| 45 | +/* Component specific header files. */ |
| 46 | +#include "Led6001.h" |
| 47 | + |
| 48 | + |
| 49 | +/* Definitions ---------------------------------------------------------------*/ |
| 50 | + |
| 51 | +/* PI. */ |
| 52 | +#ifndef M_PI |
| 53 | + #define M_PI (3.14159265358979323846f) |
| 54 | +#endif |
| 55 | + |
| 56 | +/* Loop period in micro-seconds. */ |
| 57 | +#define LOOP_PERIOD_ms (5E2) /* 0.5 seconds. */ |
| 58 | + |
| 59 | +/* Sin period in micro-seconds. */ |
| 60 | +#define LED_SIN_PERIOD_ms (1E4) /* 10 seconds. */ |
| 61 | + |
| 62 | +#define SerialPort Serial |
| 63 | + |
| 64 | +/* Variables -----------------------------------------------------------------*/ |
| 65 | + |
| 66 | +/* Main loop's ticker. */ |
| 67 | +unsigned long current_time, old_time; |
| 68 | + |
| 69 | +/* LED Control Component. */ |
| 70 | +Led6001 *led; |
| 71 | + |
| 72 | +/* Interrupt flags. */ |
| 73 | +static volatile bool xfault_irq_triggered = false; |
| 74 | + |
| 75 | + |
| 76 | +/* Functions -----------------------------------------------------------------*/ |
| 77 | + |
| 78 | +/** |
| 79 | + * @brief Handling the LED capabilities. |
| 80 | + * @param None. |
| 81 | + * @retval None. |
| 82 | + */ |
| 83 | +void led_handler(void) |
| 84 | +{ |
| 85 | + static int tick = 0; |
| 86 | + |
| 87 | + /* Handling the LED dimming when powered ON. */ |
| 88 | + float dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_ms) / LED_SIN_PERIOD_ms) + 0.5f; |
| 89 | + tick %= (int) (LED_SIN_PERIOD_ms / LOOP_PERIOD_ms); |
| 90 | + |
| 91 | + SerialPort.print("Sinusoidal PWM Dimming --> "); |
| 92 | + SerialPort.print(dimming); |
| 93 | + SerialPort.print("\r"); |
| 94 | + |
| 95 | + /* |
| 96 | + Writing PWM dimming values to the LED. |
| 97 | +
|
| 98 | + Notes: |
| 99 | + + Use "set_pwm_dimming()" for a PWM control, or "set_analog_dimming()" |
| 100 | + for an analog control. |
| 101 | + */ |
| 102 | + led->set_analog_dimming(dimming); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief Interrupt Request for the component's XFAULT interrupt. |
| 107 | + * @param None. |
| 108 | + * @retval None. |
| 109 | + */ |
| 110 | +void xfault_irq(void) |
| 111 | +{ |
| 112 | + xfault_irq_triggered = true; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @brief Interrupt Handler for the component's XFAULT interrupt. |
| 117 | + * @param None. |
| 118 | + * @retval None. |
| 119 | + */ |
| 120 | +void xfault_handler(void) |
| 121 | +{ |
| 122 | + /* Printing to the console. */ |
| 123 | + SerialPort.print("XFAULT Interrupt detected! Re-initializing LED driver..."); |
| 124 | + |
| 125 | + /* Re-starting-up LED Control Component. */ |
| 126 | + led->start_up(); |
| 127 | + |
| 128 | + /* Printing to the console. */ |
| 129 | + SerialPort.print("Done.\r\n\n"); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * @brief Initialization. |
| 134 | + * @param None. |
| 135 | + * @retval None. |
| 136 | + */ |
| 137 | +void setup() |
| 138 | +{ |
| 139 | + /* Printing to the console. */ |
| 140 | + SerialPort.begin(115200); |
| 141 | + SerialPort.print("LED Control Application Example\r\n\n"); |
| 142 | + |
| 143 | + /* Initializing LED Control Component. */ |
| 144 | + led = new Led6001(D4, A3, D6, D5); |
| 145 | + if (led->init() != COMPONENT_OK) { |
| 146 | + exit(EXIT_FAILURE); |
| 147 | + } |
| 148 | + |
| 149 | + /* Attaching interrupt request functions. */ |
| 150 | + led->attach_xfault_irq(&xfault_irq); |
| 151 | + |
| 152 | + /* Starting-up LED Control Component. */ |
| 153 | + led->start_up(); |
| 154 | + old_time = millis(); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * @brief Main loop. |
| 159 | + * @param None. |
| 160 | + * @retval None. |
| 161 | + */ |
| 162 | +void loop() |
| 163 | +{ |
| 164 | + current_time = millis(); |
| 165 | + |
| 166 | + /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
| 167 | + if (current_time - old_time >= LOOP_PERIOD_ms) { |
| 168 | + led_handler(); |
| 169 | + old_time = current_time; |
| 170 | + } else if (xfault_irq_triggered) { |
| 171 | + xfault_irq_triggered = false; |
| 172 | + xfault_handler(); |
| 173 | + } |
| 174 | +} |
0 commit comments