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.
device_names
Return a list of the names of all devices that are currently registered.
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.
Input Callback
These can be used if the device does the DigialInputCallback
role.
digital_input_callback
digital_input_callback( $name, $pin, $type, $callback );
Set a callback that will be triggered when $pin
changes state. $type
is one of the constants in the DigitalInputCallback
role, which controls when the callback is triggered--TRIGGER_RISING
, TRIGGER_FALLING
, or TRIGGER_RISING_FALLING
.
$callback
is a subref that will be called.
digital_input_begin_loop
digital_input_begin_loop( $name );
Start the loop that will trigger callbacks.
Input AnyEvent
These can be used if the device does the DigitalInputAnyEvent
role.
set_anyevent_condvar
set_anyevent_condvar( $name, $pin, $cv );
Set an AnyEvent condvar, which will trigger on both the rising and falling edge. When triggered, the condvar's callback will be called.
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()
.
Video Callback
These can be used if the device does the VideoOutputCallback
role.
vid_stream_callback
vid_stream_callback( $name, $channel, $type, $callback );
Set a callback that will be triggered when the given video channel gets a new frame. $type
is one of the MIME types returned by vid_allowed_content_types()
.
Only 1 callback per channel will be kept.
vid_stream_begin_loop
vid_stream_begin_loop( $name, $channel );
Start the loop that will trigger callbacks.
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_quality
img_quality( $name, $channel );
Return the quality 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_set_quality
img_set_quality( $name, $channel, $height );
Set the quality 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()
.
I2C
i2c_read
i2c_read( $name, $pin, $addr, $register, $num_bytes );
Read $num_bytes
bytes from the I2C register for the device on the given bus and address. Returns a list $num_bytes
long.
i2c_write
i2c_write( $name, $pin, $addr, $register, @bytes );
Write the @bytes
list of bytes to the I2C register for the device on the given bus and address.
SPI
spi_set_speed
spi_set_speed( $name, $pin, $speed );
Set the speed on the given SPI device.
spi_read
spi_read( $name, $pin, $len );
Read $len
bytes from the given SPI device. Returns an array of bytes.
spi_write
spi_write( $name, $pin, $packed_data );
Write $packed_data
to the given SPI device. This data should be a packed string For many devices, a single byte can packed using:
my $packed_data = pack 'n', $data;
For an array of bytes, try:
my $packed_data = pack 'C*', @data;
This can often be different based on the device, which is why we don't do it for you.
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.