
/** ATP3011 class
 *
 * AquesTalk pico LSI I2C interface
 * Example:
 * @code
 *      #include "ATP3011.h"
 *      ATP3011 talk(P0_10,P0_11); // I2C sda scl
 *      
 *      int main() {
 *          talk.Synthe("konnichiwa.");
 *          for(int n = 1; ; n++) {
 *              char buf[32];
 *              snprintf(buf, sizeof(buf), "<NUMK VAL=%d>.", n);
 *              talk.Synthe(buf);
 *          }
 *      } 
 * @endcode
 *
 */

package test.hara41;

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

public class ATP3011 extends DriverBaseClass{
	public final static int I2C_ADDRESS=(0x2E<<1); 
	public final static int AQTK_STARTUP_WAIT_MS = 80;
	public final static int AQTK_POLL_WAIT_MS = 10;
	 
	private final I2C _i2c;
	private final int _addr;
	private Timer _poll_wait;
	/** I2Cを内部生成したか*/
	private final boolean _is_attached;
	/**
	 * 既存のI2Cに追加する場合
	 * @param i_i2c
	 * @param i_address
	 * @throws MbedJsException
	 */
	public ATP3011(I2C i_i2c,int i_address) throws MbedJsException
	{
		this._is_attached=false;
		this._i2c=i_i2c;
		this._addr=i_address;
		this._initDevice();
	}
	/**
	 * Mcuから直接生成する場合
	 * @param i_mcu
	 * @param sda
	 * @param scl
	 * @param i_address
	 * @throws MbedJsException
	 */
	public ATP3011(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);
		this._initDevice();
	}
	private void _initDevice()
	{
	    this._poll_wait.reset();
	    this._poll_wait.start();
	}

	public void dispose() throws MbedJsException{
		if(this._is_attached){
			this._i2c.dispose();
		}
	}

 
	public boolean IsActive(int timeout_ms)
	{
	    sleep_ms(ATP3011.AQTK_STARTUP_WAIT_MS);
	    Timer t;
	    t.reset();
	    t.start();
	    while(t.read_ms() < timeout_ms) {
	        this._poll_wait.reset();
	        if (this._i2c.write(this._addr,new byte[]{0x00}, 0) == 0) {
	            return true;
	        }
	        sleep_ms(ATP3011.AQTK_POLL_WAIT_MS);
	    }
	    return false;
	}
    
public void Synthe(byte[] msg) throws MbedJsException
{
    while(this.IsBusy()) {
        ;
    }
    this.Write(msg);
    this.Write(new byte[]{'\r'});
}
 
public void Write(byte[] msg) throws MbedJsException
{
    this._i2c.write(_addr, msg, false);    
    this._poll_wait.reset();
}
 
public boolean IsBusy() throws MbedJsException
{
    if (ATP3011.AQTK_POLL_WAIT_MS > this._poll_wait.read_ms()) {
        return true;
    } 
    this._poll_wait.reset();
    I2C.ReadResult rr= this._i2c.read(_addr, 1, false);
    byte c = rr.data[0];
    if (c  != 0) {
        return false;
    }
    return c == '*' || c == 0xff;
}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

@@ -422,16 +422,17 @@
 @endcode
+%C3%9F
 %0A *%0A */%0A
@@ -1774,16 +1774,39 @@
 eout_ms)
+ throws MbedJsException
 %0A%09%7B%0A%09   
@@ -1858,16 +1858,30 @@
  Timer t
+ = new Timer()
 ;%0A%09    t
@@ -2037,17 +2037,21 @@
 %7B0x00%7D, 
-0
+false
 ) == 0) 
@@ -2402,16 +2402,21 @@
 c.write(
+this.
 _addr, m
@@ -2840,16 +2840,39 @@
 %5B%5D args)
+ throws MbedJsException
  %7B%0A%09%09// 
@@ -2903,15 +2903,292 @@
 od stub%0A
+%09%09Mcu mcu = new Mcu(%2210.0.0.2%22);%0A%09%09ATP3011 talk = new ATP3011(mcu,PinName.P0_10 , PinName.P0_11,ATP3011.I2C_ADDRESS);%0A%09%09for(int n=1 ; ; n++)%0A%09%09%7B%0A%09%09%09String str = String.format(%22%3CNUMK VAL=%7B0%7D%3E.%22, n);%0A%09%09%09byte%5B%5D msg = new byte%5B32%5D;%0A%09%09%09msg = str.getBytes();%0A%09%09%09talk.Synthe(msg);%0A%09%09%7D
 %0A%09%7D%0A%0A%7D%0A

add main function I2C address is (0x2E<<1)