package SBC.NanoPi;

import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class LibFriendlyArmHardware {
	static {
		try {
			Native.register(LibFriendlyArmHardware.class,"fahw");
			System.out.println("registered to library fahw");
		}catch(Exception e) {
			System.out.println("Unable to register fahw library");
		}
		
	}
	
	// Board
	public final static int BOARD_MINI6410 = 6410;
	public final static int BOARD_MINI210 = 210;
	public final static int BOARD_TINY4412 = 4412;
	public final static int BOARD_NANOPI_M1 = 68330; // H3
	public final static int BOARD_NANOPI_2 = 44180;
	public final static int BOARD_NANOPI_2_FIRE = 44184;
	public final static int BOARD_NANOPI_M2 = 44185;
	public final static int BOARD_NANOPC_T2 = 44181;
	public final static int BOARD_NANOPI_M3 = 68187;
	public final static int BOARD_NANOPC_T3 = 68181;
	
	native public int boardInit();
	//void clearLastError();		// gak jelas
	native public int writeValueToFile(String fileName, byte[] buff);
	native public int writeIntValueToFile(String fileName, int value);
	native public int readValueFromFile(String fileName, byte[] buff, int len);
	native public int readIntValueFromFile(String fileName);
	
	// Note : const char *devname --> string devName
	native public int openHW(String devName, int flags);
	// Note : const void *_data --> Pointer _data
	native public int writeHW(int fd, final Pointer _data, int len);	
	native public int readHW(int fd, Pointer _data, int len);			
	native public int selectHW(int fd, int sec, int usec);
	native public void closeHW(int fd);
	native public int ioctlWithIntValue(int fd, int cmd, int value);
	
	
	
	// GPIO
	// GPIO Direction
	public final static int GPIO_IN = 1;
	public final static int GPIO_OUT = 2;
	// GPIO Value
	public final static int GPIO_LOW = 0;
	public final static int GPIO_HIGH = 1;
	
	native public int initPinGPIO(int board);
	native public int pintoGPIO(int pin);
	native public int exportGPIOPin(int pin);
	native public int unexportGPIOPin(int pin);
	native public int setGPIODirection(int pin, int direction);
	native public int getGPIODirection(int pin);
	native public int setGPIOValue(int pin, int value);
	native public int getGPIOValue(int pin);
	native public int getLedState(int ledID);
	native public int setLedState(int ledID, int ledState);
	
	//I2C
	native public int I2CReadByte(int fd, int wait_ms);
	native public int I2CReadByteFrom(int fd, int pos, int wait_ms);
	// Note : unsigned char byteData --> byte byteData
	native public int I2CWriteByte(int fd, byte byteData, int wait_ms);
	// Note : unsigned char byteData --> byte byteData
	native public int I2CWriteByteTo(int fd, int pos, byte byteData, int wait_ms);
	native public int setI2CRetries(int fd, int retries);
	native public int setI2CSlave(int fd, int slave);
	native public int setI2CTimeout(int fd, int timeout);
	// gak ada ternyata
	//native public int openI2CDevice();
	
	//SPI
	// SPI Bit Order
	public static final int LSB_First = 0;
	public static final int MSB_First = 1;
	// SPI Mode
	public static final int SPI_Mode0 = 0 ;	///< CPOL  (0, CPHA  (0
	public static final int SPI_Mode1 = 1 ;	///< CPOL  (0, CPHA  (1
	public static final int SPI_Mode2 = 2 ; 	///< CPOL  (1, CPHA  (0
	public static final int SPI_Mode3 = 3 ;	///< CPOL  (1, CPHA  (1
	native public int SPItransferBytes(int spi_fd, byte[] writeData, int writeLen, byte[] readBuffer, int readLen, int spi_delay, int spi_speed, int spi_bits);
	native public int SPItransferOneByte(int spi_fd, byte byteData, int spi_delay, int spi_speed, int spi_bits);
	native public int readBytesFromSPI(int spi_fd, byte[] readBuffer, int readLen, int spi_delay, int spi_speed, int spi_bits);
	native public int writeBytesToSPI(int spi_fd, byte[] writeData, int writeLen, int spi_delay, int spi_speed, int spi_bits);
	native public int setSPIBitOrder( int spi_fd, int order);
	native public int setSPIDataMode( int spi_fd, int mode);
	// Note : unsigned int spi_speed --> int spi_speed
	native public int setSPIMaxSpeed(int spi_fd, int spi_speed);
	native public int setSPIReadBitsPerWord( int spi_fd, int bits );
	native public int setSPIWriteBitsPerWord( int spi_fd, int bits );
	
	
	
	// OLED
	native public int OLEDInit(int resetPin, int cmdDatPin);
	native public int OLEDCleanScreen(int devFD);
	// Note : char ch[] --> byte[] ch
	native public int OLEDDisp8x16Str(int devFD, int x, int y, byte[] ch);
	// Note : char ch --> byte ch
	native public int OLEDDisp8x16Char(int devFD, int x, int y, byte ch);
	native public void OLEDDeInit(int devFD);
	
	// LCD1602

	// LCD1602 Using PCF8574
	native public int LCD1602Clear(int devFD);
	native public void LCD1602DeInit(int devFD);
	// Note : unsigned char x --> byte x, unsigned char y --> byte y, unsigned char data --> byte data
	native public int LCD1602DispChar(int devFD, byte x, byte y, byte data);
	// Note : char* line1, line2 --> String line1, line2
	native public int LCD1602DispLines(int devFD, String line1, String line2);
	// Note : unsigned char x, y --> byte x, y , char* str --> String str
	native public int LCD1602DispStr(int devFD, byte x, byte y, String str);
	native public int LCD1602Init(int i2cDev);

	// LCD1602 using MCP23017
	native public int LCD1602GetKey(int devFD);
	native public int LCD1602KeyClear(int devFD);
	native public void LCD1602KeyDeInit(int devFD);
	// Note : unsigned char x, y, data --> byte x, y, data 
	native public int LCD1602KeyDispChar(int devFD, byte x, byte y, byte data);
	// Note : char* line1, line2 --> String line1, line2
	native public int LCD1602KeyDispLines(int devFD, String line1, String line2);
	// Note : unsigned char x, y --> byte x, y.  char *str --> String str
	native public int LCD1602KeyDispStr(int devFD, byte x, byte y, String str);
	native public int LCD1602KeyInit(int i2cDev);
	
	// HCSR04
	native public void Hcsr04DeInit();
	native public int Hcsr04Init(int echoPin);
	// Note int *data --> int[] data
	
	/**
	 * Hcsr04Read
	 * @param data integer array with length = 1
	 * @return not checked yet
	 */
	native public int Hcsr04Read(int[] data);
	
	// PWM
	public static final int PWM0 = 96 + 1; // GPIO D1
	public static final int PWM1 = 64+13;	// GPIO C13
	public static final int PWM2 = 64 + 14;	// GPIO C14
	native public int PWMPlay(int pin, int freq, int duty);
	native public int PWMStop(int pin);
	native public int initPwmGPIO(int board);
	native public int pwmtoGPIO(int pwm);
	
	//ADXL34X
	// Note : char *position --> String position
	native public int adxl34xRead(String position);
	
	// BMP180
	// NOte : int *data --> int[] data
	/**
	 * BMP180 read
	 * @param type what to read
	 * @param data array of integer with length = 1
	 * @return not checked yet
	 */
	native public int bmp180Read(int type, int[] data);
	
	// DHT11
	public static final int DHT_TEMP = 1;
	public static final int DHT_HUMIDITY = 2;
	// Note : int *data --> int[] data
	/**
	 * DHT read
	 * @param type what to read (DHT_TEMP or DHT_HUMIDITY)
	 * @param data array of integer with length = 1
	 * @return not checked yet
	 */
	native public int dht11Read(int type, int[] data);
	
	// DS18B20
	// Note : char * temperature --> byte[] temperature
	
	/**
	 * Read from DS18B20
	 * @param temperature array of byte with length = 1
	 * @return not checked yet
	 */
	native public int ds18b20Read(byte[] temperature);
	
	// HMC5883
	native public void hmc5883DeInit(int devFD);
	native public int hmc5883Init(int i2cDev);
	native public double hmc5883Read(int devFD);
	
	// MCP23017
	native public void mcpDeInit(int devFD);
	native public int mcpInit(int i2cDev);
	// Note : unsigned char command --> byte command
	native public int mcpWriteCmd4(int devFD, byte command);
	native public int mcpWriteCmd8(int devFD, byte command);
	// Note : unsigned char data --> byte data
	native public int mcpWriteData4(int devFD, byte data);
	native public int mcpWriteData8(int devFD, byte data);
	
	// PCF8574
	native public void pcf8574DeInit(int devFD);
	native public int pcf8574Init(int i2cDev);
	// Note : unsigned char command --> byte command
	native public int pcf8574WriteCmd4(int devFD, byte command);
	native public int pcf8574WriteCmd8(int devFD, byte command);
	// Note : unsigned char data --> byte data
	native public int pcf8574WriteData4(int devFD, byte data);
	native public int pcf8574WriteData8(int devFD, byte data);
	
	// PCF8591
	// Note : int *value --> int[] value
	/**
	 * Read from PCF8591
	 * @param channel channel number
	 * @param value array of integer with length = 1
	 * @return not checked yet
	 */
	native public int pcf8591Read(int channel, int[] value);
	
	// Rotary Encoder
	native public int rotaryEncoderDeInit();
	native public int rotaryEncoderInit(int swPin, int siaPin, int sibPin);
	// Note : int *data --> int[] data
	/**
	 * Read from Rotary encodier
	 * @param type what to read
	 * @param data aray of integer with length = 1
	 * @return not checked yet
	 */
	native public int rotaryEncoderRead(int type, int[] data);
}
