/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package horter_i2c;

import javax.comm.*;

public class I2CBus
{
    public I2CBus()
    {
        super();
    }
    
    // Set the SCL (RTS on the Com)
    public void SCL(SerialPort serialport, boolean wert)
    {
        serialport.setRTS(wert);
    }
    
    // Set the SDA (DTR on the Com)
    public void SDA(SerialPort serialport, boolean wert)
    {
        serialport.setDTR(wert);
    }

    // Get the SDA (DSR on the Com)
    public boolean SDA_in(SerialPort serialport)
    {
        boolean sda_in=false;
        if(serialport.isDSR())
        {
            sda_in = true;
        }
        else
        {
            sda_in = false;
        }
        return sda_in;
    }

    // Get the INT (CTS on the Com)
    public boolean I2C_INT(SerialPort serialport)
    {
        boolean I2C_INT=false;
        if(serialport.isCTS())
        {
            I2C_INT = true;
        }
        else
        {
            I2C_INT = false;
        }
        return I2C_INT;
    }

    // Send the I2C Initiation Sequence
    public void i2cInit(SerialPort serialport)
    {
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        if(!this.SDA_in(serialport))
        {
            System.out.println("Keine Antwort vom I2C Interface");
        }
    }

    // Send the I2C Start Sequence
    public void i2cStart(SerialPort serialport)
    {
        this.SDA(serialport, false);
        this.SCL(serialport, false);
    }

    // Send the I2C Stop Sequence
    public void i2cStop(SerialPort serialport)
    {
        this.SDA(serialport, false);
        this.SCL(serialport, true);
        this.SDA(serialport, true);
    }
    
    // Send the I2C Ack Sequence
    public void i2cAck(SerialPort serialport)
    {
        this.SDA(serialport, false);
        this.SCL(serialport, true);
        this.SCL(serialport, false);
        this.SDA(serialport, true);
    }
    
    // Send the I2C NoAck Sequence
    public void i2cNoAck(SerialPort serialport)
    {
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        this.SCL(serialport, false);
    }
    
    // Access the Slave (Adress)
    public boolean i2cSlave(SerialPort serialport, int Adresse)
    {
        boolean i2cslave = false;
        int bit=128;
        for(int n=1; n<9; n++)
        {
            if((bit & Adresse) == 0)
            {
                this.SDA(serialport, false);
            }
            else
            {
                this.SDA(serialport, true);
            }
        
            this.SCL(serialport, true);
            this.SCL(serialport, false);
            bit = bit/2;
        }
        
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        
        if(this.SDA_in(serialport))
        {
            System.out.println("Kein I2C Slave an der Adresse "+Adresse);
            i2cslave = false;
        }
        else
        {
            i2cslave = true;
        }
        this.SCL(serialport, false);
        return i2cslave;
    }
    
    // Reading Data from the I2C Interface
    public int i2cIn(SerialPort serialport)
    {
        int bit=128;
        int wert=0;
        this.SDA(serialport, true);
        for(int n=1; n<9; n++)
        {
            this.SCL(serialport, true);
            if(this.SDA_in(serialport))
            {
                wert = wert + bit;
            }
            this.SCL(serialport, false);
            bit = bit/2;
        }
        
        return wert;
    }
    
    // Sending Data to the I2C Interface
    public void i2cOut(SerialPort serialport, int wert)
    {
        int bit=128;
        for(int n=1; n<9; n++)
        {
            if((wert & bit) == bit)
            {
                this.SDA(serialport, true);
            }
            else
            {
                this.SDA(serialport, false);
            }
            this.SCL(serialport, true);
            this.SCL(serialport, false);
            bit = bit/2;
        }
        this.SDA(serialport, true);
        this.SCL(serialport, true);
        
        if(this.SDA_in(serialport))
        {
            System.out.println("Keine quittierung der Daten vom Slave!");
        }
        this.SCL(serialport, false);
    }
    
    // Connect to the Interface vie Serial Port
    public SerialPort connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
        SerialPort serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

        return serialPort;
    }
}