John Halfpenny / Mbed 2 deprecated Accellerometer_project

Dependencies:   USBDevice mbed DipCortex-USB-EEProm

Revision:
9:f37125673b91
Parent:
8:9a3d2d28e7e5
Child:
10:ba3a28345f76
--- a/main.cpp	Thu Sep 03 17:13:29 2015 +0000
+++ b/main.cpp	Fri Sep 11 10:37:33 2015 +0000
@@ -4,9 +4,13 @@
 // Laurence Halfpenny
 // August 2015
  
-#include "mbed.h"
-#include "main.h"
-#include "USBSerial.h"
+#include <stdint.h>
+#include <string.h>
+#include "mbed.h"           // Needed for MBED library functions
+#include "main.h"           // Needed for definitions of things that can be changed
+#include "USBSerial.h"      // Needed for USB serial port
+#include "DipCortex-EEprom.h"
+#include "LPC13Uxx.h"          
 
 USBSerial pc;               // USB CDC serial port
 
@@ -15,17 +19,29 @@
 AnalogIn Y_in(P0_12);       // Y accelleration voltage
 AnalogIn X_in(P0_13);       // X accelleration voltage
 
+// On-board LED
+DigitalOut LED(P0_1);
+
+// Push button
+// Logic FALSE when pushed, logic TRUE when no pushed
+DigitalIn BUTTON(P0_16);
+
 // Acceleration values
 int Z_accelleration=0;
 int Y_accelleration=0;
 int X_accelleration=0;
 
 // Storage arrays for X,Y,Z
+// Pack them to save RAM space
+#pragma pack(push,1)
 uint16_t Z_store[MAXSAMPLES]={};
 uint16_t Y_store[MAXSAMPLES]={};
 uint16_t X_store[MAXSAMPLES]={};
+#pragma pack(pop)
+
 int Sample_index=0;
 int Last_sample=0;
+int EEPROM_index=0;
 
 // Control variables
 bool monitoring=FALSE;      // Controls whether monitoring is running or stopped
@@ -57,6 +73,10 @@
     return ( prevCharIn );
 }
 
+// ------------------------------------------------------------------------------------------------------------
+//  Read and Write the EEPROM 
+// ------------------------------------------------------------------------------------------------------------
+
 
 // ------------------------------------------------------------------------------------------------------------
 //  Initialise the LPC1347 processor
@@ -72,6 +92,12 @@
     
     // Enable RAM1
     LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
+    
+    // Clear the LED
+    LED=FALSE;
+    
+    // Pull the BUTTON pin up to logic high
+    BUTTON.mode(PullUp);
 }
 
 // ------------------------------------------------------------------------------------------------------------
@@ -79,30 +105,32 @@
 // ------------------------------------------------------------------------------------------------------------
 void monitor_accelleration(void)
 {
+    LED = !LED;                         // Toggle the LED each sample
+    
     X_accelleration=X_in.read_u16();    // Read and print X accelleration
     X_store[Sample_index]=X_accelleration;
     X_accelleration=(X_accelleration-(0xFFFF/2));  // Convert to change in accelleration 
     X_accelleration=X_accelleration/16;
-    pc.printf("X: %d ",X_accelleration);  
+    //pc.printf("X: %d ",X_accelleration);  
 
     Y_accelleration=Y_in.read_u16();    // Read and print Y accelleration
     Y_store[Sample_index]=Y_accelleration;
     Y_accelleration=(Y_accelleration-(0xFFFF/2));  // Convert to change in accelleration 
     Y_accelleration=Y_accelleration/16;
-    pc.printf("Y: %d ",Y_accelleration);  
+    //pc.printf("Y: %d ",Y_accelleration);  
 
     Z_accelleration=Z_in.read_u16();    // Read and print Z accelleration
     Z_store[Sample_index]=Z_accelleration;
     Z_accelleration=(Z_accelleration-(0xFFFF/2));  // Convert to change in accelleration 
     Z_accelleration=Z_accelleration/16;
-    pc.printf("Z: %d \r\n",Z_accelleration);  
+    //pc.printf("Z: %d \r\n",Z_accelleration);  
     
-    pc.printf("Sample index: %d \r\n",Sample_index);  
+    //pc.printf("Sample index: %d \r\n",Sample_index);  
     Sample_index++;
     
     if (Sample_index>=MAXSAMPLES)
         {
-        pc.printf("Stopping - full!\r\n");
+        //pc.printf("Stopping - full!\r\n");
         monitoring=FALSE;
         monitor_tick.detach();
         Last_sample=Sample_index;
@@ -115,7 +143,7 @@
 int main( void ) 
 {   
 int i;
-    // Initalise the LPC1347
+    // Initalise the LPC1347 & WiFiDIPCORTEX board
     init(); 
     
     // Wait for terminal program to start
@@ -129,57 +157,80 @@
     pc.printf("|   Version 1.2                             |\r\n");
     pc.printf("+-------------------------------------------+\r\n");
 
-
-    // Forever check for commands from the keyboard
-    while (TRUE)
+    // If the BUTTON is pushed (logic FALSE) start logging otherwise go into keyboard menu
+    if (BUTTON==FALSE)
         {
-        pc.printf("+-------------------------------------------+\r\n");
-        pc.printf("|   V 1.2 MENU:                             |\r\n");
-        pc.printf("|   1: Start monitoring                     |\r\n");
-        pc.printf("|   2: Stop  monitoring                     |\r\n");
-        pc.printf("|   3: Stop / Dump values                   |\r\n");
-        pc.printf("+-------------------------------------------+\r\n");
- 
-        switch(WaitForSerialCommand()) 
+        // Start the monitoring
+        Sample_index=0;
+        monitoring=TRUE;
+        monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL);       
+        // Wait for the monitoring to finish and then save it to EEPROM
+        while (monitoring==TRUE)
+            {
+            // Just wait for monitoring to finish    
+            }
+        // Now copy the storage arrays to EEPROM
+        
+        // Now wait for ever
+        while (TRUE)
             {
-            case '1':      
-                pc.printf("Starting monitor\r\n");
-                Sample_index=0;
-                monitoring=TRUE;
-                monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL);
-            break;
-            
-            case '2':      
-                pc.printf("Stopping monitoring\r\n");
-                monitoring=FALSE;
-                monitor_tick.detach();
-                Last_sample=Sample_index;
-            break;
-            
-            case '3':
-                if (monitoring==TRUE)
-                    {
+            }
+        }
+    else
+        {
+        // Forever check for commands from the keyboard
+        while (TRUE)
+            {
+            pc.printf("+-------------------------------------------+\r\n");
+            pc.printf("|   V 1.2 MENU:                             |\r\n");
+            pc.printf("|   1: Start monitoring                     |\r\n");
+            pc.printf("|   2: Stop  monitoring                     |\r\n");
+            pc.printf("|   3: Stop / Dump values                   |\r\n");
+            pc.printf("+-------------------------------------------+\r\n");
+                     
+            switch(WaitForSerialCommand()) 
+                {
+                case '1':      
+                    pc.printf("Starting monitor\r\n");
+                    Sample_index=0;
+                    monitoring=TRUE;
+                    monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL);
+                break;
+                
+                case '2':      
                     pc.printf("Stopping monitoring\r\n");
                     monitoring=FALSE;
+                    LED=FALSE;
                     monitor_tick.detach();
-                    Last_sample=Sample_index;    
-                    }
+                    Last_sample=Sample_index;
+                break;
                 
-                if (Last_sample>0)
-                    {
-                    for (i=0; i<Last_sample; i++)
+                case '3':
+                    if (monitoring==TRUE)
                         {
-                        pc.printf("No.: %d ",i);     
-                        pc.printf("X: %d ",X_store[i]);  
-                        pc.printf("Y: %d ",Y_store[i]);    
-                        pc.printf("Z: %d \r\n",Z_store[i]);        
+                        pc.printf("Stopping monitoring\r\n");
+                        monitoring=FALSE;
+                        LED=FALSE;
+                        monitor_tick.detach();
+                        Last_sample=Sample_index;    
                         }
-                    }
-            break;
-            
-            default:
-                pc.printf("Invalid command\r\n");
-            break;
+                    
+                    if (Last_sample>0)
+                        {
+                        for (i=0; i<Last_sample; i++)
+                            {
+                            pc.printf("No.: %d ",i);     
+                            pc.printf("X: %d ",X_store[i]);  
+                            pc.printf("Y: %d ",Y_store[i]);    
+                            pc.printf("Z: %d \r\n",Z_store[i]);        
+                            }
+                        }
+                break;
+                
+                default:
+                    pc.printf("Invalid command\r\n");
+                break;
+                }
             }
         }