Johannes Otto
/
simple_stm_ignition
programmable ignition for an mz ts 150. The interruptor needs to be replaced by an hall effect sensor
Diff: main.cpp
- Revision:
- 2:469b3daa6b98
- Parent:
- 1:f99510926784
- Child:
- 3:5efd78be4662
--- a/main.cpp Tue Oct 13 15:14:02 2015 +0000 +++ b/main.cpp Tue Oct 13 16:41:04 2015 +0000 @@ -1,56 +1,54 @@ #include "mbed.h" InterruptIn interruptor(D2); -Timer t; -DigitalOut ignition(D3); -Timeout ignition_timer; -int round_time; float offset=34.14f; float def_ignition=26.13f; -int ign_delay; -int ignition_length=100; -int ignition_delay; - -int get_delay(int round_time) -{ - return round_time*(offset-def_ignition)/360; -} - -void ignition_off() -{ - ignition=0; -} - -void ignition_on() +class IgnMod { - ignition=1; - ignition_timer.attach_us(&ignition_off,ignition_length); -} +private: + Timeout ignition_timer; + Timer t; + + DigitalOut *pin; + + int ignition_length; + int round_time; + int ignition_delay; + + void on() { + pin->write(1); + ignition_timer.attach_us(this,&IgnMod::off,ignition_length); + } + void off() { + pin->write(0); + } -void ignite() -{ - round_time=t.read_us(); - t.reset(); - ignition_delay=get_delay(round_time); - ignition_timer.attach_us(&ignition_on,ignition_delay-t.read_us()); -} + int get_delay(int round_time) { + return round_time*(offset-def_ignition)/360; + } +public: + IgnMod() { + ignition_length=100; + DigitalOut pin(D3); + t.start(); + } + void ignite() { + round_time=t.read_us(); + t.reset(); + ignition_delay=get_delay(round_time); + ignition_timer.attach_us(this,&IgnMod::on,ignition_delay-t.read_us()); + } +}; - -void setup() -{ - interruptor.mode(PullUp); - t.reset(); - t.start(); -} - +IgnMod ignition; int main() { - setup(); - while(1) { - interruptor.rise(&ignite); + interruptor.mode(PullUp); + interruptor.rise(&ignition,&IgnMod::ignite); + while(1) { } } \ No newline at end of file