See graph
Dependencies: MCP23017 SDFileSystem WattBob_TextLCD mbed
Fork of Embedded_Software_Assignment_2 by
Tasks.cpp
00001 /* ##################################################################### 00002 Tasks.cpp 00003 --------- 00004 00005 Embedded Software - Assignment 2 00006 -------------------------------- 00007 00008 Written by: Xavier Gouesnard 00009 00010 Date: March 2017 00011 00012 Function: This code defines the operations of all of the 00013 methods, or tasks used by the cyclic executive 00014 00015 ##################################################################### */ 00016 00017 #include "mbed.h" 00018 #include "Tasks.h" 00019 00020 /* ==================================== Task 1 ==================================== */ 00021 Task1::Task1(PinName squareWaveInPin) 00022 { 00023 // Construct new DigitalIn object using the pin number provided 00024 _squareWaveIn = new DigitalIn(squareWaveInPin); 00025 } 00026 00027 void Task1::MeasureFrequency() 00028 { 00029 // If pulse is initially low, wait until rising edge before starting timer 00030 if(_squareWaveIn -> read() == LOW) 00031 { 00032 while(_squareWaveIn -> read() == LOW) 00033 { 00034 wait_us(SAMPLE_FREQ); 00035 } 00036 00037 _Task1Timer.start(); 00038 00039 // Once timer has started, wait until falling edge before breaking and 00040 // stopping timer 00041 while(_squareWaveIn -> read() == HIGH) 00042 { 00043 wait_us(SAMPLE_FREQ); 00044 } 00045 } 00046 00047 // If pulse is initially high, wait until falling edge before starting timer 00048 else if(_squareWaveIn -> read()== HIGH) 00049 { 00050 while(_squareWaveIn -> read() == HIGH) 00051 { 00052 wait_us(SAMPLE_FREQ); 00053 } 00054 00055 _Task1Timer.start(); 00056 00057 // Once timer has started, wait until rising edge before breaking and 00058 // stopping timer 00059 while(_squareWaveIn -> read() == LOW) 00060 { 00061 wait_us(SAMPLE_FREQ); 00062 } 00063 } 00064 00065 // Stop timer after breaking while loop conditions 00066 _Task1Timer.stop(); 00067 00068 // Calculate frequency from timer (either high or low time) 00069 // do this by multiplying the time by 2 (high + low time) and dividing it, 00070 // converting it to a frequency 00071 // Store frequency in private class field 00072 Task1::measuredFrequency = (1000000/(2*_Task1Timer.read_us())); 00073 00074 // Reset timer 00075 _Task1Timer.reset(); 00076 00077 } 00078 00079 int Task1::ReadFrequency() 00080 { 00081 // Run private method to calculate the frequency 00082 MeasureFrequency(); 00083 00084 // Return private field showing the newest frequency calculation 00085 return measuredFrequency; 00086 } 00087 00088 /* ==================================== Task 2 ==================================== */ 00089 Task2::Task2(PinName digitalInCheckPin) 00090 { 00091 //Construct new DigitalIn object from provided pin 00092 _digitalInCheck = new DigitalIn(digitalInCheckPin); 00093 } 00094 00095 bool Task2::digitalInState() 00096 { 00097 // Check state of pin, returning a TRUE if high, false if LOW 00098 if(_digitalInCheck -> read()) 00099 { 00100 return TRUE; 00101 } 00102 else 00103 { 00104 return FALSE; 00105 } 00106 } 00107 00108 00109 /* ==================================== Task 3 ==================================== */ 00110 Task3::Task3(PinName WatchdogPin) 00111 { 00112 // Construct new DigitalOut object using provided pin 00113 _Watchdog = new DigitalOut(WatchdogPin); 00114 } 00115 00116 void Task3::OutputWatchdogPulse() 00117 { 00118 // Produce a 15ms pulse when method called 00119 _Watchdog -> write(HIGH); 00120 wait_ms(WATCHDOG_PULSE_WIDTH); 00121 _Watchdog -> write(LOW); 00122 } 00123 00124 00125 /* ==================================== Task 4 ==================================== */ 00126 Task4::Task4(PinName Analog1Pin,PinName Analog2Pin) 00127 { 00128 // Construct new AnalogIn objects from provided pins 00129 _AnalogIn1 = new AnalogIn(Analog1Pin); 00130 _AnalogIn2 = new AnalogIn(Analog2Pin); 00131 } 00132 00133 float *Task4::returnAnalogReadings() 00134 { 00135 // Declare local scope fields to retain current totals 00136 float readBuffer_1 = 0.0; 00137 float readBuffer_2 = 0.0; 00138 00139 // Read 4 samples from AnalogIn pins 00140 for(int readCount = 0;readCount < NUM_ANALOG_SAMPLES; readCount++) 00141 { 00142 // Add to readBuffer with new weighted reading. 00143 // 3.3v due to supply voltage 00144 readBuffer_1 += ((_AnalogIn1 -> read())*V_SUPPLY); 00145 readBuffer_2 += ((_AnalogIn2 -> read())*V_SUPPLY); 00146 } 00147 00148 // Construct local buffer 00149 float outputBuffer[2]; 00150 00151 // Construct elements in outputBuffer array as averaged sample 00152 outputBuffer[0] = readBuffer_1/NUM_ANALOG_SAMPLES; 00153 outputBuffer[1] = readBuffer_2/NUM_ANALOG_SAMPLES; 00154 00155 // Construct pointer to return the initial element of the outputBuffer 00156 float *outputBufferPtr =&outputBuffer[0]; 00157 00158 // Return pointer to first element of outputBuffer 00159 return outputBufferPtr; 00160 } 00161 00162 /* ==================================== Task 5 ==================================== */ 00163 Task5::Task5(PinName sda, PinName scl, int address) 00164 { 00165 // Declare and initialise the LCD display 00166 _par_port = new MCP23017(sda,scl,address); 00167 _lcd = new WattBob_TextLCD(_par_port); 00168 _par_port -> write_bit(1,BL_BIT); 00169 } 00170 00171 void Task5::updateDisplay( int task1Param, 00172 int task2Param, 00173 int errorState, 00174 float task4Channel1, 00175 float task4Channel2 ) 00176 { 00177 // Print standard expression using input fields 00178 _lcd -> cls(); 00179 _lcd -> locate(0,0); 00180 _lcd -> printf("F-%4dHz S1-%d E%d",task1Param,task2Param,errorState); 00181 _lcd -> locate(1,0); 00182 _lcd -> printf("C1-%1.2f C2-%1.2f ",task4Channel1,task4Channel2); 00183 } 00184 00185 /* ==================================== Task 6 ==================================== */ 00186 int Task6::updateErrorCode(int switch_1, float analog1, float analog2) 00187 { 00188 // Using input fields, conduct a logical equation7 00189 // returning CDTN_MET when true, CDTN_FAIL when false 00190 if(switch_1 == HIGH && (analog1 > analog2)) 00191 { 00192 return ERROR_CODE_CDTN_MET; 00193 } 00194 else 00195 { 00196 return ERROR_CODE_CDTN_FAIL; 00197 } 00198 } 00199 00200 /* ==================================== Task 5 ==================================== */ 00201 Task7::Task7( PinName mosi, 00202 PinName miso, 00203 PinName sck, 00204 PinName cs, 00205 const char *SDName, 00206 const char *dir ) 00207 { 00208 // Construct new SDFileSystem object 00209 _sd = new SDFileSystem(mosi,miso,sck,cs, SDName); 00210 00211 // Call private method to create default directory 00212 makeDirectory(dir); 00213 } 00214 00215 void Task7::makeDirectory(const char *dir) 00216 { 00217 // Create directory onto sd card 00218 mkdir(dir,0777); 00219 } 00220 00221 int Task7::openFile(const char *dirFile,const char *accessType) 00222 { 00223 // Create pointer to FILE object 00224 Task7::fp = fopen(dirFile,accessType); 00225 00226 // If failed to open file, return 1, indicating error, else return 0 00227 if(Task7::fp == NULL) 00228 { 00229 return 1; 00230 } 00231 return 0; 00232 } 00233 00234 void Task7::writeData(const char *dataStream) 00235 { 00236 // Print Stream of data to FILE object fp 00237 fprintf(Task7::fp,dataStream); 00238 } 00239 00240 void Task7::closeFile() 00241 { 00242 // Close file located at fp 00243 fclose(Task7::fp); 00244 } 00245 00246 //void Task8::shutdown_switch (int shutdown_switch){ 00247 00248 //Shutdown the program 00249 // fprintf(Task8::fp, shut_down); 00250 //}
Generated on Tue Jul 12 2022 22:57:56 by
1.7.2
