Index: main.c
===================================================================
--- main.c	(revision 1098)
+++ main.c	(revision 1099)
@@ -1,14 +1,479 @@
 #include <dragonfly_lib.h>
-#include "smart_run_around_fsm.h"
+//#include "smart_run_around_fsm.h"
 
+#include "drive.h"
+
+// Martin "deffi" Herrmann Test
+
+uint8_t motors_on=0;
+
+static void periodic_call ()
+{
+	if (button1_read ())
+	{
+		motors_on=~motors_on;
+	}
+
+	if (motors_on)
+	{
+		int s=(wheel ()/2+127)&0xFF;
+		motor1_set (FORWARD, s);
+		motor2_set (FORWARD, s);
+	}
+	else
+		motors_off ();
+		
+	while (button1_read ()); // Wait for button release *after* setting new motor state
+}
+
+static void spirograph ()
+{
+	uint8_t left=255;
+	uint8_t right_min=160;
+	uint8_t right_max=200;
+	uint8_t delay=100;
+	uint8_t step=2;
+	
+	uint8_t dir=step, right=right_min;
+
+	orb_set (0,255,0);
+	
+	while (1)
+	{
+		if (right<right_min) dir=step;
+		if (right>right_max) dir=-step;
+		
+		right+=dir;
+		
+		motor1_set (FORWARD, left);
+		motor2_set (FORWARD, right);
+		
+		delay_ms (delay);
+	}
+}
+
+static void color_circle ()
+{
+   while(1)
+   {
+		for (uint8_t part=0; part<6; ++part)
+		{
+			for (uint8_t up=0; up<255; ++up)
+			{
+				uint8_t dn=255-up;
+				uint8_t on=255;
+				uint8_t of=0;
+				
+				if (1)
+				{
+					// Maximum brightness mode
+					switch (part)
+					{
+						case 0: orb_set (on, up, of); break;
+						case 1: orb_set (dn, on, of); break;
+						case 2: orb_set (of, on, up); break;
+						case 3: orb_set (of, dn, on); break;
+						case 4: orb_set (up, of, on); break;
+						case 5: orb_set (on, of, dn); break;
+					}
+				}
+				else
+				{
+					// Constant brightness mode (not very constant though)
+					switch (part%3)
+					{
+						case 0: orb_set (dn, up, of); break;
+						case 1: orb_set (of, dn, up); break;
+						case 2: orb_set (up, of, dn); break;
+					}
+				}
+				
+				delay_ms (2);
+				periodic_call (); 
+			}
+		}
+	}
+}
+
+static void startup_sequence()
+{
+	orb_set (255, 0, 0); delay_ms (200);
+	orb_set (0, 0, 0); delay_ms (200);
+	orb_set (255, 0, 0); delay_ms (200);
+	orb_set (0, 0, 0); delay_ms (200);
+	orb_set (255, 0, 0); delay_ms (200);
+	orb_set (0, 0, 0); delay_ms (500);
+}
+
+static void right_controller()
+{
+	encoders_init ();
+	encoder_rst_dx (0); encoder_rst_dx (1);
+
+	while (1)
+	{
+		int el=encoder_get_dx (0);
+		int er=encoder_get_dx (1);
+		
+		int diff=el-er;
+		uint8_t direction=(diff>0)?FORWARD:BACKWARD;
+		
+		if (diff>2)
+			diff+=180;
+		else if (diff<-2)
+			diff-=180;
+		else
+			diff=0;
+
+		if (diff>255) diff=255;
+		if (diff<-255) diff=-255;
+		
+		motor2_set(direction, abs (diff));
+		
+		usb_puti (el); usb_puts (" ");
+		usb_puti (er); usb_puts (" ");
+		usb_puti (diff); usb_puts (" ");
+		usb_puts ("            \r");
+	}		
+}
+
+static void acl()
+{
+	#define redval 255
+	#define greenval 150
+	#define interval 1300
+	#define flash 20
+	#define pause 200
+
+	#define def do { orb1_set(redval,0,0); orb2_set(0,greenval,0); } while (0)
+	#define wht do { orb_set(255,255,255); } while (0)
+	
+	while (1)
+	{
+		periodic_call (); 
+		wht; delay_ms (flash);
+		def; delay_ms (pause-flash);
+		periodic_call (); 
+		wht; delay_ms (flash);
+		def; delay_ms (pause-flash);
+		def; delay_ms (interval-2*pause-2*flash);
+	}
+}
+
+static void redlight ()
+{
+	uint8_t r=255;
+	int8_t d=-1;
+	
+	while (1)
+	{
+		if (r==0) d=1;
+		if (r==255) d=-1;
+		r+=d;
+		
+		orb_set(r,0,0);
+		delay_ms (5);
+	}
+}
+
+static uint8_t hex_digit(uint8_t x)
+{
+	if (x>15) return '?';
+	return "0123456789ABCDEF"[x];
+}
+
+
+static void usb_puth(int i)
+{
+	usb_putc(hex_digit((i>>12)&0xF));
+	usb_putc(hex_digit((i>>8)&0xF));
+	usb_putc(hex_digit((i>>4)&0xF));
+	usb_putc(hex_digit(i&0xF));
+}
+
+static void usb_puth8(uint8_t i)
+{
+	usb_putc(hex_digit((i>>4)&0xF));
+	usb_putc(hex_digit(i&0xF));
+}
+
+static void bom_beacon ()
+{
+	//bom_init (BOM15); // Done by dragonfly init
+
+	//bom_set_leds(1<<4);
+	//bom_set_leds(0xAAAA);
+	bom_set_leds(0xFFFF);
+	bom_on ();
+	redlight ();
+
+}
+
+static int get_color(int x)
+{
+	switch (x)
+	{
+		case 0: return RED;
+		case 1: return YELLOW;
+		case 2: return GREEN;
+		case 3: return CYAN;
+		case 4: return BLUE;
+		case 5: return MAGENTA;
+		default: return WHITE;
+	}
+}
+
+static void set_color_code(int x)
+{
+	if (0<=x && x<6)
+	{
+		orb1_set_color (get_color (x));
+		orb2_set_color (ORB_OFF);
+	}
+	else if (6<=x && x<12)
+	{
+		orb1_set_color (ORB_OFF);
+		orb2_set_color (get_color (x-6));
+	}
+	else if (12<=x && x<18)
+	{
+		orb1_set_color (get_color (x-12));
+		orb2_set_color (get_color (x-12));
+	}
+	else
+	{
+		orb1_set_color (ORB_OFF);
+		orb2_set_color (ORB_OFF);
+	}
+}
+
+static void bom_test()
+{
+	analog_init (0/*ADC_START*/);
+	//bom_init (BOM15); // Done by dragonfly init
+	bom_off ();
+
+	uint8_t num=0;
+
+	#define selected 9
+
+	while (1)
+	{
+		// Each 100 ms, print all values, hex, on one line
+		if (0)
+		{
+			bom_refresh(BOM_ALL);
+			for (uint8_t i=0; i<16; ++i)
+			{
+				usb_puti (i);
+				usb_putc (':');
+				usb_puth8 ((uint8_t)(bom_get(i)));
+				usb_putc (' ');
+			}
+			
+			usb_puts ("\r\n");
+			delay_ms (100);
+		}
+		
+		if (0)
+		{
+			// Average 10 #selected values over 1 second, print dec, one per line
+			uint16_t val=0;
+			for (int i=0; i<10; ++i)
+			{
+				bom_refresh (1<<selected);
+				val+=bom_get(selected);
+				delay_ms (100);
+			}
+			usb_puti(val);
+			usb_puts("\r\n");
+		}
+
+		// Read #selected, print dec//, 16 values per line.
+		if (0)
+		{
+			bom_refresh (1<<selected);
+			usb_puti (selected);
+			usb_puts (": ");
+			usb_puti (bom_get (selected));
+//			num++;
+//			if (num>15)
+//			{
+				usb_puts ("\r\n");
+//				num=0;
+//			}
+//			else
+//				usb_putc (' ');
+			delay_ms (100);
+		}
+		
+		// Print maximum of all, one per line
+		if (1)
+		{
+			bom_refresh (0xFFFF);
+			usb_puti (bom_get_max ());
+			set_color_code(bom_get_max ());
+			usb_puts ("\r\n");
+			delay_ms (100);
+		}
+	}
+}
+
+void rtc_callback ()
+{
+	static uint8_t x=0;
+	
+	motor1_set(x?FORWARD:BACKWARD, 180);
+	motor2_set(x?BACKWARD:FORWARD, 180);
+
+	x=!x;
+}
+
+static void rtc_test ()
+{
+	rtc_init (HALF_SECOND, rtc_callback);
+	while (1);
+}
+
+void drive_controller_test ()
+{
+
+	// Drive straight ahead
+	drive_controller dc;
+	drive_controller_set_parameters_shift (&dc, 0, -16);
+	drive_controller_init (&dc);
+
+	while (1)
+	{
+		// Use the wheel for velocity control
+		drive_controller_set_target_velocity (&dc, wheel ()/2+127);
+		
+		// Do the control
+		drive_controller_control (&dc);
+		
+		delay_ms (1);
+	}
+}
+
+void encoder_timing_test ()
+{
+	//bom_init(BOM15);
+	//bom_set_leds(0xAAAA);
+	//bom_on ();
+
+	DDRF |= 1<<1;
+	//encoders_init ();
+
+
+	while (1)
+	{
+		PORTF|=0x02;
+		PORTF&=0xFD;
+		PORTF|=0x02;
+		PORTF&=0xFD;
+		PORTF|=0x02;
+		PORTF&=0xFD;
+		PORTF|=0x02;
+		PORTF&=0xFD;
+		PORTF|=0x02;
+		PORTF&=0xFD;
+		PORTF|=0x02;
+		PORTF&=0xFD;
+		//bom_set_leds(0x0001);
+		//bom_set_leds(0x0003);
+	}
+
+}
+
+static void duty_cycle_test ()
+{
+	DDRF=0xFF;
+	while (1)
+	{
+		if (button1_read ())
+		{
+			// Button 1 => 1/20 (5%)
+			PORTF=0x02; delay_ms (1);
+			PORTF=0x00; delay_ms (19);
+		}
+		else if (button2_read ())
+		{
+			// Button 2 => 3/20 (15%)
+			PORTF=0x02; delay_ms (3);
+			PORTF=0x00; delay_ms (17);
+		}
+		else
+		{
+			// No button (or both) => Alternating, avg 2/20 (10%)
+			PORTF=0x02; delay_ms (1);
+			PORTF=0x00; delay_ms (19);
+			PORTF=0x02; delay_ms (3);
+			PORTF=0x00; delay_ms (17);
+		}
+	}
+}	
+
 int main(void) {
-	dragonfly_init(ALL_ON);
-    range_init();
-	//orb_enable();
-	usb_puts("Turned on!\n");
-    run_around_init();
-    while(1) {
-        run_around_FSM();
-    }
-	return 0;
+//	dragonfly_init(ALL_ON);
+	dragonfly_init(0);
+	
+	//encoders_init ();
+
+	analog_init(0);
+//    usb_init();
+
+//	encoder_timing_test ();
+
+
+	DDRF=0x02;
+
+	if (button2_read ())
+		duty_cycle_test ();
+	else
+	{
+		orb_init ();
+		orb_enable ();
+		color_circle ();
+	}
+	
+	orb_init ();
+	orb_enable ();
+
+//	while (1)
+//	{
+//		
+//		orb_set (128,128,0);
+//		delay_ms (1000);
+//
+//
+//		orb1_set (128,128,0);
+//		orb2_set (128,128,0);
+//		delay_ms (1000);
+//	}
+
+//	orb1_set (128,0,0);
+//	orb2_set (255,0,0);
+//	while (1);
+	
+    //analog_init(ADC_START);
+
+
+    //range_init();
+	
+	usb_puts ("Startup\r\n");
+
+
+	//bom_beacon ();
+	//bom_test ();
+	//rtc_test ();
+
+	// Do some lighting and wheel controlled motors on BTN1
+	if (!button2_read ())
+		acl ();
+	else
+		color_circle ();
+
+
+	startup_sequence ();
+	drive_controller_test ();
 }
