Index: EcnlProtoTool/trunk/prototool/src/arduino.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/arduino.c	(revision 278)
+++ EcnlProtoTool/trunk/prototool/src/arduino.c	(revision 279)
@@ -1,4 +1,7 @@
 #include <arduino.h>
 #include <mruby.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
 #include "../llbruby.h"
 
@@ -16,7 +19,4 @@
 i2c_t Wire5;
 
-FATFS RomDisk;
-SD Sd;
-
 char ExeFilename[64];
 
@@ -27,9 +27,10 @@
 void randomSeed(long seed)
 {
+	srand(seed);
 }
 
 long arduino_random(long min, long max)
 {
-	return min;
+	return (long)(((double)rand() / (double)RAND_MAX) / ((double)max - (double)min)) + min;
 }
 
@@ -223,16 +224,16 @@
 int EEP_fopen(FILEEEP *fp, const char *str, char mode)
 {
-	FRESULT ret;
-	BYTE fm;
+	int ret;
+	int fm;
 
 	switch (mode){
 	case 0:
-		fm = FA_READ;
+		fm = O_RDONLY;
 		break;
 	case 1:
-		fm = FA_READ | FA_WRITE | FA_CREATE_NEW;
+		fm = O_RDWR | O_CREAT;
 		break;
 	case 2:
-		fm = FA_READ | FA_WRITE;
+		fm = O_RDWR;
 		break;
 	default:
@@ -240,26 +241,35 @@
 	}
 
-	ret = f_open(&fp->fp, str, fm);
-
-	return (ret == FR_OK) ? 0 : -1;
+	ret = open(str, fm);
+	if (ret < 0) {
+		fp->fd = 0;
+		return -1;
+	}
+	fp->fd = ret;
+
+	return 0;
 }
 
 int EEP_fwrite(FILEEEP *fp, byte *data, int *len)
 {
-	FRESULT ret;
-
-	ret = f_write(&fp->fp, data, (UINT)*len, (UINT *)len);
-
-	return (ret == FR_OK) ? 1 : -1;
+	int ret;
+
+	ret = write(fp->fd, data, *len);
+	if (ret < 0) {
+		*len = 0;
+		return -1;
+	}
+	*len = ret;
+
+	return 0;
 }
 
 int EEP_fread(FILEEEP *fp)
 {
-	FRESULT ret;
+	int ret;
 	char c;
-	UINT br;
-
-	ret = f_read(&fp->fp, &c, 1, &br);
-	if (ret != FR_OK)
+
+	ret = read(fp->fd, &c, 1);
+	if (ret < 0)
 		return -1;
 
@@ -269,252 +279,255 @@
 void EEP_fclose(FILEEEP *fp)
 {
-	f_close(&fp->fp);
+	close(fp->fd);
 }
 
 int EEP_fseek(FILEEEP *fp, int pos, int mode)
 {
-	FRESULT ret;
+	int ret;
 
 	switch (mode){
 	case EEP_SEEKTOP:
-		ret = f_lseek(&fp->fp, pos);
+		ret = lseek(fp->fd, pos, SEEK_SET);
 		break;
 	case EEP_SEEKCUR:
-		ret = f_lseek(&fp->fp, fp->fp.fptr + pos);
+		ret = lseek(fp->fd, pos, SEEK_CUR);
 		break;
 	case EEP_SEEKEND:
-		ret = f_lseek(&fp->fp, fp->fp.fsize - pos);
+		ret = lseek(fp->fd, pos, SEEK_END);
 		break;
 	default:
-		ret = FR_INVALID_PARAMETER;
-		break;
-	}
-
-	if (ret != FR_OK)
-		f_lseek(&fp->fp, 0);
-
-	return fp->fp.fptr;
+		return -1;
+		break;
+	}
+
+	if (ret < 0)
+		return false;
+
+	return true;
 }
 
 int EEP_ffilesize(const char *str)
 {
-	FRESULT ret;
-	FILINFO fno;
-
-	ret = f_stat(str, &fno);
-	if (ret != FR_OK)
+	int ret;
+	struct stat fno;
+
+	ret = stat(str, &fno);
+	if (ret < 0)
 		return 0;
 
-	return fno.fsize;
+	return fno.st_size;
 }
 
 bool EEP_fEof(FILEEEP *fp)
 {
-	return f_tell(&fp->fp) == f_size(&fp->fp);
+	int ret, pos;
+	struct stat fno;
+
+	ret = fstat(fp->fd, &fno);
+	if (ret < 0)
+		return false;
+
+	pos = lseek(fp->fd, 0, SEEK_CUR);
+
+	return pos == fno.st_size;
 }
 
 bool EEP_fexist(const char *path)
 {
-	FRESULT ret;
-	FILINFO fno;
-
-	ret = f_stat(path, &fno);
-
-	return (ret == FR_OK);
+	int ret;
+	struct stat fno;
+
+	ret = stat(path, &fno);
+
+	return (ret == 0);
 }
 
 bool EEP_fcopy(const char *src, const char *dst)
 {
-	FIL fsrc, fdst;
-	BYTE buffer[512];
-	FRESULT res;
-	UINT br, bw;
-
-	res = f_open(&fsrc, src, FA_OPEN_EXISTING | FA_READ);
-	if (res != FR_OK)
-		return false;
-
-	res = f_open(&fdst, dst, FA_CREATE_ALWAYS | FA_WRITE);
-	if (res != FR_OK){
-		f_close(&fsrc);
+	int fsrc, fdst;
+	unsigned char buffer[512];
+	int res = true;
+	int br, bw;
+
+	fsrc = open(src, O_RDONLY);
+	if (fsrc < 0)
+		return false;
+
+	fdst = open(dst, O_RDWR | O_CREAT);
+	if (fdst < 0){
+		close(fsrc);
 		return false;
 	}
 
 	for (;;) {
-		res = f_read(&fsrc, buffer, sizeof(buffer), &br);
-		if ((res != FR_OK) || br == 0)
+		br = read(fsrc, buffer, sizeof(buffer));
+		if (br < 0) {
+			res = false;
 			break;
-
-		res = f_write(&fdst, buffer, br, &bw);
-		if ((res != FR_OK) || bw < br)
+		}
+		if (br == 0)
 			break;
-	}
-
-	f_close(&fsrc);
-	f_close(&fdst);
-
-	return (res == FR_OK);
+
+		bw = write(fdst, buffer, br);
+		if ((bw < 0) || bw < br) {
+			res = false;
+			break;
+		}
+	}
+
+	close(fsrc);
+	close(fdst);
+
+	return res;
 }
 
 bool EEP_fdelete(const char *path)
 {
-	FRESULT res;
-
-	res = f_unlink(path);
-
-	return (res == FR_OK);
-}
-
-bool SD_begin()
-{
-	DSTATUS dst;
-	FRESULT res;
-	BYTE pdrv = 1, type;
-
-	if (Sd.FatFs.fs_type != 0)
-		return true;
-
-	if ((dst = disk_initialize(pdrv)) != RES_OK) {
-		return false;
-	}
-
-	if ((dst = disk_ioctl(pdrv, MMC_GET_TYPE, &type)) != RES_OK) {
-		Sd.dst = dst;
-		Sd.type = 0;
-	}
-	else {
-		Sd.dst = RES_OK;
-		Sd.type = type;
-	}
-
-	if ((res = f_mount(&Sd.FatFs, "1:", 1)) != FR_OK) {
-		return false;
-	}
-
+	int res;
+
+	res = unlink(path);
+
+	return (res == 0);
+}
+
+bool SD_open(File *file, const char *path, int mode)
+{
+	int ret;
+
+	if (!SD_begin())
+		return false;
+
+	ret = open(path, mode);
+	if (ret < 0) {
+		file->fd = 0;
+		return false;
+	}
+
+	file->fd = ret;
 	return true;
 }
 
-bool SD_open(File *file, const char *path, int mode)
-{
-	FRESULT ret;
+bool SD_rmdir(const char *path)
+{
+	int ret;
 
 	if (!SD_begin())
 		return false;
 
-	ret = f_open(&file->fp, path, mode);
-
-	return ret == FR_OK;
-}
-
-bool SD_rmdir(const char *path)
-{
-	FRESULT ret;
+	ret = rmdir(path);
+
+	return ret == 0;
+}
+
+bool SD_rename(const char *path1, const char *path2)
+{
+	int ret;
 
 	if (!SD_begin())
 		return false;
 
-	ret = f_unlink(path);
-
-	return ret == FR_OK;
-}
-
-bool SD_rename(const char *path1, const char *path2)
-{
-	FRESULT ret;
+	ret = rename(path1, path2);
+	
+	return ret == 0;
+}
+
+bool SD_remove(const char *path)
+{
+	int ret;
 
 	if (!SD_begin())
 		return false;
 
-	ret = f_rename(path1, path2);
-	
-	return ret == FR_OK;
-}
-
-bool SD_remove(const char *path)
-{
-	FRESULT ret;
+	ret = unlink(path);
+
+	return ret == 0;
+}
+
+bool SD_exists(const char *path)
+{
+	int ret;
+	struct stat fno;
 
 	if (!SD_begin())
 		return false;
 
-	ret = f_unlink(path);
-
-	return ret == FR_OK;
-}
-
-bool SD_exists(const char *path)
-{
-	FRESULT ret;
-	FILINFO fno;
+	ret = stat(path, &fno);
+
+	return ret == 0;
+}
+
+bool SD_mkdir(const char *path)
+{
+	int ret;
 
 	if (!SD_begin())
 		return false;
 
-	ret = f_stat(path, &fno);
-
-	return ret == FR_OK;
-}
-
-bool SD_mkdir(const char *path)
-{
-	FRESULT ret;
-
-	if (!SD_begin())
-		return false;
-
-	ret = f_mkdir(path);
-
-	return ret == FR_OK;
-}
-
-int file_write(File *fd, unsigned char *str, int len)
-{
-	FRESULT ret;
-	UINT bw;
-
-	ret = f_write(&fd->fp, str, len, &bw);
-	if (ret != FR_OK)
-		return -1;
-
-	return bw;
-}
-
-void file_close(File *fd)
-{
-	f_close(&fd->fp);
-}
-
-int file_size(File *fd)
-{
-	return f_size(&fd->fp);
-}
-
-int file_position(File *fd)
-{
-	return fd->fp.fptr;
-}
-
-void file_flush(File *fd)
-{
-	f_flush(&fd->fp);
-}
-
-bool file_seek(File *fd, int pos)
-{
-	FRESULT ret;
-
-	ret = f_lseek(&fd->fp, pos);
-
-	return ret != FR_OK;
-}
-
-int file_read(File *fd)
-{
-	FRESULT ret;
+	ret = mkdir(path, 0777);
+
+	return ret == 0;
+}
+
+int file_write(File *fp, unsigned char *str, int len)
+{
+	int ret;
+
+	ret = write(fp->fd, str, len);
+	if (ret < 0)
+		return -1;
+
+	return ret;
+}
+
+void file_close(File *fp)
+{
+	close(fp->fd);
+}
+
+int file_size(File *fp)
+{
+	int ret;
+	struct stat fno;
+
+	ret = fstat(fp->fd, &fno);
+	if (ret < 0)
+		return -1;
+
+	return fno.st_size;
+}
+
+int file_position(File *fp)
+{
+	int ret;
+	struct stat fno;
+
+	ret = lseek(fp->fd, 0, SEEK_CUR);
+	if (ret < 0)
+		return -1;
+
+	return ret;
+}
+
+void file_flush(File *fp)
+{
+	fsync(fp->fd);
+}
+
+bool file_seek(File *fp, int pos)
+{
+	int ret;
+
+	ret = lseek(fp->fd, pos, SEEK_SET);
+
+	return ret == 0;
+}
+
+int file_read(File *fp)
+{
+	int ret;
 	char c;
-	UINT br;
-
-	ret = f_read(&fd->fp, &c, 1, &br);
-	if (ret != FR_OK)
+
+	ret = read(fp->fd, &c, 1);
+	if (ret < 0)
 		return -1;
 
@@ -524,5 +537,5 @@
 void system_reboot(int mode)
 {
-
+	exit(mode);
 }
 
@@ -531,27 +544,2 @@
 	return 0;
 }
-
-DWORD get_fattime(void)
-{
-	time_t temp;
-	struct tm _tm;
-
-	time(&temp);
-	gmtime_r(&temp, &_tm);
-
-	return	  ((DWORD)(_tm.tm_year - 1980) << 25)
-		| ((DWORD)_tm.tm_mon << 21)
-		| ((DWORD)_tm.tm_mday << 16)
-		| ((DWORD)_tm.tm_hour << 11)
-		| ((DWORD)_tm.tm_min << 5)
-		| ((DWORD)_tm.tm_sec >> 1);
-}
-
-void mrb_gr_peach_gem_init(mrb_state *mrb)
-{
-}
-
-void mrb_gr_peach_gem_final(mrb_state *mrb)
-{
-
-}
