Benoît GEISLER
/
ActivateOutput
Activation of outputs using serial and user button on ticker interrupt
Engine.cpp@6:63c1ee4fb6be, 2014-06-05 (annotated)
- Committer:
- ggmero67
- Date:
- Thu Jun 05 18:41:34 2014 +0000
- Revision:
- 6:63c1ee4fb6be
- Parent:
- 5:f5e9a37d4eb0
Rename SerialPort into PC_Com + find bug in write function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ggmero67 | 1:f23460f7574d | 1 | #include "mbed.h" |
ggmero67 | 1:f23460f7574d | 2 | #include <map> |
ggmero67 | 1:f23460f7574d | 3 | #include "Engine.h" |
ggmero67 | 1:f23460f7574d | 4 | |
ggmero67 | 6:63c1ee4fb6be | 5 | #define TEMPO_DEF 10.0 |
ggmero67 | 1:f23460f7574d | 6 | |
ggmero67 | 6:63c1ee4fb6be | 7 | |
ggmero67 | 6:63c1ee4fb6be | 8 | //DigitalOut myled(LED1); |
ggmero67 | 2:63a88ccbf0b9 | 9 | |
ggmero67 | 2:63a88ccbf0b9 | 10 | |
ggmero67 | 6:63c1ee4fb6be | 11 | Engine::Engine(e_modeOfUsing mode) |
ggmero67 | 1:f23460f7574d | 12 | { |
ggmero67 | 1:f23460f7574d | 13 | modeOfUsing = mode; |
ggmero67 | 4:038c13df0c2c | 14 | tempo = ConvertTempo(TEMPO_DEF); |
ggmero67 | 6:63c1ee4fb6be | 15 | |
ggmero67 | 6:63c1ee4fb6be | 16 | notes[Do_0] = Note(LED1); |
ggmero67 | 6:63c1ee4fb6be | 17 | notes[Re_0] = Note(); |
ggmero67 | 6:63c1ee4fb6be | 18 | notes[Mi_0] = Note(); |
ggmero67 | 6:63c1ee4fb6be | 19 | notes[Fa_0] = Note(); |
ggmero67 | 6:63c1ee4fb6be | 20 | notes[Sol_0] = Note(); |
ggmero67 | 4:038c13df0c2c | 21 | |
ggmero67 | 1:f23460f7574d | 22 | } |
ggmero67 | 1:f23460f7574d | 23 | Engine::~Engine() |
ggmero67 | 1:f23460f7574d | 24 | { |
ggmero67 | 6:63c1ee4fb6be | 25 | |
ggmero67 | 1:f23460f7574d | 26 | } |
ggmero67 | 1:f23460f7574d | 27 | |
ggmero67 | 4:038c13df0c2c | 28 | /**Starts the engine*/ |
ggmero67 | 4:038c13df0c2c | 29 | void Engine::start(void) |
ggmero67 | 4:038c13df0c2c | 30 | { |
ggmero67 | 4:038c13df0c2c | 31 | if(modeOfUsing == ON_TICK) { |
ggmero67 | 4:038c13df0c2c | 32 | tick.attach(this, &Engine::applyChanges, tempo); |
ggmero67 | 4:038c13df0c2c | 33 | } |
ggmero67 | 4:038c13df0c2c | 34 | } |
ggmero67 | 4:038c13df0c2c | 35 | /**Changes the Tempo*/ |
ggmero67 | 4:038c13df0c2c | 36 | void Engine::changeTempo(int newTempo) |
ggmero67 | 4:038c13df0c2c | 37 | { |
ggmero67 | 4:038c13df0c2c | 38 | if(modeOfUsing == ON_TICK) { |
ggmero67 | 4:038c13df0c2c | 39 | this->stop(); |
ggmero67 | 4:038c13df0c2c | 40 | tempo = ConvertTempo(newTempo); |
ggmero67 | 4:038c13df0c2c | 41 | } |
ggmero67 | 4:038c13df0c2c | 42 | } |
ggmero67 | 4:038c13df0c2c | 43 | /**Stops the engine*/ |
ggmero67 | 4:038c13df0c2c | 44 | void Engine::stop(void) |
ggmero67 | 4:038c13df0c2c | 45 | { |
ggmero67 | 4:038c13df0c2c | 46 | if(modeOfUsing == ON_TICK) { |
ggmero67 | 4:038c13df0c2c | 47 | tick.detach(); |
ggmero67 | 4:038c13df0c2c | 48 | } |
ggmero67 | 4:038c13df0c2c | 49 | } |
ggmero67 | 4:038c13df0c2c | 50 | |
ggmero67 | 2:63a88ccbf0b9 | 51 | void Engine::applyChanges() |
ggmero67 | 1:f23460f7574d | 52 | { |
ggmero67 | 4:038c13df0c2c | 53 | //show tempo on LED1 |
ggmero67 | 6:63c1ee4fb6be | 54 | //myled = !myled; |
ggmero67 | 1:f23460f7574d | 55 | |
ggmero67 | 6:63c1ee4fb6be | 56 | for(int i = 0; i < NB_NOTES; i++) { |
ggmero67 | 6:63c1ee4fb6be | 57 | if(notes[i].getCurrentState() != notes[i].getDesiredState()) { |
ggmero67 | 6:63c1ee4fb6be | 58 | notes[i].applyDesiredState(); |
ggmero67 | 6:63c1ee4fb6be | 59 | } |
ggmero67 | 1:f23460f7574d | 60 | } |
ggmero67 | 1:f23460f7574d | 61 | |
ggmero67 | 1:f23460f7574d | 62 | } |
ggmero67 | 1:f23460f7574d | 63 | |
ggmero67 | 1:f23460f7574d | 64 | bool Engine::startNote(e_NoteName noteToStart) |
ggmero67 | 1:f23460f7574d | 65 | { |
ggmero67 | 6:63c1ee4fb6be | 66 | //try to find the desired note in the map |
ggmero67 | 6:63c1ee4fb6be | 67 | if(noteToStart < NB_NOTES) { |
ggmero67 | 1:f23460f7574d | 68 | notes[noteToStart].setDesiredState(NOTE_ON); |
ggmero67 | 1:f23460f7574d | 69 | |
ggmero67 | 1:f23460f7574d | 70 | if(modeOfUsing == ON_COMMAND) { |
ggmero67 | 1:f23460f7574d | 71 | notes[noteToStart].applyDesiredState(); |
ggmero67 | 1:f23460f7574d | 72 | } |
ggmero67 | 1:f23460f7574d | 73 | |
ggmero67 | 1:f23460f7574d | 74 | return true; |
ggmero67 | 1:f23460f7574d | 75 | } else { |
ggmero67 | 6:63c1ee4fb6be | 76 | //note not found; |
ggmero67 | 1:f23460f7574d | 77 | return false; |
ggmero67 | 1:f23460f7574d | 78 | } |
ggmero67 | 1:f23460f7574d | 79 | } |
ggmero67 | 1:f23460f7574d | 80 | |
ggmero67 | 1:f23460f7574d | 81 | |
ggmero67 | 1:f23460f7574d | 82 | bool Engine::stopNote(e_NoteName noteToStop) |
ggmero67 | 1:f23460f7574d | 83 | { |
ggmero67 | 2:63a88ccbf0b9 | 84 | |
ggmero67 | 6:63c1ee4fb6be | 85 | if(noteToStop < NB_NOTES) { |
ggmero67 | 1:f23460f7574d | 86 | notes[noteToStop].setDesiredState(NOTE_OFF); |
ggmero67 | 1:f23460f7574d | 87 | if(modeOfUsing == ON_COMMAND) { |
ggmero67 | 1:f23460f7574d | 88 | notes[noteToStop].applyDesiredState(); |
ggmero67 | 1:f23460f7574d | 89 | } |
ggmero67 | 1:f23460f7574d | 90 | return true; |
ggmero67 | 1:f23460f7574d | 91 | } else { |
ggmero67 | 6:63c1ee4fb6be | 92 | //note not found |
ggmero67 | 1:f23460f7574d | 93 | return false; |
ggmero67 | 1:f23460f7574d | 94 | } |
ggmero67 | 2:63a88ccbf0b9 | 95 | } |
ggmero67 | 2:63a88ccbf0b9 | 96 | |
ggmero67 | 2:63a88ccbf0b9 | 97 | float Engine::ConvertTempo(int tempo) |
ggmero67 | 2:63a88ccbf0b9 | 98 | { |
ggmero67 | 4:038c13df0c2c | 99 | return (float)60.0/(float)tempo; |
ggmero67 | 6:63c1ee4fb6be | 100 | } |