Cleanup.

git-svn-id: svn://localhost/thomson@15 85ae3b6b-dc8f-4344-a89d-598714f2e4e5
diff --git a/elec/CrO2/software/Makefile b/elec/CrO2/software/Makefile
index d7bb756..0607a12 100644
--- a/elec/CrO2/software/Makefile
+++ b/elec/CrO2/software/Makefile
@@ -13,8 +13,8 @@
 endif
 
 CC		= g++
-CPPFLAGS	= $(USBFLAGS) -O -Wall -I/usr/include/IUP/ -g -std=c++0x
-LIBS	= $(USBLIBS) -L/usr/lib/ -mwindows -liup -lcomctl32 -lole32
+CPPFLAGS	= $(USBFLAGS) -O -Wall -I/usr/include/IUP/ -I/local/include -g -std=c++0x -mthreads
+LIBS	= $(USBLIBS) -L/usr/lib/ -mwindows -liup -lcomctl32 -lole32 -mthreads
 
 PROJECT = CrO2
 PROGRAM = $(PROJECT)$(EXE_SUFFIX)
diff --git a/elec/CrO2/software/cro2.cpp b/elec/CrO2/software/cro2.cpp
index 73b1a63..13b82c7 100644
--- a/elec/CrO2/software/cro2.cpp
+++ b/elec/CrO2/software/cro2.cpp
@@ -18,8 +18,6 @@
 
 int main(int argc, char **argv)
 {
-	int                 nBytes = 0;
-
     if(argc < 2){
 		Gui gui(&argc, &argv);
 		exit(0);
@@ -29,33 +27,22 @@
 		Device& dev = Device::getDevice(); // Constructor inits communication.
 
 		if(strcmp(argv[1], "get") == 0){
+			int                 nBytes = 0;
 			unsigned char       buffer[275];
 			memset(buffer, 0, 275);
 			nBytes = dev.read(buffer, sizeof(buffer));
+			if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
 		}else if(strcmp(argv[1], "put") == 0){
 
 			// load file
 			K5 file(argv[2]);
 
-			for (int k = 0; k < file.getBlockCount(); k++)
-			{
-				// wait for motor on
-				while (dev.getStatus() & 8)
-					Sleep(1000);
-
-				K5::Block block = file.getBlock(k);
-
-				nBytes = dev.write(block.data, block.length - 1, block.type);
-
-				// TODO wait for correct time (read status from usb OR compute from size+type)
-				Sleep(1400);
-			}
+			dev.write(file);
 		}else{
 			// TODO print usage
 			exit(2);
 		}
 
-		if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
 		return 0;
 	}
 	catch(const char* error)
diff --git a/elec/CrO2/software/device.cpp b/elec/CrO2/software/device.cpp
index 2ab3780..b5f0d44 100644
--- a/elec/CrO2/software/device.cpp
+++ b/elec/CrO2/software/device.cpp
@@ -7,6 +7,7 @@
  */

 

 #include "device.h"

+#include "k5.h"

 

 bool Device::initOnce = false;

 Device* Device::instance = NULL;

@@ -140,11 +141,30 @@
 

 }

 

+

+void Device::write(K5& file)

+{

+	for (int k = 0; k < file.getBlockCount(); k++)

+	{

+		// wait for motor on

+		while (getStatus() & 8)

+			Sleep(1000);

+

+		K5::Block block = file.getBlock(k);

+

+		int nBytes = write(block.data, block.length - 1, block.type);

+			// TODO error handling

+

+		// TODO wait for correct time (read status from usb OR compute from size+type)

+		Sleep(1400);

+	}

+}

+

 uint8_t Device::getStatus()

 {

 	uint8_t status;

 	usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,

-		PSCMD_STATUS, 0,0, (char*)&status, 1, 5000);

+			PSCMD_STATUS, 0,0, (char*)&status, 1, 5000);

 		// TODO handle errors (return value)

 	return status;

 }

diff --git a/elec/CrO2/software/device.h b/elec/CrO2/software/device.h
index 4c4d0e9..9b50f43 100644
--- a/elec/CrO2/software/device.h
+++ b/elec/CrO2/software/device.h
@@ -7,8 +7,9 @@
  */

 

 #include <stdint.h>

+#include <lusb0_usb.h>

 

-#include <lusb0_usb.h>    /* this is libusb, see http://libusb.sourceforge.net/ */

+class K5;

 

 class Device

 {

@@ -19,6 +20,7 @@
 

 		int read(uint8_t* buffer, size_t max); // Fill the buffer with data from device

 		int write(uint8_t* buffer, size_t size, int blktype);

+		void write(K5& file);

 		uint8_t getStatus();

 

 	private:

diff --git a/elec/CrO2/software/gui.cpp b/elec/CrO2/software/gui.cpp
index f816645..0911381 100644
--- a/elec/CrO2/software/gui.cpp
+++ b/elec/CrO2/software/gui.cpp
@@ -13,8 +13,9 @@
 #include <iupcontrols.h>

 

 	// Start status poller "thread"

-	int pollStatus(Ihandle* ih)

-	{

+int pollStatus(Ihandle* ih)

+{

+	try {

 		Ihandle* motoron = (Ihandle*)IupGetAttribute(ih, "target");

 

 		uint8_t status = Device::getDevice().getStatus();

@@ -22,8 +23,14 @@
 			IupSetAttribute(motoron, "VALUE", "0"); // motor OFF

 		else

 			IupSetAttribute(motoron, "VALUE", "1"); // motor ON

-		return IUP_DEFAULT;

+	} catch(const char*) {

+		// Silently ignore exception if device is not available - not a good

+		// idea to handle it from a timer...

+		// Keep the timer running so it starts working when the device is

+		// plugged

 	}

+	return IUP_DEFAULT;

+}

 

 void startPolling(Ihandle* target) {

 	Ihandle* timer = IupTimer();

@@ -37,11 +44,6 @@
 

 /* UI */

 

-int Gui::menu_exit()

-{

-	return IUP_CLOSE;

-}

-

 Gui::Gui(int* argc, char*** argv)

 {

 	file = NULL;

@@ -65,7 +67,6 @@
 		NULL

 	);

 

-

 	// CONTROL

 	Ihandle* motoron = IupProgressBar();

 	IupSetAttribute(motoron, "RASTERSIZE", "16x16");

@@ -80,6 +81,9 @@
 	Ihandle* blocklist = IupTree();

 	IupSetAttribute(blocklist, "EXPAND", "VERTICAL");

 

+	Ihandle* playToggle = IupToggle("play", NULL);

+	Callback<Gui, int>::create(playToggle, "ACTION", this, &Gui::setPlaying);

+

 	Ihandle* tabs = IupTabs(

 		IupVbox(

 			IupHbox(

@@ -88,7 +92,7 @@
 				NULL

 			),

 			IupHbox(

-				IupToggle("play",NULL),

+				playToggle,

 				IupToggle("REC",NULL),

 				NULL

 			),

@@ -135,6 +139,7 @@
 	delete file;

 }

 

+

 int Gui::menu_open()

 {

 	char name[65536];

@@ -146,3 +151,21 @@
 	}

 	return IUP_DEFAULT;

 }

+

+int Gui::menu_exit()

+{

+	return IUP_CLOSE;

+}

+

+int Gui::setPlaying(int state)

+{

+	if (state == 0)

+	{

+		// pause

+	} else {

+		// play

+		Device::getDevice().write(*file);

+	}

+

+	return IUP_DEFAULT;

+}

diff --git a/elec/CrO2/software/gui.h b/elec/CrO2/software/gui.h
index b3a5cdc..a32a38f 100644
--- a/elec/CrO2/software/gui.h
+++ b/elec/CrO2/software/gui.h
@@ -19,5 +19,7 @@
 		int menu_open();

 		int menu_exit();

 

+		int setPlaying(int state);

+

 		K5* file;

 };

diff --git a/elec/CrO2/software/k5.cpp b/elec/CrO2/software/k5.cpp
index d3c12bc..f57cde2 100644
--- a/elec/CrO2/software/k5.cpp
+++ b/elec/CrO2/software/k5.cpp
@@ -24,6 +24,19 @@
 	memcpy(data, other.data, length);

 }

 

+const K5::Block& K5::Block::operator=(const Block& other)

+{

+	delete[] data;

+		// May not be null ?

+

+	length = other.length;

+	type = other.type;

+	data = new uint8_t[length];

+	memcpy(data, other.data, length);

+

+	return *this;

+}

+

 K5::Block::~Block()

 {

 	delete[] data;

diff --git a/elec/CrO2/software/k5.h b/elec/CrO2/software/k5.h
index 61edc2f..18e38af 100644
--- a/elec/CrO2/software/k5.h
+++ b/elec/CrO2/software/k5.h
@@ -16,15 +16,19 @@
 				Block(int length, uint8_t type);

 				Block(const Block& other);

 				~Block();

+				const Block& operator=(const Block& other);

 

 				int length;

 				uint8_t* data;

 				uint8_t type;

+			private:

+				Block();

 		};

 

 		int getBlockCount();

 		Block getBlock(int number);

 

 	private:

+		K5();

 		std::vector<Block> blocks;

 };