Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
/ EasyButton_Handler Public archive

An handler interface class for projects involving my modified Staples Easy Button

License

Notifications You must be signed in to change notification settings

UniQHW/EasyButton_Handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Easy Button Handler

Part of my Easy Button Project

An handler interface class for projects involving my modified Staples Easy Button.

Contents

class EasyButton

class IEasyButtonHandler

Installation

Download the repository as a zip Downloading the repo

After obtaining a local copy of the library, import the library in the Arduino IDE:

Importing the library

Library Overview

This library provides a total of 2 classes:

Class Defined in Description
EasyButton EasyButton.h A primitive hardware abstraction for my modified easy button
IEasyButtonHandler IEasyButtonHandler.h A handler interface for EasyButton objects

EasyButton

A primitive hardware abstraction for my modified easy button.

Constructor

The constructor defines the pin member which is used to read the buttons state. In addition, it sets the pin mode to INPUT.

Parameters:

Parameter Description
uint8_t _pin Sets the pin that is used to read the buttons state

Members

Member Access Description
uint8_t pin Private Defines the pin that is used to receive button data

Member Functions

bool activated()

Access: Public

Returns true if button is activated.

Any of the following actions make up for an activated state:

  • The Button is pressed
  • The Button is held
  • The Button plays back audio
Example

The following example keeps the built-in LED on when the button is activated.

#include <EasyButton.h>

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  EasyButton eb(3); // Data cable is connected to pin 3

  /* LED HIGH */
  if(eb.activated()) {
    digitalWrite(LED_BUILTIN, HIGH);
  }

  /* LED LOW */
  else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

IEasyButtonHandler

A handler interface for EasyButton objects.

Constructor

The constructor is provided with a pointer to the EasyButton object that will be handled by the handler.

Parameters:

Parameter Description
EasyButton *_eb Pointer to an EasyButton object that will be handled

Members

Member Access Description
EasyButton *eb Private Points to the EasyButton object that is handled by the handler

Member Functions

void init()

Access: Public

Waits for the EasyButton to enter a idle state before proceeding with any other actions.

void handle()

Access: Public

This function initiates a permanent loop, in which the EasyButton object is constantly polled and handled. Upon handling, certain events can be triggered, based on the actions taken.

The handle function should be constantly polled in a loop

Events

Handler events are abstract and by default set to return directly after execution. On implementation of the interface, at least one event should be overwritten.

Event Description
virtual void onPush() Triggered directly on button push
virtual void onQuickPushed() Triggered after a very rapid button push, which doesn't toggle the "That was easy!" audio playback.If this event is triggered, onComplete() is ignored!
virtual void onComplete(unsigned long ms) Triggered after the buttons audio playback. The parameter indicates how long the button was held in ms, before audio playback.
virtual void whileIdle(unsigned long ms) Polled while the button is idling. The button is in a idle state when it is not activated (See activated()). The parameter indicates how long the button has already maintained a idle state
virtual void whileActivated(unsigned long ms) Polled while the button is activated. (See activated()). The parameter indicates how long the button has already maintained a activated state

Implementation example

The following example shows how a very simple implementation of IEasyButtonHandler that reports onPush(), onQuickPushed() and onComplete() on the serial bus.

#include <IEasyButtonHandler.h>

class MyEasyButtonHandler: public IEasyButtonHandler {

public:

  MyEasyButtonHandler(EasyButton *eb): IEasyButtonHandler(eb) {}

private:

  void onPush() {
    Serial.println("Button pushed!");
  }

  void onQuickPushed() {
    Serial.println("Quick push!");
  }

  void onComplete(unsigned long ms) {
    Serial.println("You've held the button for a total of " + String(ms) + "ms");
  }

  /* We're not going to implement all events */
  // void whileIdle(unsigned long ms) {}
  // void whileActivated(unsinged long ms){}
};

EasyButton *eb = new EasyButton(3); // Data cable is connected to pin 3
MyEasyButtonHandler handler(eb);

void setup() {
  Serial.begin(9600);
  handler.init();
}

void loop() {
  handler.handle();
}

About

An handler interface class for projects involving my modified Staples Easy Button

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages