John Halfpenny / Mbed 2 deprecated Accellerometer_project

Dependencies:   USBDevice mbed DipCortex-USB-EEProm

Revision:
10:ba3a28345f76
Parent:
9:f37125673b91
diff -r f37125673b91 -r ba3a28345f76 main.cpp
--- a/main.cpp	Fri Sep 11 10:37:33 2015 +0000
+++ b/main.cpp	Fri Sep 11 13:54:03 2015 +0000
@@ -12,8 +12,6 @@
 #include "DipCortex-EEprom.h"
 #include "LPC13Uxx.h"          
 
-USBSerial pc;               // USB CDC serial port
-
 // Define the X, Y & Z analogue inputs on ADCs
 AnalogIn Z_in(P0_11);       // Z accelleration voltage
 AnalogIn Y_in(P0_12);       // Y accelleration voltage
@@ -49,29 +47,6 @@
 // Ticker for monitoring
 Ticker monitor_tick;
 
-// ------------------------------------------------------------------------------------------------------------
-// Wait for a character to be typed
-// ------------------------------------------------------------------------------------------------------------
-char WaitForSerialCommand ( void )
-{
-char charIn = 0;
-char prevCharIn;
-
-    pc.printf("Enter command: ");
-  
-    while (TRUE)
-    {
-        prevCharIn = charIn;
-        charIn = pc.getc();
-        
-        pc.printf("%c", charIn);
-        if ((charIn == '\n') || (charIn == '\r'))
-        {
-            break;
-        }
-    }
-    return ( prevCharIn );
-}
 
 // ------------------------------------------------------------------------------------------------------------
 //  Read and Write the EEPROM 
@@ -98,6 +73,9 @@
     
     // Pull the BUTTON pin up to logic high
     BUTTON.mode(PullUp);
+    
+    // Initialise the IAP for EEPROM reading and writing
+    IAP_Init();
 }
 
 // ------------------------------------------------------------------------------------------------------------
@@ -142,42 +120,61 @@
 // ------------------------------------------------------------------------------------------------------------
 int main( void ) 
 {   
-int i;
+int i;  // For controlling loops
+char charIn = 0;
+char prevCharIn = 0;
+char Command = 0;
+char j=0;    // For writing to clear EEPROM 
+
     // Initalise the LPC1347 & WiFiDIPCORTEX board
     init(); 
     
-    // Wait for terminal program to start
-    wait(WAITATSTART);
+    // Wait for program to start
+    wait(1);
     
-    // Show the start banner
-    pc.printf("\r\n");
-    pc.printf("+-------------------------------------------+\r\n");
-    pc.printf("|   3-axis accelleration measurer           |\r\n");
-    pc.printf("|   Laurence Halfpenny                      |\r\n");
-    pc.printf("|   Version 1.2                             |\r\n");
-    pc.printf("+-------------------------------------------+\r\n");
-
     // If the BUTTON is pushed (logic FALSE) start logging otherwise go into keyboard menu
     if (BUTTON==FALSE)
         {
+        
         // 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    
+            wait(1);
             }
+        
         // Now copy the storage arrays to EEPROM
+        LED=TRUE;   // Turn LED solid on when writing to EEPROM
+        IAP_Eeprom_Write(EEPROMBASEADDRESS, (uint8_t *)&X_store, sizeof(X_store));                                  // Write the X array
+        IAP_Eeprom_Write(EEPROMBASEADDRESS+sizeof(X_store), (uint8_t *)&Y_store, sizeof(Y_store));                  // Write the Y array
+        IAP_Eeprom_Write(EEPROMBASEADDRESS+sizeof(X_store)+sizeof(Y_store), (uint8_t *)&Z_store, sizeof(Z_store));  // Write the Z array
         
-        // Now wait for ever
+        // Now wait for ever just flashing LED
         while (TRUE)
             {
+            LED=!LED;   // Flash the LED slowly at end
+            wait(1);    
             }
         }
     else
         {
+        USBSerial pc;               // open USB CDC serial port
+        // Wait for terminal program to start
+        wait(WAITATSTART);
+        
+        // Show the start banner
+        pc.printf("\r\n");
+        pc.printf("+-------------------------------------------+\r\n");
+        pc.printf("|   3-axis accelleration measurer           |\r\n");
+        pc.printf("|   Laurence Halfpenny                      |\r\n");
+        pc.printf("|   Version 1.2                             |\r\n");
+        pc.printf("+-------------------------------------------+\r\n");
+    
         // Forever check for commands from the keyboard
         while (TRUE)
             {
@@ -186,9 +183,26 @@
             pc.printf("|   1: Start monitoring                     |\r\n");
             pc.printf("|   2: Stop  monitoring                     |\r\n");
             pc.printf("|   3: Stop / Dump values                   |\r\n");
+            pc.printf("|   4: Dump EEPROM values                   |\r\n");
+            pc.printf("|   9: Clear EEPROM values                  |\r\n");
             pc.printf("+-------------------------------------------+\r\n");
                      
-            switch(WaitForSerialCommand()) 
+            // Get a command from the keyboard
+            pc.printf("Enter command: ");         
+            while (TRUE)
+                {
+                prevCharIn = charIn;
+                charIn = pc.getc();
+                pc.printf("%c", charIn);
+                if ((charIn == '\n') || (charIn == '\r'))
+                    {
+                    break;
+                    }
+                }
+            Command=prevCharIn;
+                
+            // Action the command
+            switch(Command) 
                 {
                 case '1':      
                     pc.printf("Starting monitor\r\n");
@@ -227,6 +241,35 @@
                         }
                 break;
                 
+                case '4':
+                    pc.printf("Dumping EEPROM\r\n");     
+                    // Read EEPROM into Storage arrays
+                    IAP_Eeprom_Read(EEPROMBASEADDRESS, (uint8_t *)&X_store, sizeof(X_store));                                   // Read EEPROM into X array       
+                    IAP_Eeprom_Read(EEPROMBASEADDRESS+sizeof(X_store), (uint8_t *)&Y_store, sizeof(Y_store));                   // Read EEPROM into Y array       
+                    IAP_Eeprom_Read(EEPROMBASEADDRESS+sizeof(X_store)+sizeof(Y_store), (uint8_t *)&Z_store, sizeof(Z_store));   // Read EEPROM into Z array       
+                    // Display them
+                    for (i=0; i<MAXSAMPLES; 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;
+                
+                case '9':
+                    pc.printf("Clearing EEPROM\r\n");     
+                    // Fill one of the arrays with zeros
+                    for (i=0; i<MAXSAMPLES; i++)
+                        {
+                        X_store[i]=(uint16_t)j;    
+                        }
+                    // Write this array to X, Y & Z EEPROM stores
+                    IAP_Eeprom_Write(EEPROMBASEADDRESS, (uint8_t *)&X_store, sizeof(X_store));              
+                    IAP_Eeprom_Write(EEPROMBASEADDRESS+sizeof(X_store), (uint8_t *)&X_store, sizeof(X_store));            
+                    IAP_Eeprom_Write(EEPROMBASEADDRESS+sizeof(X_store)+sizeof(Y_store), (uint8_t *)&X_store, sizeof(X_store));            
+                break;
+                
                 default:
                     pc.printf("Invalid command\r\n");
                 break;
@@ -235,3 +278,4 @@
         }
 
 }
+