Test session
Dependencies: FatFileSystem MCP23017 WattBob_TextLCD mbed
Fork of Assignment_2_herpe by
check_task.cpp@4:48761259552a, 2017-03-14 (annotated)
- Committer:
- xouf2114
- Date:
- Tue Mar 14 14:46:43 2017 +0000
- Revision:
- 4:48761259552a
Test of Assignment 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xouf2114 | 4:48761259552a | 1 | /** |
xouf2114 | 4:48761259552a | 2 | * The following task checks the digital inputs on pin11 and pin12 |
xouf2114 | 4:48761259552a | 3 | * it also checks for "errors" and save the error code to the state. |
xouf2114 | 4:48761259552a | 4 | * If pin12 is 1, it schedules the counting task. |
xouf2114 | 4:48761259552a | 5 | * |
xouf2114 | 4:48761259552a | 6 | * Author: Jacob Baungard Hansen |
xouf2114 | 4:48761259552a | 7 | */ |
xouf2114 | 4:48761259552a | 8 | |
xouf2114 | 4:48761259552a | 9 | #include "check_task.h" |
xouf2114 | 4:48761259552a | 10 | |
xouf2114 | 4:48761259552a | 11 | /** |
xouf2114 | 4:48761259552a | 12 | * |
xouf2114 | 4:48761259552a | 13 | * @param starting_offset when the task should but run first |
xouf2114 | 4:48761259552a | 14 | * @param frequency_ms the frequency in ms of how often the task should run |
xouf2114 | 4:48761259552a | 15 | */ |
xouf2114 | 4:48761259552a | 16 | CheckTask::CheckTask(int starting_offset, int frequency_ms, |
xouf2114 | 4:48761259552a | 17 | State * state, TaskManager * tm) |
xouf2114 | 4:48761259552a | 18 | : Task(starting_offset, frequency_ms) { |
xouf2114 | 4:48761259552a | 19 | |
xouf2114 | 4:48761259552a | 20 | this->state = state; |
xouf2114 | 4:48761259552a | 21 | this->tm = tm; |
xouf2114 | 4:48761259552a | 22 | this->counting_task = new CountingTask(0, 1800); |
xouf2114 | 4:48761259552a | 23 | this->counting_scheduled = 0; |
xouf2114 | 4:48761259552a | 24 | this->digital_in_1 = new DigitalIn(p11); |
xouf2114 | 4:48761259552a | 25 | this->digital_in_2 = new DigitalIn(p12); |
xouf2114 | 4:48761259552a | 26 | |
xouf2114 | 4:48761259552a | 27 | } |
xouf2114 | 4:48761259552a | 28 | |
xouf2114 | 4:48761259552a | 29 | /* |
xouf2114 | 4:48761259552a | 30 | * Simple destructor |
xouf2114 | 4:48761259552a | 31 | */ |
xouf2114 | 4:48761259552a | 32 | CheckTask::~CheckTask() { |
xouf2114 | 4:48761259552a | 33 | delete this->counting_task; |
xouf2114 | 4:48761259552a | 34 | delete this->digital_in_1; |
xouf2114 | 4:48761259552a | 35 | delete this->digital_in_2; |
xouf2114 | 4:48761259552a | 36 | } |
xouf2114 | 4:48761259552a | 37 | |
xouf2114 | 4:48761259552a | 38 | /** |
xouf2114 | 4:48761259552a | 39 | * The function that is run at the specified frequency. |
xouf2114 | 4:48761259552a | 40 | */ |
xouf2114 | 4:48761259552a | 41 | void CheckTask::action() { |
xouf2114 | 4:48761259552a | 42 | |
xouf2114 | 4:48761259552a | 43 | // get the digital values |
xouf2114 | 4:48761259552a | 44 | int switch_1 = digital_in_1->read(); |
xouf2114 | 4:48761259552a | 45 | int switch_2 = digital_in_2->read(); |
xouf2114 | 4:48761259552a | 46 | |
xouf2114 | 4:48761259552a | 47 | // save to the state |
xouf2114 | 4:48761259552a | 48 | this->state->set_digital_1(switch_1); |
xouf2114 | 4:48761259552a | 49 | this->state->set_digital_2(switch_2); |
xouf2114 | 4:48761259552a | 50 | |
xouf2114 | 4:48761259552a | 51 | // check possible error codes |
xouf2114 | 4:48761259552a | 52 | if (switch_1 == 1 && (this->state->get_avg_analog_1() > this->state->get_avg_analog_2() ) ) { |
xouf2114 | 4:48761259552a | 53 | this->state->set_error(3); |
xouf2114 | 4:48761259552a | 54 | } else { |
xouf2114 | 4:48761259552a | 55 | this->state->set_error(0); |
xouf2114 | 4:48761259552a | 56 | } |
xouf2114 | 4:48761259552a | 57 | |
xouf2114 | 4:48761259552a | 58 | // schedule or deschedule counting task as appropriate |
xouf2114 | 4:48761259552a | 59 | if (switch_2 == 1 && this->counting_scheduled == 0) { |
xouf2114 | 4:48761259552a | 60 | // schedule binary LED task |
xouf2114 | 4:48761259552a | 61 | this->counting_scheduled = 1; |
xouf2114 | 4:48761259552a | 62 | // set the binary LED task next run |
xouf2114 | 4:48761259552a | 63 | int next_run = this->get_next_run_ms()+300; |
xouf2114 | 4:48761259552a | 64 | this->counting_task->set_next_run_ms(next_run); |
xouf2114 | 4:48761259552a | 65 | // add task to task manager |
xouf2114 | 4:48761259552a | 66 | this->tm->add_task(this->counting_task); |
xouf2114 | 4:48761259552a | 67 | } else if (switch_2 == 0) { |
xouf2114 | 4:48761259552a | 68 | // delete task from task manager |
xouf2114 | 4:48761259552a | 69 | this->tm->remove_task(this->counting_task); |
xouf2114 | 4:48761259552a | 70 | this->counting_scheduled = 0; |
xouf2114 | 4:48761259552a | 71 | } |
xouf2114 | 4:48761259552a | 72 | |
xouf2114 | 4:48761259552a | 73 | } |