The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Device::WebIO - Duct Tape for the Internet of Things

SYNOPSIS

my $webio = Device::WebIO->new;
$webio->register( 'foo', $dev ); # Register a device with the name 'foo'

# Input pin 0 on device registered with the name 'foo'
my $in_value = $webio->digital_input( 'foo', 0 );
# Output pin 0 on device registered with the name 'foo'
$webio->digital_input( 'foo', 0, $in_value );

DESCRIPTION

Device::WebIO provides a standardized interface for accessing GPIO, Analog-to-Digital, Pulse Width Modulation, and many other devices. Drivers are available for the Raspberry Pi, PCDuino, Arduino (via Firmata attached over USB), and many others in the future.

The 35,000-foot-view is that the Device::WebIO object is registered with one or more objects that do the Device::WebIO::Device role. These objects provide certain services, in accordance with the individual roles under the Device::WebIO::Device::* namespace, such as DigitalInput, DigitalOutput, ADC, etc.

METHODS

new

Constructor.

register

register( $name, $obj );

Register a driver object with the given name. The object must do the c<Device::WebIO::Device> role.

all_desc

all_desc( $name );

Returns hashref specifying the capabilities of the device.

Entries in the hashref are:

  • UART [bool]

  • SPI [bool]

  • I2C [bool]

  • ONEWIRE [bool]

  • GPIO [hashref]

GPIO's entries are numbers mapping to each GPIO pin. The values are hashrefs containing:

  • function ["IN", "OUT", "ALTn" (where n is some number)]

  • value [bool]

pin_desc

pin_desc( $name );

Returns an arrayref containing a definition for each pin in order.

Each entry can be:

  • Some number (corresponding to a GPIO number)

  • "V33" (3.3 volt power)

  • "V50" (5.0 volt power)

  • "GND" (ground)

Input

set_as_input

set_as_input( $name, $pin );

Set the given pin as input.

is_set_input

is_set_input( $name, $pin );

Check if the given pin is already set as input.

digital_input_pin_count

digital_input_pin_count( $name );

Returns how many input pins there are for this device.

digital_input

digital_input( $name, $pin );

Returns the input status of the given pin. 1 for on, 0 for off.

digital_input_port

digital_input_port( $name );

Returns an integer with each bit representing the on or off status of the associated pin.

Output

set_as_output

set_as_output( $name, $pin );

Set the given pin as output.

is_set_output

is_set_output( $name, $pin );

Check if the given pin is set as output.

digital_output_pin_count

digital_output_pin_count( $name );

Returns the number of output pins.

digital_output

digital_output( $name, $pin, $value );

Sets the value of the output for the given pin. 1 for on, 0 for off.

digital_output_port

digital_output_port( $name, $int );

Sets the value of all output pins. Each bit of $int corresponds to a pin.

Analog-to-Digital

adc_count

adc_count( $name );

Returns the number of ADC pins.

adc_resolution

adc_resolution( $name, $pin );

Returns the number of bits of resolution for the given pin.

adc_max_int

adc_max_int( $name, $pin );

Returns the max integer value for the given pin.

adc_volt_ref

adc_volt_ref( $name, $pin );

Returns the voltage reference for the given pin. All ADC values are scaled between 0 (ground) and the volt ref.

adc_input_int

adc_input_int( $name, $pin );

Return the ADC integer input value.

adc_input_float

adc_input_float( $name, $pin );

Return the ADC floating point input value. The value will be between 0.0 and 1.0.

adc_input_volts

adc_input_volts( $name, $pin );

Return the voltage level of the given pin. This will be between 0.0 (ground) and the volt ref.

PWM

pwm_count

pwm_count( $name );

Return the number of PWM pins.

pwm_resolution

pwm_resolution( $name, $pin );

Return the number of bits of resolution for the given PWM pin.

pwm_max_int

pwm_max_int( $name, $pin );

Return the max int value for the given PWM ping.

pwm_output_int

pwm_output_int( $name, $pin, $value );

Set the value of the given PWM pin.

pwm_output_float

pwm_output_float( $name, $pin, $value );

Set the value of the given PWM pin with a floating point value between 0.0 and 1.0.

Video

vid_channels

vid_channels( $name );

Get the number of video channels.

vid_width

vid_width( $name, $channel );

Return the width of the video channel.

vid_height

vid_height( $name, $channel );

Return the height of the video channel.

vid_fps

vid_fps( $name, $channel );

Return the Frames Per Second of the video channel.

vid_kbps

vid_kbps( $name, $channel );

Return the bitrate of the video channel.

vid_set_width

vid_set_width( $name, $channel, $width );

Set the width of the video channel.

vid_set_height

vid_set_height( $name, $channel, $height );

Set the height of the video channel.

vid_allowed_content_types

vid_allowed_content_types( $name, $channel );

Returns a list of MIME types allowed for the given video channel.

vid_stream

vid_stream( $name, $channel, $type );

Returns a filehandle for streaming the video channel. $type is one of the MIME types returned by vid_allowed_content_types().

Still Image

img_channels

img_channels( $name );

Get the number of still image channels.

img_width

img_width( $name, $channel );

Return the width of the image channel.

img_height

img_height( $name, $channel );

Return the height of the image channel.

img_set_width

img_set_width( $name, $channel, $width );

Set the width of the image channel.

img_set_height

img_set_height( $name, $channel, $height );

Set the height of the image channel.

img_allowed_content_types

img_allowed_content_types( $name, $channel );

Returns a list of MIME types allowed for the given video channel.

img_stream

img_stream( $name, $channel, $type );

Returns a filehandle for streaming the video channel. $type is one of the MIME types return by img_allowed_content_types().

SEE ALSO

WebIOPi, the Python project where Device::WebIO gets its inspiration: https://code.google.com/p/webiopi/

LICENSE

Copyright (c) 2014 Timm Murray All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of 
  conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
  conditions and the following disclaimer in the documentation and/or other materials 
  provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.