001package com.pi4j.io.spi;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SpiFactory.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032import com.pi4j.io.spi.impl.SpiDeviceImpl;
033
034import java.io.IOException;
035
036/**
037 * SPI factory - it returns instances of {@link com.pi4j.io.spi.SpiDevice} interface.
038 *
039 * @author Robert Savage (<a
040 *         href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>)
041 */
042public class SpiFactory {
043
044    // private constructor
045    private SpiFactory() {
046        // forbid object construction
047    }
048
049    /**
050     * Create new SpiDevice instance with a default SPI speed of 1 MHz.
051     *
052     * @param channel
053     *            spi channel to use
054     *
055     * @return Return a new SpiDevice impl instance.
056     *
057     * @throws java.io.IOException
058     */
059    public static SpiDevice getInstance(SpiChannel channel) throws IOException {
060        return new SpiDeviceImpl(channel);
061    }
062
063    /**
064     * Create new SpiDevice instance
065     *
066     * @param channel
067     *            spi channel to use
068     * @param mode
069     *            spi mode (see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers)
070     *
071     * @return Return a new SpiDevice impl instance.
072     *
073     * @throws java.io.IOException
074     */
075    public static SpiDevice getInstance(SpiChannel channel, SpiMode mode) throws IOException {
076        return new SpiDeviceImpl(channel, mode);
077    }
078
079    /**
080     * Create new SpiDevice instance
081     *
082     * @param channel
083     *            spi channel to use
084     * @param speed
085     *            spi speed/rate (in Hertz) for channel to communicate at
086     *            (range is 500kHz - 32MHz)
087     *
088     * @return Return a new SpiDevice impl instance.
089     *
090     * @throws java.io.IOException
091     */
092    public static SpiDevice getInstance(SpiChannel channel, int speed) throws IOException {
093        return new SpiDeviceImpl(channel, speed);
094    }
095
096    /**
097     * Create new SpiDevice instance
098     *
099     * @param channel
100     *            spi channel to use
101     * @param speed
102     *            spi speed/rate (in Hertz) for channel to communicate at
103     *            (range is 500kHz - 32MHz)
104     * @param mode
105     *            spi mode (see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers)
106     *
107     * @return Return a new SpiDevice impl instance.
108     *
109     * @throws java.io.IOException
110     */
111    public static SpiDevice getInstance(SpiChannel channel, int speed, SpiMode mode) throws IOException {
112        return new SpiDeviceImpl(channel, speed, mode);
113    }
114
115}