Skip to content

Commit c7669a0

Browse files
committed
New function added: readInProvidedSteps() function have been added, check the docs for more info
1 parent ac92178 commit c7669a0

File tree

6 files changed

+108
-1
lines changed

6 files changed

+108
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Click on **Download ZIP** to download the library, place the unzipped folder int
2424

2525
## Change LOG
2626

27+
### v2.0.3 (10th March, 2022)
28+
29+
- Added ```readInProvidedSteps()``` to **MyButton**, to achieve similar behavior to the ```readMultiple()``` but continiously getting the reached step, not only on the falling edge.
30+
31+
### v2.0.2 (9th March, 2022)
32+
33+
- Added ```readRawClick()``` to **MyButton**, to get the non-debounced raw state (click) of the button.
34+
2735
### v2.0.1 (26th January, 2022)
2836

2937
- Created **MyCountingButton** library, didn't test it thoroughly though, just made sure that all functionalities work as intended.

docs/pages/MyButton.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ The ``NORMAL_UP``, and ``NORMAL_DOWN`` keywords refer to whether the push button
5454

5555
+ This function takes in a ``period`` in **milliseconds** and a ``number of steps``, and on each step of that period ``step == period/num_steps``, returns the ``index`` of the step, **0 INDEXED**, else returns NON_CLICKED (==255).
5656

57+
- ``uint8_t readInProvidedSteps(uint32_t * periods, uint8_t num_steps);``
58+
59+
+ This function takes in an **incrementally sorted list of ``periods`` in milliseconds**, and reports the ``index`` of the reached period in the list, else it returns NON_CLICKED (==255). **ZERO-INDEXED**
60+
5761
- ``uint8_t readMultiple(uint32_t * periods, uint8_t len);``
5862

5963
+ This function takes in an **incrementally sorted list of ``periods`` in milliseconds**, and if the button have been pressed for more than one of the periods (CHECKED ON RELEASE), it'd return the ``index`` of the period in the list, else it returns NON_CLICKED (==255). **ZERO-INDEXED**

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readRisingClick KEYWORD2
66
readFallingClick KEYWORD2
77
readTimedPress KEYWORD2
88
readInSteps KEYWORD2
9+
readInProvidedSteps KEYWORD2
910
readMultiple KEYWORD2
1011
beginCountingInterrupter KEYWORD2
1112
countingInterruption KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MyButton
2-
version=2.0.2
2+
version=2.0.3
33
author=Radhi
44
maintainer=Radhi <[email protected]>
55
sentence=Making buttons easy and fun to work with (normal, and counting buttons)

src/MyButton.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,98 @@ uint8_t MyButton :: readInSteps(uint32_t period, uint8_t num_steps){
204204
return NON_CLICKED;
205205
}
206206

207+
/*
208+
* Returns, as long as the button is pressed, the index of the reached
209+
* period (provided list of incrementally sorted periods), aka the step
210+
* ------------------------------------------------------------------
211+
* @brief: This function takes in a list of INCREMENTALLY SORTED
212+
* periods in Ms, and upon reaching a period of that list, returns
213+
* the index of the step, 0 INDEXED, else returns 255.
214+
* ------------------------------------------------------------------
215+
* @param periods: The list of periods to be checked
216+
* @param num_steps: The number of periods
217+
*/
218+
uint8_t MyButton :: readInProvidedSteps(uint32_t * periods, uint8_t num_steps){
219+
/* Obviously */
220+
if(num_steps == 0) return NON_CLICKED;
221+
222+
switch((flag & BTN_STATE_STEP_BITMASK) >> 4){
223+
case READ_BTN:
224+
step = 0;
225+
if(digitalRead(button_pin) == (1 - off_state)){
226+
time_since_clicked = millis();
227+
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (1 << 4);
228+
}
229+
break;
230+
231+
case WAIT_BTN:
232+
if(digitalRead(button_pin) == off_state){
233+
flag &= ~BTN_STATE_STEP_BITMASK;
234+
}
235+
else if(millis() - time_since_clicked >= debounce_time){
236+
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (2 << 4); // We have a true click
237+
}
238+
break;
239+
240+
case TRUE_CLICK:
241+
if(digitalRead(button_pin) == off_state){
242+
flag &= ~BTN_STATE_STEP_BITMASK;
243+
if(step < num_steps - 1) return ABORTED_STEPS;
244+
}
245+
else if(step < num_steps && millis() - time_since_clicked >= periods[step]){
246+
return step++;
247+
}
248+
break;
249+
}
250+
return NON_CLICKED;
251+
}
252+
253+
/*
254+
* Returns, as long as the button is pressed, the index of the reached
255+
* period (provided list of incrementally sorted periods), aka the step
256+
* ------------------------------------------------------------------
257+
* @brief: This function takes in a list of INCREMENTALLY SORTED
258+
* periods in Ms, and upon reaching a period of that list, returns
259+
* the index of the step, 0 INDEXED, else returns 255.
260+
* ------------------------------------------------------------------
261+
* @param periods: The list of periods to be checked
262+
* @param num_steps: The number of periods
263+
*/
264+
uint8_t MyButton :: readInProvidedSteps(uint32_t * periods, uint8_t num_steps, uint8_t starting_step){
265+
/* Obviously */
266+
if(num_steps == 0) return NON_CLICKED;
267+
268+
switch((flag & BTN_STATE_STEP_BITMASK) >> 4){
269+
case READ_BTN:
270+
step = starting_step;
271+
if(digitalRead(button_pin) == (1 - off_state)){
272+
time_since_clicked = millis();
273+
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (1 << 4);
274+
}
275+
break;
276+
277+
case WAIT_BTN:
278+
if(digitalRead(button_pin) == off_state){
279+
flag &= ~BTN_STATE_STEP_BITMASK;
280+
}
281+
else if(millis() - time_since_clicked >= debounce_time){
282+
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (2 << 4); // We have a true click
283+
}
284+
break;
285+
286+
case TRUE_CLICK:
287+
if(digitalRead(button_pin) == off_state){
288+
flag &= ~BTN_STATE_STEP_BITMASK;
289+
if(step < num_steps - 1) return ABORTED_STEPS;
290+
}
291+
else if(step < num_steps && millis() - time_since_clicked >= periods[step]){
292+
return step++;
293+
}
294+
break;
295+
}
296+
return NON_CLICKED;
297+
}
298+
207299
/*
208300
* Returns, if the button have been pressed for longer than one of the
209301
* periods in the input list, the index of the corresponding period

src/MyButton.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
bool readFallingClick();
8484
uint32_t readTimedPress(uint8_t unit);
8585
uint8_t readInSteps(uint32_t period, uint8_t num_steps);
86+
uint8_t readInProvidedSteps(uint32_t * periods, uint8_t num_steps);
87+
uint8_t readInProvidedSteps(uint32_t * periods, uint8_t num_steps, uint8_t starting_step);
8688
uint8_t readMultiple(uint32_t * periods, uint8_t len);
8789
};
8890

0 commit comments

Comments
 (0)