
/*
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

https://mbed.org/users/chris/code/TMP102/
*/


package test.hara41;

import jp.nyatla.mimic.mbedjs.*;
import jp.nyatla.mimic.mbedjs.javaapi.*;

public class TMP102 {
	public final static int I2C_ADDRESS=0x90;
	private final static byte TEMP_REG_ADDR=0x00;
	private final I2C _i2c;
	private final int _addr;
	/** I2Cを内部生成したか*/
	private final boolean _is_attached;
	/**
	 * 既存のI2Cに追加する場合
	 * @param i_i2c
	 * @param i_address
	 * @throws MbedJsException
	 */
	public TMP102(I2C i_i2c,int i_address) throws MbedJsException
	{
		this._is_attached=false;
		this._i2c=i_i2c;
		this._addr=i_address;
	}
	/**
	 * Mcuから直接生成する場合
	 * @param i_mcu
	 * @param sda
	 * @param scl
	 * @param i_address
	 * @throws MbedJsException
	 */
	public TMP102(Mcu i_mcu, int sda, int scl, int i_address) throws MbedJsException
	{
		this._is_attached=true;
		this._i2c=new I2C(i_mcu, sda, scl);
		this._addr=i_address;
		this._i2c.frequency(10000);
	}
	public void dispose() throws MbedJsException{
		if(this._is_attached){
			this._i2c.dispose();
		}
	}
	/**
	 * 温度を取得する
	 * @return 温度
	 * @throws MbedJsException
	 */
	public float read() throws MbedJsException
	{
		  byte[] tempRegAddr = {TMP102.TEMP_REG_ADDR};
		  
		  this._i2c.write(this._addr, tempRegAddr, false); //Pointer to the temperature register
		 
		  I2C.ReadResult rr;
		  byte [] reg = {0,0};
		  rr = this._i2c.read(this._addr, 2, false); //Rea
		  reg = rr.data;
		  int res  = ((int)reg[0] << 4) | ((int)reg[1] >> 4);  
		  
		  float temp =  (float) ((float)res * 0.0625);
		   
		  return temp;
	}

}

@@ -1238,16 +1238,68 @@
 vaapi.*;
+%0Aimport jp.nyatla.mimic.mbedjs.javaapi.driver.LM75B;
 %0A%0Apublic
@@ -2695,12 +2695,290 @@
 temp;%0A%09%7D
+%0A%09public static void main(String%5B%5D args)%0A%09%7B%0A%09%09try %7B%0A%09%09%09Mcu mcu=new Mcu(%2210.0.0.2%22);%0A%09%09%09TMP102 a=new TMP102(mcu,PinName.p28,PinName.p27,0x90);%0A%09%09%09System.out.println(a.read());%0A%09%09%09mcu.close();%0A%09%09%09System.out.println(%22done%22);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%7D
 %0A%0A%7D%0A

add main function