/***************************************************************************** source::worx libPiGPio Copyright © 2022 c.holzheuer chris@sourceworx.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Uses: pigpiod_if2 by joan2937, pigpio @ abyz.me.uk https://abyz.me.uk/rpi/pigpio sigslot by 2017 Pierre-Antoine Lacaze https://github.com/palacaze/sigslot ***************************************************************************/ #include #include #include #include pigTimer::pigTimer() { } pigTimer::pigTimer( int timeOn, int timeOff, int repcnt ) : _timeOn{ timeOn }, _timeOff{ timeOff }, _repcnt{ repcnt } { if( !_timeOff ) _timeOff = _timeOn; } pigTimer::~pigTimer() { } void pigTimer::change_state( bool newState ) { set_state( newState ); } int pigTimer::timeOn() { return _timeOn; } void pigTimer::setTimeOn( int timeOn ) { _timeOn = timeOn; // reset?? } int pigTimer::timeOff() { return _timeOff; } void pigTimer::setTimeOff( int timeOff ) { _timeOff = timeOff; } int pigTimer::repetitions() { return _repcnt; } void pigTimer::setRepetitions( int repcnt ) { _repcnt = repcnt; } void pigTimer::start() { _active = true; // ewig if( _repcnt < 1 ) { while( _active ) sleepCycle(); return; } // abgezählt for( int i=0; i<_repcnt; ++i ) sleepCycle(); } void pigTimer::stop() { _active = false; } void pigTimer::sleepCycle() { sleepThread( _timeOff ); if( !_active ) return; sleepThread( _timeOn ); } void pigTimer::sleepThread( int delay ) { std::thread thrd( [=]() { if( !_active ) return; std::this_thread::sleep_for( std::chrono::milliseconds( delay ) ); toggle_state(); } ); thrd.detach(); }