public class ClientPigpiod extends Object
ClientPigpiod
object should be
created and used (and the ClientPigpiod
object will be
associated with this Pi
object). The IO methods and most
others are threadsafe. Nevertheless, initialising and shutting down /
cleaning up the control IO application is usually better done in one
thread. Modifier and Type | Class and Description |
---|---|
static class |
ClientPigpiod.CmdState
Status of a command executed.
|
Modifier and Type | Field and Description |
---|---|
ThePi |
thePi
The Raspberry Pi used for IO.
|
Constructor and Description |
---|
ClientPigpiod(ThePi thePi,
Object app)
Construct and connect to a given Pi.
|
Modifier and Type | Method and Description |
---|---|
int |
areOut()
Stores all output GPIO as bitmask.
|
Socket |
connect()
Connecting.
|
int |
debugCommand(int ret)
Debug info on last command execution.
|
void |
disconnect()
Disconnecting.
|
boolean |
equals(Object other)
Equal with other ClientPigPiod.
|
int |
extCmd(int cmd,
int p1,
int p2,
int p3,
byte[] buffC,
byte[] buffR)
Implementation of the commands with extensions.
|
int |
getInp(int gpio)
Read the IO state.
|
int |
getMode(int gpio)
Get the mode of one GPIO pin.
|
int |
getPadS(int pad)
Get the output strength of a set of GPIO pins.
|
int |
getPWMcycle(int gpio)
Get the PWM duty cycle.
|
int |
getPWMhertz(int gpio)
Get the PWM frequency.
|
int |
getServoPw(int gpio)
Get the servo pulse width.
|
static int |
gpio2bit(int gpio)
GPIO number to bank pin number lookup.
|
void |
gpioActionsByMsk(int mask,
IntConsumer action)
Perform a separate action on every GPIO (0..31) given by mask.
|
PrimitiveIterator.OfInt |
gpiosByMsk(int mask)
Iterate over GPIOs (0..31) given by mask.
|
int |
hashCode()
Hashcode.
|
int |
initAsDrive(int gpio,
int init)
Initialise a GPIO pin as output.
|
int |
initAsHiDrive(int gpio,
int init)
Initialise a GPIO pin as high drive.
|
int |
initAsHiInput(int gpio)
Initialise a GPIO pin as input with pull down.
|
int |
initAsInput(int gpio)
Initialise a GPIO pin as input.
|
void |
initAsInputs(int[] lesGPIOs)
Initialise one or more GPIO pin as input.
|
int |
initAsLoInput(int gpio)
Initialise a GPIO pin as input with pull up.
|
int |
initAsOutput(int gpio)
Initialise a GPIO pin as output.
|
int |
initAsOutputs(int[] lesGPIOs)
Put one or more GPIO pins to output mode.
|
int |
logCommand(int ret)
Log of last command execution.
|
int |
logIfBad(int ret)
Log of last bad command execution.
|
static void |
main(String[] args)
ClientPigpoid application.
|
static ClientPigpiod |
make(PiUtil app)
Make a new Pi and connect to it.
|
static ClientPigpiod |
make(String host,
int port,
int timeout,
int type,
Object app)
Make and connect to a new Pi.
|
static String |
pudTxt(int pud)
Names for input's pull resistor settings.
|
int |
releaseOutputs()
Release all GPIO pins set as output.
|
int |
releaseOutputsReport(Appendable log)
Release all GPIO pins set as output with report.
|
void |
reportPinOp(Appendable out,
String op,
int[] lesGPIOs)
Report an arbitrary operation on a list of GPIOs.
|
protected int |
rErr(int error,
int cmd)
Recording helper for IO methods for errors.
|
protected int |
rErr(int error,
int cmd,
int p1,
int p2)
Recording helper for IO methods.
|
protected int |
rIgn(int cmd,
int p2)
Recording helper for IO methods.
|
int |
setAsOutputs(boolean report,
String grp,
int mask)
Set all GPIO pins named by mask as outputs with report.
|
int |
setAsOutputs(int mask)
Set all GPIO pins named by mask as outputs.
|
int |
setMode(int gpio,
int mode)
Set the mode of one GPIO pin.
|
protected PrintWriter |
setOut(Object app)
Set own PrintWriter out.
|
int |
setOutput(int gpio,
boolean level)
Set one GPIO as output.
|
int |
setOutput(int gpio,
String out)
Output to one GPIO pin.
|
int |
setOutSet(int lesOuts,
boolean level)
Set a list/mask/set of GPIOs as output pins.
|
int |
setPadS(int pad,
int mA)
Set the output strength of a set of GPIO pins.
|
int |
setPullR(int gpio,
int pud)
Set the pull resistor for one GPIO.
|
int |
setPullR(ThePi.Port port,
int pud)
Set the pull resistor for one port and record the setting.
|
int |
setPWMcycle(int gpio,
int val)
Set the PWM duty cycle.
|
int |
setPWMhertz(int gpio,
int hz)
Set the PWM frequency.
|
int |
setServoPos(int gpio,
int val)
Set the servo position.
|
int |
stdCmd(int cmd,
int p1,
int p2)
Implementation of two parameter (standard) commands.
|
int |
stdCmd(int cmd,
int p1,
int p2,
int pNum)
Implementation of three parameter (numerical extension) commands.
|
String |
toString()
The connection's settings as String.
|
static boolean |
uint32ret(int cmd)
Command result type.
|
public final ThePi thePi
public ClientPigpiod(ThePi thePi, Object app) throws IOException
thePi
- if null a default Pi3
is made and usedapp
- the application using the ClientPigpiod object to be madeIOException
- when connecting to the pigpiod server fails{@link #setOut(Object)}
protected PrintWriter setOut(Object app)
ClientPigpiod
objects PrintWriter
for reports. If the parameter app
is a PrintWriter
app
will be be used. app
is of type App
,
App
or PiUtil
that objects
own (current) PrintWriter
out will be taken. System.out
.public Socket connect() throws IOException
disconnect()
in this
case.disconnect()
. So far, no use case for
this was reported. If this does not change connect()
might be
removed / made private in future.IOException
- if the connecting
to host
.port
failspublic static ClientPigpiod make(String host, int port, int timeout, int type, Object app) throws IOException
Pi
object. Then it tries to make a a new ClientPigpiod
connected
to a real Raspberry Pi according to parameters host, port and timeout.setOut(app)
. Pi
= host
connected via socket port within
timeout
IOException
- when connecting to the pigpiod server failsPi3.make(String, int, int, int)
,
Pi1.make(String, int, int)
public static ClientPigpiod make(PiUtil app) throws IOException
make(host, port, timeout, type, app)
.
The values host, port, timeout and type are got from app's
PiUtil
genes as default values or by start parameter evaluation.app
- the application as PiUtil
objectPi
= host
connected via socket port within
timeout
IOException
- when connecting to the pigpiod server failsPi3.make(String, int, int, int)
,
Pi1.make(String, int, int)
public void disconnect() throws IOException
connect()
will be
closed.IOException
- on closing problemspublic String toString()
public final boolean equals(Object other)
hashCode()
.public final int hashCode()
hashCode
in class Object
equals(Object)
public int debugCommand(int ret)
out
.logCommand(int)
debugCommand also logs the socket
request and response buffers which is only very seldom of any
interest.ret
- the return value of the command method; logCommand(commandMethod(...))
#logIfBad(int)
protected final int rErr(int error, int cmd, int p1, int p2)
stdCmd(int, int, int)
when recognising erroneous
parameters (e.g.) hence bypassing the socket communication for the
thrill of just getting an error value in return (for those cases where
stdCmd(int, int, int)
does not do pre-checks itself). lastCmdState
object.rErr(int, int)
and rIgn(int, int)
.protected final int rErr(int error, int cmd)
rErr(int, int, int, int)
protected final int rIgn(int cmd, int p2)
rErr(int, int, int, int)
public int logCommand(int ret)
out
.[int r = ]logCommand(anyIOcommand(p1, p2));
ret
- the return value of the command method; logCommand(commandMethod(...))
logIfBad(int)
public int logIfBad(int ret)
out
if and only if the execution
failed (with a negative return code). (In that case it will delegate to
logCommand(int)
.)ret
- the return value of the command method; logIfBad(commandMethod(...))
logCommand(int)
public int stdCmd(int cmd, int p1, int p2)
stdCmd()
does the communication with the
pigpiod daemon as well as the bookkeeping threadsafe. setMode(int, int)
,
getMode(int)
, setPadS(int, int)
, getPadS(int)
,
setPullR(int, int)
, setOutput(int, boolean)
etc. pp..cmd
- the command numberp1
- first parameter, mostly GPIO numberp2
- optional second parameteruint32ret(int)
public int stdCmd(int cmd, int p1, int p2, int pNum)
stdCmd(cmd, p1, p2)
to be rejected
or (if one of the 75 command without extensions) be executed there.cmd
- the command numberp1
- first parameter, mostly GPIO numberp2
- second parameterpNum
- the one numerical (extension) parameteruint32ret(int)
stdCmd(int, int, int)
public int extCmd(int cmd, int p1, int p2, int p3, byte[] buffC, byte[] buffR)
cmd
is one of the 11 with a numerical command extension
(fixed length 4) and no extended response use better
stdCmd(cmd, p1, p2, intBuffc)
directly instead of letting this method delegate.cmd
is one of the 75 non extended command (and for the
most trivial errors) this method delegated to
stdCmd(int, int, int)
.cmd
- the command numberp1
- first parameter, mostly GPIO numberp2
- optional second parameterp3
- the length of the command extensionbuffC
- the command extension bufferbuffR
- the response buffer (might be the same as buffC)public PrimitiveIterator.OfInt gpiosByMsk(int mask)
forEachRemaining
IntConsumer action = gpio -> doWhatOn(gpio);
gpiosByMsk(mask).forEachRemaining(action);
hasNext()
and
nextInt()
directly in a while loop, e.g. for (type i : typeIiterator)
won't work for
PrimitiveIterator
s (at least in Java 8).mask
- a bank 0 bit mask of GPIOs in the range 0..31public void gpioActionsByMsk(int mask, IntConsumer action)
gpiosByMsk(int)
plus some checks and synchronising: synchronized (this ClientPigdiod object)
block all actions are done.mask
- a bank 0 bit mask of GPIOs in the range 0..31action
- what to do with each GPIO in maskpublic int setMode(int gpio, int mode)
gpio
- a legal IO number 0.. 31mode
- a mode like PiGpioDdefs.PI_INPUT
,
PiGpioDdefs.PI_OUTPUT
, PiGpioDdefs.PI_ALT0
etc.public int getMode(int gpio)
gpio
- a legal BCM IO number 0.. 56PiGpioDdefs.PI_INPUT
,
PiGpioDdefs.PI_OUTPUT
, PiGpioDdefs.PI_ALT0
etc.
or < 0: errorpublic int setPullR(int gpio, int pud)
gpio
- a legal BCM IO number 0.. 31 or PiVals.PINig
pud
- a mode: PiGpioDdefs.PI_PUD_OFF
,
PiGpioDdefs.PI_PUD_DOWN
or PiGpioDdefs.PI_PUD_UP
public int setPullR(ThePi.Port port, int pud)
port
- a Pi port; null means ignore
pud
- a mode: PiGpioDdefs.PI_PUD_OFF
,
PiGpioDdefs.PI_PUD_DOWN
or PiGpioDdefs.PI_PUD_UP
PI_PUD_DT (default)
will be
replaced by the port's pull resistor flag.setPullR(int, int)
public static String pudTxt(int pud)
pud
- a mode: PiGpioDdefs.PI_PUD_OFF
,
PiGpioDdefs.PI_PUD_DOWN
, PiGpioDdefs.PI_PUD_UP
,
PiVals.PI_PUD_KP
, PiVals.PI_PUD_DT
public int setPadS(int pad, int mA)
setMode(int, int)
e.g.)
this function sets the drive capacity in the range of 2..16 mA.pad
- The pad or I/O port set number; 0 is GPIO numbers 0..27. mA
- 2..16, the strength in mAgetPadS(int)
,
PiGpioDdefs.PI_BAD_PAD
,
PiGpioDdefs.PI_BAD_STRENGTH
public int getPadS(int pad)
setMode(int, int)
e.g.)
this function gets the drive capacity in the range of 2..16 mA.pad
- The pad or I/O port set number; 0 is GPIO numbers 0..27setPadS(int, int)
,
PiGpioDdefs.PI_BAD_PAD
,
PiGpioDdefs.PI_BAD_STRENGTH
public void initAsInputs(int[] lesGPIOs)
lesGPIOs
- array of GPIO numbers (0..53); use PiVals.PINix
as end marker if the array is too long and PiVals.PINig
as
an entry to be ignored.initAsOutputs(int[])
public int initAsOutputs(int[] lesGPIOs)
lesGPIOs
- array of GPIO numbers (0..53); use PiVals.PINix
as end marker if the array is too long and PiVals.PINig
as
an entry to be ignored.initAsInputs(int[])
public void reportPinOp(Appendable out, String op, int[] lesGPIOs)
out
will be
progNam operation GPIO: 13 pin: 27
.out
- the writer to output toop
- the operation displayed as 12 characters right justifiedlesGPIOs
- a GPIO list
(terminated by a value >= PiVals.PINix
)public final int areOut()
0..31
set by ClientPigpiod
as
output *) the corresponding bit is set. For all GPIOs in the range
0..31
set by ClientPigpiod
as input the
corresponding bit is cleared. releaseOutputs()
) mainly for
the end / shutdown of the application. public int initAsOutput(int gpio)
gpio
- the GPIO number (0..31); PINig means no actionpublic int releaseOutputs()
releaseOutputsReport(Appendable)
public int releaseOutputsReport(Appendable log)
releaseOutputs()
plus a "releaseToIn" report line for
every output released.log
- a destination to report to;
for example a PrintWriter, PrintStream or StringBuilder public int setAsOutputs(boolean report, String grp, int mask)
report
is false
this method does the same as
setAsOutputs(mask)
(ignoring the parameter
grp
.report
is true
an extra "make output" report line for
every output set so is printed to out
.report
- true: report every every pin/GPIO set as outputgrp
- a short (best 7 char) description of the pin / signal
or device group like "redLEDs", "relaysH" etc.mask
- GPIOs to be set as outputs as bit maskpublic int setAsOutputs(int mask)
mask
as outputs. PiGpioDdefs.PI_CMD_BS1
,
PiGpioDdefs.PI_CMD_BC1
respectively
setOutSet(int, boolean)
) don't. Hence, outputs
to be used so, will have to be explicitly set as such. This method
offers the comfort (and error avoidance) to use the very same mask
parameters as the bulk output commands.mask
- GPIOs to be set as outputs as bit maskpublic int initAsInput(int gpio)
gpio
- the GPIO number (0..53)public int initAsLoInput(int gpio)
gpio
- the GPIO number (0..53)public int initAsHiInput(int gpio)
gpio
- the GPIO number (0..53)public int initAsHiDrive(int gpio, int init)
gpio
- the GPIO number (0..53)
init
- 0 or 1: the initial output value; else: leave unchangedpublic int initAsDrive(int gpio, int init)
gpio
- the GPIO number (0..53)
init
- 0 or 1: the initial output value; else: leave unchangedpublic int getInp(int gpio)
gpio
- a legal BCM IO number (0..56)
public int setOutput(int gpio, String out)
out
:null
, empty, 0, F, f
as well as all interpreted as
false
by
TextHelper.asBoolObj(out)
255, T, t
as well as all interpreted as true
by TextHelper.asBoolObj(out)
0..255
(i.e. 0..100%):out
is a decimal value in that range.0=0%
and 255=100%
will be done as binary
output.500
(left) .. 2500
(right):
when out
is a numerical value in that range.1500 =
middle, neutral.PiVals.PINig
nothing is done and 0 is returned.
That implements the meaning of PINig
as unused.gpio
- a GPIO (0..31)
to output toout
- the output level or PWM 7 servo settingpublic int setOutput(int gpio, boolean level)
PiVals.PINig
nothing is done and 0 is returned.
That handles the meaning of PINig
as unused.gpio
- a GPIO (0..31)
to output tolevel
- OFF or ON (0 or 1)public int setOutSet(int lesOuts, boolean level)
lesOuts
- bank mask of outputs to be setlevel
- OFF or ON (0 or 1)public int setPWMcycle(int gpio, int val)
gpio
- a legal BCM IO number 0..28val
- 0..255 as 0 .. 100% (in default pwm range)public int getPWMcycle(int gpio)
gpio
- a legal BCM IO number 0.28public int setServoPos(int gpio, int val)
gpio
- a legal BCM IO number 0..28val
- 0 (Off) or 500 (full left) .. 2500 (full right)public int getServoPw(int gpio)
gpio
- a legal BCM IO number 0.28public int setPWMhertz(int gpio, int hz)
gpio
- a legal BCM IO number 0..28hz
- frequency in Hz 5... 40000 depending on duty cyclepublic int getPWMhertz(int gpio)
gpio
- a legal BCM IO number 0..28public static int gpio2bit(int gpio)
ThePi.gpio2pin(int)
,
ThePi.gpio4pin(int)
public static final boolean uint32ret(int cmd)
PiGpioDdefs.PI_INIT_FAILED
..PiGpioDdefs.PI_BAD_EVENT_ID
.cmd
- command number (0..117)public static void main(String[] args)
ClientPigpiod
and
PiGpioDdefs
against the official socket interface
documentation.args
- ignored