Example software for a Cyclic Executive

Dependencies:   MCP23017 SDFileSystem WattBob_TextLCD mbed

Tasks.cpp

Committer:
sk398
Date:
2016-02-29
Revision:
6:ceda53939eb8
Parent:
5:250f51c80ac1
Child:
9:46408a8dea0c

File content as of revision 6:ceda53939eb8:

/* #####################################################################
                               Tasks.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 1 ==================================== */
Task1::Task1(PinName squareWaveInPin)
{
    _squareWaveIn = new InterruptIn(squareWaveInPin);
}
    
void Task1::MeasureFrequency()
{
    _Task1Timer.start();
    _squareWaveIn -> rise(this,&Task1::StopCounter);    
}

void Task1::StopCounter()
{
    _Task1Timer.stop();
    measuredFrequency = _Task1Timer.read_us();
    _Task1Timer.reset();
}

int Task1::ReadFrequency()
{
    _squareWaveIn -> rise(this,&Task1::MeasureFrequency);
    
    return measuredFrequency;
}

/* ==================================== Task 2 ==================================== */
Task2::Task2(PinName digitalInCheckPin)
{
    _digitalInCheck = new DigitalIn(digitalInCheckPin);
}

bool Task2::digitalInState()
{
    if(_digitalInCheck -> read())
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


/* ==================================== Task 3 ==================================== */
Task3::Task3(PinName WatchdogPin)
{
    _Watchdog = new DigitalOut(WatchdogPin);
}
    
void Task3::OutputWatchdogPulse()
{
    _Watchdog -> write(HIGH);
    wait_ms(WATCHDOG_PULSE_WIDTH);
    _Watchdog -> write(LOW);
}


/* ==================================== Task 4 ==================================== */
Task4::Task4(PinName Analog1Pin,PinName Analog2Pin)
{
    _AnalogIn1 = new AnalogIn(Analog1Pin);
    _AnalogIn2 = new AnalogIn(Analog2Pin);
}

float *Task4::returnAnalogReadings()
{
    float readBuffer_1 = 0.0;
    float readBuffer_2 = 0.0;
    
    float outputBuffer[2];
       
//    outputBuffer[0] = _AnalogIn1 -> read();
//    outputBuffer[1] = _AnalogIn2 -> read();

    for(int readCount = 0;readCount < NUM_ANALOG_SAMPLES; readCount++)
    {
        readBuffer_1 += ((_AnalogIn1 -> read())*3.3);
//        printf("Buffer 1 %f\r\n",readBuffer_1);
        readBuffer_2 += ((_AnalogIn2 -> read())*3.3); 
//        printf("Buffer 2 %f\r\n",readBuffer_2); 
    }
    
    outputBuffer[0] = readBuffer_1/NUM_ANALOG_SAMPLES;
//    printf("outputBuffer[0] %f\r\n",outputBuffer[0]);
    outputBuffer[1] = readBuffer_2/NUM_ANALOG_SAMPLES;
//    printf("outputBuffer[1] %f\r\n",outputBuffer[1]);
    
    float *outputBufferPtr =&outputBuffer[0];
        
    return outputBufferPtr;    
}

/* ==================================== Task 5 ==================================== */
Task5::Task5(PinName sda, PinName scl, int address)
{
    _par_port = new MCP23017(sda,scl,address);
    _lcd = new WattBob_TextLCD(_par_port);
    _par_port -> write_bit(1,BL_BIT);
}

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 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);    
}