001package com.pi4j.io.gpio;
002
003import java.util.Map;
004
005/*
006 * #%L
007 * **********************************************************************
008 * ORGANIZATION  :  Pi4J
009 * PROJECT       :  Pi4J :: Java Library (Core)
010 * FILENAME      :  SimulatedGpioProvider.java
011 *
012 * This file is part of the Pi4J project. More information about
013 * this project can be found here:  https://www.pi4j.com/
014 * **********************************************************************
015 * %%
016 * Copyright (C) 2012 - 2019 Pi4J
017 * %%
018 * This program is free software: you can redistribute it and/or modify
019 * it under the terms of the GNU Lesser General Public License as
020 * published by the Free Software Foundation, either version 3 of the
021 * License, or (at your option) any later version.
022 *
023 * This program is distributed in the hope that it will be useful,
024 * but WITHOUT ANY WARRANTY; without even the implied warranty of
025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
026 * GNU General Lesser Public License for more details.
027 *
028 * You should have received a copy of the GNU General Lesser Public
029 * License along with this program.  If not, see
030 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
031 * #L%
032 */
033
034
035/**
036 * A simulator to aid in development of RI Pi systems using a standard PC dev
037 * environment.
038 *
039 * To use the simulator you need two environment variables:
040 *
041 * The standard PI4J platform statement MUST point to the simulator:
042 *
043 * PI4J_PLATFORM=Simulated
044 *
045 * A second environment variable that defines the platform that is to be
046 * simulated.
047 *
048 * SimulatedPlatform=<Real Platform's Name>
049 *
050 * e.g.
051 *
052 * SimultatedPlatform=RaspberryPi GPIO Provider
053 *
054 * If you don't provide a value for theSimulatedPlatform the system assumes that
055 * you want to use the raspberry pi platform: RaspiGpioProvider
056 *
057 * @author bsutton
058 *
059 */
060public class SimulatedGpioProvider extends GpioProviderBase implements GpioProvider {
061
062    // We use the name of the platform that we are simulating.
063    public static String NAME;
064
065    public SimulatedGpioProvider() {
066        Map<String, String> env = System.getenv();
067
068        String config = env.get("SimulatedPlatform");
069
070        // If no specific platform is specified we default to simulating the raspberry pi
071        if (config == null) {
072            NAME = RaspiGpioProvider.NAME;
073            return;
074        }
075
076        NAME = config;
077    }
078
079    @Override
080    public String getName() {
081        return NAME;
082    }
083
084    public void setState(Pin pin, PinState state) {
085        // cache pin state
086        getPinCache(pin).setState(state);
087
088        // dispatch event
089        dispatchPinDigitalStateChangeEvent(pin, state);
090    }
091
092    public void setAnalogValue(Pin pin, double value) {
093        // cache pin state
094        getPinCache(pin).setAnalogValue(value);
095
096        // dispatch event
097        dispatchPinAnalogValueChangeEvent(pin, value);
098    }
099
100}