See graph

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Fork of Embedded_Software_Assignment_2 by Steven Kay

main.cpp

Committer:
sk398
Date:
2016-03-02
Revision:
9:46408a8dea0c
Parent:
8:7f3594882cec
Child:
10:c0531edf4850

File content as of revision 9:46408a8dea0c:

/* #####################################################################
                               main.cpp
                               ---------
                
                     Embedded Software - Assignment 2
                     --------------------------------
 
 Written by:        Steven Kay
 
 Date:              February 2016
 
 Function:          This 
 
 Version:           1.0
 
 Version History
 ---------------
 
 1.1                rgdfgdfgdfggdfgdg
 
 1.0                gdgddfdddgd
    
 ##################################################################### */

#include "mbed.h"
#include "Tasks.h"

// ============================================================================
// Task Declerations
// ============================================================================
Task1 task1(p11);                               // Square wave Measurement
Task2 task2_switch1(p12);                       // Read digital Input
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

// ============================================================================
// Cyclic Executive Objects and Declerations
// ============================================================================
DigitalOut ErrorLED(LED1);                      // Error Indicating LED
DigitalOut LenPin(p21);                         // Pulse Pin
DigitalIn SDRemoval(p18);                       // Switch state to indicate to remove SD
Timer BusyWait;                                 // Wasted time Timer
Ticker CyclicTicker;                            // Ticker object to cycle tasks

// ============================================================================
// Global Data Parameters used in Cyclic Executive
// ============================================================================
// Counter to record the number of ticks went through by the Cyclic Executive
int cyclicTicks = 1;

// Global parameter storing the most up to date value of the return from Task 1
volatile int task1Frequency;

// Global parameter storing the most up to date value of the return from Task 2
volatile int task2SwitchState;

// Global parameter storing the most up to date value of the return from Task 4
volatile float task4AnalogChannels[2];

// Global parameter storing the most up to date value of the return from Task 6
volatile int task6ErrorState;

// Char array to store the concatenated string for output onto the SD Card
char logData[50];

int slotCounter = 0;
int taskNum = 2;

// ============================================================================
// Cyclic Executive Function Prototypes
// ============================================================================
void CyclicExec();

// ============================================================================
// Main Execution Program
// ============================================================================
int main() {
   
    // Attempt to open SD file
    // If open failed, do not run Cyclic Exec
    if(!task7.openFile("/SD/A2/test.csv","a"))
    {    
        // Start Cyclic Executive 
        CyclicTicker.attach(&CyclicExec,0.025); // 25ms pulses
        
        // Keep program running until RESET
        while(1)
        {
        }
    }
    
    // If FIle is not opened, prompt user and show Error on LED
    else
    {
        // Prompt user about error
        printf("File not opened\r\nNot executing Cyclic Executive");
        
        // Execute error code on LED
        while(1)
        {
            ErrorLED = 1;
            wait(1);
            ErrorLED = 0;
            wait(1);
        }
    }
}

#define TASK1_TICKS 40
#define TASK2_TICKS 12
#define TASK3_TICKS 12
#define TASK4_TICKS 16
#define TASK5_TICKS 80
#define TASK6_TICKS 32
#define TASK7_TICKS 200

#define HIGH 1

Timer stampTime;
Timer task1t;
Timer task2t;
Timer task3t;
Timer task4t;
Timer task5t;
Timer task6t;

void CyclicExec()
{
    cyclicTicks++;
    if(cyclicTicks % 200 == 0)
    {
        stampTime.stop();
        printf("T7\r\n");
        int a = sprintf(    logData,"Time=%1.2f,Freq=%d,SW1=%d,A1=%1.3f,A2=%1.3f\n",
                            stampTime.read(),
                            task1Frequency,
                            task2SwitchState,
                            task4AnalogChannels[0],
                            task4AnalogChannels[1]  );
       task7.writeData(logData);
       stampTime.reset();
       stampTime.start();
    }
    
    if(cyclicTicks % 84 == 0)
    {
        task5t.stop();
        printf("T5 %1.2f\r\n",task5t.read());
        task5.updateDisplay(    task1Frequency,
                                task2SwitchState,
                                task6ErrorState,
                                task4AnalogChannels[0],
                                task4AnalogChannels[1]  );
        task5t.reset();
        task5t.start();                   
    }
    
    if(cyclicTicks % 38 == 0)
    {
        task1t.stop();
        printf("T1 %d\r\n",task1t.read_ms());
        task1Frequency = task1.ReadFrequency();
        task1t.reset();
        task1t.start();
    }

    if(cyclicTicks % 32 == 0)
    {
        task6t.stop();
        printf("T6 %d\r\n",task6t.read_ms());
        task6ErrorState = task6.updateErrorCode(    task2SwitchState,
                                                    task4AnalogChannels[0],
                                                    task4AnalogChannels[1]  );
        task6t.reset();
        task6t.start();
    }
    
    if(cyclicTicks % 16 == 0)
    {
        task4t.stop();
        printf("T4 %d\r\n",task4t.read_ms());
        float *analogReading = task4.returnAnalogReadings();
        task4AnalogChannels[0] = *(analogReading);
        task4AnalogChannels[1]= *(analogReading+1);
        task4t.reset();
        task4t.start();
    }
    
    if(cyclicTicks % 6 == 0)
    {
        if(taskNum == 2)
        {
            task2t.stop();
            printf("T2 %d\r\n",task2t.read_ms());
            task2SwitchState = task2_switch1.digitalInState();
            taskNum = 3;
            task2t.reset();
            task2t.start();
        }
        else if(taskNum == 3)
        {
            task3t.stop();
            printf("T3 %d\r\n",task3t.read_ms());
            task3.OutputWatchdogPulse();
            taskNum = 2;
            task3t.reset();
            task3t.start();
        }
    }

    if(SDRemoval == HIGH)
    {
        printf("SD Removed");
        task7.closeFile();
        CyclicTicker.detatch();
    }
}