![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
See graph
Dependencies: MCP23017 SDFileSystem WattBob_TextLCD mbed
Fork of Embedded_Software_Assignment_2 by
Diff: Tasks.cpp
- Revision:
- 10:c0531edf4850
- Parent:
- 9:46408a8dea0c
- Child:
- 11:1069d300847b
diff -r 46408a8dea0c -r c0531edf4850 Tasks.cpp --- a/Tasks.cpp Wed Mar 02 09:38:47 2016 +0000 +++ b/Tasks.cpp Wed Mar 02 13:38:27 2016 +0000 @@ -9,17 +9,9 @@ Date: February 2016 - Function: This - - Version: 1.0 - - Version History - --------------- - - 1.1 rgdfgdfgdfggdfgdg - - 1.0 gdgddfdddgd - + Function: This code defines the operations of all of the + methods, or tasks used by the cyclic executive + ##################################################################### */ #include "mbed.h" @@ -28,37 +20,81 @@ /* ==================================== Task 1 ==================================== */ Task1::Task1(PinName squareWaveInPin) { + // Construct new DigitalIn object using the pin number provided _squareWaveIn = new DigitalIn(squareWaveInPin); } void Task1::MeasureFrequency() { - _Task1Timer.start(); - _squareWaveIn -> rise(this,&Task1::StopCounter); -} - -void Task1::StopCounter() -{ + // If pulse is initially low, wait until rising edge before starting timer + if(_squareWaveIn -> read() == LOW) + { + while(_squareWaveIn -> read() == LOW) + { + wait_us(SAMPLE_FREQ); + } + + _Task1Timer.start(); + + // Once timer has started, wait until falling edge before breaking and + // stopping timer + while(_squareWaveIn -> read() == HIGH) + { + wait_us(SAMPLE_FREQ); + } + } + + // If pulse is initially high, wait until falling edge before starting timer + else if(_squareWaveIn -> read()== HIGH) + { + while(_squareWaveIn -> read() == HIGH) + { + wait_us(SAMPLE_FREQ); + } + + _Task1Timer.start(); + + // Once timer has started, wait until rising edge before breaking and + // stopping timer + while(_squareWaveIn -> read() == LOW) + { + wait_us(SAMPLE_FREQ); + } + } + + // Stop timer after breaking while loop conditions _Task1Timer.stop(); - measuredFrequency = _Task1Timer.read_us(); + + // Calculate frequency from timer (either high or low time) + // do this by multiplying the time by 2 (high + low time) and dividing it, + // converting it to a frequency + // Store frequency in private class field + Task1::measuredFrequency = (1000000/(2*_Task1Timer.read_us())); + + // Reset timer _Task1Timer.reset(); + } int Task1::ReadFrequency() { - _squareWaveIn -> rise(this,&Task1::MeasureFrequency); + // Run private method to calculate the frequency + MeasureFrequency(); + // Return private field showing the newest frequency calculation return measuredFrequency; } /* ==================================== Task 2 ==================================== */ Task2::Task2(PinName digitalInCheckPin) { + //Construct new DigitalIn object from provided pin _digitalInCheck = new DigitalIn(digitalInCheckPin); } bool Task2::digitalInState() { + // Check state of pin, returning a TRUE if high, false if LOW if(_digitalInCheck -> read()) { return TRUE; @@ -73,11 +109,13 @@ /* ==================================== Task 3 ==================================== */ Task3::Task3(PinName WatchdogPin) { + // Construct new DigitalOut object using provided pin _Watchdog = new DigitalOut(WatchdogPin); } void Task3::OutputWatchdogPulse() { + // Produce a 15ms pulse when method called _Watchdog -> write(HIGH); wait_ms(WATCHDOG_PULSE_WIDTH); _Watchdog -> write(LOW); @@ -87,41 +125,44 @@ /* ==================================== Task 4 ==================================== */ Task4::Task4(PinName Analog1Pin,PinName Analog2Pin) { + // Construct new AnalogIn objects from provided pins _AnalogIn1 = new AnalogIn(Analog1Pin); _AnalogIn2 = new AnalogIn(Analog2Pin); } float *Task4::returnAnalogReadings() { + // Declare local scope fields to retain current totals float readBuffer_1 = 0.0; float readBuffer_2 = 0.0; - - float outputBuffer[2]; - -// outputBuffer[0] = _AnalogIn1 -> read(); -// outputBuffer[1] = _AnalogIn2 -> read(); + // Read 4 samples from AnalogIn pins 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); + // Add to readBuffer with new weighted reading. + // 3.3v due to supply voltage + readBuffer_1 += ((_AnalogIn1 -> read())*V_SUPPLY); + readBuffer_2 += ((_AnalogIn2 -> read())*V_SUPPLY); } + // Construct local buffer + float outputBuffer[2]; + + // Construct elements in outputBuffer array as averaged sample 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]); + // Construct pointer to return the initial element of the outputBuffer float *outputBufferPtr =&outputBuffer[0]; - + + // Return pointer to first element of outputBuffer return outputBufferPtr; } /* ==================================== Task 5 ==================================== */ Task5::Task5(PinName sda, PinName scl, int address) { + // Declare and initialise the LCD display _par_port = new MCP23017(sda,scl,address); _lcd = new WattBob_TextLCD(_par_port); _par_port -> write_bit(1,BL_BIT); @@ -133,6 +174,7 @@ float task4Channel1, float task4Channel2 ) { + // Print standard expression using input fields _lcd -> cls(); _lcd -> locate(0,0); _lcd -> printf("F-%4dHz S1-%d E%d",task1Param,task2Param,errorState); @@ -143,10 +185,16 @@ /* ==================================== Task 6 ==================================== */ int Task6::updateErrorCode(int switch_1, float analog1, float analog2) { - if(switch_1 == 1 && (analog1 > analog2)) - return ERROR_CODE_CDTN_MET; + // Using input fields, conduct a logical equation7 + // returning CDTN_MET when true, CDTN_FAIL when false + if(switch_1 == HIGH && (analog1 > analog2)) + { + return ERROR_CODE_CDTN_MET; + } else - return ERROR_CODE_CDTN_FAIL; + { + return ERROR_CODE_CDTN_FAIL; + } } /* ==================================== Task 5 ==================================== */ @@ -157,19 +205,26 @@ const char *SDName, const char *dir ) { + // Construct new SDFileSystem object _sd = new SDFileSystem(mosi,miso,sck,cs, SDName); + + // Call private method to create default directory makeDirectory(dir); } void Task7::makeDirectory(const char *dir) { + // Create directory onto sd card mkdir(dir,0777); } int Task7::openFile(const char *dirFile,const char *accessType) { - fp = fopen(dirFile,accessType); - if(fp == NULL) + // Create pointer to FILE object + Task7::fp = fopen(dirFile,accessType); + + // If failed to open file, return 1, indicating error, else return 0 + if(Task7::fp == NULL) { return 1; } @@ -178,10 +233,12 @@ void Task7::writeData(const char *dataStream) { - fprintf(fp,dataStream); + // Print Stream of data to FILE object fp + fprintf(Task7::fp,dataStream); } void Task7::closeFile() { - fclose(fp); + // Close file located at fp + fclose(Task7::fp); } \ No newline at end of file