Example software for a Cyclic Executive
Dependencies: MCP23017 SDFileSystem WattBob_TextLCD mbed
Tasks.cpp@9:46408a8dea0c, 2016-03-02 (annotated)
- Committer:
- sk398
- Date:
- Wed Mar 02 09:38:47 2016 +0000
- Revision:
- 9:46408a8dea0c
- Parent:
- 6:ceda53939eb8
- Child:
- 10:c0531edf4850
All tasks operating and Cyclic Exec working. Just need to implement task 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 4:b85bc0d810e1 | 1 | /* ##################################################################### |
sk398 | 4:b85bc0d810e1 | 2 | Tasks.cpp |
sk398 | 4:b85bc0d810e1 | 3 | --------- |
sk398 | 4:b85bc0d810e1 | 4 | |
sk398 | 4:b85bc0d810e1 | 5 | Embedded Software - Assignment 2 |
sk398 | 4:b85bc0d810e1 | 6 | -------------------------------- |
sk398 | 4:b85bc0d810e1 | 7 | |
sk398 | 4:b85bc0d810e1 | 8 | Written by: Steven Kay |
sk398 | 4:b85bc0d810e1 | 9 | |
sk398 | 4:b85bc0d810e1 | 10 | Date: February 2016 |
sk398 | 4:b85bc0d810e1 | 11 | |
sk398 | 4:b85bc0d810e1 | 12 | Function: This |
sk398 | 4:b85bc0d810e1 | 13 | |
sk398 | 4:b85bc0d810e1 | 14 | Version: 1.0 |
sk398 | 4:b85bc0d810e1 | 15 | |
sk398 | 4:b85bc0d810e1 | 16 | Version History |
sk398 | 4:b85bc0d810e1 | 17 | --------------- |
sk398 | 4:b85bc0d810e1 | 18 | |
sk398 | 4:b85bc0d810e1 | 19 | 1.1 rgdfgdfgdfggdfgdg |
sk398 | 4:b85bc0d810e1 | 20 | |
sk398 | 4:b85bc0d810e1 | 21 | 1.0 gdgddfdddgd |
sk398 | 4:b85bc0d810e1 | 22 | |
sk398 | 4:b85bc0d810e1 | 23 | ##################################################################### */ |
sk398 | 2:22ebabd78084 | 24 | |
sk398 | 1:221d677fe0d3 | 25 | #include "mbed.h" |
sk398 | 1:221d677fe0d3 | 26 | #include "Tasks.h" |
sk398 | 1:221d677fe0d3 | 27 | |
sk398 | 1:221d677fe0d3 | 28 | /* ==================================== Task 1 ==================================== */ |
sk398 | 1:221d677fe0d3 | 29 | Task1::Task1(PinName squareWaveInPin) |
sk398 | 1:221d677fe0d3 | 30 | { |
sk398 | 9:46408a8dea0c | 31 | _squareWaveIn = new DigitalIn(squareWaveInPin); |
sk398 | 1:221d677fe0d3 | 32 | } |
sk398 | 1:221d677fe0d3 | 33 | |
sk398 | 4:b85bc0d810e1 | 34 | void Task1::MeasureFrequency() |
sk398 | 4:b85bc0d810e1 | 35 | { |
sk398 | 4:b85bc0d810e1 | 36 | _Task1Timer.start(); |
sk398 | 4:b85bc0d810e1 | 37 | _squareWaveIn -> rise(this,&Task1::StopCounter); |
sk398 | 1:221d677fe0d3 | 38 | } |
sk398 | 1:221d677fe0d3 | 39 | |
sk398 | 4:b85bc0d810e1 | 40 | void Task1::StopCounter() |
sk398 | 4:b85bc0d810e1 | 41 | { |
sk398 | 4:b85bc0d810e1 | 42 | _Task1Timer.stop(); |
sk398 | 4:b85bc0d810e1 | 43 | measuredFrequency = _Task1Timer.read_us(); |
sk398 | 4:b85bc0d810e1 | 44 | _Task1Timer.reset(); |
sk398 | 4:b85bc0d810e1 | 45 | } |
sk398 | 4:b85bc0d810e1 | 46 | |
sk398 | 4:b85bc0d810e1 | 47 | int Task1::ReadFrequency() |
sk398 | 4:b85bc0d810e1 | 48 | { |
sk398 | 4:b85bc0d810e1 | 49 | _squareWaveIn -> rise(this,&Task1::MeasureFrequency); |
sk398 | 4:b85bc0d810e1 | 50 | |
sk398 | 4:b85bc0d810e1 | 51 | return measuredFrequency; |
sk398 | 4:b85bc0d810e1 | 52 | } |
sk398 | 1:221d677fe0d3 | 53 | |
sk398 | 1:221d677fe0d3 | 54 | /* ==================================== Task 2 ==================================== */ |
sk398 | 1:221d677fe0d3 | 55 | Task2::Task2(PinName digitalInCheckPin) |
sk398 | 1:221d677fe0d3 | 56 | { |
sk398 | 3:c611b9bb5770 | 57 | _digitalInCheck = new DigitalIn(digitalInCheckPin); |
sk398 | 1:221d677fe0d3 | 58 | } |
sk398 | 3:c611b9bb5770 | 59 | |
sk398 | 3:c611b9bb5770 | 60 | bool Task2::digitalInState() |
sk398 | 3:c611b9bb5770 | 61 | { |
sk398 | 3:c611b9bb5770 | 62 | if(_digitalInCheck -> read()) |
sk398 | 3:c611b9bb5770 | 63 | { |
sk398 | 3:c611b9bb5770 | 64 | return TRUE; |
sk398 | 3:c611b9bb5770 | 65 | } |
sk398 | 3:c611b9bb5770 | 66 | else |
sk398 | 3:c611b9bb5770 | 67 | { |
sk398 | 3:c611b9bb5770 | 68 | return FALSE; |
sk398 | 3:c611b9bb5770 | 69 | } |
sk398 | 3:c611b9bb5770 | 70 | } |
sk398 | 3:c611b9bb5770 | 71 | |
sk398 | 1:221d677fe0d3 | 72 | |
sk398 | 1:221d677fe0d3 | 73 | /* ==================================== Task 3 ==================================== */ |
sk398 | 1:221d677fe0d3 | 74 | Task3::Task3(PinName WatchdogPin) |
sk398 | 1:221d677fe0d3 | 75 | { |
sk398 | 1:221d677fe0d3 | 76 | _Watchdog = new DigitalOut(WatchdogPin); |
sk398 | 1:221d677fe0d3 | 77 | } |
sk398 | 1:221d677fe0d3 | 78 | |
sk398 | 1:221d677fe0d3 | 79 | void Task3::OutputWatchdogPulse() |
sk398 | 1:221d677fe0d3 | 80 | { |
sk398 | 1:221d677fe0d3 | 81 | _Watchdog -> write(HIGH); |
sk398 | 1:221d677fe0d3 | 82 | wait_ms(WATCHDOG_PULSE_WIDTH); |
sk398 | 1:221d677fe0d3 | 83 | _Watchdog -> write(LOW); |
sk398 | 1:221d677fe0d3 | 84 | } |
sk398 | 1:221d677fe0d3 | 85 | |
sk398 | 1:221d677fe0d3 | 86 | |
sk398 | 3:c611b9bb5770 | 87 | /* ==================================== Task 4 ==================================== */ |
sk398 | 3:c611b9bb5770 | 88 | Task4::Task4(PinName Analog1Pin,PinName Analog2Pin) |
sk398 | 3:c611b9bb5770 | 89 | { |
sk398 | 3:c611b9bb5770 | 90 | _AnalogIn1 = new AnalogIn(Analog1Pin); |
sk398 | 3:c611b9bb5770 | 91 | _AnalogIn2 = new AnalogIn(Analog2Pin); |
sk398 | 3:c611b9bb5770 | 92 | } |
sk398 | 3:c611b9bb5770 | 93 | |
sk398 | 3:c611b9bb5770 | 94 | float *Task4::returnAnalogReadings() |
sk398 | 3:c611b9bb5770 | 95 | { |
sk398 | 3:c611b9bb5770 | 96 | float readBuffer_1 = 0.0; |
sk398 | 3:c611b9bb5770 | 97 | float readBuffer_2 = 0.0; |
sk398 | 3:c611b9bb5770 | 98 | |
sk398 | 3:c611b9bb5770 | 99 | float outputBuffer[2]; |
sk398 | 3:c611b9bb5770 | 100 | |
sk398 | 3:c611b9bb5770 | 101 | // outputBuffer[0] = _AnalogIn1 -> read(); |
sk398 | 3:c611b9bb5770 | 102 | // outputBuffer[1] = _AnalogIn2 -> read(); |
sk398 | 3:c611b9bb5770 | 103 | |
sk398 | 3:c611b9bb5770 | 104 | for(int readCount = 0;readCount < NUM_ANALOG_SAMPLES; readCount++) |
sk398 | 3:c611b9bb5770 | 105 | { |
sk398 | 4:b85bc0d810e1 | 106 | readBuffer_1 += ((_AnalogIn1 -> read())*3.3); |
sk398 | 4:b85bc0d810e1 | 107 | // printf("Buffer 1 %f\r\n",readBuffer_1); |
sk398 | 4:b85bc0d810e1 | 108 | readBuffer_2 += ((_AnalogIn2 -> read())*3.3); |
sk398 | 4:b85bc0d810e1 | 109 | // printf("Buffer 2 %f\r\n",readBuffer_2); |
sk398 | 3:c611b9bb5770 | 110 | } |
sk398 | 3:c611b9bb5770 | 111 | |
sk398 | 3:c611b9bb5770 | 112 | outputBuffer[0] = readBuffer_1/NUM_ANALOG_SAMPLES; |
sk398 | 4:b85bc0d810e1 | 113 | // printf("outputBuffer[0] %f\r\n",outputBuffer[0]); |
sk398 | 4:b85bc0d810e1 | 114 | outputBuffer[1] = readBuffer_2/NUM_ANALOG_SAMPLES; |
sk398 | 4:b85bc0d810e1 | 115 | // printf("outputBuffer[1] %f\r\n",outputBuffer[1]); |
sk398 | 3:c611b9bb5770 | 116 | |
sk398 | 4:b85bc0d810e1 | 117 | float *outputBufferPtr =&outputBuffer[0]; |
sk398 | 4:b85bc0d810e1 | 118 | |
sk398 | 3:c611b9bb5770 | 119 | return outputBufferPtr; |
sk398 | 3:c611b9bb5770 | 120 | } |
sk398 | 4:b85bc0d810e1 | 121 | |
sk398 | 4:b85bc0d810e1 | 122 | /* ==================================== Task 5 ==================================== */ |
sk398 | 5:250f51c80ac1 | 123 | Task5::Task5(PinName sda, PinName scl, int address) |
sk398 | 4:b85bc0d810e1 | 124 | { |
sk398 | 5:250f51c80ac1 | 125 | _par_port = new MCP23017(sda,scl,address); |
sk398 | 5:250f51c80ac1 | 126 | _lcd = new WattBob_TextLCD(_par_port); |
sk398 | 5:250f51c80ac1 | 127 | _par_port -> write_bit(1,BL_BIT); |
sk398 | 4:b85bc0d810e1 | 128 | } |
sk398 | 4:b85bc0d810e1 | 129 | |
sk398 | 6:ceda53939eb8 | 130 | void Task5::updateDisplay( int task1Param, |
sk398 | 6:ceda53939eb8 | 131 | int task2Param, |
sk398 | 6:ceda53939eb8 | 132 | int errorState, |
sk398 | 6:ceda53939eb8 | 133 | float task4Channel1, |
sk398 | 6:ceda53939eb8 | 134 | float task4Channel2 ) |
sk398 | 5:250f51c80ac1 | 135 | { |
sk398 | 5:250f51c80ac1 | 136 | _lcd -> cls(); |
sk398 | 5:250f51c80ac1 | 137 | _lcd -> locate(0,0); |
sk398 | 6:ceda53939eb8 | 138 | _lcd -> printf("F-%4dHz S1-%d E%d",task1Param,task2Param,errorState); |
sk398 | 5:250f51c80ac1 | 139 | _lcd -> locate(1,0); |
sk398 | 5:250f51c80ac1 | 140 | _lcd -> printf("C1-%1.2f C2-%1.2f ",task4Channel1,task4Channel2); |
sk398 | 5:250f51c80ac1 | 141 | } |
sk398 | 6:ceda53939eb8 | 142 | |
sk398 | 6:ceda53939eb8 | 143 | /* ==================================== Task 6 ==================================== */ |
sk398 | 6:ceda53939eb8 | 144 | int Task6::updateErrorCode(int switch_1, float analog1, float analog2) |
sk398 | 6:ceda53939eb8 | 145 | { |
sk398 | 6:ceda53939eb8 | 146 | if(switch_1 == 1 && (analog1 > analog2)) |
sk398 | 6:ceda53939eb8 | 147 | return ERROR_CODE_CDTN_MET; |
sk398 | 6:ceda53939eb8 | 148 | else |
sk398 | 6:ceda53939eb8 | 149 | return ERROR_CODE_CDTN_FAIL; |
sk398 | 6:ceda53939eb8 | 150 | } |
sk398 | 6:ceda53939eb8 | 151 | |
sk398 | 6:ceda53939eb8 | 152 | /* ==================================== Task 5 ==================================== */ |
sk398 | 6:ceda53939eb8 | 153 | Task7::Task7( PinName mosi, |
sk398 | 6:ceda53939eb8 | 154 | PinName miso, |
sk398 | 6:ceda53939eb8 | 155 | PinName sck, |
sk398 | 6:ceda53939eb8 | 156 | PinName cs, |
sk398 | 6:ceda53939eb8 | 157 | const char *SDName, |
sk398 | 6:ceda53939eb8 | 158 | const char *dir ) |
sk398 | 6:ceda53939eb8 | 159 | { |
sk398 | 6:ceda53939eb8 | 160 | _sd = new SDFileSystem(mosi,miso,sck,cs, SDName); |
sk398 | 6:ceda53939eb8 | 161 | makeDirectory(dir); |
sk398 | 6:ceda53939eb8 | 162 | } |
sk398 | 6:ceda53939eb8 | 163 | |
sk398 | 6:ceda53939eb8 | 164 | void Task7::makeDirectory(const char *dir) |
sk398 | 6:ceda53939eb8 | 165 | { |
sk398 | 6:ceda53939eb8 | 166 | mkdir(dir,0777); |
sk398 | 6:ceda53939eb8 | 167 | } |
sk398 | 6:ceda53939eb8 | 168 | |
sk398 | 6:ceda53939eb8 | 169 | int Task7::openFile(const char *dirFile,const char *accessType) |
sk398 | 6:ceda53939eb8 | 170 | { |
sk398 | 6:ceda53939eb8 | 171 | fp = fopen(dirFile,accessType); |
sk398 | 6:ceda53939eb8 | 172 | if(fp == NULL) |
sk398 | 6:ceda53939eb8 | 173 | { |
sk398 | 6:ceda53939eb8 | 174 | return 1; |
sk398 | 6:ceda53939eb8 | 175 | } |
sk398 | 6:ceda53939eb8 | 176 | return 0; |
sk398 | 6:ceda53939eb8 | 177 | } |
sk398 | 6:ceda53939eb8 | 178 | |
sk398 | 6:ceda53939eb8 | 179 | void Task7::writeData(const char *dataStream) |
sk398 | 6:ceda53939eb8 | 180 | { |
sk398 | 6:ceda53939eb8 | 181 | fprintf(fp,dataStream); |
sk398 | 6:ceda53939eb8 | 182 | } |
sk398 | 6:ceda53939eb8 | 183 | |
sk398 | 6:ceda53939eb8 | 184 | void Task7::closeFile() |
sk398 | 6:ceda53939eb8 | 185 | { |
sk398 | 6:ceda53939eb8 | 186 | fclose(fp); |
sk398 | 6:ceda53939eb8 | 187 | } |