flutter_gpiod 0.1.0+3

  • Readme
  • Changelog
  • Example
  • Installing
  • new64

flutter_gpiod #

A library for GPIO access on linux using libgpiod.

Currently, flutter_gpiod is only supports flutter-pi as a platform. It will not work on linux desktop or any other platform.

You need to have libgpiod.so on your system for it to work. You can install it using sudo apt install gpiod.

Getting Started #

Then, you can retrieve the list of GPIO chips attached to your system using [FlutterGpiod.chips]. Each chip has a name, label and a number of GPIO lines associated with it.

final gpio = await FlutterGpiod.getInstance();

final chips = gpio.chips;

for (final chip in chips) {
    print("chip name: ${chip.name}, chip label: ${chip.label}");

    for (final line in chip.lines) {
        print("  line: $line");
    }
}

Each line also has some information associated with it that can be retrieved using [GpioLine.info]. The information can change at any time if the line is not owned/requested by you.

// Get the instance of the FlutterGpiod singleton.
final gpio = await FlutterGpiod.getInstance();

// Get the chip with label 'pinctrl-bcm2835'.
// This is the main Raspberry Pi GPIO chip.
final chip = gpio.chips.singleWhere((chip) => chip.label == 'pinctrl-bcm2835');

// Get line 22 of the 'pinctrl-bcm2835' GPIO chip.
// This is the BCM 22 pin of the Raspberry Pi.
final line = chip.lines[22];

print("line info: ${await line.info}")

To control a line (to read or write values or to listen for edges), you need to request it using [GpioLine.requestInput] or [GpioLine.requestOutput].

final gpio = await FlutterGpiod.getInstance();
final chip = gpio.chips.singleWhere((chip) => chip.label == 'pinctrl-bcm2835');
final line = chip.lines[22];

// request it as input.
await line.requestInput();
print("line value: ${await line.getValue()}");
await line.release();

// now we're requesting it as output.
await line.requestOutput(initialValue: true);
line.setValue(false);
await line.release();

// request it as input again, but this time we're also listening
// for edges; both in this case.
await line.requestInput(triggers: {SignalEdge.falling, SignalEdge.rising});

print("line value: ${await line.getValue()}");

// line.onEvent will not emit any events if no triggers
// are requested for the line.
// this will run forever
await for (final event in line.onEvent) {
  print("got GPIO line signal event: $event");
}

await line.release();

0.1.0+3 #

  • Removed link in pubspec.yaml because it wasn't working.

0.1.0+2 #

  • Format sources
  • Better description in pubspec.yaml

0.1.0+1 #

  • Fix README.md

0.1.0 #

  • Initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter_gpiod/flutter_gpiod.dart';

void main() => runApp(FlutterGpiodTestApp());

class FlutterGpiodTestApp extends StatefulWidget {
  @override
  _FlutterGpiodTestAppState createState() => _FlutterGpiodTestAppState();
}

class _FlutterGpiodTestAppState extends State<FlutterGpiodTestApp> {
  Future<void> _testFlutterGpiod() async {
    final gpio = await FlutterGpiod.getInstance();

    /// Retrieve the list of GPIO chips.
    final chips = gpio.chips;

    /// Print out all GPIO chips and all lines
    /// for all GPIO chips.
    for (var chip in chips) {
      print("$chip");

      for (var line in chip.lines) {
        print("  ${await line.info}");
      }
    }

    /// Retrieve the line with index 23 of the first chip.
    /// This is BCM pin 23 for the Raspberry Pi.
    ///
    /// I recommend finding the chip you want
    /// based on the chip label, as is done here.
    ///
    /// In this example, we search for the main Raspberry Pi GPIO chip,
    /// which has the label `pinctrl-bcm2835`, and then retrieve the line
    /// with index 23 of it. So [line] is GPIO pin BCM 23.
    final line =
        chips.singleWhere((chip) => chip.label == 'pinctrl-bcm2835').lines[23];

    /// Request BCM 23 as output.
    await line.requestOutput(
        consumer: "flutter_gpiod test", initialValue: true);

    /// Pulse the line.
    /// Set it to inactive. (so low voltage = GND)
    await line.setValue(false);
    await Future.delayed(Duration(milliseconds: 500));
    await line.setValue(true);
    await Future.delayed(Duration(milliseconds: 500));

    await line.release();

    /// Now we're listening for falling and rising edge events
    /// on BCM 23.
    await line.requestInput(
        consumer: "flutter_gpiod input test",
        triggers: {SignalEdge.falling, SignalEdge.rising});

    /// Log line events for eternity.
    await for (final event in line.onEvent) {
      print("flutter_gpiod line event: $event");
    }

    /// Release the line, though we'll never reach this point.
    await line.release();
  }

  @override
  void initState() {
    super.initState();
    _testFlutterGpiod();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('flutter_gpiod Test App'),
          ),
          body: Container()),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_gpiod: ^0.1.0+3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_gpiod/flutter_gpiod.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
async ^2.4.0 2.4.1
flutter 0.0.0
meta ^1.1.8 1.1.8
mutex ^1.0.3 1.0.3
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test