/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package horter_i2c;

import javax.comm.*;
/**
 *
 * @author Philipp
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        // Open Serial Connection for I2C Interface
        try
        {
            I2CBus i2cBus = new I2CBus();
            SerialPort serialport = i2cBus.connect("COM1"); // Linux: /dev/ttyS0
            i2cBus.i2cInit(serialport);
            i2cBus.i2cStart(serialport);
            i2cBus.i2cStop(serialport);
            
             // Tempsensor 1
            i2cBus.i2cStart(serialport);
            // Register setzen
            if(i2cBus.i2cSlave(serialport, 158))
            {
                i2cBus.i2cOut(serialport, 0);
                i2cBus.i2cStop(serialport);
            }

            i2cBus.i2cStart(serialport);
            // Register Setzen
            if(i2cBus.i2cSlave(serialport, 159))
            {
                int by1 = i2cBus.i2cIn(serialport);
                i2cBus.i2cAck(serialport);
                int by2 = i2cBus.i2cIn(serialport);
                i2cBus.i2cNoAck(serialport);
                i2cBus.i2cStop(serialport);

                double wert=0.0;

                if((by1 & 128) == 0)
                {
                    wert = by1;
                }
                else
                {
                    wert = by1 - 255;
                }

                if((by2 & 128) != 0)
                {
                    wert = wert + 0.5;
                }

                System.out.println("Es hat "+wert+" °C");
            }
        }
        catch(java.lang.Exception e)
        {
            System.out.println(e);
        }
    }
}
