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 org.photonvision.common.configuration.HardwareConfig;
021import org.photonvision.common.hardware.Platform;
022
023public class CustomGPIO extends GPIOBase {
024    private boolean currentState;
025    private final int port;
026
027    public CustomGPIO(int port) {
028        this.port = port;
029    }
030
031    @Override
032    public void togglePin() {
033        if (this.port != -1) {
034            execute(
035                    commands
036                            .get("setState")
037                            .replace("{s}", String.valueOf(!currentState))
038                            .replace("{p}", String.valueOf(this.port)));
039            currentState = !currentState;
040        }
041    }
042
043    @Override
044    public int getPinNumber() {
045        return port;
046    }
047
048    @Override
049    public void setStateImpl(boolean state) {
050        if (this.port != -1) {
051            execute(
052                    commands
053                            .get("setState")
054                            .replace("{s}", String.valueOf(state))
055                            .replace("{p}", String.valueOf(port)));
056            currentState = state;
057        }
058    }
059
060    @Override
061    public boolean shutdown() {
062        if (this.port != -1) {
063            execute(commands.get("shutdown"));
064            return true;
065        }
066        return false;
067    }
068
069    @Override
070    public boolean getStateImpl() {
071        return currentState;
072    }
073
074    @Override
075    public void blinkImpl(int pulseTimeMillis, int blinks) {
076        execute(
077                commands
078                        .get("blink")
079                        .replace("{pulseTime}", String.valueOf(pulseTimeMillis))
080                        .replace("{blinks}", String.valueOf(blinks))
081                        .replace("{p}", String.valueOf(this.port)));
082    }
083
084    @Override
085    public void setBrightnessImpl(int brightness) {
086        execute(
087                commands
088                        .get("dim")
089                        .replace("{p}", String.valueOf(port))
090                        .replace("{v}", String.valueOf(brightness)));
091    }
092
093    public static void setConfig(HardwareConfig config) {
094        if (Platform.isRaspberryPi()) return;
095        commands.replace("setState", config.ledSetCommand);
096        commands.replace("dim", config.ledDimCommand);
097        commands.replace("blink", config.ledBlinkCommand);
098    }
099}