001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.common.hardware.GPIO.pi;
019
020import java.util.HashMap;
021
022/**
023 * A class that defines the exceptions that can be thrown by Pigpio.
024 *
025 * <p>Credit to nkolban
026 * https://github.com/nkolban/jpigpio/blob/master/JPigpio/src/jpigpio/PigpioException.java
027 */
028@SuppressWarnings({"SpellCheckingInspection", "unused", "RedundantSuppression"})
029public class PigpioException extends Exception {
030    private int rc = -99999999;
031    private static final long serialVersionUID = 443595760654129068L;
032
033    public PigpioException() {
034        super();
035    }
036
037    public PigpioException(int rc) {
038        super();
039        this.rc = rc;
040    }
041
042    public PigpioException(int rc, String msg) {
043        super(msg);
044        this.rc = rc;
045    }
046
047    public PigpioException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
048        super(arg0, arg1, arg2, arg3);
049    }
050
051    public PigpioException(String arg0, Throwable arg1) {
052        super(arg0, arg1);
053    }
054
055    public PigpioException(String arg0) {
056        super(arg0);
057    }
058
059    public PigpioException(Throwable arg0) {
060        super(arg0);
061    }
062
063    @Override
064    public String getMessage() {
065        return "(" + rc + ") " + getMessageForError(rc);
066    }
067
068    /**
069     * Retrieve the error code that was returned by the underlying Pigpio call.
070     *
071     * @return The error code that was returned by the underlying Pigpio call.
072     */
073    public int getErrorCode() {
074        return rc;
075    } // End of getErrorCode
076
077    // Public constants for the error codes that can be thrown by Pigpio
078    public static final int PI_INIT_FAILED = -1; // gpioInitialise failed
079    public static final int PI_BAD_USER_GPIO = -2; // gpio not 0-31
080    public static final int PI_BAD_GPIO = -3; // gpio not 0-53
081    public static final int PI_BAD_MODE = -4; // mode not 0-7
082    public static final int PI_BAD_LEVEL = -5; // level not 0-1
083    public static final int PI_BAD_PUD = -6; // pud not 0-2
084    public static final int PI_BAD_PULSEWIDTH = -7; // pulsewidth not 0 or 500-2500
085    public static final int PI_BAD_DUTYCYCLE = -8; // dutycycle outside set range
086    public static final int PI_BAD_TIMER = -9; // timer not 0-9
087    public static final int PI_BAD_MS = -10; // ms not 10-60000
088    public static final int PI_BAD_TIMETYPE = -11; // timetype not 0-1
089    public static final int PI_BAD_SECONDS = -12; // seconds < 0
090    public static final int PI_BAD_MICROS = -13; // micros not 0-999999
091    public static final int PI_TIMER_FAILED = -14; // gpioSetTimerFunc failed
092    public static final int PI_BAD_WDOG_TIMEOUT = -15; // timeout not 0-60000
093    public static final int PI_NO_ALERT_FUNC = -16; // DEPRECATED
094    public static final int PI_BAD_CLK_PERIPH = -17; // clock peripheral not 0-1
095    public static final int PI_BAD_CLK_SOURCE = -18; // DEPRECATED
096    public static final int PI_BAD_CLK_MICROS = -19; // clock micros not 1, 2, 4, 5, 8, or 10
097    public static final int PI_BAD_BUF_MILLIS = -20; // buf millis not 100-10000
098    public static final int PI_BAD_DUTYRANGE = -21; // dutycycle range not 25-40000
099    public static final int PI_BAD_DUTY_RANGE = -21; // DEPRECATED (use PI_BAD_DUTYRANGE)
100    public static final int PI_BAD_SIGNUM = -22; // signum not 0-63
101    public static final int PI_BAD_PATHNAME = -23; // can't open pathname
102    public static final int PI_NO_HANDLE = -24; // no handle available
103    public static final int PI_BAD_HANDLE = -25; // unknown handle
104    public static final int PI_BAD_IF_FLAGS = -26; // ifFlags > 3
105    public static final int PI_BAD_CHANNEL = -27; // DMA channel not 0-14
106    public static final int PI_BAD_PRIM_CHANNEL = -27; // DMA primary channel not 0-14
107    public static final int PI_BAD_SOCKET_PORT = -28; // socket port not 1024-32000
108    public static final int PI_BAD_FIFO_COMMAND = -29; // unrecognized fifo command
109    public static final int PI_BAD_SECO_CHANNEL = -30; // DMA secondary channel not 0-6
110    public static final int PI_NOT_INITIALISED = -31; // function called before gpioInitialise
111    public static final int PI_INITIALISED = -32; // function called after gpioInitialise
112    public static final int PI_BAD_WAVE_MODE = -33; // waveform mode not 0-1
113    public static final int PI_BAD_CFG_INTERNAL = -34; // bad parameter in gpioCfgInternals call
114    public static final int PI_BAD_WAVE_BAUD = -35; // baud rate not 50-250K(RX)/50-1M(TX)
115    public static final int PI_TOO_MANY_PULSES = -36; // waveform has too many pulses
116    public static final int PI_TOO_MANY_CHARS = -37; // waveform has too many chars
117    public static final int PI_NOT_SERIAL_GPIO = -38; // no serial read in progress on gpio
118    public static final int PI_BAD_SERIAL_STRUC = -39; // bad (null) serial structure parameter
119    public static final int PI_BAD_SERIAL_BUF = -40; // bad (null) serial buf parameter
120    public static final int PI_NOT_PERMITTED = -41; // gpio operation not permitted
121    public static final int PI_SOME_PERMITTED = -42; // one or more gpios not permitted
122    public static final int PI_BAD_WVSC_COMMND = -43; // bad WVSC subcommand
123    public static final int PI_BAD_WVSM_COMMND = -44; // bad WVSM subcommand
124    public static final int PI_BAD_WVSP_COMMND = -45; // bad WVSP subcommand
125    public static final int PI_BAD_PULSELEN = -46; // trigger pulse length not 1-100
126    public static final int PI_BAD_SCRIPT = -47; // invalid script
127    public static final int PI_BAD_SCRIPT_ID = -48; // unknown script id
128    public static final int PI_BAD_SER_OFFSET = -49; // add serial data offset > 30 minutes
129    public static final int PI_GPIO_IN_USE = -50; // gpio already in use
130    public static final int PI_BAD_SERIAL_COUNT = -51; // must read at least a byte at a time
131    public static final int PI_BAD_PARAM_NUM = -52; // script parameter id not 0-9
132    public static final int PI_DUP_TAG = -53; // script has duplicate tag
133    public static final int PI_TOO_MANY_TAGS = -54; // script has too many tags
134    public static final int PI_BAD_SCRIPT_CMD = -55; // illegal script command
135    public static final int PI_BAD_VAR_NUM = -56; // script variable id not 0-149
136    public static final int PI_NO_SCRIPT_ROOM = -57; // no more room for scripts
137    public static final int PI_NO_MEMORY = -58; // can't allocate temporary memory
138    public static final int PI_SOCK_READ_FAILED = -59; // socket read failed
139    public static final int PI_SOCK_WRIT_FAILED = -60; // socket write failed
140    public static final int PI_TOO_MANY_PARAM = -61; // too many script parameters (> 10)
141    public static final int PI_NOT_HALTED = -62; // script already running or failed
142    public static final int PI_BAD_TAG = -63; // script has unresolved tag
143    public static final int PI_BAD_MICS_DELAY = -64; // bad MICS delay (too large)
144    public static final int PI_BAD_MILS_DELAY = -65; // bad MILS delay (too large)
145    public static final int PI_BAD_WAVE_ID = -66; // non existent wave id
146    public static final int PI_TOO_MANY_CBS = -67; // No more CBs for waveform
147    public static final int PI_TOO_MANY_OOL = -68; // No more OOL for waveform
148    public static final int PI_EMPTY_WAVEFORM = -69; // attempt to create an empty waveform
149    public static final int PI_NO_WAVEFORM_ID = -70; // no more waveforms
150    public static final int PI_I2C_OPEN_FAILED = -71; // can't open I2C device
151    public static final int PI_SER_OPEN_FAILED = -72; // can't open serial device
152    public static final int PI_SPI_OPEN_FAILED = -73; // can't open SPI device
153    public static final int PI_BAD_I2C_BUS = -74; // bad I2C bus
154    public static final int PI_BAD_I2C_ADDR = -75; // bad I2C address
155    public static final int PI_BAD_SPI_CHANNEL = -76; // bad SPI channel
156    public static final int PI_BAD_FLAGS = -77; // bad i2c/spi/ser open flags
157    public static final int PI_BAD_SPI_SPEED = -78; // bad SPI speed
158    public static final int PI_BAD_SER_DEVICE = -79; // bad serial device name
159    public static final int PI_BAD_SER_SPEED = -80; // bad serial baud rate
160    public static final int PI_BAD_PARAM = -81; // bad i2c/spi/ser parameter
161    public static final int PI_I2C_WRITE_FAILED = -82; // i2c write failed
162    public static final int PI_I2C_READ_FAILED = -83; // i2c read failed
163    public static final int PI_BAD_SPI_COUNT = -84; // bad SPI count
164    public static final int PI_SER_WRITE_FAILED = -85; // ser write failed
165    public static final int PI_SER_READ_FAILED = -86; // ser read failed
166    public static final int PI_SER_READ_NO_DATA = -87; // ser read no data available
167    public static final int PI_UNKNOWN_COMMAND = -88; // unknown command
168    public static final int PI_SPI_XFER_FAILED = -89; // spi xfer/read/write failed
169    public static final int PI_BAD_POINTER = -90; // bad (NULL) pointer
170    public static final int PI_NO_AUX_SPI = -91; // need a A+/B+/Pi2 for auxiliary SPI
171    public static final int PI_NOT_PWM_GPIO = -92; // gpio is not in use for PWM
172    public static final int PI_NOT_SERVO_GPIO = -93; // gpio is not in use for servo pulses
173    public static final int PI_NOT_HCLK_GPIO = -94; // gpio has no hardware clock
174    public static final int PI_NOT_HPWM_GPIO = -95; // gpio has no hardware PWM
175    public static final int PI_BAD_HPWM_FREQ = -96; // hardware PWM frequency not 1-125M
176    public static final int PI_BAD_HPWM_DUTY = -97; // hardware PWM dutycycle not 0-1M
177    public static final int PI_BAD_HCLK_FREQ = -98; // hardware clock frequency not 4689-250M
178    public static final int PI_BAD_HCLK_PASS = -99; // need password to use hardware clock 1
179    public static final int PI_HPWM_ILLEGAL = -100; // illegal, PWM in use for main clock
180    public static final int PI_BAD_DATABITS = -101; // serial data bits not 1-32
181    public static final int PI_BAD_STOPBITS = -102; // serial (half) stop bits not 2-8
182    public static final int PI_MSG_TOOBIG = -103; // socket/pipe message too big
183    public static final int PI_BAD_MALLOC_MODE = -104; // bad memory allocation mode
184    public static final int PI_TOO_MANY_SEGS = -105; // too many I2C transaction parts
185    public static final int PI_BAD_I2C_SEG = -106; // a combined I2C transaction failed
186    public static final int PI_BAD_SMBUS_CMD = -107;
187    public static final int PI_NOT_I2C_GPIO = -108;
188    public static final int PI_BAD_I2C_WLEN = -109;
189    public static final int PI_BAD_I2C_RLEN = -110;
190    public static final int PI_BAD_I2C_CMD = -111;
191    public static final int PI_BAD_I2C_BAUD = -112;
192    public static final int PI_CHAIN_LOOP_CNT = -113;
193    public static final int PI_BAD_CHAIN_LOOP = -114;
194    public static final int PI_CHAIN_COUNTER = -115;
195    public static final int PI_BAD_CHAIN_CMD = -116;
196    public static final int PI_BAD_CHAIN_DELAY = -117;
197    public static final int PI_CHAIN_NESTING = -118;
198    public static final int PI_CHAIN_TOO_BIG = -119;
199    public static final int PI_DEPRECATED = -120;
200    public static final int PI_BAD_SER_INVERT = -121;
201    public static final int PI_BAD_EDGE = -122;
202    public static final int PI_BAD_ISR_INIT = -123;
203    public static final int PI_BAD_FOREVER = -124;
204    public static final int PI_BAD_FILTER = -125;
205
206    public static final int PI_PIGIF_ERR_0 = -2000;
207    public static final int PI_PIGIF_ERR_99 = -2099;
208
209    public static final int PI_CUSTOM_ERR_0 = -3000;
210    public static final int PI_CUSTOM_ERR_999 = -3999;
211
212    private static final HashMap<Integer, String> errorMessages = new HashMap<>();
213
214    static {
215        errorMessages.put(PI_INIT_FAILED, "pigpio initialisation failed");
216        errorMessages.put(PI_BAD_USER_GPIO, "GPIO not 0-31");
217        errorMessages.put(PI_BAD_GPIO, "GPIO not 0-53");
218        errorMessages.put(PI_BAD_MODE, "mode not 0-7");
219        errorMessages.put(PI_BAD_LEVEL, "level not 0-1");
220        errorMessages.put(PI_BAD_PUD, "pud not 0-2");
221        errorMessages.put(PI_BAD_PULSEWIDTH, "pulsewidth not 0 or 500-2500");
222        errorMessages.put(PI_BAD_DUTYCYCLE, "dutycycle not 0-range (default 255)");
223        errorMessages.put(PI_BAD_TIMER, "timer not 0-9");
224        errorMessages.put(PI_BAD_MS, "ms not 10-60000");
225        errorMessages.put(PI_BAD_TIMETYPE, "timetype not 0-1");
226        errorMessages.put(PI_BAD_SECONDS, "seconds < 0");
227        errorMessages.put(PI_BAD_MICROS, "micros not 0-999999");
228        errorMessages.put(PI_TIMER_FAILED, "gpioSetTimerFunc failed");
229        errorMessages.put(PI_BAD_WDOG_TIMEOUT, "timeout not 0-60000");
230        errorMessages.put(PI_NO_ALERT_FUNC, "DEPRECATED");
231        errorMessages.put(PI_BAD_CLK_PERIPH, "clock peripheral not 0-1");
232        errorMessages.put(PI_BAD_CLK_SOURCE, "DEPRECATED");
233        errorMessages.put(PI_BAD_CLK_MICROS, "clock micros not 1, 2, 4, 5, 8, or 10");
234        errorMessages.put(PI_BAD_BUF_MILLIS, "buf millis not 100-10000");
235        errorMessages.put(PI_BAD_DUTYRANGE, "dutycycle range not 25-40000");
236        errorMessages.put(PI_BAD_SIGNUM, "signum not 0-63");
237        errorMessages.put(PI_BAD_PATHNAME, "can't open pathname");
238        errorMessages.put(PI_NO_HANDLE, "no handle available");
239        errorMessages.put(PI_BAD_HANDLE, "unknown handle");
240        errorMessages.put(PI_BAD_IF_FLAGS, "ifFlags > 3");
241        errorMessages.put(PI_BAD_CHANNEL, "DMA channel not 0-14");
242        errorMessages.put(PI_BAD_SOCKET_PORT, "socket port not 1024-30000");
243        errorMessages.put(PI_BAD_FIFO_COMMAND, "unknown fifo command");
244        errorMessages.put(PI_BAD_SECO_CHANNEL, "DMA secondary channel not 0-14");
245        errorMessages.put(PI_NOT_INITIALISED, "function called before gpioInitialise");
246        errorMessages.put(PI_INITIALISED, "function called after gpioInitialise");
247        errorMessages.put(PI_BAD_WAVE_MODE, "waveform mode not 0-1");
248        errorMessages.put(PI_BAD_CFG_INTERNAL, "bad parameter in gpioCfgInternals call");
249        errorMessages.put(PI_BAD_WAVE_BAUD, "baud rate not 50-250000(RX)/1000000(TX)");
250        errorMessages.put(PI_TOO_MANY_PULSES, "waveform has too many pulses");
251        errorMessages.put(PI_TOO_MANY_CHARS, "waveform has too many chars");
252        errorMessages.put(PI_NOT_SERIAL_GPIO, "no bit bang serial read in progress on GPIO");
253        errorMessages.put(PI_NOT_PERMITTED, "no permission to update GPIO");
254        errorMessages.put(PI_SOME_PERMITTED, "no permission to update one or more GPIO");
255        errorMessages.put(PI_BAD_WVSC_COMMND, "bad WVSC subcommand");
256        errorMessages.put(PI_BAD_WVSM_COMMND, "bad WVSM subcommand");
257        errorMessages.put(PI_BAD_WVSP_COMMND, "bad WVSP subcommand");
258        errorMessages.put(PI_BAD_PULSELEN, "trigger pulse length not 1-100");
259        errorMessages.put(PI_BAD_SCRIPT, "invalid script");
260        errorMessages.put(PI_BAD_SCRIPT_ID, "unknown script id");
261        errorMessages.put(PI_BAD_SER_OFFSET, "add serial data offset > 30 minute");
262        errorMessages.put(PI_GPIO_IN_USE, "GPIO already in use");
263        errorMessages.put(PI_BAD_SERIAL_COUNT, "must read at least a byte at a time");
264        errorMessages.put(PI_BAD_PARAM_NUM, "script parameter id not 0-9");
265        errorMessages.put(PI_DUP_TAG, "script has duplicate tag");
266        errorMessages.put(PI_TOO_MANY_TAGS, "script has too many tags");
267        errorMessages.put(PI_BAD_SCRIPT_CMD, "illegal script command");
268        errorMessages.put(PI_BAD_VAR_NUM, "script variable id not 0-149");
269        errorMessages.put(PI_NO_SCRIPT_ROOM, "no more room for scripts");
270        errorMessages.put(PI_NO_MEMORY, "can't allocate temporary memory");
271        errorMessages.put(PI_SOCK_READ_FAILED, "socket read failed");
272        errorMessages.put(PI_SOCK_WRIT_FAILED, "socket write failed");
273        errorMessages.put(PI_TOO_MANY_PARAM, "too many script parameters (> 10)");
274        errorMessages.put(PI_NOT_HALTED, "script already running or failed");
275        errorMessages.put(PI_BAD_TAG, "script has unresolved tag");
276        errorMessages.put(PI_BAD_MICS_DELAY, "bad MICS delay (too large)");
277        errorMessages.put(PI_BAD_MILS_DELAY, "bad MILS delay (too large)");
278        errorMessages.put(PI_BAD_WAVE_ID, "non existent wave id");
279        errorMessages.put(PI_TOO_MANY_CBS, "No more CBs for waveform");
280        errorMessages.put(PI_TOO_MANY_OOL, "No more OOL for waveform");
281        errorMessages.put(PI_EMPTY_WAVEFORM, "attempt to create an empty waveform");
282        errorMessages.put(PI_NO_WAVEFORM_ID, "No more waveform ids");
283        errorMessages.put(PI_I2C_OPEN_FAILED, "can't open I2C device");
284        errorMessages.put(PI_SER_OPEN_FAILED, "can't open serial device");
285        errorMessages.put(PI_SPI_OPEN_FAILED, "can't open SPI device");
286        errorMessages.put(PI_BAD_I2C_BUS, "bad I2C bus");
287        errorMessages.put(PI_BAD_I2C_ADDR, "bad I2C address");
288        errorMessages.put(PI_BAD_SPI_CHANNEL, "bad SPI channel");
289        errorMessages.put(PI_BAD_FLAGS, "bad i2c/spi/ser open flags");
290        errorMessages.put(PI_BAD_SPI_SPEED, "bad SPI speed");
291        errorMessages.put(PI_BAD_SER_DEVICE, "bad serial device name");
292        errorMessages.put(PI_BAD_SER_SPEED, "bad serial baud rate");
293        errorMessages.put(PI_BAD_PARAM, "bad i2c/spi/ser parameter");
294        errorMessages.put(PI_I2C_WRITE_FAILED, "I2C write failed");
295        errorMessages.put(PI_I2C_READ_FAILED, "I2C read failed");
296        errorMessages.put(PI_BAD_SPI_COUNT, "bad SPI count");
297        errorMessages.put(PI_SER_WRITE_FAILED, "ser write failed");
298        errorMessages.put(PI_SER_READ_FAILED, "ser read failed");
299        errorMessages.put(PI_SER_READ_NO_DATA, "ser read no data available");
300        errorMessages.put(PI_UNKNOWN_COMMAND, "unknown command");
301        errorMessages.put(PI_SPI_XFER_FAILED, "SPI xfer/read/write failed");
302        errorMessages.put(PI_BAD_POINTER, "bad (NULL) pointer");
303        errorMessages.put(PI_NO_AUX_SPI, "no auxiliary SPI on Pi A or B");
304        errorMessages.put(PI_NOT_PWM_GPIO, "GPIO is not in use for PWM");
305        errorMessages.put(PI_NOT_SERVO_GPIO, "GPIO is not in use for servo pulses");
306        errorMessages.put(PI_NOT_HCLK_GPIO, "GPIO has no hardware clock");
307        errorMessages.put(PI_NOT_HPWM_GPIO, "GPIO has no hardware PWM");
308        errorMessages.put(PI_BAD_HPWM_FREQ, "hardware PWM frequency not 1-125M");
309        errorMessages.put(PI_BAD_HPWM_DUTY, "hardware PWM dutycycle not 0-1M");
310        errorMessages.put(PI_BAD_HCLK_FREQ, "hardware clock frequency not 4689-250M");
311        errorMessages.put(PI_BAD_HCLK_PASS, "need password to use hardware clock 1");
312        errorMessages.put(PI_HPWM_ILLEGAL, "illegal, PWM in use for main clock");
313        errorMessages.put(PI_BAD_DATABITS, "serial data bits not 1-32");
314        errorMessages.put(PI_BAD_STOPBITS, "serial (half) stop bits not 2-8");
315        errorMessages.put(PI_MSG_TOOBIG, "socket/pipe message too big");
316        errorMessages.put(PI_BAD_MALLOC_MODE, "bad memory allocation mode");
317        errorMessages.put(PI_TOO_MANY_SEGS, "too many I2C transaction segments");
318        errorMessages.put(PI_BAD_I2C_SEG, "an I2C transaction segment failed");
319        errorMessages.put(PI_BAD_SMBUS_CMD, "SMBus command not supported");
320        errorMessages.put(PI_NOT_I2C_GPIO, "no bit bang I2C in progress on GPIO");
321        errorMessages.put(PI_BAD_I2C_WLEN, "bad I2C write length");
322        errorMessages.put(PI_BAD_I2C_RLEN, "bad I2C read length");
323        errorMessages.put(PI_BAD_I2C_CMD, "bad I2C command");
324        errorMessages.put(PI_BAD_I2C_BAUD, "bad I2C baud rate, not 50-500k");
325        errorMessages.put(PI_CHAIN_LOOP_CNT, "bad chain loop count");
326        errorMessages.put(PI_BAD_CHAIN_LOOP, "empty chain loop");
327        errorMessages.put(PI_CHAIN_COUNTER, "too many chain counters");
328        errorMessages.put(PI_BAD_CHAIN_CMD, "bad chain command");
329        errorMessages.put(PI_BAD_CHAIN_DELAY, "bad chain delay micros");
330        errorMessages.put(PI_CHAIN_NESTING, "chain counters nested too deeply");
331        errorMessages.put(PI_CHAIN_TOO_BIG, "chain is too long");
332        errorMessages.put(PI_DEPRECATED, "deprecated function removed");
333        errorMessages.put(PI_BAD_SER_INVERT, "bit bang serial invert not 0 or 1");
334        errorMessages.put(PI_BAD_EDGE, "bad ISR edge value, not 0-2");
335        errorMessages.put(PI_BAD_ISR_INIT, "bad ISR initialisation");
336        errorMessages.put(PI_BAD_FOREVER, "loop forever must be last chain command");
337        errorMessages.put(PI_BAD_FILTER, "bad filter parameter");
338    }
339
340    public static String getMessageForError(int errorCode) {
341        return errorMessages.get(errorCode);
342    }
343}