package SBC.RaspberryPi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import SBC.BasicSBCInfo;
import anywheresoftware.b4a.BA;

@BA.ShortName("Raspberry_Pi3_Pin")

/**
 * Source :
 * https://docs.particle.io/datasheets/discontinued/raspberrypi-datasheet/
 * Source : https://www.raspberrypi.org/documentation/usage/gpio/
 * 
 * @author rdkartono
 * 
 *         GPIO14 TX UART hardware serial transmit GPIO15 RX UART hardware
 *         serial receive Disabled by default on the Raspberry Pi 3. Must be
 *         enabled by adding enable_uart=1 to /boot/config.txt
 *
 *         GPIO2 SDA I2C data line GPIO3 SCL I2C clock line Disabled by default
 *         on the Raspberry Pi 3. Must be enabled by adding dtparam=i2c_arm=on
 *         to /boot/config.txt
 * 
 *         GPIO9 MISO SPI master-in slave-out GPIO10 MOSI SPI master-out
 *         slave-in Disabled by default on the Raspberry Pi 3. Must be enabled
 *         by adding dtparam=spi=on to /boot/config.txt
 * 
 *         Compatible with Pi-Zero, Pi-Zero-W,
 */
public class Pi3_Pin extends BasicSBCInfo  {

	/**
	 * Equal Pi4J GPIO 8
	 */
	public final int pin03 = 2;

	/**
	 * Equal Pi4J GPIO 9
	 */
	public final int pin05 = 3;

	/**
	 * Equal Pi4J GPIO 7
	 */
	public final int pin07 = 4;

	/**
	 * Equal Pi4J GPIO 0
	 */
	public final int pin11 = 17;

	/**
	 * Equal Pi4J GPIO 2
	 */
	public final int pin13 = 27;

	/**
	 * Equal Pi4J GPIO 3
	 */
	public final int pin15 = 22;

	/**
	 * Equal Pi4J GPIO 12
	 */
	public final int pin19 = 10;

	/**
	 * Equal Pi4J GPIO 13
	 */
	public final int pin21 = 9;

	/**
	 * Equal Pi4J GPIO 14
	 */
	public final int pin23 = 11;

	/**
	 * Equal Pi4J GPIO 30
	 */
	public final int pin27 = 0;
	/**
	 * Equal Pi4J GPIO 21
	 */
	public final int pin29 = 5;

	/**
	 * Equal Pi4J GPIO 22
	 */
	public final int pin31 = 6;

	/**
	 * Equal Pi4J GPIO 23
	 */
	public final int pin33 = 13;

	/**
	 * Equal Pi4J GPIO 24
	 */
	public final int pin35 = 19;

	/**
	 * Equal Pi4J GPIO 25
	 */
	public final int pin37 = 26;

	/**
	 * Equal Pi4J GPIO 15
	 */
	public final int pin08 = 14;

	/**
	 * Equal Pi4J GPIO 16
	 */
	public final int pin10 = 15;

	/**
	 * Equal Pi4J GPIO 1
	 */
	public final int pin12 = 18;

	/**
	 * Equal Pi4J GPIO 4
	 */
	public final int pin16 = 23;

	/**
	 * Equal Pi4J GPIO 5
	 */
	public final int pin18 = 24;

	/**
	 * Equal Pi4J GPIO 6
	 */
	public final int pin22 = 25;

	/**
	 * Equal Pi4J GPIO 10
	 */
	public final int pin24 = 8;

	/**
	 * Equal Pi4J GPIO 11
	 */
	public final int pin26 = 7;

	/**
	 * Equal Pi4J GPIO 26
	 */
	public final int pin32 = 12;

	/**
	 * Equal Pi4J GPIO 27
	 */
	public final int pin36 = 16;

	/**
	 * Equal Pi4J GPIO 28
	 */
	public final int pin38 = 20;

	/**
	 * Equal Pi4J GPIO 29
	 */
	public final int pin40 = 21;

	public final String wlan_name = "wlan0";
	public final String eth_name = "eth0";

	/**
	 * Default Raspbian Serial Name untuk port 0.
	 * akan symbolic-link ke /dev/ttyAMA0 --> hardware based UART
	 * RXD = pin 10, TXD = pin 8
	 * 
	 * Di Pi3 standard, ttyAMA0 handle bluetooth. Kalau mau dijadikan hardware UART biasa
	 * mesti setting dtoverlay=pi3-disable-bt di /boot/config.txt
	 */
	public final String serial0_name = "/dev/serial0";
	
	/**
	 * Default Raspbian Serial name untuk port 1
	 * akan symbolic-link ke /dev/ttyS0 --> software based UART
	 * 
	 * di Pi3 standard ttyS0 handle serial, tapi akurasi kurang pas.
	 * kalau mau swap ttyS0 handle bluetooth, mesti setting dtoverlay=pi3-miniuart-bt
	 * di /boot/config.txt
	 */
	public final String serial1_name = "/dev/serial1";

	
	public Pi3_Pin() {

		super();

	}

	

	/**
	 * I2C SDA : pin 3 SCL : pin 5
	 */
	public final String i2c_1 = "/dev/i2c-1";

	/**
	 * SPI Dev 0.0 MOSI : pin 19 MISO : pin 21 SCLOCK : pin 23 CE : pin 24
	 */
	public final String spi0 = "/dev/spidev0.0";

	/**
	 * SPI Dev 0.1 MOSI : pin 38 MISO : pin 35 SCLOCK : pin 40 CE : pin 26
	 */
	public final String spi1 = "/dev/spidev0.1";
	
	
	public void Initialize(BA bax, Object callerobject, String event) {
		setup_events(bax, callerobject, event, this);

	}

	

	/**
	 * Get RPi Core Temperature
	 * 
	 * @return value in xx.y value
	 */
	@Override
	public double Get_CPU_CoreVolt() {
		try {
			Process process = Runtime.getRuntime().exec("vcgencmd measure_volts core");
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String result = reader.readLine();
			if (result!=null) {
				result = result.trim();
				if (result.startsWith("volt=")) {
					int startindex = result.indexOf(61); // tanda =
					if (startindex < 0) {
						return -1;
					}
					int stopindex = result.indexOf(86); // tanda V
					if (stopindex < 0) {
						return -1;
					}
					double value = Double.parseDouble(result.substring(startindex + 1, stopindex));
					int exitvalue = process.waitFor();
					if (exitvalue == 0)
						return Math.round(value * 10.0) / 10.0; // pembulatan 1 desimal
					else
						return -1;
				} else
					return -1;
			} else return -1;
			
		} catch (IOException | InterruptedException e) {
			BA.Log("Get_RPI_Temp Exception, Msg : " + e.getMessage() + ", Caused: " + e.getCause());
			return -1;
		}
	}

	
}
