chad

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

tasks.cpp

Committer:
f_legge
Date:
2017-03-10
Revision:
16:bebcc7d24f3e
Parent:
15:85616bc0e2ae
Child:
17:bc25d5f47bab

File content as of revision 16:bebcc7d24f3e:

#include "main.h"

// Task 1: Measure input frequency
void Task1(void)
{
    //  Wait for Posedge
    if (Fq == 0){
        while (Fq == 0){
            wait_us (1);
            }
        //  Posedge detected, log start time
        if (Fq == 1){
            timer.start ();
        }
            
        //  Wait for Negedge
        while (Fq == 1){
            wait_us (1);
        }
        //  Negedge detected, log end time
        if (Fq == 0){
            timer.stop ();
        }
    }
    
    else if (Fq == 1){
        
        while (Fq == 1){
            wait_us (1);
            }
        //  Posedge detected, log start time
        if (Fq == 0){
            timer.start ();
        }
            
        //  Wait for Negedge
        while (Fq == 0){
            wait_us (1);
        }
        //  Negedge detected, log end time
        if (Fq == 1){
            timer.stop ();
        }
    }
    
    //timer.stop(); // Stop counting when signal changes*/

    lcd->locate(0,0);
    lcd->printf("F: %.0f",(1/(2*timer.read())));
    
    timer.reset();
}

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",freq,switch_state);
    lcd->locate(1,0);
    lcd->printf("A1:%1.1f A2:%1.1f E%d",A1_val,A2_val,error_code);

    //lcd->printf("F%d",freq);
    //lcd->printf("test");

    T5.stop();

    lcd->locate(0,8);
    lcd->printf(":%.1f",(T5.read()*1000));
}

////////////////////////////////////////////////////////////////////////////////
//
// 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((A1_val > A2_val))
        error_code = 3;
    else
        error_code = 0;



    //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: %fHz,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) {
        ticker.detach();
        fprintf(fp, "\nTask 5 took %dms to complete\n", T5.read_ms());
        fprintf(fp, "\nCyclic Executive stopped\n");
        fclose(fp);
    } else {
    }
}