File Coverage

blib/lib/RPi/PIGPIO.pm
Criterion Covered Total %
statement 27 189 14.2
branch 0 22 0.0
condition 0 20 0.0
subroutine 9 54 16.6
pod 43 44 97.7
total 79 329 24.0


line stmt bran cond sub pod time code
1             package RPi::PIGPIO;
2              
3             =head1 NAME
4              
5             RPi::PIGPIO - remotely control the GPIO on a RaspberryPi using the pigpiod daemon
6              
7             =head1 DESCRIPTION
8              
9             This module impements a client for the pigpiod daemon, and can be used to control
10             the GPIO on a local or remote RaspberryPi
11              
12             On every RapberryPi that you want to use you must have pigpiod daemon running!
13              
14             You can download pigpiod from here L
15              
16             =head2 SYNOPSYS
17              
18             Blink a led connecyed to GPIO17 on the RasPi connected to the network with ip address 192.168.1.10
19              
20             use RPi::PIGPIO ':all';
21              
22             my $pi = RPi::PIGPIO->connect('192.168.1.10');
23              
24             $pi->set_mode(17, PI_OUTPUT);
25              
26             $pi->write(17, HI);
27              
28             sleep 3;
29              
30             $pi->write(17, LOW);
31              
32             Easier mode to controll leds / switches :
33              
34             use RPi::PIGPIO;
35             use RPi::PIGPIO::Device::LED;
36              
37             my $pi = RPi::PIGPIO->connect('192.168.1.10');
38              
39             my $led = RPi::PIGPIO::Device::LED->new($pi,17);
40              
41             $led->on;
42              
43             sleep 3;
44              
45             $led->off;
46              
47             Same with a switch (relay):
48              
49             use RPi::PIGPIO;
50             use RPi::PIGPIO::Device::Switch;
51              
52             my $pi = RPi::PIGPIO->connect('192.168.1.10');
53              
54             my $switch = RPi::PIGPIO::Device::Switch->new($pi,4);
55              
56             $switch->on;
57              
58             sleep 3;
59              
60             $switch->off;
61              
62             Read the temperature and humidity from a DHT22 sensor connected to GPIO4
63              
64             use RPi::PIGPIO;
65             use RPi::PIGPIO::Device::DHT22;
66              
67             my $dht22 = RPi::PIGPIO::Device::DHT22->new($pi,4);
68              
69             $dht22->trigger(); #trigger a read
70              
71             print "Temperature : ".$dht22->temperature."\n";
72             print "Humidity : ".$dht22->humidity."\n";
73              
74             =head1 ALREADY IMPLEMENTED DEVICES
75              
76             Note: you can write your own code using methods implemeted here to controll your own device
77              
78             This is just a list of devices for which we already implemented some functionalities to make your life easier
79              
80             =head2 Generic LED
81              
82             See complete documentations here: L
83              
84             Usage:
85              
86             use RPi::PIGPIO;
87             use RPi::PIGPIO::Device::LED;
88              
89             my $pi = RPi::PIGPIO->connect('192.168.1.10');
90              
91             my $led = RPi::PIGPIO::Device::LED->new($pi,17);
92              
93             $led->on;
94              
95             sleep 3;
96              
97             $led->off;
98              
99              
100             =head2 Seneric switch / relay
101              
102             See complete documentations here: L
103              
104             Usage:
105              
106             use RPi::PIGPIO;
107             use RPi::PIGPIO::Device::Switch;
108              
109             my $pi = RPi::PIGPIO->connect('192.168.1.10');
110              
111             my $switch = RPi::PIGPIO::Device::Switch->new($pi,4);
112              
113             $switch->on;
114              
115             sleep 3;
116              
117             $switch->off;
118              
119             =head2 DHT22 temperature/humidity sensor
120              
121             See complete documentations here : L
122              
123             Usage:
124              
125             use RPi::PIGPIO;
126             use RPi::PIGPIO::Device::DHT22;
127              
128             my $pi = RPi::PIGPIO->connect('192.168.1.10');
129              
130             my $dht22 = RPi::PIGPIO::Device::DHT22->new($pi,4);
131              
132             $dht22->trigger(); #trigger a read
133              
134             print "Temperature : ".$dht22->temperature."\n";
135             print "Humidity : ".$dht22->humidity."\n";
136              
137              
138             =head2 BMP180 atmospheric presure/temperature sensor
139              
140             See complete documentations here : L
141              
142             Usage:
143              
144             use RPi::PIGPIO;
145             use RPi::PIGPIO::Device::BMP180;
146              
147             my $pi = RPi::PIGPIO->connect('192.168.1.10');
148              
149             my $bmp180 = RPi::PIGPIO::Device::BMP180->new($pi,1);
150              
151             $bmp180->read_sensor(); #trigger a read
152              
153             print "Temperature : ".$bmp180->temperature." C\n";
154             print "Presure : ".$bmp180->presure." mbar\n";
155              
156             =head2 DSM501A dust particle concentraction sensor
157              
158             See complete documentations here : L
159              
160             Usage:
161             use RPi::PIGPIO;
162             use RPi::PIGPIO::Device::DSM501A;
163              
164             my $pi = RPi::PIGPIO->connect('192.168.1.10');
165              
166             my $dust_sensor = RPi::PIGPIO::Device::DSM501A->new($pi,4);
167              
168             # Sample the air for 30 seconds and report
169             my ($ratio, $mg_per_m3, $pcs_per_m3, $pcs_per_ft3) = $dust_sensor->sample();
170              
171             =head2 MH-Z14 CO2 module
172              
173             See complete documentations here: L
174              
175             Usage:
176             use RPi::PIGPIO;
177             use RPi::PIGPIO::Device::MH_Z14;
178              
179             my $pi = RPi::PIGPIO->connect('192.168.1.10');
180              
181             my $co2_sensor = RPi::PIGPIO::Device::MH_Z14->new($pi,mode => 'serial', tty => '/dev/ttyAMA0');
182              
183             $ppm = $co2_sensor->read();
184              
185              
186             =head2 MCP3008/MCP3004 analog-to-digital convertor
187              
188             See complete documentations here: L
189              
190             Usage:
191             use feature 'say';
192             use RPi::PIGPIO;
193             use RPi::PIGPIO::Device::ADC::MCP300x;
194              
195             my $pi = RPi::PIGPIO->connect('192.168.1.10');
196              
197             my $mcp = RPi::PIGPIO::Device::ADC::MCP300x->new(0);
198              
199             say "Sensor 1: " .$mcp->read(0);
200             say "Sensor 2: " .$mcp->read(1);
201              
202             =back
203              
204             =cut
205              
206 2     2   145781 use strict;
  2         25  
  2         59  
207 2     2   10 use warnings;
  2         2  
  2         113  
208              
209             our $VERSION = '0.017';
210              
211 2     2   13 use Exporter 5.57 'import';
  2         54  
  2         79  
212              
213 2     2   12 use Carp;
  2         3  
  2         152  
214 2     2   1217 use IO::Socket::INET;
  2         46456  
  2         14  
215 2     2   2224 use Package::Constants;
  2         4775  
  2         109  
216              
217             use constant {
218 2         756 PI_INPUT => 0,
219             PI_OUTPUT => 1,
220             PI_ALT0 => 4,
221             PI_ALT1 => 5,
222             PI_ALT2 => 6,
223             PI_ALT3 => 7,
224             PI_ALT4 => 3,
225             PI_ALT5 => 2,
226              
227             HI => 1,
228             LOW => 0,
229              
230             RISING_EDGE => 0,
231             FALLING_EDGE => 1,
232             EITHER_EDGE => 2,
233            
234             PI_PUD_OFF => 0,
235             PI_PUD_DOWN => 1,
236             PI_PUD_UP => 2,
237 2     2   15 };
  2         3  
238              
239             use constant {
240 2         1710 PI_CMD_MODES => 0,
241             PI_CMD_MODEG => 1,
242             PI_CMD_PUD => 2,
243             PI_CMD_READ => 3,
244             PI_CMD_WRITE => 4,
245             PI_CMD_PWM => 5,
246             PI_CMD_PRS => 6,
247             PI_CMD_PFS => 7,
248             PI_CMD_SERVO => 8,
249             PI_CMD_WDOG => 9,
250             PI_CMD_BR1 => 10,
251             PI_CMD_BR2 => 11,
252             PI_CMD_BC1 => 12,
253             PI_CMD_BC2 => 13,
254             PI_CMD_BS1 => 14,
255             PI_CMD_BS2 => 15,
256             PI_CMD_TICK => 16,
257             PI_CMD_HWVER => 17,
258              
259             PI_CMD_NO => 18,
260             PI_CMD_NB => 19,
261             PI_CMD_NP => 20,
262             PI_CMD_NC => 21,
263              
264             PI_CMD_PRG => 22,
265             PI_CMD_PFG => 23,
266             PI_CMD_PRRG => 24,
267             PI_CMD_HELP => 25,
268             PI_CMD_PIGPV => 26,
269              
270             PI_CMD_WVCLR => 27,
271             PI_CMD_WVAG => 28,
272             PI_CMD_WVAS => 29,
273             PI_CMD_WVGO => 30,
274             PI_CMD_WVGOR => 31,
275             PI_CMD_WVBSY => 32,
276             PI_CMD_WVHLT => 33,
277             PI_CMD_WVSM => 34,
278             PI_CMD_WVSP => 35,
279             PI_CMD_WVSC => 36,
280              
281             PI_CMD_TRIG => 37,
282              
283             PI_CMD_PROC => 38,
284             PI_CMD_PROCD => 39,
285             PI_CMD_PROCR => 40,
286             PI_CMD_PROCS => 41,
287              
288             PI_CMD_SLRO => 42,
289             PI_CMD_SLR => 43,
290             PI_CMD_SLRC => 44,
291              
292             PI_CMD_PROCP => 45,
293             PI_CMD_MICRO => 46,
294             PI_CMD_MILLI => 47,
295             PI_CMD_PARSE => 48,
296              
297             PI_CMD_WVCRE => 49,
298             PI_CMD_WVDEL => 50,
299             PI_CMD_WVTX => 51,
300             PI_CMD_WVTXR => 52,
301             PI_CMD_WVNEW => 53,
302              
303             PI_CMD_I2CO => 54,
304             PI_CMD_I2CC => 55,
305             PI_CMD_I2CRD => 56,
306             PI_CMD_I2CWD => 57,
307             PI_CMD_I2CWQ => 58,
308             PI_CMD_I2CRS => 59,
309             PI_CMD_I2CWS => 60,
310             PI_CMD_I2CRB => 61,
311             PI_CMD_I2CWB => 62,
312             PI_CMD_I2CRW => 63,
313             PI_CMD_I2CWW => 64,
314             PI_CMD_I2CRK => 65,
315             PI_CMD_I2CWK => 66,
316             PI_CMD_I2CRI => 67,
317             PI_CMD_I2CWI => 68,
318             PI_CMD_I2CPC => 69,
319             PI_CMD_I2CPK => 70,
320              
321             PI_CMD_SPIO => 71,
322             PI_CMD_SPIC => 72,
323             PI_CMD_SPIR => 73,
324             PI_CMD_SPIW => 74,
325             PI_CMD_SPIX => 75,
326              
327             PI_CMD_SERO => 76,
328             PI_CMD_SERC => 77,
329             PI_CMD_SERRB => 78,
330             PI_CMD_SERWB => 79,
331             PI_CMD_SERR => 80,
332             PI_CMD_SERW => 81,
333             PI_CMD_SERDA => 82,
334              
335             PI_CMD_GDC => 83,
336             PI_CMD_GPW => 84,
337              
338             PI_CMD_HC => 85,
339             PI_CMD_HP => 86,
340              
341             PI_CMD_CF1 => 87,
342             PI_CMD_CF2 => 88,
343              
344             PI_CMD_NOIB => 99,
345              
346             PI_CMD_BI2CC => 89,
347             PI_CMD_BI2CO => 90,
348             PI_CMD_BI2CZ => 91,
349              
350             PI_CMD_I2CZ => 92,
351              
352             PI_CMD_WVCHA => 93,
353              
354             PI_CMD_SLRI => 94,
355              
356             PI_CMD_CGI => 95,
357             PI_CMD_CSI => 96,
358              
359             PI_CMD_FG => 97,
360             PI_CMD_FN => 98,
361              
362             PI_CMD_WVTXM => 100,
363             PI_CMD_WVTAT => 101,
364              
365             PI_CMD_PADS => 102,
366             PI_CMD_PADG => 103,
367              
368             PI_CMD_FO => 104,
369             PI_CMD_FC => 105,
370             PI_CMD_FR => 106,
371             PI_CMD_FW => 107,
372             PI_CMD_FS => 108,
373             PI_CMD_FL => 109,
374             PI_CMD_SHELL => 110,
375            
376             PI_CMD_BSPIC => 112, # bbSPIClose
377             PI_CMD_BSPIO => 134, # bbSPIOpen
378             PI_CMD_BSPIX => 193, # bbSPIXfer
379 2     2   16 };
  2         4  
380              
381              
382             # notification flags
383             use constant {
384 2         5725 NTFY_FLAGS_ALIVE => (1 << 6),
385             NTFY_FLAGS_WDOG => (1 << 5),
386             NTFY_FLAGS_GPIO => 31,
387 2     2   15 };
  2         4  
388              
389              
390             our @EXPORT_OK = Package::Constants->list( __PACKAGE__ );
391             our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
392              
393             =head1 METHODS
394              
395             =head2 connect
396              
397             Connects to the pigpiod running on the given IP address/port and returns an object
398             that will allow us to manipulate the GPIO on that Raspberry Pi
399              
400             Usage:
401              
402             my $pi = RPi::PIGPIO->connect('127.0.0.1');
403              
404             Arguments:
405              
406             =over 4
407              
408             =item * arg1: ip_address - The IP address of the pigpiod daemon
409              
410             =item * arg2: port - optional, defaults to 8888
411              
412             =back
413              
414             Note: The pigiod daemon must be running on the raspi that you want to use
415              
416             =cut
417             sub connect {
418 0     0 1   my ($class,$address,$port) = @_;
419            
420 0   0       $port ||= 8888;
421            
422 0           my $sock = IO::Socket::INET->new(
423             PeerAddr => $address,
424             PeerPort => $port,
425             Proto => 'tcp'
426             );
427              
428 0           my $pi = {
429             sock => $sock,
430             host => $address,
431             port => $port,
432             };
433            
434 0           bless $pi, $class;
435             }
436              
437              
438             =head2 connected
439              
440             Returns true is we have an established connection with the remote pigpiod daemon
441              
442             =cut
443             sub connected {
444 0     0 1   my $self = shift;
445            
446 0   0       return $self->{sock} && $self->{sock}->connected();
447             }
448              
449              
450             =head2 disconnect
451              
452             Disconnect from the gpiod daemon.
453              
454             The current object is no longer usable once we disconnect.
455              
456             =cut
457             sub disconnect {
458 0     0 1   my $self = shift;
459            
460 0           $self->prepare_for_exit();
461            
462 0           undef $_[0];
463             }
464              
465             =head2 get_mode
466              
467             Returns the mode of a given GPIO pin
468              
469             Usage :
470              
471             my $mode = $pi->get_mode(4);
472              
473             Arguments:
474              
475             =over 4
476              
477             =item * arg1: gpio - GPIO for which you want to change the mode
478              
479             =back
480              
481             Return values (constant exported by this module):
482              
483             0 = PI_INPUT
484             1 = PI_OUTPUT
485             4 = PI_ALT0
486             5 = PI_ALT1
487             6 = PI_ALT2
488             7 = PI_ALT3
489             3 = PI_ALT4
490             2 = PI_ALT5
491              
492             =cut
493             sub get_mode {
494 0     0 1   my $self = shift;
495 0           my $gpio = shift;
496            
497 0 0         die "Usage : \$pi->get_mode()" unless (defined($gpio));
498              
499 0           return $self->send_command(PI_CMD_MODEG,$gpio);
500             }
501              
502             =head2 set_mode
503              
504             Sets the GPIO mode
505              
506             Usage:
507              
508             $pi->set_mode(17, PI_OUTPUT);
509              
510             Arguments :
511              
512             =over 4
513              
514             =item * arg1: gpio - GPIO for which you want to change the mode
515              
516             =item * arg2: mode - the mode that you want to set.
517             Valid values for I are exported as constants and are : PI_INPUT, PI_OUTPUT, PI_ALT0, PI_ALT1, PI_ALT2, PI_ALT3, PI_ALT4, PI_ALT5
518              
519             =back
520              
521             Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_MODE, or PI_NOT_PERMITTED.
522              
523             =cut
524              
525             sub set_mode {
526 0     0 1   my ($self,$gpio,$mode) = @_;
527            
528 0 0 0       die "Usage : \$pi->set_mode(, )" unless (defined($gpio) && defined($mode));
529            
530 0           return $self->send_command(PI_CMD_MODES,$gpio,$mode);
531             }
532              
533             =head2 write
534              
535             Sets the voltage level on a GPIO pin to HI or LOW
536              
537             Note: You first must set the pin mode to PI_OUTPUT
538              
539             Usage :
540              
541             $pi->write(17, HI);
542             or
543             $pi->write(17, LOW);
544              
545             Arguments:
546              
547             =over 4
548              
549             =item * arg1: gpio - GPIO to witch you want to write
550              
551             =item * arg2: level - The voltage level that you want to write (one of HI or LOW)
552              
553             =back
554              
555             Note: This method will set the GPIO mode to "OUTPUT" and leave it like this
556              
557             =cut
558             sub write {
559 0     0 1   my ($self,$gpio,$level) = @_;
560            
561 0           return $self->send_command(PI_CMD_WRITE,$gpio,$level);
562             }
563              
564              
565             =head2 read
566              
567             Gets the voltage level on a GPIO
568              
569             Note: You first must set the pin mode to PI_INPUT
570              
571             Usage :
572              
573             $pi->read(17);
574              
575             Arguments:
576              
577             =over 4
578              
579             =item * arg1: gpio - gpio that you want to read
580              
581             =back
582              
583             Note: This method will set the GPIO mode to "INPUT" and leave it like this
584              
585             =cut
586             sub read {
587 0     0 1   my ($self,$gpio) = @_;
588              
589 0           return $self->send_command(PI_CMD_READ,$gpio);
590             }
591              
592             =head2 set_watchdog
593              
594             If no level change has been detected for the GPIO for timeout milliseconds any notification
595             for the GPIO has a report written to the fifo with the flags set to indicate a watchdog timeout.
596              
597             Arguments:
598              
599             =over 4
600              
601             =item * arg1: gpio - GPIO for which to set the watchdog
602              
603             =item * arg2. timeout - time to wait for a level change in milliseconds.
604              
605             =back
606              
607             Only one watchdog may be registered per GPIO.
608              
609             The watchdog may be cancelled by setting timeout to 0.
610              
611             NOTE: This method requires another connection to be created and subcribed to
612             notifications for this GPIO (see DHT22 implementation)
613              
614             =cut
615             sub set_watchdog {
616 0     0 1   my ($self,$gpio,$timeout) = @_;
617            
618 0           $self->send_command( PI_CMD_WDOG, $gpio, $timeout);
619             }
620              
621              
622             =head2 set_pull_up_down
623              
624             Set or clear the GPIO pull-up/down resistor.
625              
626             =over 4
627              
628             =item * arg1: gpio - GPIO for which we want to modify the pull-up/down resistor
629              
630             =item * arg2: level - PI_PUD_UP, PI_PUD_DOWN, PI_PUD_OFF.
631              
632             =back
633              
634             Usage:
635              
636             $pi->set_pull_up_down(18, PI_PUD_DOWN);
637              
638             =cut
639             sub set_pull_up_down {
640 0     0 1   my ($self,$gpio,$level) = @_;
641            
642 0           $self->send_command(PI_CMD_PUD, $gpio, $level);
643             }
644              
645              
646             =head2 gpio_trigger
647              
648             This function sends a trigger pulse to a GPIO. The GPIO is set to level for pulseLen microseconds and then reset to not level.
649              
650             Arguments (in this order):
651              
652             =over 4
653              
654             =item * arg1: gpio - number of the GPIO pin we want to monitor
655              
656             =item * arg2: length - pulse length in microseconds
657              
658             =item * arg3: level - level to use for the trigger (HI or LOW)
659              
660             =back
661              
662             Usage:
663            
664             $pi->gpio_trigger(4,17,LOW);
665              
666             Note: After running you call this method the GPIO is left in "INPUT" mode
667              
668             =cut
669             sub gpio_trigger {
670 0     0 1   my ($self,$gpio,$length,$level) = @_;
671            
672 0           $self->send_command_ext(PI_CMD_TRIG, $gpio, $length, [ $level ]);
673             }
674              
675             =head2 SPI interface
676              
677             =head3 spi_open
678              
679             Comunication is done via harware SPI so MAKE SURE YOU ENABLED SPI on your RPi (use raspi-config command and go to "Advanced")
680              
681             Returns a handle for the SPI device on channel. Data will be
682             transferred at baud bits per second. The flags may be used to
683             modify the default behaviour of 4-wire operation, mode 0,
684             active low chip select.
685              
686             An auxiliary SPI device is available on all models but the
687             A and B and may be selected by setting the A bit in the
688             flags. The auxiliary device has 3 chip selects and a
689             selectable word size in bits.
690              
691             Arguments:
692              
693             =over 4
694              
695             =item * arg1: spi_channel:= 0-1 (0-2 for the auxiliary SPI device).
696              
697             =item * arg2: baud:= 32K-125M (values above 30M are unlikely to work).
698              
699             =item * arg3: spi_flags:= see below.
700              
701             =back
702              
703             spi_flags consists of the least significant 22 bits.
704              
705             21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
706             b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m
707              
708             mm defines the SPI mode.
709              
710             WARNING: modes 1 and 3 do not appear to work on
711             the auxiliary device.
712              
713             Mode POL PHA
714             0 0 0
715             1 0 1
716             2 1 0
717             3 1 1
718              
719             px is 0 if CEx is active low (default) and 1 for active high.
720              
721             ux is 0 if the CEx GPIO is reserved for SPI (default)
722             and 1 otherwise.
723              
724             A is 0 for the standard SPI device, 1 for the auxiliary SPI.
725              
726             W is 0 if the device is not 3-wire, 1 if the device is 3-wire.
727             Standard SPI device only.
728              
729             nnnn defines the number of bytes (0-15) to write before
730             switching the MOSI line to MISO to read data. This field
731             is ignored if W is not set. Standard SPI device only.
732              
733             T is 1 if the least significant bit is transmitted on MOSI
734             first, the default (0) shifts the most significant bit out
735             first. Auxiliary SPI device only.
736              
737             R is 1 if the least significant bit is received on MISO
738             first, the default (0) receives the most significant bit
739             first. Auxiliary SPI device only.
740              
741             bbbbbb defines the word size in bits (0-32). The default (0)
742             sets 8 bits per word. Auxiliary SPI device only.
743              
744             The C, C, and C functions
745             transfer data packed into 1, 2, or 4 bytes according to
746             the word size in bits.
747              
748             For bits 1-8 there will be one byte per character.
749             For bits 9-16 there will be two bytes per character.
750             For bits 17-32 there will be four bytes per character.
751              
752             E.g. 32 12-bit words will be transferred in 64 bytes.
753              
754             The other bits in flags should be set to zero.
755              
756              
757             Example: open SPI device on channel 1 in mode 3 at 50k bits per second
758              
759             my $spi_handle = $pi->spi_open(1, 50_000, 3);
760              
761             =cut
762             sub spi_open {
763 0     0 1   my ($self, $spi_channel, $baud, $spi_flags) = @_;
764            
765 0   0       $spi_flags //= 0;
766            
767 0           return $self->send_command_ext(PI_CMD_SPIO, $spi_channel, $baud, [ $spi_flags ]);
768             }
769              
770              
771             =head3 spi_close
772              
773             Closes an SPI channel
774              
775             Usage :
776              
777             my $spi = $pi->spi_open(0,32_000);
778             ...
779             $pi->spi_close($spi);
780              
781             =cut
782             sub spi_close {
783 0     0 1   my ($self, $handle) = @_;
784            
785 0           return $self->send_command(PI_CMD_SPIC, $handle, 0);
786             }
787            
788              
789             =head3 spi_read
790              
791             Arguments (in this order):
792              
793             =over 4
794              
795             =item * arg1: handle= >=0 (as returned by a prior call to C).
796              
797             =item * arg2: count= >0, the number of bytes to read.
798              
799             =back
800              
801             The returned value is a bytearray containing the bytes.
802              
803             Usage:
804              
805             my $spi_handle = $pi->spi_open(1, 50_000, 3);
806              
807             my $data = $pi->spi_read($spi_handle, 12);
808              
809             $pi->spi_close($spi_handle);
810              
811             =cut
812             sub spi_read {
813 0     0 1   my ($self, $handle, $count) = @_;
814            
815 0           $self->send_command(PI_CMD_SPIR, $handle, $count);
816            
817 0           my $response;
818            
819 0           $self->{sock}->recv($response,3);
820            
821 0           return $response;
822             }
823              
824              
825             =head3 spi_write
826              
827             Writes the data bytes to the SPI device associated with handle.
828              
829             Arguments (in this order):
830              
831             =over 4
832              
833             =item * arg1: handle:= >=0 (as returned by a prior call to C).
834              
835             =item * arg2: data:= the bytes to write.
836              
837             =back
838              
839             Examples :
840              
841             my $spi_handle = $pi->spi_open(1, 50_000, 3);
842              
843             $pi->spi_write($spi_handle, [2, 192, 128]); # write 3 bytes to device 1
844              
845             =cut
846             sub spi_write {
847 0     0 1   my ($self, $handle, $data) = @_;
848            
849 0 0         if (! ref($data) ) {
850 0           $data = [ map {ord} split("",$data) ];
  0            
851             }
852            
853 0           return $self->send_command_ext(PI_CMD_SPIW, $handle, 0, $data);
854             }
855              
856             =head3 spi_xfer
857              
858             Writes the data bytes to the SPI device associated with handle,
859             returning the data bytes read from the device.
860              
861             Arguments (in this order):
862              
863             =over 4
864              
865             =item * arg1: handle= >=0 (as returned by a prior call to C).
866              
867             =item * arg2: data= the bytes to write.
868              
869             =back
870              
871             The returned value is a bytearray containing the bytes.
872              
873             Examples :
874              
875             my $spi_handle = $pi->spi_open(1, 50_000, 3);
876              
877             my $rx_data = $pi->spi_xfer($spi_handle, [1, 128, 0]);
878              
879             =cut
880             sub spi_xfer {
881 0     0 1   my ($self, $handle, $data) = @_;
882            
883 0 0         if (! ref($data) ) {
884 0           $data = [ map {ord} split("",$data) ];
  0            
885             }
886            
887 0           my $bytes = $self->send_command_ext(PI_CMD_SPIX, $handle, 0, $data);
888            
889 0           my $response;
890            
891 0           $self->{sock}->recv($response,$bytes);
892            
893 0           return $response;
894             }
895              
896             =head2 Serial interface
897              
898             =head3 serial_open
899              
900             Returns a handle for the serial tty device opened
901             at baud bits per second. The device name must start
902             with /dev/tty or /dev/serial.
903              
904             Arguments :
905              
906             =over 4
907              
908             =item * arg1 : tty => the serial device to open.
909              
910             =item * arg2 : baud => baud rate in bits per second, see below.
911              
912             =back
913              
914             Returns: a handle for the serial connection which will be used
915             in calls to C methods
916              
917             The baud rate must be one of 50, 75, 110, 134, 150,
918             200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200,
919             38400, 57600, 115200, or 230400.
920              
921             Notes: On the raspi on which you want to use the serial device you have to :
922              
923             =over 4
924              
925             =item 1 enable UART -> run C and add the bottom C
926              
927             =item 2 run C and disable the login via the Serial port
928              
929             =back
930              
931             More info (usefull for Raspi 3) here : L
932              
933             Usage:
934              
935             $h1 = $pi->serial_open("/dev/ttyAMA0", 300)
936              
937             $h2 = $pi->serial_open("/dev/ttyUSB1", 19200, 0)
938              
939             $h3 = $pi->serial_open("/dev/serial0", 9600)
940              
941             =cut
942             sub serial_open {
943 0     0 1   my ($self,$tty,$baud) = @_;
944            
945 0           my $sock = $self->{sock};
946              
947 0           my $msg = pack('IIII', PI_CMD_SERO, $baud, 0, length($tty));
948            
949 0           $sock->send($msg);
950 0           $sock->send($tty);
951            
952 0           my $response;
953            
954 0           $sock->recv($response,16);
955            
956 0           my ($x, $val) = unpack('a[12] I', $response);
957              
958 0           return $val;
959             }
960              
961              
962             =head3 serial_close
963              
964             Closes the serial device associated with handle.
965              
966             Arguments:
967              
968             =over 4
969              
970             =item * arg1: handle => the connection as returned by a prior call to C
971              
972             =back
973              
974             Usage:
975              
976             $pi->serial_close($handle);
977              
978             =cut
979             sub serial_close {
980 0     0 1   my ($self,$handle) = @_;
981            
982 0           return $self->send_command(PI_CMD_SERC,$handle);
983             }
984              
985             =head3 serial_write
986              
987             Write a string to the serial handle opened with C
988              
989             Arguments:
990              
991             =over 4
992              
993             =item * arg1: handle => connection handle obtained from calling C
994              
995             =item * arg2: data => data to write (string)
996              
997             =back
998              
999             Usage :
1000              
1001             my $h = $pi->serial_open('/dev/ttyAMA0',9600);
1002              
1003             my $data = 'foo bar';
1004              
1005             $pi->serial_write($h, $data);
1006              
1007             $pi->serial_close($h);
1008              
1009             =cut
1010             sub serial_write {
1011 0     0 1   my ($self,$handle,$data) = @_;
1012            
1013 0           my $sock = $self->{sock};
1014            
1015 0           my $msg = pack('IIII', PI_CMD_SERW, $handle, 0, length($data));
1016            
1017 0           $sock->send($msg);
1018 0           $sock->send($data);
1019            
1020 0           my $response;
1021            
1022 0           $sock->recv($response,16);
1023            
1024 0           my ($x, $val) = unpack('a[12] I', $response);
1025             }
1026              
1027             =head3 serial_read
1028              
1029             Read a string from the serial handle opened with C
1030              
1031             Arguments:
1032              
1033             =over 4
1034              
1035             =item * arg1: handle => connection handle obtained from calling C
1036              
1037             =item * arg2: count => number of bytes to read
1038              
1039             =back
1040              
1041             Usage :
1042              
1043             my $h = $pi->serial_open('/dev/ttyAMA0',9600);
1044              
1045             my $data = $pi->serial_read($h, 10); #read 10 bytes
1046              
1047             $pi->serial_close($h);
1048              
1049             Note: Between a read and a write you might want to sleep for half a second
1050              
1051             =cut
1052             sub serial_read {
1053 0     0 1   my ($self,$handle,$count) = @_;
1054            
1055 0           my $bytes = $self->send_command(PI_CMD_SERR, $handle, $count);
1056            
1057 0           my $response;
1058            
1059 0           $self->{sock}->recv($response,$count);
1060            
1061 0           return $response;
1062             }
1063              
1064             =head3 serial_data_available
1065              
1066             Checks if we have any data waiting to be read from the serial handle
1067              
1068             Usage :
1069              
1070             my $h = $pi->serial_open('/dev/ttyAMA0',9600);
1071              
1072             my $count = $pi->serial_data_available($h);
1073              
1074             my $data = $pi->serial_read($h, $count);
1075              
1076             $pi->serial_close($h);
1077              
1078             =cut
1079             sub serial_data_available {
1080 0     0 1   my ($self,$handle) = @_;
1081            
1082 0           return $self->send_command(PI_CMD_SERDA, $handle);
1083             }
1084              
1085             =head2 I2C interface
1086              
1087             =head3 i2c_open
1088              
1089             Returns a handle (>=0) for the device at the I2C bus address.
1090              
1091             Arguments:
1092              
1093             =over 4
1094              
1095             =item * i2c_bus: >=0 the i2c bus number
1096              
1097             =item * i2c_address: 0-0x7F => the address of the device on the bus
1098              
1099             =item * i2c_flags: defaults to 0, no flags are currently defined (optional).
1100              
1101             =back
1102              
1103             Physically buses 0 and 1 are available on the Pi. Higher
1104             numbered buses will be available if a kernel supported bus
1105             multiplexor is being used.
1106              
1107             Usage :
1108            
1109             my $handle = $pi->i2c_open(1, 0x53) # open device at address 0x53 on bus 1
1110              
1111             =cut
1112             sub i2c_open {
1113 0     0 1   my ($self, $i2c_bus, $i2c_address, $i2c_flags) = @_;
1114            
1115 0   0       $i2c_flags ||= 0;
1116            
1117 0           return $self->send_command_ext(PI_CMD_I2CO, $i2c_bus, $i2c_address, [$i2c_flags]);
1118             }
1119              
1120             =head3 i2c_close
1121              
1122             Closes the I2C device associated with handle.
1123              
1124             Arguments:
1125              
1126             =over 4
1127              
1128             =item * handle: >=0 ( as returned by a prior call to C )
1129              
1130             =back
1131              
1132             =cut
1133             sub i2c_close {
1134 0     0 1   my ($self, $handle) = @_;
1135              
1136 0           return $self->send_command(PI_CMD_I2CC, $handle, 0);
1137             }
1138              
1139             =head3 i2c_write_quick
1140              
1141             Sends a single bit to the device associated with handle.
1142              
1143             Arguments:
1144              
1145             =over 4
1146              
1147             =item * handle: >=0 ( as returned by a prior call to C )
1148              
1149             =item * bit: 0 or 1, the value to write.
1150              
1151             =back
1152              
1153             Usage:
1154              
1155             $pi->i2c_write_quick(0, 1) # send 1 to device 0
1156             $pi->i2c_write_quick(3, 0) # send 0 to device 3
1157              
1158             =cut
1159             sub i2c_write_quick {
1160 0     0 1   my ($self, $handle, $bit) = @_;
1161            
1162 0           return $self->send_command(PI_CMD_I2CWQ, $handle, $bit);
1163             }
1164              
1165             =head3 i2c_write_byte
1166              
1167             Sends a single byte to the device associated with handle.
1168              
1169             =over 4
1170              
1171             =item * handle: >=0 ( as returned by a prior call to C )
1172              
1173             =item * byte_val: 0-255, the value to write.
1174              
1175             =back
1176              
1177             Usage:
1178              
1179             $pi->i2c_write_byte(1, 17) # send byte 17 to device 1
1180             $pi->i2c_write_byte(2, 0x23) # send byte 0x23 to device 2
1181              
1182             =cut
1183             sub i2c_write_byte {
1184 0     0 1   my ($self, $handle, $byte_val) = @_;
1185            
1186 0           return $self->send_command(PI_CMD_I2CWS, $handle, $byte_val);
1187             }
1188              
1189             =head3 i2c_read_byte
1190              
1191             Reads a single byte from the device associated with handle.
1192              
1193             Arguments:
1194              
1195             =over 4
1196              
1197             =item * handle: >=0 ( as returned by a prior call to C )
1198              
1199             =back
1200              
1201             Usage:
1202              
1203             my $val = $pi->i2c_read_byte(2) # read a byte from device 2
1204              
1205             =cut
1206             sub i2c_read_byte {
1207 0     0 1   my ($self, $handle) = @_;
1208            
1209 0           return $self->send_command(PI_CMD_I2CRS, $handle, 0);
1210             }
1211              
1212             =head3 i2c_write_byte_data
1213              
1214             Writes a single byte to the specified register of the device associated with handle.
1215              
1216             Arguments:
1217              
1218             =over 4
1219              
1220             =item * handle: >=0 ( as returned by a prior call to C )
1221              
1222             =item * reg: >=0, the device register.
1223              
1224             =item * byte_val: 0-255, the value to write.
1225              
1226             =back
1227              
1228             Usage:
1229              
1230             # send byte 0xC5 to reg 2 of device 1
1231             $pi->i2c_write_byte_data(1, 2, 0xC5);
1232              
1233             # send byte 9 to reg 4 of device 2
1234             $pi->i2c_write_byte_data(2, 4, 9);
1235              
1236             =cut
1237             sub i2c_write_byte_data {
1238 0     0 1   my ($self, $handle, $reg, $byte_val) = @_;
1239            
1240 0           $self->send_command_ext(PI_CMD_I2CWB, $handle, $reg, [$byte_val]);
1241             }
1242              
1243              
1244             =head3 i2c_write_word_data
1245              
1246             Writes a single 16 bit word to the specified register of the device associated with handle.
1247              
1248             Arguments:
1249              
1250             =over 4
1251              
1252             =item * handle: >=0 ( as returned by a prior call to C )
1253              
1254             =item * reg: >=0, the device register.
1255              
1256             =item * word_val: 0-65535, the value to write.
1257              
1258             =back
1259              
1260             Usage:
1261              
1262             # send word 0xA0C5 to reg 5 of device 4
1263             $pi->i2c_write_word_data(4, 5, 0xA0C5);
1264              
1265             # send word 2 to reg 2 of device 5
1266             $pi->i2c_write_word_data(5, 2, 23);
1267              
1268             =cut
1269             sub i2c_write_word_data {
1270 0     0 1   my ($self, $handle, $reg, $word_val) = @_;
1271            
1272 0           return $self->send_command_ext(PI_CMD_I2CWW, $handle, $reg, [$word_val]);
1273             }
1274              
1275             =head3 i2c_read_byte_data
1276              
1277             Reads a single byte from the specified register of the device associated with handle.
1278              
1279             Arguments:
1280              
1281             =over 4
1282              
1283             =item * handle: >=0 ( as returned by a prior call to C )
1284              
1285             =item * reg: >=0, the device register.
1286              
1287             =back
1288              
1289             Usage:
1290             # read byte from reg 17 of device 2
1291             my $b = $pi->i2c_read_byte_data(2, 17);
1292              
1293             # read byte from reg 1 of device 0
1294             my $b = pi->i2c_read_byte_data(0, 1);
1295              
1296             =cut
1297             sub i2c_read_byte_data {
1298 0     0 1   my ($self, $handle, $reg) = @_;
1299              
1300 0           return $self->send_command(PI_CMD_I2CRB, $handle, $reg);
1301             }
1302              
1303             =head3 i2c_read_word_data
1304              
1305             Reads a single 16 bit word from the specified register of the device associated with handle.
1306              
1307             Arguments:
1308              
1309             =over 4
1310              
1311             =item * handle: >=0 ( as returned by a prior call to C )
1312              
1313             =item * reg: >=0, the device register.
1314              
1315             =back
1316              
1317             Usage:
1318             # read byte from reg 17 of device 2
1319             my $w = $pi->i2c_read_word_data(2, 17);
1320              
1321             # read byte from reg 1 of device 0
1322             my $w = pi->i2c_read_word_data(0, 1);
1323              
1324             =cut
1325             sub i2c_read_word_data {
1326 0     0 1   my ($self, $handle, $reg) = @_;
1327              
1328 0           return $self->send_command(PI_CMD_I2CRW, $handle, $reg);
1329             }
1330              
1331             =head3 i2c_process_call
1332              
1333             Writes 16 bits of data to the specified register of the device associated with handle and reads 16 bits of data in return.
1334              
1335             Arguments:
1336              
1337             =over 4
1338              
1339             =item * handle: >=0 ( as returned by a prior call to C )
1340              
1341             =item * reg: >=0, the device register.
1342              
1343             =item * word_val: 0-65535, the value to write.
1344              
1345             =back
1346              
1347             Usage:
1348              
1349             my $r = $pi->i2c_process_call(1, 4, 0x1231);
1350            
1351             my $r = $pi->i2c_process_call(2, 6, 0);
1352              
1353             =cut
1354             sub i2c_process_call {
1355 0     0 1   my ($self, $handle, $reg, $word_val) = @_;
1356              
1357 0           return $self->send_command_ext(PI_CMD_I2CPC, $handle, $reg, [$word_val]);
1358             }
1359              
1360              
1361             =head3 i2c_write_block_data
1362              
1363             Writes up to 32 bytes to the specified register of the device associated with handle.
1364              
1365             Arguments:
1366              
1367             =over 4
1368              
1369             =item * handle: >=0 ( as returned by a prior call to C )
1370              
1371             =item * reg: >=0, the device register.
1372              
1373             =item * data: arrayref of bytes to write
1374              
1375             =back
1376              
1377             Usage:
1378              
1379             $pi->i2c_write_block_data(6, 2, [0, 1, 0x22]);
1380              
1381             =cut
1382             sub i2c_write_block_data {
1383 0     0 1   my ($self, $handle, $reg, $data) = @_;
1384            
1385 0 0         return 0 unless $data;
1386            
1387 0 0         croak "data needs to be an arrayref" unless (ref $data eq "ARRAY");
1388            
1389 0           return $self->send_command_ext(PI_CMD_I2CWK, $handle, $reg, $data);
1390             }
1391              
1392             =head3 i2c_read_block_data
1393              
1394             Reads a block of up to 32 bytes from the specified register of the device associated with handle.
1395              
1396             Arguments:
1397              
1398             =over 4
1399              
1400             =item * handle: >=0 ( as returned by a prior call to C )
1401              
1402             =item * reg: >=0, the device register.
1403              
1404             =back
1405              
1406             The amount of returned data is set by the device.
1407              
1408             The returned value is a tuple of the number of bytes read and a bytearray containing the bytes.
1409             If there was an error the number of bytes read will be less than zero (and will contain the error code).
1410              
1411             Usage:
1412              
1413             my ($bytes,$data) = $pi->i2c_read_block_data($handle, 10);
1414              
1415             =cut
1416             sub i2c_read_block_data {
1417 0     0 1   my ($self, $handle, $reg) = @_;
1418            
1419 0           my ($bytes, $data) = $self->send_i2c_command(PI_CMD_I2CRK, $handle, $reg);
1420            
1421 0           return ($bytes, $data);
1422             }
1423              
1424             =head3 i2c_block_process_call
1425              
1426             Writes data bytes to the specified register of the device associated with handle and
1427             reads a device specified number of bytes of data in return.
1428              
1429             Arguments:
1430              
1431             =over 4
1432              
1433             =item * handle: >=0 ( as returned by a prior call to C )
1434              
1435             =item * reg: >=0, the device register.
1436              
1437             =item * data: arrayref of bytes to write
1438              
1439             =back
1440              
1441             Usage:
1442              
1443             my ($bytes,$data) = $pi->i2c_block_process_call($handle, 10, [2, 5, 16]);
1444            
1445             The returned value is a tuple of the number of bytes read and a bytearray containing the bytes.
1446              
1447             If there was an error the number of bytes read will be less than zero (and will contain the error code).
1448              
1449             =cut
1450             sub i2c_block_process_call {
1451 0     0 1   my ($self, $handle, $reg, $data) = @_;
1452            
1453 0           my ($bytes, $recv_data) = $self->send_i2c_command(PI_CMD_I2CPK, $handle, $reg, [$data]);
1454            
1455 0           return ($bytes, $recv_data);
1456             }
1457              
1458             =head3 i2c_write_i2c_block_data
1459              
1460             Writes data bytes to the specified register of the device associated with handle.
1461             1-32 bytes may be written.
1462              
1463             Arguments:
1464              
1465             =over 4
1466              
1467             =item * handle: >=0 ( as returned by a prior call to C )
1468              
1469             =item * reg: >=0, the device register.
1470              
1471             =item * data: arrayref of bytes to write
1472              
1473             =back
1474              
1475             Usage:
1476              
1477             $pi->i2c_write_i2c_block_data(6, 2, [0, 1, 0x22]);
1478              
1479             =cut
1480             sub i2c_write_i2c_block_data {
1481 0     0 1   my ($self, $handle, $reg, $data) = @_;
1482            
1483 0           return $self->send_command_ext(PI_CMD_I2CWI, $handle, $reg, $data);
1484             }
1485              
1486             =head3 i2c_read_i2c_block_data
1487              
1488             Reads count bytes from the specified register of the device associated with handle.
1489             The count may be 1-32.
1490              
1491             Arguments:
1492              
1493             =over 4
1494              
1495             =item * handle: >=0 ( as returned by a prior call to C )
1496              
1497             =item * reg: >=0, the device register.
1498              
1499             =item * count: >0, the number of bytes to read (1-32).
1500              
1501             =back
1502              
1503             Usage:
1504              
1505             my ($bytes, $data) = $pi->i2c_read_i2c_block_data($handle, 4, 32);
1506              
1507             The returned value is a tuple of the number of bytes read and a bytearray containing the bytes.
1508             If there was an error the number of bytes read will be less than zero (and will contain the error code).
1509              
1510             =cut
1511             sub i2c_read_i2c_block_data {
1512 0     0 1   my ($self, $handle, $reg, $count) = @_;
1513            
1514 0           my ($bytes, $data) = $self->send_i2c_command(PI_CMD_I2CRI, $handle, $reg, [$count]);
1515            
1516 0           return ($bytes, $data);
1517             }
1518              
1519             =head3 i2c_read_device
1520              
1521             Returns count bytes read from the raw device associated with handle.
1522              
1523             Arguments:
1524              
1525             =over 4
1526              
1527             =item * handle: >=0 ( as returned by a prior call to C )
1528              
1529             =item * count: >0, the number of bytes to read (1-32).
1530              
1531             =back
1532              
1533             Usage:
1534              
1535             my ($count, $data) = $pi->i2c_read_device($handle, 12);
1536              
1537             =cut
1538             sub i2c_read_device {
1539 0     0 1   my ($self, $handle, $count) = @_;
1540            
1541 0           my ($bytes, $data) = $self->send_i2c_command(PI_CMD_I2CRD, $handle, $count);
1542              
1543 0           return ($bytes, $data);
1544             }
1545              
1546             =head3 i2c_write_device
1547              
1548             Writes the data bytes to the raw device associated with handle.
1549              
1550             Arguments:
1551              
1552             =over 4
1553              
1554             =item * handle: >=0 ( as returned by a prior call to C )
1555              
1556             =item * data: arrayref of bytes to write
1557              
1558             =back
1559              
1560             Usage:
1561              
1562             $pi->i2c_write_device($handle, [23, 56, 231]);
1563              
1564             =cut
1565             sub i2c_write_device {
1566 0     0 1   my ($self, $handle, $data) = @_;
1567            
1568 0           return $self->send_command_ext(PI_CMD_I2CWD, $handle, 0, $data);
1569             }
1570              
1571              
1572             =head3 i2c_zip
1573              
1574             This function executes a sequence of I2C operations.
1575             The operations to be performed are specified by the contents of data which contains the concatenated command codes and associated data.
1576              
1577             Arguments:
1578              
1579             =over 4
1580              
1581             =item * handle: >=0 ( as returned by a prior call to C )
1582              
1583             =item * data: arrayref of the concatenated I2C commands, see below
1584              
1585             =back
1586              
1587             The returned value is a tuple of the number of bytes read and a bytearray containing the bytes.
1588             If there was an error the number of bytes read will be less than zero (and will contain the error code).
1589              
1590             Usage:
1591              
1592             my ($count, $data) = $pi->i2c_zip($handle, [4, 0x53, 7, 1, 0x32, 6, 6, 0])
1593              
1594             The following command codes are supported:
1595              
1596             Name @ Cmd & Data @ Meaning
1597             End @ 0 @ No more commands
1598             Escape @ 1 @ Next P is two bytes
1599             On @ 2 @ Switch combined flag on
1600             Off @ 3 @ Switch combined flag off
1601             Address @ 4 P @ Set I2C address to P
1602             Flags @ 5 lsb msb @ Set I2C flags to lsb + (msb << 8)
1603             Read @ 6 P @ Read P bytes of data
1604             Write @ 7 P ... @ Write P bytes of data
1605              
1606             The address, read, and write commands take a parameter P. Normally P is one byte (0-255).
1607             If the command is preceded by the Escape command then P is two bytes (0-65535, least significant byte first).
1608              
1609             The address defaults to that associated with the handle.
1610             The flags default to 0. The address and flags maintain their previous value until updated.
1611              
1612             Any read I2C data is concatenated in the returned bytearray.
1613              
1614             Set address 0x53, write 0x32, read 6 bytes
1615             Set address 0x1E, write 0x03, read 6 bytes
1616             Set address 0x68, write 0x1B, read 8 bytes
1617             End
1618              
1619             0x04 0x53 0x07 0x01 0x32 0x06 0x06
1620             0x04 0x1E 0x07 0x01 0x03 0x06 0x06
1621             0x04 0x68 0x07 0x01 0x1B 0x06 0x08
1622             0x00
1623              
1624             =cut
1625             sub i2c_zip {
1626 0     0 1   my ($self, $handle, $commands) = @_;
1627            
1628 0           my ($bytes, $data) = $self->send_i2c_command(PI_CMD_I2CZ, $handle, 0, $commands);
1629            
1630 0           return ($bytes, $data);
1631             }
1632              
1633              
1634             =head2 PWM
1635              
1636             =head3 write_pwm
1637              
1638             Sets the voltage level on a GPIO pin to a value from 0-255 (PWM)
1639             approximating a lower voltage. Useful for dimming LEDs for example.
1640              
1641             Note: You first must set the pin mode to PI_OUTPUT
1642              
1643             Usage :
1644              
1645             $pi->write_pwm(17, 255);
1646             or
1647             $pi->write_pwm(17, 120);
1648             or
1649             $pi->write_pwm(17, 0);
1650              
1651             Arguments:
1652              
1653             =over 4
1654              
1655             =item * arg1: gpio - GPIO to which you want to write
1656              
1657             =item * arg2: level - The voltage level that you want to write (one of 0-255)
1658              
1659             =back
1660              
1661             Note: This method will set the GPIO mode to "OUTPUT" and leave it like this
1662              
1663             =cut
1664             sub write_pwm {
1665 0     0 1   my ($self,$gpio,$level) = @_;
1666              
1667 0           return $self->send_command(PI_CMD_PWM,$gpio,$level);
1668             }
1669              
1670             ################################################################################################################################
1671              
1672             =head1 PRIVATE METHODS
1673              
1674             =cut
1675              
1676             =head2 send_command
1677              
1678             Sends a command to the pigiod daemon and waits for a response
1679              
1680             =over 4
1681              
1682             =item * arg1: command - code of the command you want to send (see package constants)
1683              
1684             =item * arg2: param1 - first parameter (usualy the GPIO)
1685              
1686             =item * arg3: param2 - second parameter - optional - usualy the level to which to set the GPIO (HI/LOW)
1687              
1688             =back
1689              
1690             =cut
1691             sub send_command {
1692 0     0 1   my $self = shift;
1693            
1694 0 0         if (! $self->{sock}->connected) {
1695             $self->{sock} = IO::Socket::INET->new(
1696             PeerAddr => $self->{address},
1697             PeerPort => $self->{port},
1698 0           Proto => 'tcp'
1699             );
1700             }
1701            
1702 0 0         return unless $self->{sock};
1703            
1704 0           return $self->send_command_on_socket($self->{sock},@_);
1705             }
1706              
1707             =head2 send_command_on_socket
1708              
1709             Same as C but allows you to specify the socket you want to use
1710              
1711             The pourpose of this is to allow you to use the send_command functionality on secondary
1712             connections used to monitor changes on GPIO
1713              
1714             Arguments:
1715              
1716             =over 4
1717              
1718             =item * arg1: socket - Instance of L
1719              
1720             =item * arg2: command - code of the command you want to send (see package constants)
1721              
1722             =item * arg3: param1 - first parameter (usualy the GPIO)
1723              
1724             =item * arg3: param2 - second parameter - optional - usualy the level to which to set the GPIO (HI/LOW)
1725              
1726             =back
1727              
1728             =cut
1729             sub send_command_on_socket {
1730 0     0 1   my ($self, $sock, $cmd, $param1, $param2) = @_;
1731            
1732 0   0       $param2 //= 0;
1733            
1734 0           my $msg = pack('IIII', $cmd, $param1, $param2, 0);
1735            
1736 0           $sock->send($msg);
1737            
1738 0           my $response;
1739            
1740 0           $sock->recv($response,16);
1741            
1742 0           my ($x, $val) = unpack('a[12] I', $response);
1743              
1744 0           return $val;
1745             }
1746              
1747              
1748             =head2 send_command_ext
1749              
1750             Sends an I to the pigpiod daemon
1751              
1752             =cut
1753             sub send_command_ext {
1754 0     0 1   my ($self,$cmd,$param1,$param2,$extra_params) = @_;
1755            
1756 0           my $sock;
1757 0 0         if (ref($self) ne "IO::Socket::INET") {
1758 0           $sock = $self->{sock};
1759             }
1760             else {
1761 0           $sock = $self;
1762             }
1763            
1764 0   0       my $msg = pack('IIII', $cmd, $param1, $param2, 4 * scalar(@{$extra_params // []}));
  0            
1765            
1766 0           $sock->send($msg);
1767            
1768 0   0       foreach (@{$extra_params // []}) {
  0            
1769 0           $sock->send(pack("I",$_));
1770             }
1771            
1772 0           my $response;
1773            
1774 0           $sock->recv($response,16);
1775            
1776 0           my ($x, $val) = unpack('a[12] I', $response);
1777              
1778 0           return $val;
1779             }
1780              
1781             =head2 send_i2c_command
1782              
1783             Method used for sending and reading back i2c data
1784              
1785             =cut
1786             sub send_i2c_command {
1787 0     0 1   my ($self, $command, $handle, $reg, $data) = @_;
1788            
1789 0   0       $data //= [];
1790            
1791 0           my $bytes = $self->send_command_ext($command, $handle, $reg, $data);
1792              
1793 0 0         if ($bytes > 0) {
1794 0           my $response;
1795 0           $self->{sock}->recv($response,$bytes);
1796 0           return $bytes, [unpack("C"x$bytes, $response)];
1797             }
1798             else {
1799 0           return $bytes, "";
1800             }
1801             }
1802              
1803             sub prepare_for_exit {
1804 0     0 0   my $self = shift;
1805            
1806 0 0         $self->{sock}->close() if $self->{sock};
1807             }
1808              
1809             sub DESTROY {
1810 0     0     my $self = shift;
1811            
1812 0           $self->prepare_for_exit();
1813             }
1814              
1815             1;