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;
019
020import java.util.Arrays;
021import java.util.HashMap;
022import org.photonvision.common.logging.LogGroup;
023import org.photonvision.common.logging.Logger;
024import org.photonvision.common.util.ShellExec;
025
026public abstract class GPIOBase {
027    private static final Logger logger = new Logger(GPIOBase.class, LogGroup.General);
028    private static final ShellExec runCommand = new ShellExec(true, true);
029
030    protected static HashMap<String, String> commands =
031            new HashMap<>() {
032                {
033                    put("setState", "");
034                    put("shutdown", "");
035                    put("dim", "");
036                    put("blink", "");
037                }
038            };
039
040    protected static String execute(String command) {
041        try {
042            runCommand.executeBashCommand(command);
043        } catch (Exception e) {
044            logger.error(Arrays.toString(e.getStackTrace()));
045            return "";
046        }
047        return runCommand.getOutput();
048    }
049
050    public abstract int getPinNumber();
051
052    public void setState(boolean state) {
053        if (getPinNumber() != -1) {
054            setStateImpl(state);
055        }
056    }
057
058    protected abstract void setStateImpl(boolean state);
059
060    public final void setOff() {
061        setState(false);
062    }
063
064    public final void setOn() {
065        setState(true);
066    }
067
068    public void togglePin() {
069        setState(!getStateImpl());
070    }
071
072    public abstract boolean shutdown();
073
074    public final boolean getState() {
075        if (getPinNumber() != -1) {
076            return getStateImpl();
077        } else return false;
078    }
079
080    public abstract boolean getStateImpl();
081
082    public final void blink(int pulseTimeMillis, int blinks) {
083        if (getPinNumber() != -1) {
084            blinkImpl(pulseTimeMillis, blinks);
085        }
086    }
087
088    protected abstract void blinkImpl(int pulseTimeMillis, int blinks);
089
090    public final void setBrightness(int brightness) {
091        if (getPinNumber() != -1) {
092            if (brightness > 100) brightness = 100;
093            if (brightness < 0) brightness = 0;
094            setBrightnessImpl(brightness);
095        }
096    }
097
098    protected abstract void setBrightnessImpl(int brightness);
099}