chad

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

tasks.cpp

Committer:
f_legge
Date:
2017-03-08
Revision:
15:85616bc0e2ae
Parent:
14:ede0e7ed2745
Child:
16:bebcc7d24f3e

File content as of revision 15:85616bc0e2ae:

#include "main.h"

// Task 1: Measure input frequency
void Task1(void)
{
    timer.reset();
    
    if (FqIn == 0)
    {
        PosEdge();          //Wait for Pos 
        timer.start(); // Start timer
        while(FqIn == 1) // Keep counting as long as signal is high
        {
            wait_us(SampFreq);
        }
    }
    
    else if (FqIn == 1)
    {
        NegEdge();          // Wait for Neg
        timer.start();      // Start timer
        while(FqIn == 0)    // Keep counting as long as signal is high
        {
            wait_us(SampFreq);
        }
    }

    timer.stop(); // Stop counting when signal changes
    period = timer.read_us()*2; // Convert the time into a period
    freq = 1000000/period; // Convert the period into a frequency
    
    lcd->locate(0,0);
    lcd->printf("F: %d",freq);
}

void PosEdge(void)
{
    // Sub will end when high, Timer will start.
    while(FqIn == 0)
    {        
            wait_us(SampFreq);        
    }
}

void NegEdge(void)
{
    // Sub will end when low, Timer will start.
    while(FqIn == 1)
    {
        wait_us(SampFreq);
    }
}

////////////////////////////////////////////////////////////////////////////////
//
// Read Digital input switch
//
void Task2(void)
{
    //switch_state = DSIn == 1 ? 1: 0;     
    if(DSIn == 1)
        switch_state = 1;
    else if(DSIn == 0)
        switch_state = 0;
}


////////////////////////////////////////////////////////////////////////////////
//
// Output watchdog timer pulse
//
void Task3(void)
{
    WD_pulse = 1;       // Pulse High
    wait_us(WD);        // Leave high for specified length
    WD_pulse = 0;
}

////////////////////////////////////////////////////////////////////////////////
//
// Read and filter 2 analogue inputs
// 3.3V max
// 
void Task4(void)
{ 
    A1_val = 0;
    A2_val = 0;
    
    A1_in = (A1_in * 3.3);
    A2_in = (A2_in * 3.3);
    
    for(int i=0; i<3; i++)
    {
        A1_val = A1_val + A1_in;
        A2_val = A2_val + A2_in; 
    }
    
    A1_val = (A1_val / 3);
    A2_val = (A2_val / 3);
    
    lcd->locate(0,0);
    lcd->printf("A1:%1.2f A2:%1.2f",A1_val,A2_val);

}

////////////////////////////////////////////////////////////////////////////////
//
// Display Frequency, digital and filterd analogue values on LCD 
// 
void Task5(void)
{
    //T5.reset();
    //T5.start();
    
    lcd->cls();
    lcd->locate(0,0);
    //lcd->printf("F%d S%d% A1d% A2d",freq,switch_state,A1_val,A2_val); 
    lcd->printf("F%d",freq);
    //lcd->printf("test");
    
    //T5.stop();
}

////////////////////////////////////////////////////////////////////////////////
//
// Error check
// If switch_1 is ON & (average_analogue_in_1 > average_analogue_in_2) error code 3
// Else error code 0
// 
void Task6(void)
{/*
    if(switch_state == 1 && (A1_val > A2_val))
        error_code = 3;
    else
        error_code = 0;*/
    
    error_code++;
        
    //lcd->cls();
    lcd->locate(1,0);
    lcd->printf("Error: %d", error_code);
    
    wait_us(100);
}

////////////////////////////////////////////////////////////////////////////////
//
// Log frequency, digital and filtered analogue values to uSD
// 
void Task7(void)
{
    logcount++;
    fprintf(fp,"Log: %d,Freq: %dHz,Digital_In: %d,Analogue_1: %f,Analogue_2: %f\n",logcount,freq,switch_state,A1_val,A2_val); 

}

////////////////////////////////////////////////////////////////////////////////
//
// Shutdown on switch 
// 
/*void Task8()
{
    if(DS_sIn == 1)
    {
        tick.detach();
        fprintf(fp, "\nTask 5 took %dms to complete\n", T5.read_ms());
        fprintf(fp, "\nCyclic Executive stopped\n");
        fclose(fp);
    }
    else{
    }
}
*/