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 ST

Revision:
5:b00bbf1edfa9
Parent:
4:aeeb361d0227
Child:
6:899828fd9d9d
--- a/main.cpp	Fri Dec 18 17:05:26 2015 +0000
+++ b/main.cpp	Thu Jan 14 17:07:33 2016 +0000
@@ -50,22 +50,25 @@
 
 /* PI. */
 #ifndef M_PI
-    #define M_PI                      3.14159265358979323846f
+    #define M_PI                      (3.14159265358979323846f)
 #endif
 
 /* Loop period in micro-seconds. */
-#define LOOP_PERIOD_us                1E5
+#define LOOP_PERIOD_us                (1E5)
 
 /* Sin period in micro-seconds. */
-#define PWM_SIN_PERIOD_us             1E7
-#define ANALOG_SIN_PERIOD_us          1E7
+#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
-#define SWITCH_POWER_BUTTON_PRESS_ms  2000
+#define SWITCH_DEMO_BUTTON_PRESS_ms   (500)
+#define SWITCH_POWER_BUTTON_PRESS_ms  (2000)
+
+/* Duration of initialization interval in milli-seconds. */
+#define INITIALIZATION_INTERVAL_ms    (2000)
 
 /* Dimming Step. */
-#define DIMMING_STEP                  0.1f
+#define DIMMING_STEP                  (0.05f)
 
 
 /* Types ---------------------------------------------------------------------*/
@@ -73,11 +76,11 @@
 /* Demos. */
 typedef enum
 {
-  PWM_DIMMING_STEPS = 0,
-  ANALOG_DIMMING_STEPS,
-  PWM_DIMMING_VARY,
-  ANALOG_DIMMING_VARY,
-  ANALOG_DIMMING_PHOTO,
+  MANUAL_PWM_DIMMING = 0,
+  MANUAL_ANALOG_DIMMING,
+  AUTOMATIC_PWM_DIMMING,
+  AUTOMATIC_ANALOG_DIMMING,
+  PHOTO_BASED_ANALOG_DIMMING,
   LED_DEMO_SIZE
 } LED_Demo_t;
 
@@ -127,7 +130,7 @@
     "Manual Analog Dimming",
     "Sinusoidal PWM Dimming",
     "Sinusoidal Analog Dimming",
-    "Photo-based PWM Dimming"
+    "Photo-based Analog Dimming"
 };
 
 
@@ -140,6 +143,9 @@
  */
 void LEDInit(void)
 {
+    /* Printing to the console. */
+    printf("Initializing LED driver...");
+
     /* Initializing Interrupt flags. */
     button_irq_triggered = false;
     xfault_irq_triggered = false;
@@ -153,6 +159,14 @@
     /* Initializing PWM and Analog dimming to maximum values. */
     pwm_dimming = 1.0f;
     analog_dimming = 1.0f;
+
+    /* Start-up sequence. */
+    led->PowerOFF();
+    wait_ms(INITIALIZATION_INTERVAL_ms);
+    led->PowerON();
+
+    /* Printing to the console. */
+    printf("Done.\r\n\nDemo:\r\n");
 }
 
 /**
@@ -202,40 +216,39 @@
         switch (demo)
         {
             /* Changing PWM dimming according to the user button. */
-            case PWM_DIMMING_STEPS:
+            case MANUAL_PWM_DIMMING:
                 if (action == SWITCH_STATE)
                 {
                     pwm_dimming -= DIMMING_STEP;
-                    pwm_dimming = (pwm_dimming <= 0.0f ? 1.0f : pwm_dimming);
+                    pwm_dimming = (pwm_dimming < 0.0f ? 1.0f : pwm_dimming);
                 }
                 break;
 
             /* Changing Analog dimming according to the user button. */
-            case ANALOG_DIMMING_STEPS:
+            case MANUAL_ANALOG_DIMMING:
                 if (action == SWITCH_STATE)
                 {
                     analog_dimming -= DIMMING_STEP;
-                    analog_dimming = (analog_dimming <= 0.0f ? 1.0f : analog_dimming);
+                    analog_dimming = (analog_dimming < 0.0f ? 1.0f : analog_dimming);
                 }
                 break;
 
             /* Changing PWM dimming continuously. */
-            case PWM_DIMMING_VARY:
+            case AUTOMATIC_PWM_DIMMING:
                 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:
+            case AUTOMATIC_ANALOG_DIMMING:
                 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);
+            case PHOTO_BASED_ANALOG_DIMMING:
                 analog_dimming = 1.0f - led->GetCurrent();
                 action = SWITCH_STATE;
                 break;
@@ -245,7 +258,7 @@
         if (action != NO_ACTION)
         {
             /* Printing to the console. */
-            printf("%d) %-26s PWM/Analog: %.3f %.3f\r", demo + 1, demos[demo], pwm_dimming, analog_dimming);
+            printf("%d) %-26s --> Dimming: %0.2f\r", demo + 1, demos[demo], demo == MANUAL_PWM_DIMMING || demo == AUTOMATIC_PWM_DIMMING ? pwm_dimming : analog_dimming);
 
             /* Writing PWM and Analog dimming values to the LED. */
             led->SetPWMDimming(pwm_dimming);
@@ -350,10 +363,15 @@
     /*----- Initialization. -----*/
 
     /* Printing to the console. */
-    printf("LED Control Application Example\r\n\n"\
-           "+ Short Button Press [<%.1fs] --> Manual Dimming\r\n"\
-           "+ Medium Button Press        --> Switch Demo\r\n"\
-           "+ Long Button Press  [>%.1fs] --> Switch Power ON/OFF\r\n\n"\
+    printf("LED Control Application Example\r\n\n" \
+           "Demos:\r\n");
+    int demo = 0;
+    for (demo = 0; demo < LED_DEMO_SIZE; demo++)
+        printf("%d) %-26s\r\n", demo, demos[demo]);
+    printf("\r\nActions:\r\n" \
+           "+ Short Button Press [<%.1fs]  --> Manual Dimming\r\n" \
+           "+ Medium Button Press         --> Switch Demo\r\n" \
+           "+ Long Button Press  [>%.1fs]  --> Switch Power ON/OFF\r\n\n" \
            , SWITCH_DEMO_BUTTON_PRESS_ms / 1000.0f, SWITCH_POWER_BUTTON_PRESS_ms / 1000.0f);
 
     /* Initializing LED Control Component. */
@@ -365,6 +383,7 @@
     button.fall(ButtonIRQ);
     button.rise(ButtonIRQ);
     led->AttachXFaultIRQ(&XFaultIRQ);
+    led->EnableXFaultIRQ();
     ticker.attach_us(TickerIRQ, LOOP_PERIOD_us);
 
     /* Initializing the demo. */
@@ -384,12 +403,11 @@
         {
             button_irq_triggered = false;
             ButtonHandler();
-        } else if(xfault_irq_triggered)
+        } else if (xfault_irq_triggered)
         {
             xfault_irq_triggered = false;
             XFaultHandler();
-        } else
-        {
+        } else {
             /* It is recommended that SEVONPEND in the System Control Register is NOT set. */
             __WFE();
         }