ButtonEvent

This example shows how to blink LEDs at different rates using multitasking in Energia MT.

Hardware Required

  • MSP432P401R LaunchPad (Or any other LaunchPad that supports multi tasking.)

Relevant groundwork

None

Circuit

Only your LaunchPad Board is needed for this example.

Code Explanation

This Sketch demononstrates a button library that uses TI-RTOS’s event feature. The button library has 3 functions.

  • begin() will setup the button library.

  • read() which will read and debounce the button.

  • waitForPress() which will wait for a button to be pressed.

This library will allows you to read a button in one task and have another task wait for the button to be pressed. The task waiting for the button to be pressed will sleep until a button press event is detected. In this example Sketch, the ButtonEvent tab is the task reading the button and the Serial task is the task waiting for the button to be presses.

Each of the three tabs will execute as separate tasks, toggling the LED at a rate of 100, 500, and 1000 milliseconds respectively. During the delay() functions, the RTOS will automatically put the MCU into deep sleep mode to conserve power.

Usage

Upload the Sketch and open the Serial monitor. Press button PUSH1 (S1) and observe

Code

ButtonEvent tab

#include "Button.h"

Button button(PUSH1);

void ButtonSetup() {
  button.begin();
}

void ButtonLoop() {
  button.read();
}

Serial tab

void serialSetup()
{
  pinMode(RED_LED, OUTPUT);
  Serial.begin(115200);
  delay(500);
  Serial.println("Press button PUSH1!");
}

void serialLoop()
{
  static uint8_t state = 1;

  button.waitForPress();
  Serial.println("Button was pressed!");

  digitalWrite(RED_LED, state);
  state = !state;
}

Button.h tab

#include <Energia.h>
#include <xdc/runtime/Error.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/BIOS.h>

class Button {
  private:
    Event_Handle _event;
    uint8_t _pin;
    int buttonState;
    int lastButtonState;
    long lastDebounceTime;
    long debounceDelay;

  public:
    Button(uint8_t pin);
    void begin();
    void waitForPress();
    void waitForRelease();
    bool read();
};

Button.h tab

#include "Button.cpp"

Button::Button(uint8_t pin)
{
  _pin = pin;
  lastButtonState = HIGH;
  buttonState = HIGH;
  lastDebounceTime = 0;
  debounceDelay = 50;
}

void Button::begin()
{
  pinMode(_pin, INPUT_PULLUP);

  Error_Block eb;
  Error_init(&eb);
  /* Default instance configuration params */
  _event = Event_create(NULL, &eb);
}

bool Button::read()
{
  int reading = digitalRead(_pin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
        buttonState = reading;

      if (buttonState == HIGH) {
        Event_post(_event, Event_Id_00);
      }
    }
  }
  lastButtonState = reading;
}

void Button::waitForPress()
{
  xdc_UInt events;
  events = Event_pend(_event, Event_Id_NONE,
    Event_Id_00, BIOS_WAIT_FOREVER);
}
Guide Home