Digital Electronics Lab 2018 – Assignment for Week 9: Creating and Using Custom Classes and Objects

Link to the code for this week:

https://github.com/hexbokeh/Custom_button_class_001

How the BetterButton class works

The BetterButton class is a neatly-packaged way of handling many of the common requirements of working with buttons. Basically, it streamlines the process of working with buttons by hiding much of the code away in the .h (header) and .cpp files. It is also convenient because an unlimited number of instances of a class – objects – can created, thereby recycling the same bit of code, for whatever circumstances the programmer intends to implement.

BetterButton.h

The .h extension implies that this is the “header file” of the BetterButton class. This means that it contains a rough outline of the class, including its public and private components. It contains the declarations for things like the constructor method (which will be called outside the class to instantiate objects), variables, and more. Reading over the header file provides a good understanding of what the BetterButton class is capable of doing.

As far as implementation is concerned, there are several preprocessor commands at the top and bottom of the header document. These ensure that no class with an existing name of ‘BetterButton’ is overwritten. In addition, the main ‘Arduino’ class is invoked by calling its respective header file.

Within the class, all elements are public, meaning that they can be accessed by code outside those that define the BetterButton class. The method with the same name as the class is the constructor. Other functions are named, with return types and arguments; however, none are defined. Finally, essential variables for proper class functionality are named at the bottom.

BetterButton.cpp

The .cpp contains the “meat” of the class, in that it defines the functionality of the constructor, the other methods, and variables. At the top, it also invokes the Arduino class header file, as well as the BetterButton header file just created.

The constructor is set up to receive incoming arguments, and pass these parameters to a newly-created object, an instance of the BetterButton class. Internal variables will store and modify these incoming arguments as necessary, through the execution of other functions.

The three methods other than the constructor (defined below) are also fleshed out.

Explaining the purposes of:

process()

This is the primary method of the BetterButton class. It is constantly running because it is called in the loop() function on a per-object basis. Its purpose is to monitor incoming digital signals from the respective physical button. If it detects a change in state (either a HIGH or LOW) from what there was in the last frame, it calls the appropriate functions. (Read on.)

pressHandler() and releaseHandler()

How does process() know what to do upon detecting a change in state? It needs to know what user of the class wants to happen. To do this, process() calls pressHandler() or releaseHandler(), depending on the nature of the incoming signal. These methods are called upon in the setup() of the code made by the user (I’ll refer to it as the example file). As arguments, they receive the references to the methods that the user has defined, which describe exactly what should happen upon the detection of a button press or release (respectively). pressHandler() and releaseHandler() update the references of the variables pressCallback and releaseCallback, respectively, to point to the user-defined functions. That way, when process() detects a button press or a button release, it knows what function to call, and thus what operations the user wants the Teensy to perform. (In this case, it was to print the on/off state of the button, the number of the button, and send out a MIDI note.)

 

Leave a comment