/*
 *
 */
#ifndef _ARDUINO_H_
#define _ARDUINO_H_

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <mbed_api.h>

#ifndef bool
#define bool int
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif

typedef unsigned char byte;

void arduino_init(void);

typedef enum pintype_e {
	pintype_none,
	pintype_gpio,
	pintype_dac,
	pintype_pwmout,
	pintype_analogin,
} PinType;

struct pin_node {
	struct pin_node *next;
	PinName pin;
	PinType type;
	union {
		gpio_t gpio;
#if DEVICE_ANALOGOUT
		dac_t dac;
#endif
		pwmout_t pwmout;
		analogin_t analogin;
	};
	uint32_t period;
	uint32_t pulsewidth;
	int min;
	int max;
};

struct pin_node *get_pin(PinType type, PinName pinno);
PinType get_pin_type(PinName pin);

#define DEFAULT 0
#define INTERNAL 1
#define EXTERNAL 2
#define RAW12BIT 3

void analogReference(int type);
void setPinModeDac(int pin);
void tone(int pin, int frequency, int duration);
void noTone(int pin);
void randomSeed(long seed);
long arduino_random(long min, long max);

typedef enum sci_port {
	SCI_USB0,
	SCI_SCI0P2x,
	SCI_SCI2B,
	SCI_SCI6B,
	SCI_SCI2A,
} SCI_PORT;

bool portToPins(SCI_PORT port, PinName *txpin, PinName *rxpin);

extern serial_t *Serial;
extern serial_t *Serial1;	/*1:Serial1*/
extern serial_t *Serial2;	/*2:Serial3*/
extern serial_t *Serial3;	/*3:Serial2*/
extern serial_t *Serial4;	/*4:Serial6*/
extern serial_t *Serial5;	/*5:Serial7*/

int serial_write(serial_t *serial, const unsigned char *c, int len);
int serial_print(serial_t *serial, const char *c);
int serial_println(serial_t *serial, const char *c);

extern i2c_t Wire1;
extern i2c_t Wire2;
extern i2c_t Wire3;
extern i2c_t Wire4;
extern i2c_t Wire5;

typedef struct File {
	int  fd;
} File;

#define FILE_READ 0x01
#define FILE_WRITE 0x02

int file_write(File *fd, unsigned char *str, int len);
void file_close(File *fd);
int file_size(File *fd);
int file_position(File *fd);
void file_flush(File *fd);
bool file_seek(File *fd, int pos);
int file_read(File *fd);

bool SD_begin();
bool SD_open(File *file, const char *path, int mode);
bool SD_rmdir(const char *path);
bool SD_rename(const char *path1, const char *path2);
bool SD_remove(const char *path);
bool SD_exists(const char *path);
bool SD_mkdir(const char *path);

typedef struct FILEEEP {
	int fd;
} FILEEEP;

int EEP_fopen(FILEEEP *fp, const char *str, char mode);
int EEP_fwrite(FILEEEP *fp, byte *data, int *len);
int EEP_fread(FILEEEP *fp);
void EEP_fclose(FILEEEP *fp);
int EEP_fseek(FILEEEP *fp, int pos, int mode);
int EEP_ffilesize(const char *str);
bool EEP_fEof(FILEEEP *fp);
int EEP_fexist(const char *path);
int EEP_fcopy(const char *src, const char *dst);
int EEP_fdelete(const char *path);

#define EEP_SEEKTOP 1
#define EEP_SEEKCUR 2
#define EEP_SEEKEND 3

#define EEP_READ 0x01
#define EEP_WRITE 0x02
#define EEP_APPEND 0x4

uint8_t EEPROM_write(int addr, uint8_t data);
uint8_t EEPROM_read(int addr);

#define REBOOT_USERAPP 1
void system_reboot(int mode);
int fileloader(const char *progVer, const char *binVer);

extern volatile char ProgVer[];
extern char RubyFilename[64];
extern char ExeFilename[64];

#endif /* _ARDUINO_H_ */
