My take on the obligatory Hello World app for the FRD-KL25Z platform. This app shows off some of the onboard peripherals. I have commented a chunk of the code, and will work to revise this to add more commenting of all sub-routines.

Dependencies:   FRDM_MMA8451Q TSI mbed

Revision:
1:6a1079e4e6ab
Parent:
0:216148fa726d
--- a/main.cpp	Thu Jun 26 15:11:39 2014 +0000
+++ b/main.cpp	Sun Aug 17 23:47:51 2014 +0000
@@ -1,29 +1,36 @@
 #include "mbed.h"
+#include "ctype.h"
 #include "TSISensor.h"
 #include "MMA8451Q.h"
 
-
-// This is the default address as specified by the mfg
-#define MMA8451_I2C_ADDRESS (0x1d<<1)
+#define MMA8451_I2C_ADDRESS (0x1d<<1)   // 0x01D<<1 is the default address as specified by the mfg
 
 
-PwmOut myRedLed(LED1);
-PwmOut myGreenLed(LED2);
-PwmOut myBlueLed(LED3);
-PwmOut externalLight(D0);
+/********************************
+ * GLOBAL VARIABLES / GPIO PINS *
+ ********************************/
+PwmOut myRedLed(LED1);      //
+PwmOut myGreenLed(LED2);    // LED1, LED2 and LED3 are the R, G and B onboard leds
+PwmOut myBlueLed(LED3);     //
+PwmOut externalLight(PTA1);   // For use with an external led.
 
 MMA8451Q accel(PTE25, PTE24, MMA8451_I2C_ADDRESS);    // 3-axis MEMS accelerometer.
-TSISensor touchSlider;      // Capacitive touch sensor, defined as touchSlider.
+TSISensor touchSlider;      // Capacitive touch sensor, instantiated as touchSlider.
+Serial pc(USBTX, USBRX);    // Instantiate a new serial port using the Serial over USB
+
 
-//Function forward declarations
+//-----------------------------------------------------------//
+//  Function prototypes for the accelerometer demo function  //
+//  as well as the capacitive touch slider demo              //
+//-----------------------------------------------------------//
 void AccelDemo();
 void SliderFadeTimeDemo();
 void TouchFadeIn(float val);
 void TouchFadeOut(float val);
 
-//
-// The following prototypes are all related to the morse code blinking.
-//
+//-----------------------------------------------------------------------//
+//  The following prototypes are all related to the morse code blinking. //
+//-----------------------------------------------------------------------//
 void morseCodeBlinker(char input[], float speed);
 void SendLetter(char input, float speed);
 void WordSpace(float speed);
@@ -32,18 +39,33 @@
 void Dot(float speed);
 
 
-//Main constructor
+/****************
+ * MAIN ROUTINE *
+ ****************/
 int main()
 {
     // Blink the LED's to morse code "HELLO WORLD"
-    morseCodeBlinker("How are you", 0.25f);
-
+    float blinkSpeed = 0.05f;
+    morseCodeBlinker("Hello World", blinkSpeed);
 
-    while (1)
+    while (true)
     {
-        // In the infinite loop of Main, we check to see if the touch slider is being touched
-        // and if it is we call the touch slider demo function
-        // if it is not we will be performing the accelerometer demo function
+        //--------------------------------------------------------------------------------------//
+        //  In the infinite loop of Main, we check to see if the touch slider is being touched  //
+        //  and if it is we call the touch slider demo function                                 //
+        //  if it is not we will be performing the accelerometer demo function                  //
+        //--------------------------------------------------------------------------------------//
+        
+        char userInput;
+    
+        if (pc.readable())
+        {
+            userInput = pc.getc();
+            pc.putc(userInput);
+            pc.putc('\n');
+            pc.putc('\r');
+        }
+        
         if (touchSlider.readDistance() > 1)
         {
             SliderFadeTimeDemo();
@@ -58,33 +80,38 @@
 
 
 
+/****************
+ * SUB ROUTINES *
+ ****************/
 void SliderFadeTimeDemo()
 {
     float val = 0.00f;
+    float speedFactor = 0.5f;
+    float ledOff = 1.00f;
+    int minDistance = 2;
 
-    if (touchSlider.readDistance() > 2)
+    if (touchSlider.readDistance() > minDistance)
     {
-        val = touchSlider.readDistance() * 0.5f;
+        val = touchSlider.readDistance() * speedFactor;
         TouchFadeIn(val);
         TouchFadeOut(val);
     }
     else
     {
-        if (val > 4)
+        if (val > minDistance)
         {
             TouchFadeIn(val);
             TouchFadeOut(val);
         }
         else
         {
-            myRedLed = 1.00f;
-            myGreenLed = 1.00f;
-            myBlueLed = 1.00f;
+            myRedLed = ledOff;
+            myGreenLed = ledOff;
+            myBlueLed = ledOff;
         }
     }
 }
 
-
 void TouchFadeIn(float val)
 {   
     for (float i = 1.00f; i > 0.00f; i -= 0.01f)
@@ -110,8 +137,6 @@
     }
 }
 
-
-
 void AccelDemo()
 {
     float xAxisData = 1 - abs(accel.getAccX());
@@ -142,7 +167,6 @@
     }
 }
 
-
 void SendLetter(char input, float speed)
 {
     char lowerCaseLetter = tolower(input);