<commit_before>package BPUControl;

import java.io.IOException;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.lang.Math;
import java.util.concurrent.atomic.AtomicBoolean;

import com.serialpundit.core.SerialComException;
import com.serialpundit.core.util.SerialComUtil;
import com.serialpundit.serial.SerialComManager;
import com.serialpundit.serial.SerialComManager.BAUDRATE;
import com.serialpundit.serial.SerialComManager.DATABITS;
import com.serialpundit.serial.SerialComManager.FLOWCONTROL;
import com.serialpundit.serial.SerialComManager.PARITY;
import com.serialpundit.serial.SerialComManager.STOPBITS;
/**
 * @author <NAME>
 *
 */

public final class BPUSerial implements Runnable {
	
	private final String API_VERSION = "2.0.1";
	private final String LINEBREAK = "\n";
	
	public enum Message {
		API_VERSION("API version: "), 
		BPU_VERSION("BPU version: "),
		AC_STATE("AC state: "), // AC has been switched on
		HV_ENABLED("HV enabled: "), // High voltage switched on
		PINSTATE("pin state: "), // high voltage pinout setting in binary
		HV_REPORTED("LOG "), // measured output voltage: only available when log is enabled
		VIN_REPORTED("LOG "), // measured input voltage: only available when log is enabled
		VOLTAGECONTROLSTATE("pot state: "); // state of the pot that sets the voltage
		private String message;
		private Message(String message) {
			this.message = message;
		}
		public String toString() {
			return this.message;
		}
	}
    
	private boolean logCommands = false;
    private boolean logOutput = false;
    private boolean logStore = false; // log whenever a logged value is stored as a variable
    
	private volatile boolean stop = false;
    private volatile boolean topElectrode = true;
    private volatile byte[] ChannelState;
	
	private byte[] dataRead;
    private String dataStr;

    private final SerialComManager scm;
    public String output = "";
    private long comPortHandle;
    private String status;
    DATABITS databits = DATABITS.DB_8;
	STOPBITS stopbits = STOPBITS.SB_1;
    PARITY parity = PARITY.P_NONE;
    BAUDRATE baudrate = BAUDRATE.B9600;
    
    private Map<Message, String> BPUState = new EnumMap<Message, String>(Message.class);
    
    private void interpretOutput(String output) {
    	for(Message M : Message.values()) {
    		if(output.startsWith(M.message)) {
    			String value;
    			if(M == Message.HV_REPORTED) {
    				value = output.substring(output.indexOf(";")).trim();
    			}
    			else if(M == Message.VIN_REPORTED) {
    				value = output.substring(M.message.length(), output.indexOf(";"));
    			} 
    			else value = output.substring(M.message.length()).trim();
    			BPUState.put(M, value);
    			if(logStore) System.out.println("Storing variable: " + M.name() + ": " + value);
    		}
    	}
    }
    
    public BPUSerial() throws IOException {
    	this.scm = new SerialComManager();
    }
    public void stopRunning() {
    	System.out.println("stopping thread");
    	this.stop = true;
    }
    public void openComPort(String portName) throws SerialComException {
    	comPortHandle = scm.openComPort(portName, true, true, true);
        scm.configureComPortData(comPortHandle, databits, stopbits, parity, baudrate, 0);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {}
    }
    
    public void closeComPort() throws SerialComException {
    	scm.closeComPort(comPortHandle);
    }
    /*
     * enable this to print output of BPU to the console (default false)
     * 
     * previously: setOutputLogging
     */
    public void toggleOutputLogging(boolean enable) {
    	this.logOutput = enable;
    }
    /* 
     * enable this to print commands sent to the BPU to the console (default false)
     * 
     * previously: setCommandLogging
     */
    public void toggleCommandLogging(boolean enable) {
    	this.logCommands = enable;
    }
    /* 
     * enable this when you have a top layer on the biochip (default true)
     * 
     * previously: setTopElectrode
     */
    public void toggleTopElectrode(boolean enabled) throws SerialComException {
    	this.topElectrode = enabled;
    	if(ChannelState.length > 0) {
    		this.updateChannels(ChannelState);
    	}
    }
    
    /*
     * send signal to set the voltage; use number between 0 and 127
     * 
     * previously: SetDigiPotState
     */
    public void setVoltageControl(int val) throws SerialComException {
    	sendLine("pot " + Math.max(0, 127 - val));
	}
    /*
     * switch HV on and off in the BPU
     * 
     * previously: EnableHighVoltage
     */
    public void toggleHighVoltage(Boolean enable) throws SerialComException {
		sendLine("hvgen " + (enable ? "1" : "0"));
	}
    
    /*
     * switch status reports of BPU of the meaured voltage
     * 
     * previously: EnableLog
     */
    public void toggleVoltageLog(Boolean enable) throws SerialComException {
		sendLine("log " + (enable ? "1" : "0"));
	}
    
    /*
     * switch AC on and off
     * 
     * previously called: SetAC
     */
	public void toggleAC(Boolean enable) throws SerialComException {
		sendLine("polac " + (enable ? "1" : "0"));
	}
	/*
	 * select which channels of the device get switched on and off using byte array
	 * 
	 * previously called: UpdateHV
	 */
	public void updateChannels(byte[] state) throws SerialComException
	{
		byte[] values = Arrays.copyOf(state, 8);
		updateChannels(HelperMethods.bytesToHex(values));
	}
	/*
	 * select which channels of the device get switched on and off using a 	hexadecimal number<commit_msg>add license and version number<commit_after>package BPUControl;

import java.io.IOException;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.lang.Math;
import java.util.concurrent.atomic.AtomicBoolean;

import com.serialpundit.core.SerialComException;
import com.serialpundit.core.util.SerialComUtil;
import com.serialpundit.serial.SerialComManager;
import com.serialpundit.serial.SerialComManager.BAUDRATE;
import com.serialpundit.serial.SerialComManager.DATABITS;
import com.serialpundit.serial.SerialComManager.FLOWCONTROL;
import com.serialpundit.serial.SerialComManager.PARITY;
import com.serialpundit.serial.SerialComManager.STOPBITS;
/**
 * @author <NAME>
 * 
 * version 0.1.0
 * 
 * license: MIT
 *
 */

public final class BPUSerial implements Runnable {
	
	private final String API_VERSION = "2.0.1";
	private final String LINEBREAK = "\n";
	
	public enum Message {
		API_VERSION("API version: "), 
		BPU_VERSION("BPU version: "),
		AC_STATE("AC state: "), // AC has been switched on
		HV_ENABLED("HV enabled: "), // High voltage switched on
		PINSTATE("pin state: "), // high voltage pinout setting in binary
		HV_REPORTED("LOG "), // measured output voltage: only available when log is enabled
		VIN_REPORTED("LOG "), // measured input voltage: only available when log is enabled
		VOLTAGECONTROLSTATE("pot state: "); // state of the pot that sets the voltage
		private String message;
		private Message(String message) {
			this.message = message;
		}
		public String toString() {
			return this.message;
		}
	}
    
	private boolean logCommands = false;
    private boolean logOutput = false;
    private boolean logStore = false; // log whenever a logged value is stored as a variable
    
	private volatile boolean stop = false;
    private volatile boolean topElectrode = true;
    private volatile byte[] ChannelState;
	
	private byte[] dataRead;
    private String dataStr;

    private final SerialComManager scm;
    public String output = "";
    private long comPortHandle;
    private String status;
    DATABITS databits = DATABITS.DB_8;
	STOPBITS stopbits = STOPBITS.SB_1;
    PARITY parity = PARITY.P_NONE;
    BAUDRATE baudrate = BAUDRATE.B9600;
    
    private Map<Message, String> BPUState = new EnumMap<Message, String>(Message.class);
    
    private void interpretOutput(String output) {
    	for(Message M : Message.values()) {
    		if(output.startsWith(M.message)) {
    			String value;
    			if(M == Message.HV_REPORTED) {
    				value = output.substring(output.indexOf(";")).trim();
    			}
    			else if(M == Message.VIN_REPORTED) {
    				value = output.substring(M.message.length(), output.indexOf(";"));
    			} 
    			else value = output.substring(M.message.length()).trim();
    			BPUState.put(M, value);
    			if(logStore) System.out.println("Storing variable: " + M.name() + ": " + value);
    		}
    	}
    }
    
    public BPUSerial() throws IOException {
    	this.scm = new SerialComManager();
    }
    public void stopRunning() {
    	System.out.println("stopping thread");
    	this.stop = true;
    }
    public void openComPort(String portName) throws SerialComException {
    	comPortHandle = scm.openComPort(portName, true, true, true);
        scm.configureComPortData(comPortHandle, databits, stopbits, parity, baudrate, 0);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {}
    }
    
    public void closeComPort() throws SerialComException {
    	scm.closeComPort(comPortHandle);
    }
    /*
     * enable this to print output of BPU to the console (default false)
     * 
     * previously: setOutputLogging
     */
    public void toggleOutputLogging(boolean enable) {
    	this.logOutput = enable;
    }
    /* 
     * enable this to print commands sent to the BPU to the console (default false)
     * 
     * previously: setCommandLogging
     */
    public void toggleCommandLogging(boolean enable) {
    	this.logCommands = enable;
    }
    /* 
     * enable this when you have a top layer on the biochip (default true)
     * 
     * previously: setTopElectrode
     */
    public void toggleTopElectrode(boolean enabled) throws SerialComException {
    	this.topElectrode = enabled;
    	if(ChannelState.length > 0) {
    		this.updateChannels(ChannelState);
    	}
    }
    
    /*
     * send signal to set the voltage; use number between 0 and 127
     * 
     * previously: SetDigiPotState
     */
    public void setVoltageControl(int val) throws SerialComException {
    	sendLine("pot " + Math.max(0, 127 - val));
	}
    /*
     * switch HV on and off in the BPU
     * 
     * previously: EnableHighVoltage
     */
    public void toggleHighVoltage(Boolean enable) throws SerialComException {
		sendLine("hvgen " + (enable ? "1" : "0"));
	}
    /*
     * switch status reports of BPU of the meaured voltage
     * 
     * previously: EnableLog
     */
    public void toggleVoltageLog(Boolean enable) throws SerialComException {
		sendLine("log " + (enable ? "1" : "0"));
	}
    /*
     * switch AC on and off
     * 
     * previously called: SetAC
     */
	public void toggleAC(Boolean enable) throws SerialComException {
		sendLine("polac " + (enable ? "1" : "0"));
	}
	/*
	 * select which channels of the device get switched on and off using byte array
	 * 
	 * previously called: UpdateHV
	 */
	public void updateChannels(byte[] state) throws SerialComException
	{
		byte[] values = Arrays.copyOf(state, 8);
		updateChannels(HelperMethods.bytesToHex(values));
	}
	/*
	 * select which channels of the device get switched on and off using a 	hexadecimal number
{'fraction_non_alphanumeric': 0.10159716060337179, 'fraction_numerical': 0.005767524401064774, 'mean_word_length': 3.094079186342172, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4985627', 'n_tokens_mistral': 3597, 'n_tokens_neox': 3427, 'n_words': 929}