Skip to content

paulo-raca/YetAnotherArduinoPcIntLibrary

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yet Another Arduino PcInt Library

As you may have guessed, this is an Arduino library to handle Pin Change Interrupts.

Examples

#define PCINT_PIN A15

#include <YetAnotherPcInt.h>

void pinChanged(const char* message, bool pinstate) {
  Serial.print(message);
  Serial.println(pinstate ? "HIGH" : "LOW");
}

void setup() {
  Serial.begin(115200);
  pinMode(PCINT_PIN, INPUT_PULLUP);
  PcInt::attachInterrupt(PCINT_PIN, pinChanged, "Pin has changed to ", CHANGE);
}

void loop() {}

Features

  • Easy to install and use

  • Callbacks provide useful arguments:

    • A User-provided pointer: This can be an object with some context, a string to print, or to whatever else you want.

      This is specially useful when developing components that can be instantiated many times (See example)

    • The pin's state: That's the very first thing you were going to do inside the callbacks, wasn't it?

      Providing it as an argument prevents concurrency issues.

  • It should support RISING/FALLING/CHANGE modes

  • Code is very efficient (It's an ISR, after all)

  • Code is very small (~250 lines) and readable

API

attachInterrupt

Attaches a callback function to Pin Change Interruptions on the specified pin.

void PcInt::attachInterrupt(
    uint8_t pin, 
    callback func, 
    T *userdata, 
    uint8_t mode=CHANGE, 
    bool trigger_now=false);

Arguments:

  • pin: The pin number you are listening.

  • callback: The funcion called whenever the interrupt was triggered.

    Your funcion must look like one of these:

    • void myfunction() { ... }
    • void myfunction(bool newstate) { ... }
    • void myfunction(T* userdata) { ... }
    • void myfunction(T* userdata, bool newstate) { ... }
  • userdata: User-provided argument for callback. Skip this If your callback doesn't have a userdata argument.

    User-provided arguments are useful when reusing the same callback funcion to handle changes on multiple pins.

  • mode: The transition Which triggers the callbacks: CHANGE, RISING or FALLING.

  • trigger_now: If set, the callback is called immediately. This is useful for initialization.

detachInterrupt

void PcInt::detachInterrupt(
    uint8_t pin);

Removes the callback function from Pin Change Interruptions on the specified pin.

Arguments:

  • pin: The pin number you are no longer listening to.

About Pin Change Interruptions

AVR microcontrollers only have a few external Interruption pins. But I want to monitor more pins... Looots more!

The alternative is using pin change interrupts, which is supported on lots of pins simultaneously.

The interrupt can be enabled for each supported pin individually, but there are only a few interrupt vectors, so up to 8 pins share the same service routine.

It's up to the software to figure out which pins changed state and act accordingly. This library does it for you :)

Comparisson with other libraries

As the name suggests, there are many other libraries out there for handling Pin Change Interruptions.

In particular, this project started as a fork of Sodaq_PcInt and uses PinChangeInterruptBoards.h from NicoHood's PinChangeInterrupt Library

I've looked at many of them before I decided to create this library, and this is how they compare.

  • PcInt example code from arduino.cc

    • Not available on Arduino's library manager (It's not really a library, after all)
    • Doesn't support user data on callbacks
    • Doesn't provide trigger type (RISING/FALLING)
    • Supports RISING/FALLING/CHANGE modes
    • Not very optimized
    • Code quality is Okay
    • I think it might need some tweaks depending on the microcontroller used
  • Sodaq_PcInt by SODAQ

    • Available on Arduino's library manager
    • Doesn't support user data on callbacks
    • Doesn't provide trigger type (RISING/FALLING)
    • Doesn't supports RISING/FALLING/CHANGE modes (Unless you are using GabrielNotman's fork)
    • Very optimized
    • Excelent code
    • Should support all/most AVR Arduinos
  • PinChangeInterrupt Library by NicoHood

    • Available on Arduino's library manager
    • Doesn't support user data on callbacks
    • Provides trigger type (RISING/FALLING) via getPinChangeInterruptTrigger()
    • Supports RISING/FALLING/CHANGE modes
    • It's super-optimized and configurable, but...
    • The code is a bit messy (IMHO, anyway)
    • Should on all/most AVR Arduinos
  • EnableInterrupt by Mike "GreyGnome" Schwager

    • Available on Arduino's library manager
    • Doesn't support user data on callbacks (but does provide the pin number via the EI_ARDUINO_INTERRUPTED_PIN macro)
    • Doesn't provide trigger type (RISING/FALLING)
    • Supports RISING/FALLING/CHANGE modes
    • It's super-optimized and configurable, but...
    • Big monolithic messy code
    • Should support all/most AVR Arduinos (It also supports External Interrupts!)
  • PciManager by Balazs Kelemen

    • Available on Arduino's library manager
    • Doesn't support user data on callbacks
    • Provides trigger type (RISING/FALLING) via getPinChangeInterruptTrigger()
    • Doesn't supports RISING/FALLING/CHANGE modes
    • Not very optimized
    • Good code quality
    • Should support all/most AVR Arduinos

About

An Arduino library to handle Pin Change Interrupts

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • C++ 79.4%
  • C 20.6%