Button debounce class

I’m trying to make this button debounce class work:

It does not work per example, as there is an error:
ERROR: the index ‘Button’ does not exist

This is their example:

//Example Instantiation
b1 <- Button(hardware.pin1, DIGITAL_IN_PULLUP, button.NORMALLY_HIGH,
function(){server.log(“Button 1 Pressed”)},
function(){server.log(“Button 1 released”)}
);

Has anyone ever been able to get this to work?

You need the code from button.nut too

Yes, I have that … the actual class. I didn’t post it here because it’s the exact same as the github code. All code in my device is a copy and paste of github. I’m just guessing something is wrong with the code they are calling “Example Instantiation”.

In the instantiation, should “button.NORAMLLY_HIGH” be “Button.NORMALLY_HIGH”? I would think the error would have indicated "…the index “button” does not exist.

That being said, I’m really just getting acquainted with OOP.

@hvacspei is spot on - looks like an error in the example code, I’ll get it fixed.

Here’s what the code should look like:

`//include Button class

//Example Instantiation
b1 <- Button(hardware.pin1, DIGITAL_IN_PULLUP, Button.NORMALLY_HIGH,
function(){server.log(“Button 1 Pressed”)},
function(){server.log(“Button 1 released”)}
);`

For those that struggled like me, I figured out that the class has to be above the “Instantiation”. It now works fine.

Below is my entire Device Code

` // Copyright (c) 2013 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
//
// Description: Debounced button press with callbacks
 
class Button{
    static NORMALLY_HIGH = 1;
    static NORMALLY_LOW  = 0;
    _pin             = null;
    _pull            = null;
    _polarity        = null;
    _pressCallback   = null;
    _releaseCallback = null;

    constructor(pin, pull, polarity, pressCallback, releaseCallback){
        _pin             = pin;               //Unconfigured IO pin, eg hardware.pin2
        _pull            = pull;              //DIGITAL_IN_PULLDOWN, DIGITAL_IN or DIGITAL_IN_PULLUP
        _polarity        = polarity;          //Normal button state, ie 1 if button is pulled up and the button shorts to GND
        _pressCallback   = pressCallback;     //Function to call on a button press (may be null)
        _releaseCallback = releaseCallback;   //Function to call on a button release (may be null)

        _pin.configure(_pull, debounce.bindenv(this));
    }

    function debounce(){
        _pin.configure(_pull);
        imp.wakeup(0.020, getState.bindenv(this));  //Based on googling, bounce times are usually limited to 10ms
    }

    function getState(){ 
        if( _polarity == _pin.read() ){
            if(_releaseCallback != null){
                _releaseCallback();
            }
        }else{
            if(_pressCallback != null){
                _pressCallback();
            }
        }
        _pin.configure(_pull, debounce.bindenv(this)); 
    }
}


//Example Instantiation
b1 <- Button(hardware.pin1, DIGITAL_IN_PULLUP, Button.NORMALLY_HIGH,
 function(){server.log("Button 1 Pressed")},
 function(){server.log("Button 1 Released")}
 );`