package jGPIO.I2C;

import anywheresoftware.b4a.keywords.Bit;

public class I2CException extends Exception{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8227969179418888752L;
	public final byte bytes[];
	public I2CException(String msg) {
		super(msg);
		bytes = null;
	}
	public I2CException(String msg, final byte[] bytes) {
		super(msg);
		this.bytes = bytes;
	}
	public I2CException(String msg, byte command) {
		super(msg);
		this.bytes = new byte[] {command};
	}
	
	public I2CException(String function, String msg, byte command) {
		super("Function="+function+", Message="+msg);
		this.bytes = new byte[] {command};
	}
	
	public I2CException(String function, String msg, byte[] bytes) {
		super("Function="+function+", Message="+msg);
		this.bytes = bytes;
	}
	
	@Override
	public String toString() {
		StringBuilder str = new StringBuilder();
		str.append(this.getMessage());
		if (bytes!=null) {
			if (bytes.length>0) {
				str.append(" [ ");
				for(byte xx:bytes) {
					str.append(Bit.ToHexString(Bit.And(xx, 0xFF))).append(" ");
				}
				str.append("]");
			}
		}
		return str.toString();
	}
}
