• AVR Freaks

Hot!Someone that please explain me part of the following code

Author
vra
Senior Member
  • Total Posts : 142
  • Reward points : 0
  • Joined: 2014/07/14 18:35:19
  • Location: 0
  • Status: offline
2020/05/30 17:58:07 (permalink)
0

Someone that please explain me part of the following code

Hi everybody, how are you?. This time I bring you a code adapted to filter the bounce of the buttons through code, it is based on a digital filter which does not remember its name but I know it is a weighted sum for approximately 200 or 300 ms -depends who implements it-. I mean, the function "enum btnStat_t doDebounce(uint8_t *state, volatile bool value)" is executed about 20 or 30 times depending on the coder.
 
El código es the following:
 

enum btnStat_t{
high,
low,
rising,
falling,
};
 
enum btnStat_t doDebounce(uint8_t *state, volatile bool value) {
uint8_t old = *state & 0x7F;
bool flag = (*state & 0x80)? true : false;
enum btnStat_t btnStd = (true == flag)? low : high;

// Digital filter part, value = (old * .75) + (new * .25)
old -= (old >> 2); // 1 - (1/4) = .75
old += value? 0x1F : 0x00; // if bit set, add .25

// Software schmitt trigger
// Newly detected rising edge
if ( (true == flag) && (old > 0x70) ) {
flag = false;
btnStd = rising;
}
// Newly detected falling edge
else if ( (false == flag) && (old < 0x07) ){
flag = true;
btnStd = falling;
}

// Update the state variable
*state = (old & 0x7F) | ((flag & 0x01) << 7);

// Return the state
return btnStd;
}

 
The function that interests me the most of the two is "enum btnStat_t doDebounce (uint8_t * state, volatile bool value)", I understand perfectly what the digital filter functions, shmitt trigger and almost all the internal code of the function do except the following:
 
I don't understand why they define the enumerator in the fourth line of the second function as follows:
 

enum btnStat_t btnStd = (true == flag)? low : high;

 





And I also don't understand why the function is defined as an enumerator the way it is with argument.

Could someone explain that to me please?

Thank you very much in advance for the help.
 
NOTE: Its a piece of code modified from one published in Hackaday.
 
#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27977
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/05/30 18:50:04 (permalink)
    +2 (2)
    Do you understand what the "?" operator does?
    https://en.wikipedia.org/wiki/%3F:#C
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    NorthGuy
    Super Member
    • Total Posts : 6222
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: Someone that please explain me part of the following code 2020/05/30 19:16:13 (permalink)
    +1 (1)
    It is not clear what exactly confuses you. "enum btnStat_t" is the name of a type. With usual type this would look like:
     
    int btnStd = flag ? low : high;

     
     
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11930
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/05/30 19:51:29 (permalink)
    +2 (2)
    And I also don't understand why the function is defined as an enumerator the way it is with argument.

     
    It's not defined as an enumerator, it's defined as returning an enumeration value.
    #4
    vra
    Senior Member
    • Total Posts : 142
    • Reward points : 0
    • Joined: 2014/07/14 18:35:19
    • Location: 0
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/05/30 21:04:04 (permalink)
    0
    ric
    Do you understand what the "?" operator does?
    https://en.wikipedia.org/wiki/%3F:#C
     


    Yes, I understand perfectly what the "?" operator does. In the next post I will try to clarify more my question.
    #5
    vra
    Senior Member
    • Total Posts : 142
    • Reward points : 0
    • Joined: 2014/07/14 18:35:19
    • Location: 0
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/05/30 21:28:44 (permalink)
    +1 (1)
    jtemples
    And I also don't understand why the function is defined as an enumerator the way it is with argument.

     
    It's not defined as an enumerator, it's defined as returning an enumeration value.




    My doubt goes more to the understood by jtemples, I know that I have an enum type called btnStat_t which can have the four possible states that a button can have as it has been declared, then I have another "function ?" of type enum and has the type btnStat_t but has arguments uint8_t *state, volatile bool value; I didn't understand why it was declared with arguments because the original variable was declared without arguments but now that jtemples pointed that its not declared as an enum but as a function returning a enum type then is a lot clear to me. Then the rest are simply variables of type btnStat_t which can have only four possible values.
     
    Thanks again for the clarification.
    #6
    mlp
    boots too small
    • Total Posts : 946
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/06/01 13:55:33 (permalink)
    +1 (1)
    Valery
     
    I know that I have an enum type called btnStat_t

    You know wrong.
    An enum is not a type.
    An enum defines a set of names with integer values.
     
    variables of type btnStat_t which can have only four possible values.

    You misspelled "variables of some integer type large enough to hold all values of enum btnStat_t, which can likely have many more than four possible values."

    Mark (this opinion available for hire)
    #7
    vra
    Senior Member
    • Total Posts : 142
    • Reward points : 0
    • Joined: 2014/07/14 18:35:19
    • Location: 0
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/06/01 17:29:08 (permalink)
    0
    mark.pappin
    Valery
     
    I know that I have an enum type called btnStat_t

    You know wrong.
    An enum is not a type.
    An enum defines a set of names with integer values.
     
    variables of type btnStat_t which can have only four possible values.

    You misspelled "variables of some integer type large enough to hold all values of enum btnStat_t, which can likely have many more than four possible values."
     
    Hi mlp, how are you?, thanks for your comment.
     
    Well, I know that is a lot of things about programming that I still don't know but that definition -if I understood it well enough- I've extrated it from the microchip presentation about C programming whose specific page is attached as a imagein this post
     
    There is said specifically that enumerations are integer data types that can have a limited range of values, is what microchip presentation says and this page is from 2013.
     
    I also read a book called "Just Enough C-C++ Programming" and in the page 65 explicitly defined enumerations like follows:
     
    "Enumerated Types
    Another kind of type, known as the enumerated type, allows you to create sets of
    values that are referenced by name, but that enumerate to an indexed position
    within that set. For example, you might like to create a user-defined type to store
    days of the week:
    enum week_days {
    Mon = 0, Tue, Wed, Thu, Fri, Sat, Sun
    };"




    So, am I wrong about my understanding of what is an enumeration?.
     
    I hope that nobody thinks that I am arguing in a bad way, I only want to clarify if mi understanding of enumerations is good or bad.

    Attached Image(s)

    #8
    dan1138
    Super Member
    • Total Posts : 3718
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/06/01 18:12:57 (permalink)
    +2 (2)
    vra
    So, am I wrong about my understanding of what is an enumeration?.

    Not wrong, just under experienced.

    There is no hard boundary on the maximum value the scaler type may assume. The C specification requires that is has enough bits to hold the maximum enumeration value assigned in the declaration. Also there is no intrinsic value check in C like there is in C++.

    The enum construct has only weak type checking and is barely better than a #define. The enumeration elements tend to pollute the global name space and complicate symbolic debug sessions. Used in thoughtless ways they cause more bugs than they avoid.

    /*
     * File     : main.c
     * Author   : dan1138
     * Target   : PIC18F46Q10
     * Compiler : XC8 v2.20
     * IDE      : MPLABX v5.40
     *
     * Created on June 1, 2020, 5:42 PM
     */

    #pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_64MHZ, CLKOUTEN = OFF, CSWEN = ON
    #pragma config FCMEN = OFF, MCLRE = EXTMCLR, PWRTE = OFF, LPBOREN = OFF
    #pragma config BOREN = SBORDIS, BORV = VBOR_190, ZCD = OFF, PPS1WAY = OFF
    #pragma config STVREN = ON, XINST = OFF, WDTCPS = WDTCPS_31, WDTE = OFF
    #pragma config WDTCWS = WDTCWS_7, WDTCCS = SC
    #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
    #pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
    #pragma config SCANE = ON, LVP = OFF, CP = OFF, CPD = OFF
    #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF

    #include <xc.h>
    #include <stdint.h>
    #include <stdbool.h>

    enum btnStat_t{
        high,
        low,
        rising,
        falling,
    };

    enum btnStat_t doDebounce(uint8_t *state, volatile bool value) {
        uint8_t old = *state & 0x7F;
        bool flag = (*state & 0x80)? true : false;
        enum btnStat_t btnStd = (true == flag)? low : high;

        // Digital filter part, value = (old * .75) + (new * .25)
        old -= (old >> 2); // 1 - (1/4) = .75
        old += value? 0x1F : 0x00; // if bit set, add .25

        // Software schmitt trigger
        // Newly detected rising edge
        if ( (true == flag) && (old > 0x70) ) {
            flag = false;
            btnStd = rising;
        }
        // Newly detected falling edge
        else if ( (false == flag) && (old < 0x07) ){
            flag = true;
            btnStd = falling;
        }

        // Update the state variable
        *state = (old & 0x7F) | ((flag & 0x01) << 7);

        // Return the state
        return btnStd;
    }
    void main(void) {
        uint8_t b1_state;
        enum btnStat_t ReturnState;

        for(;;)
        {
            ReturnState = doDebounce(&b1_state, PORTAbits.RA0);
            switch (ReturnState)
            {
                case high:
                    break;
                case low:
                    break;
                case rising:
                    break;
                case falling:
                    break;
                default:
                    break;
            }
            ReturnState = 66;   /* This asserts an error in C++, but nothing in plain C */
        }
    }



    #9
    mlp
    boots too small
    • Total Posts : 946
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Someone that please explain me part of the following code 2020/06/02 08:12:42 (permalink)
    0
    Valery
    mark.pappin
    You know wrong.
    An enum is not a type.
    An enum defines a set of names with integer values.
    ... 
    You misspelled "variables of some integer type large enough to hold all values of enum btnStat_t, which can likely have many more than four possible values."
     
    Hi mlp

    Learn to quote correctly. Type your new text outside the [ quote] ... [ /quote] block, or it will look like you are claiming somebody else wrote what you in fact wrote.
     
    There is said specifically that enumerations are integer data types that can have a limited range of values, is what microchip presentation says and this page is from 2013.

    It is wrong, and was wrong 24 years before it was published (enumerations have not changed since the first published C standard in 1989, and they never created a range-limited type).
     
    I also read a book called "Just Enough C-C++ Programming"

    C is different from C++, and there is no such think as "C-C++".
     
    So, am I wrong about my understanding of what is an enumeration?.

    I'm not as generous as Dan.
    Your understanding is wrong.
     
    As Dan says: enum creates new named integer constants that are only different from #defined macros that expand to constants in limited ways (the names become unavailable for some uses as identifiers; symbolic debuggers might be more likely to know about them; values are automagically incremented for sequential names; what type they actually are is defined by the compiler vendor rather than the standard, ... ).
     
    The name "enum btnStat_t" becomes shorthand for "some integer type large enough to hold all of the values of this enumeration", and if you define a variable of that type you can assign any values to it you like (not limited to only the names you have defined, nor just to the values you have defined them as), or perform any other operations you would expect to perform on an integer (+-*/%&|^) even where such operations make no sense (e.g. high * 23 + low^falling).
     

    Mark (this opinion available for hire)
    #10
    NorthGuy
    Super Member
    • Total Posts : 6222
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: Someone that please explain me part of the following code 2020/06/02 09:23:23 (permalink)
    +1 (1)
    The program uses _t next to the enum name. This is very confising. Enums (and structs) have their own name spaces. _t is normally used in global name space to denote types defined with typedef. Such as:
     
    enum btnStat{
    high,
    low,
    rising,
    falling,
    };
     
    typedef enum btnStat btnStat_t;

     
    Then you would create your variable as
     
    btnStat_t The_Variable_Which_Holds_the_Button_State;

     
    which is the same as:
     
    enum btnStat The_Variable_Which_Holds_the_Button_State;

     
    You can define the type in a single statement:
     
    typedef enum btnStat{
    high,
    low,
    rising,
    falling,
    } btnStat_t;

     
    or since the enum name is not going to be used, you can do
     
    typedef enum {
    high,
    low,
    rising,
    falling,
    } btnStat_t;

     
    If you later decide to change the type to something else, such as
     
    typedef unsigned char btnStat_t;

     
    the rest of the code will still work.
     
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5