package ioio.tests.torture;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;
import android.util.Log;

public class AnalogInputTest implements Test<Boolean> {
	private final IOIO ioio_;
	private final ResourceAllocator alloc_;
	private final int pin1_;
	private final int pin2_;

	public AnalogInputTest(IOIO ioio, ResourceAllocator alloc)
			throws InterruptedException {
		ioio_ = ioio;
		alloc_ = alloc;
		pin1_ = alloc.allocatePinPair(ResourceAllocator.PIN_PAIR_ANALOG);
		pin2_ = pin1_ + 1;
	}

	@Override
	public Boolean run() throws ConnectionLostException, InterruptedException {
		Log.i("IOIOTortureTest", "Starting AnalogInputTest on pins: " + pin1_
				+ ", " + pin2_);
		try {
			if (!runTest(pin1_, pin2_)) {
				return false;
			}
			if (!runTest(pin2_, pin1_)) {
				return false;
			}
		} finally {
			alloc_.freePinPair(pin1_);
		}
		Log.i("IOIOTortureTest", "Passed AnalogInputTest on pins: " + pin1_
				+ ", " + pin2_);
		return true;
	}
	
		AnalogInput in = ioio_.openAnalogInput(inPin);
		DigitalOutput out = ioio_.openDigitalOutput(outPin);
	private boolean runTest(int inPin, int outPin)
			throws ConnectionLostException, InterruptedException {
		try {
			boolean value = false;
			for (int i = 0; i < 10; ++i) {
				out.write(value);
				Thread.sleep(100);
				if (value && in.read() < 0.7 || !value && in.read() > 0.3) {
					Log.w("IOIOTortureTest", "Failed AnalogInputTest input: "
							+ inPin + ", output: " + outPin);
					return false;
				}
				value = !value;
			}
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
		return true;
	}
}

@@ -1056,113 +1056,8 @@
 %0A%09%7D%0A
-%09%0A%09%09AnalogInput in = ioio_.openAnalogInput(inPin);%0A%09%09DigitalOutput out = ioio_.openDigitalOutput(outPin);
 %0A%09pr
@@ -1165,119 +1165,503 @@
 %7B%0A%09%09
-try %7B%0A%09%09%09boolean value = false;%0A%09%09%09for (int i = 0; i %3C 10; ++i) %7B%0A%09%09%09%09out.write(value);%0A%09%09%09%09Thread.sleep(100);%0A
+AnalogInput in = null;%0A%09%09DigitalOutput out = null;%0A%09%09try %7B%0A%09%09%09in = ioio_.openAnalogInput(inPin);%0A%09%09%09out = ioio_.openDigitalOutput(outPin);%0A%09%09%09boolean value = false;%0A%09%09%09for (int i = 0; i %3C 10; ++i) %7B%0A%09%09%09%09out.write(value);%0A%09%09%09%09boolean correct = false;%0A%09%09%09%09// We don't care about latency here, so we're sampling the input%0A%09%09%09%09// at 10ms intervals and will give up only if it doesn't reach%0A%09%09%09%09// the expect value after 50 attempts.%0A%09%09%09%09for (int attempt = 0; !correct && attempt %3C 50; ++attempt) %7B%0A%09
 %09%09%09%09
@@ -1687,13 +1687,13 @@
 d() 
-%3C 0.7
+%3E 0.9
  %7C%7C 
@@ -1716,13 +1716,106 @@
 d() 
-%3E 0.3
+%3C 0.1) %7B%0A%09%09%09%09%09%09correct = true;%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09Thread.sleep(10);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09if (!correct
 ) %7B%0A

Make the Analog Input test succeed faster and fail slower.