HiPi
Perl Modules for Raspberry Pi
Version 0.67 - released 08 October 2017

HiPi::Interface::HobbyTronicsBackpackV2

This module provides an interface to LCD devices using the HobbyTronics Backpack V2 controller.

It can use HiPi::Device::I2C or HiPi::Device::SerialPort as a backend.

Methods

%params contains a number of key value pairs

Required key value pairs are width and lines. You must specify the geometry of your LCD:

use HiPi qw( :lcd );
use HiPi::Interface::HobbyTronicsBackpackV2;

my $lcd = HiPi::Interface::HobbyTronicsBackpackV2->new( 
    width =>  16,
    lines =>  4,
);

Common optional params and their defaults

backlightcontrol => 0

specify if methods setting the level of backlight can be used.
The method $lcd->backlight will only work if you set
backlightcontrol => 1 in the constructor.

devicetype =>  'serialrx'

The Backpack supports two connection types, 'serialrx' and 'i2c'
You can specify either - the default is 'serialrx'

Options and their defaults when devicetype is 'serialrx'

devicename      => '/dev/ttyAMA0'
baudrate        => 9600
parity          => 'none',
stopbits        => 1,
databits        => 8,

If you are using a Pi 3 with default settings, the header pins
on the Rpi gpio are connected to the mini uart. You will therefore need
to specify the device to override the default.

devicename      => '/dev/ttyS0'

Options and their defaults when devicetype is 'i2c'

devicename      => ( raspbi board version == 1 ) 
                   ? '/dev/i2c-0' : '/dev/i2c-1'

address         => 0x3A

Example complete constructor calls:

my $lcd = HiPi::Interface::HobbyTronicsBackpackV2->new( 
    width            => 16,
    lines            => 4,
    devicetype       => 'serialrx',
    backlightcontrol => 1,
    devicename       => '/dev/ttyS0',
);

my $lcd = HiPi::Interface::HobbyTronicsBackpackV2->new( 
    width            => 16,
    lines            => 4,
    devicetype       => 'i2c',
    devicename       => '/dev/i2c-1',
    address          => 0x3A
    backlightcontrol => 1
);

Switch the LCD on / off

$lcd->enable( 1 ); # on
$lcd->enable( 0 ); # off

Set the current cursor position.

# set the position to the leftmost column of the top row.
$lcd->set_cursor_position(0, 0);

Move the cursor one position to the left

$lcd->move_cursor_left();

Move the cursor one position to the right

$lcd->move_cursor_right();

Move the cursor to the top left postion

$lcd->home();

Clear all text and move the cursore position to the top left.

$lcd->clear();

Set the cursor mode. Valid constants for mode are :

  • SRX_CURSOR_OFF
  • SRX_CURSOR_BLINK
  • SRX_CURSOR_UNDERLINE

use HiPi qw( :lcd );
.....
$lcd->set_cursor_mode( SRX_CURSOR_BLINK );

Set the backlight light level. Valid value for $percent is a number between 0 and 100

$lcd->backlight( 25 );

Send $text to be 'printed' at the current cursor position.

$lcd->send_text( 'Hello World' );

Send a raw HD44780 command. $command can be one of :

  • HD44780_CLEAR_DISPLAY
  • HD44780_HOME_UNSHIFT
  • HD44780_CURSOR_MODE_LEFT
  • HD44780_CURSOR_MODE_LEFT_SHIFT
  • HD44780_CURSOR_MODE_RIGHT
  • HD44780_CURSOR_MODE_RIGHT_SHIFT
  • HD44780_DISPLAY_OFF
  • HD44780_DISPLAY_ON
  • HD44780_CURSOR_OFF
  • HD44780_CURSOR_UNDERLINE
  • HD44780_CURSOR_BLINK
  • HD44780_SHIFT_CURSOR_LEFT
  • HD44780_SHIFT_CURSOR_RIGHT
  • HD44780_SHIFT_DISPLAY_LEFT
  • HD44780_SHIFT_DISPLAY_RIGHT

use HiPi qw( :lcd );
.....
$lcd->send_command( HD44780_DISPLAY_OFF );

Update the baudrate to the new value set in 'baudrate' property. Only useful when backend is 'serialrx'. Following this command, the backback will require a hard reset.

use HiPi qw( :lcd );
.....
$lcd->baudrate( HTV2_BAUD_9600 );
$lcd->update_baudrate;

Set the geometry stored in the backpack with the 'width' and 'lines' values passed in the $lcd constructor. Some implementations may require a hard reset for the device after this method is called

$lcd->update_geometry();

Change the I2C address if using in i2c mode.
The i2c address must be in the range 1 - 127 ( 0x01 - 0x7F )

$lcd->change_i2c_address( 0x6C );

You can use the HobbyTronics Backpack documented commands directly if you wish. The $command param is the backpack command number and you can follow this with any number of parameters required.

Valid command constants are :

  • HTV2_CMD_PRINT
  • HTV2_CMD_SET_CURSOR_POS
  • HTV2_CMD_CLEAR_LINE
  • HTV2_CMD_CLEAR_DISPLAY
  • HTV2_CMD_LCD_TYPE
  • HTV2_CMD_HD44780_CMD
  • HTV2_CMD_BACKLIGHT
  • HTV2_CMD_WRITE_CHAR
  • HTV2_CMD_I2C_ADDRESS
  • HTV2_CMD_BAUD_RATE
  • HTV2_CMD_CUSTOM_CHAR
use HiPi qw( :lcd );
.....
$lcd->send_htv2_command( HTV2_CMD_SET_CURSOR_POS, 2, 12 );