Xavier Gouesnard / Mbed 2 deprecated Assignment_2_XG

Dependencies:   FatFileSystem MCP23017 WattBob_TextLCD mbed

Fork of Assignment_2_herpe by Xavier Herpe

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers check_task.cpp Source File

check_task.cpp

00001 /**
00002  * The following task checks the digital inputs on pin11 and pin12
00003  * it also checks for "errors" and save the error code to the state.
00004  * If pin12 is 1, it schedules the counting task.
00005  *
00006  * Author: Jacob Baungard Hansen
00007  */
00008 
00009 #include "check_task.h"
00010 
00011 /**
00012  *
00013  * @param starting_offset       when the task should but run first
00014  * @param frequency_ms          the frequency in ms of how often the task should run
00015  */
00016 CheckTask::CheckTask(int starting_offset, int frequency_ms,
00017                                                                      State * state, TaskManager * tm)
00018                                     : Task(starting_offset, frequency_ms) {
00019                                         
00020     this->state = state;
00021     this->tm = tm;
00022     this->counting_task = new CountingTask(0, 1800);
00023     this->counting_scheduled = 0;
00024     this->digital_in_1 = new DigitalIn(p11);
00025     this->digital_in_2 = new DigitalIn(p12);
00026     
00027 }
00028 
00029 /*
00030  * Simple destructor
00031  */
00032 CheckTask::~CheckTask() {
00033     delete this->counting_task;
00034     delete this->digital_in_1;
00035     delete this->digital_in_2;
00036 }
00037 
00038 /** 
00039  * The function that is run at the specified frequency.
00040  */
00041 void CheckTask::action() {
00042     
00043     // get the digital values
00044     int switch_1 = digital_in_1->read();
00045     int switch_2 = digital_in_2->read();
00046     
00047     // save to the state
00048     this->state->set_digital_1(switch_1);
00049     this->state->set_digital_2(switch_2);
00050 
00051     // check possible error codes
00052     if (switch_1 == 1 && (this->state->get_avg_analog_1() > this->state->get_avg_analog_2() ) ) {
00053         this->state->set_error(3);
00054     } else {
00055         this->state->set_error(0);
00056     }
00057     
00058     // schedule or deschedule counting task as appropriate
00059     if (switch_2 == 1 && this->counting_scheduled == 0) {
00060         // schedule binary LED task
00061         this->counting_scheduled = 1;
00062         // set the binary LED task next run
00063         int next_run = this->get_next_run_ms()+300;
00064         this->counting_task->set_next_run_ms(next_run);
00065         // add task to task manager
00066         this->tm->add_task(this->counting_task);
00067     } else if (switch_2 == 0) {
00068         // delete task from task manager
00069         this->tm->remove_task(this->counting_task);
00070         this->counting_scheduled = 0;
00071     }
00072 
00073 }