![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test session
Dependencies: FatFileSystem MCP23017 WattBob_TextLCD mbed
Fork of Assignment_2_herpe by
freq_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 | * Task for getting the frequency from digital input on pin13. |
xouf2114 | 4:48761259552a | 3 | * The frequency is calculated by counting the number of rises |
xouf2114 | 4:48761259552a | 4 | * on the pin. This is done using an interupt handler. |
xouf2114 | 4:48761259552a | 5 | * |
xouf2114 | 4:48761259552a | 6 | * Author: Jacob Baungard Hansen |
xouf2114 | 4:48761259552a | 7 | */ |
xouf2114 | 4:48761259552a | 8 | #include "freq_task.h" |
xouf2114 | 4:48761259552a | 9 | |
xouf2114 | 4:48761259552a | 10 | FrequencyTask::FrequencyTask(int starting_offset, int frequency_ms, |
xouf2114 | 4:48761259552a | 11 | State * state) |
xouf2114 | 4:48761259552a | 12 | : Task(starting_offset, frequency_ms) { |
xouf2114 | 4:48761259552a | 13 | |
xouf2114 | 4:48761259552a | 14 | this->state = state; |
xouf2114 | 4:48761259552a | 15 | this->count = 0; |
xouf2114 | 4:48761259552a | 16 | this->interrupt = new InterruptIn(p13); |
xouf2114 | 4:48761259552a | 17 | |
xouf2114 | 4:48761259552a | 18 | } |
xouf2114 | 4:48761259552a | 19 | |
xouf2114 | 4:48761259552a | 20 | FrequencyTask::~FrequencyTask() { |
xouf2114 | 4:48761259552a | 21 | delete this->interrupt; |
xouf2114 | 4:48761259552a | 22 | } |
xouf2114 | 4:48761259552a | 23 | |
xouf2114 | 4:48761259552a | 24 | /** |
xouf2114 | 4:48761259552a | 25 | * This is called by the interupt, simply adds one to the count |
xouf2114 | 4:48761259552a | 26 | */ |
xouf2114 | 4:48761259552a | 27 | void FrequencyTask::counter() { |
xouf2114 | 4:48761259552a | 28 | this->count++; |
xouf2114 | 4:48761259552a | 29 | } |
xouf2114 | 4:48761259552a | 30 | |
xouf2114 | 4:48761259552a | 31 | void FrequencyTask::action() { |
xouf2114 | 4:48761259552a | 32 | // reset the count |
xouf2114 | 4:48761259552a | 33 | this->count=0; |
xouf2114 | 4:48761259552a | 34 | // Enable the iterrupt |
xouf2114 | 4:48761259552a | 35 | this->interrupt->rise<FrequencyTask>(this, &FrequencyTask::counter); |
xouf2114 | 4:48761259552a | 36 | // wait for a specied amout of time |
xouf2114 | 4:48761259552a | 37 | wait_ms(SAMPLE_LENGTH_MS); |
xouf2114 | 4:48761259552a | 38 | // disable interrupts |
xouf2114 | 4:48761259552a | 39 | this->interrupt->rise(NULL); |
xouf2114 | 4:48761259552a | 40 | //interupt.rise<FrequencyTask>(this, NULL); This crashes, should report bug to ARM |
xouf2114 | 4:48761259552a | 41 | int freq = 0; |
xouf2114 | 4:48761259552a | 42 | if (this->count > 0) { |
xouf2114 | 4:48761259552a | 43 | freq = count/(SAMPLE_LENGTH_MS*0.001); |
xouf2114 | 4:48761259552a | 44 | } |
xouf2114 | 4:48761259552a | 45 | |
xouf2114 | 4:48761259552a | 46 | this->state->set_freq(freq); |
xouf2114 | 4:48761259552a | 47 | } |