Skip to content

DigitalService

Daniel Johnson edited this page Jul 9, 2021 · 4 revisions
  1. Introduction
  2. Function Descriptions
  3. Examples
      Change LED with button Interrupt

1. Introduction

The DigitalService is a basic hardware abstraction to Read and Write to digital pins as well as attach interrupts. All functions are passed down to the underlying abstraction. Internal pullup settings should be setup in the abstraction based on the hardware implementation.

2. Function Descriptions

void InitPin(uint16_t pin, PinDirection direction)

Initializes a pin in the selected direction, either In or Out. Configuring the pin as In will set the pin as a high impedance input.

bool ReadPin(uint16_t pin)

Returns the value of the pin. High = true, Low = false.

void WritePin(uint16_t pin, bool value)

Writes a value of either High (true) or Low (false) to the pin.

void AttachInterrupt(uint16_t pin, std::function<void()> callBack)

Attaches an interrupt to any changing event on the pin. Perform a ReadPin to check the state. Calling this function with a new callBack will overwrite any existing callBack.

void DetachInterrupt(uint16_t pin)

Detach any interrupt from the pin.

3. Examples

Change LED with button Interrupt

Declaration and CallBack Function

IDigitalService *_digitalService;
void buttonInterrupt()
{
     // set LED pin to the state of PA0
    _digitalService->WritePin(45, _digitalService->ReadPin(0)); //PC13 = PA0
}

Setup

 // Initialize LED pin
_digitalService->InitPin(45, PinDirection::Out); //PC13 as Output
 // Initialize button pin
_digitalService->InitPin(0, PinDirection::In); //PA0 as Input
 // Setup interrupt callback
_digitalService->AttachInterrupt(0, &buttonInterrupt); //PA0