Skip to content

Commit

Permalink
New function added: readInProvidedSteps() function have been added, c…
Browse files Browse the repository at this point in the history
…heck the docs for more info
  • Loading branch information
Rad-hi committed Mar 10, 2022
1 parent ac92178 commit c7669a0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Click on **Download ZIP** to download the library, place the unzipped folder int

## Change LOG

### v2.0.3 (10th March, 2022)

- Added ```readInProvidedSteps()``` to **MyButton**, to achieve similar behavior to the ```readMultiple()``` but continiously getting the reached step, not only on the falling edge.

### v2.0.2 (9th March, 2022)

- Added ```readRawClick()``` to **MyButton**, to get the non-debounced raw state (click) of the button.

### v2.0.1 (26th January, 2022)

- Created **MyCountingButton** library, didn't test it thoroughly though, just made sure that all functionalities work as intended.
Expand Down
4 changes: 4 additions & 0 deletions docs/pages/MyButton.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ The ``NORMAL_UP``, and ``NORMAL_DOWN`` keywords refer to whether the push button

+ 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).

- ``uint8_t readInProvidedSteps(uint32_t * periods, uint8_t num_steps);``

+ 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**

- ``uint8_t readMultiple(uint32_t * periods, uint8_t len);``

+ 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**
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ readRisingClick KEYWORD2
readFallingClick KEYWORD2
readTimedPress KEYWORD2
readInSteps KEYWORD2
readInProvidedSteps KEYWORD2
readMultiple KEYWORD2
beginCountingInterrupter KEYWORD2
countingInterruption KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MyButton
version=2.0.2
version=2.0.3
author=Radhi
maintainer=Radhi <radhisghaier33@gmail.com>
sentence=Making buttons easy and fun to work with (normal, and counting buttons)
Expand Down
92 changes: 92 additions & 0 deletions src/MyButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,98 @@ uint8_t MyButton :: readInSteps(uint32_t period, uint8_t num_steps){
return NON_CLICKED;
}

/*
* Returns, as long as the button is pressed, the index of the reached
* period (provided list of incrementally sorted periods), aka the step
* ------------------------------------------------------------------
* @brief: This function takes in a list of INCREMENTALLY SORTED
* periods in Ms, and upon reaching a period of that list, returns
* the index of the step, 0 INDEXED, else returns 255.
* ------------------------------------------------------------------
* @param periods: The list of periods to be checked
* @param num_steps: The number of periods
*/
uint8_t MyButton :: readInProvidedSteps(uint32_t * periods, uint8_t num_steps){
/* Obviously */
if(num_steps == 0) return NON_CLICKED;

switch((flag & BTN_STATE_STEP_BITMASK) >> 4){
case READ_BTN:
step = 0;
if(digitalRead(button_pin) == (1 - off_state)){
time_since_clicked = millis();
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (1 << 4);
}
break;

case WAIT_BTN:
if(digitalRead(button_pin) == off_state){
flag &= ~BTN_STATE_STEP_BITMASK;
}
else if(millis() - time_since_clicked >= debounce_time){
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (2 << 4); // We have a true click
}
break;

case TRUE_CLICK:
if(digitalRead(button_pin) == off_state){
flag &= ~BTN_STATE_STEP_BITMASK;
if(step < num_steps - 1) return ABORTED_STEPS;
}
else if(step < num_steps && millis() - time_since_clicked >= periods[step]){
return step++;
}
break;
}
return NON_CLICKED;
}

/*
* Returns, as long as the button is pressed, the index of the reached
* period (provided list of incrementally sorted periods), aka the step
* ------------------------------------------------------------------
* @brief: This function takes in a list of INCREMENTALLY SORTED
* periods in Ms, and upon reaching a period of that list, returns
* the index of the step, 0 INDEXED, else returns 255.
* ------------------------------------------------------------------
* @param periods: The list of periods to be checked
* @param num_steps: The number of periods
*/
uint8_t MyButton :: readInProvidedSteps(uint32_t * periods, uint8_t num_steps, uint8_t starting_step){
/* Obviously */
if(num_steps == 0) return NON_CLICKED;

switch((flag & BTN_STATE_STEP_BITMASK) >> 4){
case READ_BTN:
step = starting_step;
if(digitalRead(button_pin) == (1 - off_state)){
time_since_clicked = millis();
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (1 << 4);
}
break;

case WAIT_BTN:
if(digitalRead(button_pin) == off_state){
flag &= ~BTN_STATE_STEP_BITMASK;
}
else if(millis() - time_since_clicked >= debounce_time){
flag = (flag & ~BTN_STATE_STEP_BITMASK) | (2 << 4); // We have a true click
}
break;

case TRUE_CLICK:
if(digitalRead(button_pin) == off_state){
flag &= ~BTN_STATE_STEP_BITMASK;
if(step < num_steps - 1) return ABORTED_STEPS;
}
else if(step < num_steps && millis() - time_since_clicked >= periods[step]){
return step++;
}
break;
}
return NON_CLICKED;
}

/*
* Returns, if the button have been pressed for longer than one of the
* periods in the input list, the index of the corresponding period
Expand Down
2 changes: 2 additions & 0 deletions src/MyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
bool readFallingClick();
uint32_t readTimedPress(uint8_t unit);
uint8_t readInSteps(uint32_t period, uint8_t num_steps);
uint8_t readInProvidedSteps(uint32_t * periods, uint8_t num_steps);
uint8_t readInProvidedSteps(uint32_t * periods, uint8_t num_steps, uint8_t starting_step);
uint8_t readMultiple(uint32_t * periods, uint8_t len);
};

Expand Down

0 comments on commit c7669a0

Please sign in to comment.