Simple test application for the STMicroelectronics X-NUCLEO-LED61A1 LED Control Expansion Board, built against mbed OS.
Dependencies: X_NUCLEO_LED61A1
Fork of HelloWorld_LED61A1 by
Diff: main.cpp
- Revision:
- 3:ed347127fc38
- Parent:
- 2:dbd596540d46
- Child:
- 4:aeeb361d0227
diff -r dbd596540d46 -r ed347127fc38 main.cpp
--- a/main.cpp Thu Dec 17 18:13:57 2015 +0000
+++ b/main.cpp Fri Dec 18 16:53:09 2015 +0000
@@ -48,8 +48,17 @@
/* Definitions ---------------------------------------------------------------*/
+/* PI. */
+#ifndef M_PI
+ #define M_PI 3.14159265358979323846f
+#endif
+
/* Loop period in micro-seconds. */
-#define LOOP_PERIOD_us 1E6
+#define LOOP_PERIOD_us 1E5
+
+/* Sin period in micro-seconds. */
+#define PWM_SIN_PERIOD_us 1E7
+#define ANALOG_SIN_PERIOD_us 1E7
/* Duration of button press in milli-seconds. */
#define SWITCH_DEMO_BUTTON_PRESS_ms 500
@@ -116,8 +125,8 @@
{
"Manual PWM Dimming",
"Manual Analog Dimming",
- "Cyclic PWM Dimming",
- "Cyclic Analog Dimming",
+ "Sinusoidal PWM Dimming",
+ "Sinusoidal Analog Dimming",
"Photo-based PWM Dimming"
};
@@ -153,11 +162,31 @@
*/
void LEDHandler(void)
{
- /* Switching LED power ON/OFF. */
+ static int tick = 0;
+
+ /* Handling the power switch. */
if (action == SWITCH_POWER)
+ {
+ /* Switching the LED power ON/OFF. */
power_on = !power_on;
- /* Handling the LED when powered ON. */
+ if (power_on)
+ {
+ /* Initializing PWM and Analog dimming to maximum values. */
+ pwm_dimming = 1.0f;
+ analog_dimming = 1.0f;
+ }
+ else
+ {
+ /* Printing to the console. */
+ printf("%-56s\r", "Power OFF");
+
+ /* Powering OFF the LED. */
+ led->PowerOFF();
+ }
+ }
+
+ /* Handling the LED dimming when powered ON. */
if (power_on)
{
/* Switching to the next demo. */
@@ -165,10 +194,11 @@
{
pwm_dimming = 1.0f;
analog_dimming = 1.0f;
+ tick = 0;
demo = (LED_Demo_t) ((demo + 1) % LED_DEMO_SIZE);
}
- /* Setting the LED depending on the selected demo. */
+ /* Setting the LED dimming depending on the selected demo. */
switch (demo)
{
/* Changing PWM dimming according to the user button. */
@@ -179,7 +209,7 @@
pwm_dimming = (pwm_dimming <= 0.0f ? 1.0f : pwm_dimming);
}
break;
-
+
/* Changing Analog dimming according to the user button. */
case ANALOG_DIMMING_STEPS:
if (action == SWITCH_STATE)
@@ -188,68 +218,43 @@
analog_dimming = (analog_dimming <= 0.0f ? 1.0f : analog_dimming);
}
break;
-
+
/* Changing PWM dimming continuously. */
case PWM_DIMMING_VARY:
- pwm_dimming -= DIMMING_STEP;
- pwm_dimming = (pwm_dimming <= 0.0f ? 1.0f : pwm_dimming);
- //wait_ms(50);
+ pwm_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / PWM_SIN_PERIOD_us) + 0.5f;
+ tick %= (int) (PWM_SIN_PERIOD_us / LOOP_PERIOD_us) + 1;
action = SWITCH_STATE;
break;
-
+
/* Changing Analog dimming continuously. */
case ANALOG_DIMMING_VARY:
- analog_dimming -= DIMMING_STEP;
- analog_dimming = (analog_dimming <= 0.0f ? 1.0f : analog_dimming);
- //wait_ms(500);
+ analog_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / ANALOG_SIN_PERIOD_us) + 0.5f;
+ tick %= (int) (ANALOG_SIN_PERIOD_us / LOOP_PERIOD_us) + 1;
action = SWITCH_STATE;
break;
-
+
/* Setting Analog dimming according to the photo sensor. */
case ANALOG_DIMMING_PHOTO:
//analog_dimming = (uint8_t) (led->GetCurrent() * ADC_RANGE / 72 + 8);
- analog_dimming = ((1.0f - led->GetCurrent()) * (1.0f - DIMMING_STEP) + DIMMING_STEP);
- //wait_ms(100);
+ analog_dimming = 1.0f - led->GetCurrent();
action = SWITCH_STATE;
break;
}
- }
-
- /* Writing PWM and Analog dimming values to the LED and printing to the console. */
- if (action != NO_ACTION)
- {
- if (action == SWITCH_POWER)
- if (power_on)
- {
- /* Initializing PWM and Analog dimming to maximum values. */
- pwm_dimming = 1.0f;
- analog_dimming = 1.0f;
- /* Printing to the console. */
- printf("Power ON\r\n");
- }
- else
- {
- /* Powering OFF the LED. */
- led->PowerOFF();
+ /* Writing PWM and Analog dimming values to the LED. */
+ if (action != NO_ACTION)
+ {
+ /* Printing to the console. */
+ printf("%d) %-26s PWM/Analog: %.3f %.3f\r", demo + 1, demos[demo], pwm_dimming, analog_dimming);
- /* Printing to the console. */
- printf("Power OFF\r\n");
- }
-
- if (power_on)
- {
/* Writing PWM and Analog dimming values to the LED. */
led->SetPWMDimming(pwm_dimming);
led->SetAnalogDimming(analog_dimming);
-
- /* Printing to the console. */
- printf("%-24s PWM/Analog: %.3f %.3f\r\n", demos[demo], pwm_dimming, analog_dimming);
}
+ }
- /* Resetting action. */
- action = NO_ACTION;
- }
+ /* Resetting the action. */
+ action = NO_ACTION;
}
/**
