Steven Kay / Mbed 2 deprecated Embedded_Software_Assignment_2

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Files at this revision

API Documentation at this revision

Comitter:
sk398
Date:
Mon Feb 29 11:20:48 2016 +0000
Parent:
5:250f51c80ac1
Child:
7:2973bf297f3d
Commit message:
Tasks all written in. Just got to implement the Cyclic Executive OS element

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
Tasks.cpp Show annotated file Show diff for this revision Revisions of this file
Tasks.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Feb 29 11:20:48 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- a/Tasks.cpp	Fri Feb 26 10:44:38 2016 +0000
+++ b/Tasks.cpp	Mon Feb 29 11:20:48 2016 +0000
@@ -24,8 +24,6 @@
 
 #include "mbed.h"
 #include "Tasks.h"
-#include "MCP23017.h"
-#include "WattBob_TextLCD.h"
 
 /* ==================================== Task 1 ==================================== */
 Task1::Task1(PinName squareWaveInPin)
@@ -129,11 +127,61 @@
     _par_port -> write_bit(1,BL_BIT);
 }
 
-void Task5::updateDisplay(int task1Param,int task2Param, float task4Channel1, float task4Channel2)
+void Task5::updateDisplay(  int task1Param,
+                            int task2Param,
+                            int errorState,
+                            float task4Channel1,
+                            float task4Channel2  )
 {
     _lcd -> cls();
     _lcd -> locate(0,0);
-    _lcd -> printf("F-%4dHz S1-%d ",task1Param,task2Param);
+    _lcd -> printf("F-%4dHz S1-%d E%d",task1Param,task2Param,errorState);
     _lcd -> locate(1,0);
     _lcd -> printf("C1-%1.2f C2-%1.2f ",task4Channel1,task4Channel2);
 }
+
+/* ==================================== Task 6 ==================================== */
+int Task6::updateErrorCode(int switch_1, float analog1, float analog2)
+{
+    if(switch_1 == 1 && (analog1 > analog2))
+    return ERROR_CODE_CDTN_MET;
+    else
+    return ERROR_CODE_CDTN_FAIL;
+}
+
+/* ==================================== Task 5 ==================================== */
+Task7::Task7(   PinName mosi,
+                PinName miso,
+                PinName sck,
+                PinName cs,
+                const char *SDName,
+                const char *dir    )
+{
+    _sd = new SDFileSystem(mosi,miso,sck,cs, SDName);
+    makeDirectory(dir);
+}
+
+void Task7::makeDirectory(const char *dir)
+{
+    mkdir(dir,0777);
+}
+
+int Task7::openFile(const char *dirFile,const char *accessType)
+{
+    fp = fopen(dirFile,accessType);
+    if(fp == NULL)
+    {
+        return 1;
+    }
+    return 0;
+}
+
+void Task7::writeData(const char *dataStream)
+{
+    fprintf(fp,dataStream);
+}
+
+void Task7::closeFile()
+{
+    fclose(fp);    
+}
\ No newline at end of file
--- a/Tasks.h	Fri Feb 26 10:44:38 2016 +0000
+++ b/Tasks.h	Mon Feb 29 11:20:48 2016 +0000
@@ -25,9 +25,13 @@
 #ifndef _TASKS_H_
 #define _TASKS_H_
 
+// Includes to operate the LCD Display
 #include "MCP23017.h"
 #include "WattBob_TextLCD.h"
 
+// Includes to operate the SD Card system
+#include "SDFileSystem.h"
+
 // Global definitions
 #define HIGH 1
 #define LOW 0
@@ -48,7 +52,8 @@
 // Task 5 definitions
 
 // Task 6 definitions
-
+#define ERROR_CODE_CDTN_MET 3
+#define ERROR_CODE_CDTN_FAIL 0
 
 /* ####################### Class definitions and prototypes ####################### */
 
@@ -69,6 +74,8 @@
     volatile int measuredFrequency;
     
     InterruptIn *_squareWaveIn;
+    
+protected:
         
 };
 
@@ -82,8 +89,11 @@
     bool digitalInState();
     
 private:
+
+    
+protected:
     DigitalIn *_digitalInCheck;
-        
+    
 };
 
 
@@ -96,7 +106,11 @@
     void OutputWatchdogPulse();
         
 private:
-    DigitalOut *_Watchdog;    
+
+
+protected:
+    DigitalOut *_Watchdog;
+
 };
 
 
@@ -109,8 +123,12 @@
     float *returnAnalogReadings();
     
 private:
+
+
+protected:
     AnalogIn *_AnalogIn1;
-    AnalogIn *_AnalogIn2;        
+    AnalogIn *_AnalogIn2;   
+
 };
 
 //* ==================================== Task 5 ==================================== */
@@ -119,41 +137,58 @@
 {
 public:
     Task5(PinName sda, PinName scl, int address);
-    void updateDisplay(int task1Param,int task2Param, float task4Channel1, float task4Channel2);
-
-        
+    void updateDisplay( int task1Param,
+                        int task2Param,
+                        int errorState,
+                        float task4Channel1,
+                        float task4Channel2  );
+                             
 private:
 
 
 protected:
     MCP23017 *_par_port;
-    WattBob_TextLCD *_lcd;
-    
-        
+    WattBob_TextLCD *_lcd; 
+
 };
-//
+
 ///* ==================================== Task 6 ==================================== */
-//// Logical checks
-//class Task6
-//{
-//public:
-//    Task5();
-//        
-//private:
-//    
-//        
-//};
-//
-///* ==================================== Task 7 ==================================== */
-//// Save data to SD Card
-//class Task7
-//{
-//public:
-//    Task7();
-//        
-//private:
-//    
-//        
-//};
+// Logical checks
+class Task6
+{
+public:
+    int updateErrorCode(int switch_1, float analog1, float analog2);  
+
+private:
+
+
+protected:
+
+
+};
+
+/* ==================================== Task 7 ==================================== */
+// Save data to SD Card
+class Task7
+{
+public:
+    Task7(  PinName mosi,
+            PinName miso,
+            PinName sck,
+            PinName cs,
+            const char* SDName,
+            const char *dir     );
+
+    void writeData(const char *dataStream);
+    int openFile(const char *dirFile, const char *accessType);
+    void closeFile();
+    
+private:
+    void makeDirectory(const char *dir);
+
+protected:
+    SDFileSystem *_sd;
+    FILE *fp;
+};
 
 #endif
--- a/main.cpp	Fri Feb 26 10:44:38 2016 +0000
+++ b/main.cpp	Mon Feb 29 11:20:48 2016 +0000
@@ -32,29 +32,57 @@
 
 DigitalOut myled(LED1);
 
-Task1 task1(p11);                   // Square wave Measurement
-Task2 task2_switch1(p12);           // Read digital Output
-Task3 task3(p13);                   // Watchdog Pulse
-Task4 task4(p15,p16);               // Read analog Inputs
-Task5 task5(p9,p10,0x40);           // Output to LCD Display
+Task1 task1(p11);                               // Square wave Measurement
+Task2 task2_switch1(p12);                       // Read digital Output
+Task3 task3(p13);                               // Watchdog Pulse
+Task4 task4(p15,p16);                           // Read analog Inputs
+Task5 task5(p9,p10,0x40);                       // Output to LCD Display
+Task6 task6;                                    // Logical checks
+Task7 task7(p5,p6,p7,p8,"SD","/SD/A2");         // SD Card Write
 
 int main() {
-
-    int task1Frequency = task1.ReadFrequency();
-    printf("Task 1 Frequency %d Hz\r\n",task1Frequency);
     
-    int task2SwitchState = task2_switch1.digitalInState();
-    printf("Switch 1 State: %d\r\n",task2SwitchState);
+    volatile int task1Frequency;
+    volatile int task2SwitchState;
+    volatile float task4AnalogChannels[2];
+    volatile int errorState;
+    char logData[50];
+    
+    task1Frequency = task1.ReadFrequency();
+//    printf("Task 1 Frequency %d Hz\r\n",task1Frequency);
+    
+    task2SwitchState = task2_switch1.digitalInState();
+//    printf("Switch 1 State: %d\r\n",task2SwitchState);
 
     task3.OutputWatchdogPulse();
     
     float *analogReading = task4.returnAnalogReadings();
-    float channel1 = *(analogReading);
-    float channel2 = *(analogReading+1);
-    printf("Analog Readings:\r\nChannel 1-%f\r\nChannel 2-%f\r\n",channel1,channel2);
+    task4AnalogChannels[0] = *(analogReading);
+    task4AnalogChannels[1]= *(analogReading+1);
+//    printf("Analog Readings:\r\nChannel 1-%f\r\nChannel 2-%f\r\n",task4AnalogChannels[0],task4AnalogChannels[1]);
+    
+    errorState = task6.updateErrorCode( task2SwitchState,
+                                        task4AnalogChannels[0],
+                                        task4AnalogChannels[1]  );
     
-    task5.updateDisplay(task1Frequency,task2SwitchState,channel1,channel2);
-    
+    task5.updateDisplay(task1Frequency,
+                        task2SwitchState,
+                        errorState,
+                        task4AnalogChannels[0],
+                        task4AnalogChannels[1]  );
+
+    if(task7.openFile("/SD/A2/test.csv","a"))
+    {
+        printf("File not opened");
+    }
+    else
+    {
+        int a = sprintf(logData,"Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",task1Frequency,
+                        task2SwitchState,task4AnalogChannels[0],task4AnalogChannels[1]);
+        task7.writeData(logData);
+        task7.closeFile();
+    }
+
     while(1) {
         myled = 1;
         wait(0.2);