/* For free support for VSIDE, please visit www.vsdsp-forum.com */

// Starting point template for creating VSOS applications.
// This will create a <projectname>.APP file, which you can copy to 
// your VS1005 Developer Board and run it from there.

// If you rename your application to INIT.APP, then the kernel will
// load it automatically after booting.

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include "devUMass.h"
#include <ctype.h>
#include <string.h>
#include <kernel.h>

DEVICE usbFlash[4];
char usbSystemDevices[4] = {0};
char origParam[5] = "";


int ParseAndCreate(void) {
	u_int16 nDisks, nShowDisks;
	u_int16 i;

	nDisks = DevUMassProbe();
	
#if 0
	if (nDisks == 0) {
		fprintf(vo_stderr, "No device.\n");
		return S_ERROR;
	}
#endif

	fprintf(vo_stderr,"%d drive(s).\n", nDisks);
	nShowDisks = strlen(origParam);
	if (nShowDisks > 4) {
		nShowDisks = i;
	}
	if (nDisks > 4) nDisks = 4;
	
	for (i=0; i<nShowDisks; i++) {
		char c = toupper(origParam[i]);
		if (i >= nDisks) {
			CreateUsbFlashDisk(&usbFlash[i], i-256);
			fprintf(vo_stderr, "%c: Invalid\n", c);
		} else {
			if (CreateUsbFlashDisk(&usbFlash[i], i) == S_OK) {
				fprintf(vo_stderr, "%c: %s\n", c, usbFlash[i].Identify(&usbFlash[i], NULL, 0));
			} else {
			  fprintf(vo_stderr, "%c: No disk\n", c);
			}
		}
		usbSystemDevices[i] = c;
		vo_pdevices[c-'A'] = &usbFlash[i];
	}

	return S_OK;
}


int main(char *parameters) {
	strncpy(origParam, parameters, 4);
	
	RunProgram("SetClock", "usb");

	ParseAndCreate();

	return S_OK;
}

	
void fini() {
	u_int16 i;
	for (i=0; i<4; i++) {
		if (usbSystemDevices[i]) {
			printf("Close device %c:\n", usbSystemDevices[i]);
			vo_pdevices[usbSystemDevices[i]-'A'] = NULL;
		}
	}
}
