64
63
unsigned long millis = ::millis();
65
// get state and check for change
67
66
bool state = digitalRead( _pin )? false : true;
69
// if the button is pressed
72
// if it has just been pressed, record the time now and reset how much
73
// of the press we have already generated events for
75
_millis_state = millis;
69
// if we're not sending interim presses, we care about when the button
71
if( !_send_interim && !state )
73
// are we being told to ignore the next unpress?
74
if( _ignore_next_unpress )
75
_ignore_next_unpress = false;
78
// check through events to see if the button was pressed long
79
// enough to trigger one
80
for( int a = 0; _event_times[ a ]; a++ )
82
// if this event is in the future, we can stop looking
83
if( _state_millis < (unsigned long)_event_times[ a ] )
87
_pending_event = a + 1;
79
// calculate time that has elapsed in total this press
80
unsigned long elapsed = millis - _millis_state;
82
// look through the events to see if any need to be triggered
94
_state_millis = millis;
95
_state_duration_done = 0;
98
// if we are sending interim presses, we care about all the time that the
99
// button is being pressed
100
if( _send_interim && state )
102
// calculate new duration for this state
103
unsigned long duration = millis - _state_millis;
105
// check through events to see if the button has been pressed long
106
// enough to trigger one
83
107
for( int a = 0; _event_times[ a ]; a++ )
85
// if this event is in the future, stop
86
if( (unsigned)_event_times[ a ] > elapsed ) break;
88
// if this event has already been triggered, skip
89
if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
109
// if this event is in the future, we can stop looking
110
if( duration < (unsigned long)_event_times[ a ] )
113
// if this event happened since the previous update, trigger it
114
if( _state_duration_done < (unsigned long)_event_times[ a ] )
93
115
_pending_event = a + 1;
95
_interim_event = a + 1;
98
// update the amount of press we have processed
99
_millis_done = elapsed;
102
// if the button has just become unpressed and we're not sending interim
103
// events, we'll need to trigger it now
104
if( !state && _state_last && !_send_interim )
106
// unless we're being told not to, trigger any interim events
107
if( _ignore_next_unpress )
108
_ignore_next_unpress = false;
110
_pending_event = _interim_event;
112
// clear any interim event
118
// update the duration we've accounted for
119
_state_duration_done = duration;