HOME PCB
..Accelerometer.javaAccelerometerData.javaAccelerometerDataHandler.javaDefaultSerial.javaGForceAxis.javaInt16.java
package SerialImpl;

import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataEventListener;

import java.io.*;
import java.nio.file.Path;

public abstract class Accelerometer implements SerialDataEventListener
{
    public AccelerometerData data;

    private int pos = 0;
    private GForceAxis curr = null;

    private final PrintStream out;

    protected Accelerometer()
    {
        this.out = null;
    }

    protected Accelerometer(PrintStream out)
    {
        this.out = out;
    }

    protected Accelerometer(String s) throws FileNotFoundException
    {
        this.out = new PrintStream(new BufferedOutputStream(new FileOutputStream(s)), true);
    }

    protected Accelerometer(File file) throws FileNotFoundException
    {
        this.out = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)), true);
    }

    protected Accelerometer(Path path) throws FileNotFoundException
    {
        this.out = new PrintStream(new BufferedOutputStream(new FileOutputStream(path.toFile())), true);
    }

    @Override
    public void dataReceived(SerialDataEvent serialDataEvent)
    {
        try
        {
            byte[] bytes = serialDataEvent.getBytes();

            for (byte b : bytes)
            {
                switch (this.pos)
                {

                    case 0: {
                        if (b == '\n')
                        {
                            this.pos++;
                            continue;
                        }
                        break;
                    }
                    case 1: {
                        switch (b)
                        {
                            case 'X':
                            {
                                this.curr = this.data.getX();
                                this.pos++;
                                continue;
                            }
                            case 'Y':
                            {
                                this.curr = this.data.getY();
                                this.pos++;
                                continue;
                            }
                            case 'Z':
                            {
                                this.curr = this.data.getZ();
                                this.pos++;
                                continue;
                            }
                            default:
                            {
                                break;
                            }
                        }
                        break;
                    }
                    case 2: {
                        if (b == '=')
                        {
                            this.pos++;
                            continue;
                        }
                        break;
                    }
                    case 3: {
                        this.curr.setHigh(b);
                        this.pos++;
                        continue;
                    }
                    case 4: {
                        this.curr.setLow(b);

                        if (this.data.isFull())
                        {
                            this.flush();
                        }
                        break;
                    }
                }
                this.pos = 0;
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }


    private void flush()
    {
        if (this.out == null) {
            System.out.println(this);
        }
        else
        {
            this.out.println(this);
        }
        this.handleData();
        this.data.clear();
    }

    protected abstract void handleData();

    @Override
    public String toString()
    {
        return this.data.toString();
    }
}