Output Demonstration 1

Introduction

This program uses the circuits for an LED or DC motor described previously.

The code of the output unit and form follows a screenshot of the program in action.

GPIO Output Demonstration 1 in Action

GPIO Output Demonstration 1 in Action

The Code of u_output_demo1.pas

unit u_output_demo1;
{Demo output application for GPIO on Raspberry Pi
based on http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi, which was
inspired by the Python input/output demo application by Gareth Halfacree
written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5}

//Build with Ctrl+F9 then run with SuperUser permissions e.g. sudo ./gpio_input

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Unix;

type
  TfrmOutputDemo1 = class(TForm)
    LogMemo: TMemo;
    GPIO17ToggleBox: TToggleBox;
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure GPIO17ToggleBoxChange(Sender: TObject);
  end;

var
  frmOutputDemo1: TfrmOutputDemo1;
  gReturnCode: longint; //stores the result of the IO operation

implementation

{$R *.lfm}

procedure TfrmOutputDemo1.FormCreate(Sender: TObject);
begin
   //Prepare GPIO17 (P1-11) for access.
  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export');
  if gReturnCode = 0 then
    LogMemo.Lines.Add('GPIO17 prepared for access')
  else
    LogMemo.Lines.Add('Error preparing GPIO17 for access');
  //Set GPIO17 as output.
  gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction');
  if gReturnCode = 0 then
    LogMemo.Lines.Add('GPIO17 set as output')
  else
    LogMemo.Lines.Add('Error setting GPIO17 as output');
  LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1;
end;

procedure TfrmOutputDemo1.GPIO17ToggleBoxChange(Sender: TObject);
begin
  if GPIO17ToggleBox.Checked then
    begin
      //Switch on GPIO17.
      gReturnCode := fpsystem('echo "1" > /sys/class/gpio/gpio17/value');
      if gReturnCode = 0 then
        begin
          LogMemo.Lines.Add('GPIO17 turned on');
          GPIO17ToggleBox.Caption := 'On';
        end
      else
        LogMemo.Lines.Add('Error turning on GPIO17');
    end
  else
    begin
      //Switch off GPIO17.
      gReturnCode := fpsystem('echo "0" > /sys/class/gpio/gpio17/value');
      if gReturnCode = 0 then
        begin
          LogMemo.Lines.Add('GPIO17 turned off');
          GPIO17ToggleBox.Caption := 'Off';
        end
      else
        LogMemo.Lines.Add('Error turning off GPIO17');
    end;
  LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1;
end;

procedure TfrmOutputDemo1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/unexport');
end;

end.

The Code of u_output_demo1.lfm

object frmOutputDemo1: TfrmOutputDemo1
  Left = 671
  Height = 185
  Top = 422
  Width = 335
  Caption = 'GPIO Output Demo1'
  ClientHeight = 185
  ClientWidth = 335
  OnClose = FormClose
  OnCreate = FormCreate
  LCLVersion = '0.9.30.4'
  object LogMemo: TMemo
    Left = 16
    Height = 176
    Top = 0
    Width = 264
    Lines.Strings = (
      'LogMemo'
    )
    TabOrder = 0
  end
  object GPIO17ToggleBox: TToggleBox
    Left = 288
    Height = 23
    Top = 8
    Width = 42
    Caption = 'Off'
    OnChange = GPIO17ToggleBoxChange
    TabOrder = 1
  end
end    
Programming - a skill for life!

How to use Raspberry Pi's GPIO port for output and input