rpi_spi 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 51

rpi_spi.dart #

pub package Build Status

rpi_spi is a Dart package for using SPI on the Raspberry Pi.

Overview #

  • The Spi library provides the API for accessing devices using the SPI protocol

  • The RpiSpi library provides implementation of the SPI protocol on the Raspberry Pi derived from the WiringPi library.

Setup #

Be sure to enable SPI on the Raspberry Pi using

    sudo raspi-config

RpiSpi uses a native library written in C. For security reasons, authors cannot publish binary content to pub.dartlang.org, so there are some extra steps necessary to compile the native library on the RPi before this package can be used. These two steps must be performed when you install and each time you upgrade the rpi_spi package.

  1. Activate the rpi_spi package using the pub global command.
    pub global activate rpi_spi
  1. From your application directory (the application that references the rpi_spi package) run the following command to build the native library
    pub global run rpi_spi:build_lib

pub global activate makes the Dart scripts in the rpi_spi/bin directory runnable from the command line. pub global run rpi_spi:build_lib runs the rpi_spi/bin/build_lib.dart program which in turn calls the build_lib script to compile the native librpi_spi_ext.so library for the rpi_spi package.

Example #

  • example.dart demonstrates instantiating and accessing a SPI device.

  • mcp3008.dart demonstractes how the SPI API is used to interact with a MCP3008 analog to digital converter

Connect the following pins on the Raspberry Pi to the following pins on the Adafruit MCP3008. Pi pins 13 and 15 are connected to MCP3008 input #0 for both for the example and as a predictable test input. The other connected Pi pins are for controlling the MCP3008 and reading it's output values via the SPI API.

Rpi PinMCP3008
PIN #13 to 4.7K resistor to --->PIN #1 (CH0)
PIN #15 to 10K resistor to --->PIN #1 (CH0)
PIN #17 (3.3V)PIN #16 (VDD) & PIN #15 (VREF)
PIN #19 (SPI0 MOSI)PIN #11 (DIN)
PIN #23 (SIP0 SCLK)PIN #13 (CLK)
PIN #24 (SPI0 CS0)PIN #10 (CS/SHDN)
PIN #25 (GND)PIN #9 (DGND) & PIN #14 (AGND)

Changelog #

0.1.1 #

  • update README and format source

0.1.0 #

  • Initial implementation


import 'dart:async';

import 'package:rpi_spi/rpi_spi.dart';

import 'mcp3008.dart';

main() async {
  final spi = new RpiSpi();
  await readSensor(new Mcp3008(spi, 0, 24));

Future readSensor(Mcp3008 mcp3008) async {
  StringBuffer out;
  print('Read analog values from MCP3008 channels 0 - 7:');

  print('      | Channel');
  out = new StringBuffer('      ');
  for (int channel = 0; channel < 8; ++channel) {
    out.write('| ${channel.toString().padLeft(4)} ');
  print('-' * 63);

  for (int count = 1; count <= 10; ++count) {
    out = new StringBuffer(' ${count.toString().padLeft(4)} ');
    for (int channel = 0; channel < 8; ++channel) {
      var value = mcp3008.read(channel);
      out.write('| ${value.toString().padLeft(4)} ');
    await new Future.delayed(new Duration(milliseconds: 10));

Use this package as a library

1. Depend on it

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

  rpi_spi: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or 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:rpi_spi/rpi_spi.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix bin/build_lib.dart. (-4.41 points)

Analysis of bin/build_lib.dart reported 9 hints, including:

line 11 col 17: Unnecessary new keyword.

line 14 col 14: Unnecessary new keyword.

line 25 col 22: Unnecessary new keyword.

line 29 col 15: Unnecessary new keyword.

line 34 col 18: Unnecessary new keyword.

Fix lib/rpi_spi.dart. (-3.93 points)

Analysis of lib/rpi_spi.dart reported 8 hints, including:

line 14 col 33: Unnecessary new keyword.

line 21 col 13: Unnecessary new keyword.

line 26 col 13: Unnecessary new keyword.

line 29 col 20: Unnecessary new keyword.

line 38 col 30: Unnecessary new keyword.

Fix lib/spi.dart. (-1.49 points)

Analysis of lib/spi.dart reported 3 hints:

line 20 col 13: Unnecessary new keyword.

line 31 col 13: Unnecessary new keyword.

line 92 col 17: Unnecessary new keyword.

Maintenance suggestions

Package is getting outdated. (-27.67 points)

The package was last published 66 weeks ago.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.60.0 <3.0.0
path >=1.0.0 <2.0.0 1.6.4
Dev dependencies
grinder ^0.8.0
pedantic ^1.1.0
test ^1.2.0