001package com.pi4j.io.i2c; 002 003/* 004 * #%L 005 * ********************************************************************** 006 * ORGANIZATION : Pi4J 007 * PROJECT : Pi4J :: Java Library (Core) 008 * FILENAME : I2CFactory.java 009 * 010 * This file is part of the Pi4J project. More information about 011 * this project can be found here: http://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2016 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 java.io.IOException; 033import java.util.concurrent.TimeUnit; 034 035import com.pi4j.io.i2c.impl.I2CFactoryProviderRaspberryPi; 036 037/** 038 * I2C factory - it returns instances of {@link I2CBus} interface. 039 * 040 * @author Robert Savage (<a href="http://www.savagehomeautomation.com">http://www .savagehomeautomation.com</a>) 041 */ 042public class I2CFactory { 043 044 public static final long DEFAULT_LOCKAQUIRE_TIMEOUT = 1000; 045 046 public static final TimeUnit DEFAULT_LOCKAQUIRE_TIMEOUT_UNITS = TimeUnit.MILLISECONDS; 047 048 public static class UnsupportedBusNumberException extends Exception { 049 private static final long serialVersionUID = 1L; 050 051 public UnsupportedBusNumberException() { 052 super(); 053 } 054 } 055 056 volatile static I2CFactoryProvider provider = new I2CFactoryProviderRaspberryPi(); 057 058 // private constructor 059 private I2CFactory() { 060 // forbid object construction 061 } 062 063 /** 064 * Create new I2CBus instance. 065 * <p> 066 * The timeout for locking the bus for exclusive communication is set to DEFAULT_LOCKAQUIRE_TIMEOUT. 067 * 068 * @param busNumber The bus number 069 * @return Return a new I2CBus instance 070 * @throws UnsupportedBusNumberException If the given bus-number is not supported by the underlying system 071 * @throws IOException If communication to i2c-bus fails 072 * @see I2CProvider#DEFAULT_LOCKAQUIRE_TIMEOUT 073 * @see I2CProvider#DEFAULT_LOCKAQUIRE_TIMEOUT_UNITS 074 */ 075 public static I2CBus getInstance(int busNumber) throws UnsupportedBusNumberException, IOException { 076 return provider.getBus(busNumber, DEFAULT_LOCKAQUIRE_TIMEOUT, DEFAULT_LOCKAQUIRE_TIMEOUT_UNITS); 077 } 078 079 /** 080 * Create new I2CBus instance. 081 * 082 * @param busNumber The bus number 083 * @param lockAquireTimeout The timeout for locking the bus for exclusive communication 084 * @param lockAquireTimeoutUnit The units of lockAquireTimeout 085 * @return Return a new I2CBus instance 086 * @throws UnsupportedBusNumberException If the given bus-number is not supported by the underlying system 087 * @throws IOException If communication to i2c-bus fails 088 */ 089 public static I2CBus getInstance(int busNumber, long lockAquireTimeout, TimeUnit lockAquireTimeoutUnit) throws UnsupportedBusNumberException, IOException { 090 return provider.getBus(busNumber, lockAquireTimeout, lockAquireTimeoutUnit); 091 } 092 093 /** 094 * allow changing the provider for the factory 095 * 096 * @param factoryProvider 097 */ 098 public static void setFactory(I2CFactoryProvider factoryProvider) { 099 provider = factoryProvider; 100 } 101 102}