Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Using GPIO

Cai Biesinger edited this page Jul 1, 2018 · 4 revisions

Unfamiliar with GPIO? We have a whole guide on it!

Scarlet supports GPIO usage on the Beaglebone Black and Raspberry Pi, but all GPIO lines have the same core methods as required by the Scarlet.IO.IDigitalIn, Scarlet.IO.IDigitalOut, or Scarlet.IO.IInterruptSource interfaces (shown below).

IDigitalIn Interface

  • void SetResistor(ResistorState Resistor)
    • Sets the input resistor configuration. Select between PULL_UP, PULL_DOWN or NONE.
  • bool GetInput()
    • Gets the current input state. true for high state, false for low.
  • void Dispose()
    • Cleans up resources for others' use.

IDigitalOut Interface

  • void SetOutput(bool Output)
    • Sets the current output level. true for high, false for low.
  • void Dispose()
    • Cleans up resources for others' use.

IInterruptSource Interface

  • void RegisterInterruptHandler(EventHandler<InputInterrupt> Handler, IInterruptType Type)
    • Registers an interrupt handler. The given method will be called when the selected type of transition occurs, which can be chosen from FALLING_EDGE, RISING_EDGE or ANY_EDGE.

💡 NOTE: You cannot assume a class implementing IDigitalIn also implements IInterruptSource, and vice versa. Check before trying to cast, as not all devices support both functionalities.


Usage on BeagleBone Black

To start, you'll need to add a GPIO mapping. The method to do this is BBBPinManager.AddMappingGPIO.

AddMappingGPIO(BBBPin SelectedPin, bool IsOutput, ResistorState Resistor = ResistorState.NONE, bool FastSlew = true)
  • BBBPin SelectedPin
  • bool IsOutput
    • Whether this pin will be used for input or output.
  • ResistorState Resistor
    • The pull-up, pull-down, or none options allow you to set a resistor to weakly bias the pin in a certain way, or leave it floating internally. When the input pin is disconnected, it will then default back to this state (for example, if you have a switch connected, and it is open, this sets what you would see on the input). For outputs, this is irrelevant, and will always be set to NONE.
  • bool FastSlew
    • Toggles between a fast or slow slew rate (rise- and fall-time). Unless you have specific requirements, just leave this as true.

Here's an example of using this:

BBBPinManager.AddMappingGPIO(BBBPin.P8_07, true);
BBBPinManager.AddMappingGPIO(BBBPin.P8_08, false, ResistorState.PULL_DOWN);

This prepares the following:
P8_07 is prepared for output use, using fast slew mode.
P8_08 is prepared for input use, with a pull-down resistor and fast slew mode.

Now, we will construct our input and output objects:

IDigitalOut Output = new DigitalOutBBB(BBBPin.P8_07);
IDigitalIn Input = new DigitalInBBB(BBBPin.P8_08);

And then we can use them:

Log.Output(Log.Severity.DEBUG, Log.Source.HARDWAREIO, Input.GetInput().ToString());

Output.SetOutput(false);
Thread.Sleep(1000);
Output.SetOutput(true);

This will read the state of the input and print it to the log, then turn off the output, wait a second, then turn it on.

If you'd like to receive interrupt events, you can write a receiver method, and the event handler addition as such: (Since we know that the BeagleBone Black's onboard GPIO system, DigitalInBBB is guaranteed to support IInterruptSource, we don't need to do any checking here, but it would be good practice.

public void MyIntHandler(object Sender, InputInterrupt Event)
{
    // Do something with the interrupt
}

((IInterruptSource)Input).RegisterInterruptHandler(MyIntHandler, InterruptType.ANY_EDGE);

Usage on Raspberry Pi

Unlike the BeagleBone Black, preparing to use a GPIO pin is quite simple on the RPi. You can simply contruct the objects, as below.

IDigitalOut Output = new DigitalOutPi(13);
IDigitalIn Input = new DigitalInPi(15);

And then we can use them:

Log.Output(Log.Severity.DEBUG, Log.Source.HARDWAREIO, Input.GetInput().ToString());

Output.SetOutput(false);
Thread.Sleep(1000);
Output.SetOutput(true);

This will read the state of the input and print it to the log, then turn off the output, wait a second, then turn it on.

If you'd like to receive interrupt events, you can write a receiver method, and the event handler addition as such: (Since we know that the Raspberr Pi's onboard GPIO system, DigitalInPi is guaranteed to support IInterruptSource, we don't need to do any checking here, but it would be good practice.

public void MyIntHandler(object Sender, InputInterrupt Event)
{
    // Do something with the interrupt
}

((IInterruptSource)Input).RegisterInterruptHandler(MyIntHandler, InterruptType.ANY_EDGE);

💡 You'll notice, that once the IDigitalIn/IDigitalOut objects are constructed, that the rest of the procedure is identical between the BeagleBone and RPi. This is how Scarlet's I/O systems are meant to work, and means that moving your code from one platform to the other is extremely simple. This is the same for the other types of I/O as well.

Logo

Quick Links:
NuGet
Pin Diagrams: RPi | BBB
Developers: CaiB, Baldstrom

General Info:
Home
Common Issues
Getting Started
Supported Devices

Sections:
Logging
DataLog
Filters
Hardware I/O:
- BeagleBone Black
- Raspberry Pi
- Pin Diagrams: RPi | BBB
- GPIO: Using | For Beginners
- PWM: Using | For Beginners
- ADC: Using | For Beginners
- I2C: Using | For Beginners
- SPI: Using | For Beginners
- UART: Using | For Beginners
- CAN: Using | For Beginners
Networking
Sensors
StateStore

Other: Interesting Case Studies

Clone this wiki locally