package dk.cmol.arduinorgb_controller;

import java.util.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.PorterDuff;

public class ArduinoRGBActivity extends Activity {
	
	public boolean lamp_toggle[] = {false,false,false,false};
	private ArduinoSocket sock = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_arduino_rgb);
		sock = new ArduinoSocket(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.arduino_rgb, menu);
		return true;
	}
	
	
	// Toggle lamp sequence
	public void toggleLamp(View v) {
		Button button = (Button) v;
		int pos = Integer.parseInt(button.getText().toString()) - 1;
		
		if (lamp_toggle[pos]) {
			button.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
		}
		else {
			button.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
		}
		
		// Change value of button
		lamp_toggle[pos] = !lamp_toggle[pos];
		
		TextView tw = (TextView) findViewById(R.id.debug_1);
		tw.setText(lamp_toggle[pos] ? "TRUE" : "FALSE");
		
	}
	
	// React to a pressed color
	public void colorPress(View v) {
		
		TextView tw = (TextView) findViewById(R.id.debug_1);
		
		// Check to see if any lamps are toggled on
		boolean go = false;
		for (int i = 0; i < lamp_toggle.length; i++) {
			if (lamp_toggle[i]) {
				go = true;
				break;
			}
		}
		
		if (go) {
			
			// Get color value
			String str_cols[] = v.getTag().toString().split("-");
			int cols[] = new int[3];
			for (int i = 0; i < str_cols.length; i++) {
				cols[i] = Integer.parseInt(str_cols[i]);
			}
			
			// Find out which lamp(s) to light
			boolean all_lamps = true;
			for (int i = 0; i < lamp_toggle.length; i++) {
				if (!lamp_toggle[i]) {
					all_lamps = false;
					break;
				}
			}
			
			int ctrl = 128;
			
			byte[] a = {(byte) ctrl,(byte) cols[0],(byte) cols[1],(byte) cols[2]};
			sock.write(a);
			
			tw.setText(str_cols[0]+"-"+str_cols[1]+"-"+str_cols[1]+((all_lamps) ? "TRUE" : "FALSE"));
			
		}
	}

}

@@ -443,16 +443,40 @@
  = null;
+%0A%09private LampParser lp;
 %0A%0A%09@Over
@@ -655,16 +655,41 @@
 (this);%0A
+%09%09lp = new LampParser();%0A
 %09%7D%0A%0A%09@Ov
@@ -1772,616 +1772,64 @@
 %0A%09%09%09
-// Get color value%0A%09%09%09String str_cols%5B%5D = v.getTag().toString().split(%22-%22);%0A%09%09%09int cols%5B%5D = new int%5B3%5D;%0A%09%09%09for (int i = 0; i %3C str_cols.length; i++) %7B%0A%09%09%09%09cols%5Bi%5D = Integer.parseInt(str_cols%5Bi%5D);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09// Find out which lamp(s) to light%0A%09%09%09boolean all_lamps = true;%0A%09%09%09for (int i = 0; i %3C lamp_toggle.length; i++) %7B%0A%09%09%09%09if (!lamp_toggle%5Bi%5D) %7B%0A%09%09%09%09%09all_lamps = false;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09int ctrl = 128;%0A%09%09%09%0A%09%09%09byte%5B%5D a = %7B(byte) ctrl,(byte) cols%5B0%5D,(byte) cols%5B1%5D,(byte) cols%5B2%5D%7D;%0A%09%09%09sock.write(a);%0A%09%09%09%0A%09%09%09tw.setText(str_cols%5B0%5D+%22-%22+str_cols%5B1%5D+%22-%22+str_cols%5B1%5D+((all_lamps) ? %22TRUE%22 : %22FALSE%22
+sock.write(lp.fade(lamp_toggle, v.getTag().toString(), 1
 ));%0A

Add LampParser and remove test packet sender