fitres = analyticsClass.fitExponential(x, y);
- if (fitres != null) {
- // Not return extra data as in python-communication library. Not required at this point.
- return fitres.get(0);
- }
- return null;
- }
-
- public Double capacitanceViaRCDischarge() {
- double cap = getCapacitorRange()[1];
- double time = 2 * cap * 20e3 * 1e6; // uSec
- int samples = 500;
- if (time > 5000 && time < 10e6) {
- if (time > 50e3) samples = 250;
- double RC = this.captureCapacitance(samples, (int) (time / samples))[1];
- return RC / 10e3;
- } else {
- Log.v(TAG, "cap out of range " + time + cap);
- return null;
- }
- }
-
- /**
- * Charges a capacitor connected to IN1 via a 20K resistor from a 3.3V source for a fixed interval.
- *
- * @param cTime range of time
- * @return the capacitance calculated using the formula Vc = Vs(1-exp(-t/RC))
- */
- public double[] getCapacitorRange(int cTime) {
- // returns values as a double array arr[0] = v, arr[1] = c
- this.dischargeCap(30000, 1000);
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.GET_CAP_RANGE);
- mPacketHandler.sendInt(cTime);
- int vSum = mPacketHandler.getVoltageSummation();
- double v = vSum * 3.3 / 16 / 4095;
- double c = -cTime * 1e-6 / 1e4 / Math.log(1 - v / 3.3);
- return new double[]{v, c};
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * Charges a capacitor connected to IN1 via a 20K resistor from a 3.3V source for a fixed interval
- *
- * @return the capacitance calculated using the formula Vc = Vs(1-exp(-t/RC))
- */
- public double[] getCapacitorRange() {
- double[] range = new double[]{1.5, 50e-12};
- for (int i = 0; i < 4; i++) {
- range = getCapacitorRange(50 * (int) (pow(10, i)));
- if (range[0] > 1.5) {
- if (i == 0 && range[0] > 3.28) {
- range[1] = 50e-12;
- }
- break;
- }
- }
- return range;
- }
-
- public void dischargeCap(int dischargeTime, double timeout) {
- Instant startTime = null;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- startTime = Instant.now();
- }
- double voltage = getVoltage("CAP", 1);
- double previousVoltage = voltage;
-
- while (voltage > CAPACITOR_DISCHARGE_VOLTAGE) {
- setCapacitorState(0, dischargeTime);
- voltage = getVoltage("CAP", 1);
-
- if (Math.abs(previousVoltage - voltage) < CAPACITOR_DISCHARGE_VOLTAGE) {
- break;
- }
-
- previousVoltage = voltage;
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- if (Duration.between(startTime, Instant.now()).toMillis() > timeout) {
- break;
- }
- }
- }
- }
-
- /**
- * Measures capacitance of component connected between CAP and ground
- *
- * @return Capacitance (F)
- */
- public Double getCapacitance() {
- double[] GOOD_VOLTS = new double[]{2.5, 3.3};
- int CT = 10;
- int CR = 1;
- int iterations = 0;
- long startTime = System.currentTimeMillis() / 1000;
- while (System.currentTimeMillis() / 1000 - startTime < 5) {
- if (CT > 65000) {
- Log.v(TAG, "CT too high");
- CT = (int) (CT / pow(10, 4 - CR));
- CR = 0;
- }
- double[] temp = getCapacitance(CR, 0, CT);
- double V = temp[0];
- double C = temp[1];
- if (CT > 30000 && V < 0.1) {
- Log.v(TAG, "Capacitance too high for this method");
- return null;
- } else if (V > GOOD_VOLTS[0] && V < GOOD_VOLTS[1])
- return C;
- else if (V < GOOD_VOLTS[0] && V > 0.01 && CT < 40000) {
- if (GOOD_VOLTS[0] / V > 1.1 && iterations < 10) {
- CT = (int) (CT * GOOD_VOLTS[0] / V);
- iterations += 1;
- Log.v(TAG, "Increased CT " + CT);
- } else if (iterations == 10)
- return null;
- else return C;
- } else if (V <= 0.1 && CR <= 3)
- if (CR == 3) {
- CR = 0;
- } else {
- CR += 1;
- }
- else if (CR == 0) {
- Log.v(TAG, "Capacitance too high!");
- return capacitanceViaRCDischarge();
- }
- }
- return null;
- }
-
- public double[] getCapacitance(int currentRange, double trim, int chargeTime) { // time in uSec
- this.dischargeCap(30000, 1000);
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.GET_CAPACITANCE);
- mPacketHandler.sendByte(currentRange);
- if (trim < 0)
- mPacketHandler.sendByte((int) (31 - Math.abs(trim) / 2) | 32);
- else
- mPacketHandler.sendByte((int) trim / 2);
- mPacketHandler.sendInt(chargeTime);
- Thread.sleep((long) (chargeTime * 1e-6 + .02));
- int VCode;
- int i = 0;
- do VCode = mPacketHandler.getVoltageSummation();
- while (VCode == -1 & i++ < 10);
- double v = 3.3 * VCode / 4095;
- double chargeCurrent = this.currents[currentRange] * (100 + trim) / 100.0;
- double c = 0;
- if (v != 0) {
- c = (chargeCurrent * chargeTime * 1e-6 / v - this.SOCKET_CAPACITANCE);
- }
- return new double[]{v, c};
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * Temperature of the MCU in degrees Celsius.
- *
- * @return temperature : double
- */
- public double getTemperature() {
- // TODO: Get rid of magic numbers
- int cs = 3;
- double V = getCTMUVoltage(CTMU_CHANNEL, cs, 0);
- if (cs == 1) {
- return (646 - V * 1000) / 1.92; // current source = 1
- } else if (cs == 2) {
- return (701.5 - V * 1000) / 1.74; // current source = 2
- } else {
- return (760 - V * 1000) / 1.56; // current source = 3
- }
- }
-
- /**
- * Control the Charge Time Measurement Unit (CTMU).
- * get_ctmu_voltage(5,2) will activate a constant current source of 5.5uA on CAP and then measure the voltage at the output.
- * If a diode is used to connect CAP to ground, the forward voltage drop of the diode will be returned, e.g. 0.6 V for a 4148 diode.
- * If a resistor is connected, Ohm's law will be followed within reasonable limits.
- *
- * @param channel int
- * Pin number on which to generate a current and measure output
- * voltage. Refer to the PIC24EP64GP204 datasheet for channel
- * numbering.
- * @param cRange {0, 1, 2, 3}
- * 0 -> 550 uA
- * 1 -> 550 nA
- * 2 -> 5.5 uA
- * 3 -> 55 uA
- * @param tgen int, optional
- * Use Time Delay mode instead of Measurement mode. The default value
- * is True.
- * @return voltage : double
- */
- public double getCTMUVoltage(int channel, int cRange, int tgen) {
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.GET_CTMU_VOLTAGE);
- mPacketHandler.sendByte((channel) | (cRange << 5) | (tgen << 7));
- double raw_voltage = (double) mPacketHandler.getInt() / 16; // 16*voltage across the current source
- mPacketHandler.getAcknowledgement();
- double max_voltage = 3.3;
- double resolution = 12;
- return (max_voltage * raw_voltage / (pow(2, resolution) - 1));
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
-
- public double getCTMUVoltage(int channel, int cRange) {
- return getCTMUVoltage(channel, cRange, 1);
- }
-
- public void startCTMU(int cRange, int trim, int tgen) {
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.START_CTMU);
- mPacketHandler.sendByte(cRange | (tgen << 7));
- mPacketHandler.sendByte(trim);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void startCTMU(int cRange, int trim) {
- startCTMU(cRange, trim, 1);
- }
-
- public void stopCTMU() {
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.STOP_CTMU);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void resetDevice() {
- /*
- Reset the device. Standalone mode will be enabled if an OLED is connected to the I2C port.
- */
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.RESTORE_STANDALONE);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Reboot and stay in bootloader mode.
- *
- * @throws IOException
- * @throws InterruptedException
- */
- public void enterBootloader() throws IOException, InterruptedException {
- mCommunicationHandler.close();
- mCommunicationHandler.open(460800);
- mPacketHandler = new PacketHandler(50, mCommunicationHandler);
- // The PSLab's RGB LED flashes some colors on boot.
- int bootLightShowTime = 600;
- // Wait before sending magic number to make sure UART is initialized.
- Thread.sleep(bootLightShowTime / 2);
- // PIC24 UART RX buffer is four bytes deep; no need to time it perfectly.
- mPacketHandler.commonWrite(mCommandsProto.pack(0xDECAFBAD));
- // Wait until lightshow is done to prevent accidentally overwriting magic number.
- Thread.sleep(bootLightShowTime);
- }
-
- /**
- * Set shade of a WS2812 RGB LED.
- *
- * @param colors ArrayList
- * List of three values between 0-255, where each value is the
- * intensity of red, green and blue, respectively. When daisy
- * chaining several LEDs, colors should be a list of three-value
- * lists.
- * @param output {"RGB", "PGC", "SQ1"}, optional
- * Pin on which to output the pulse train setting the LED color. The
- * default value, "RGB", sets the color of the built-in WS2812B
- * (PSLav v6 only).
- * @param order String, optional
- * Color order of the connected LED as a three-letter string. The
- * built-in LED has order "GRB", which is the default.
- */
- public void RGBLED(ArrayList> colors, String output, String order) {
- HashMap pins = new HashMap<>();
- int pin;
- if (CommunicationHandler.PSLAB_VERSION == 6) {
- pins.put("ONBOARD", 0);
- pins.put("SQR1", 1);
- pins.put("SQR2", 2);
- pins.put("SQR3", 3);
- pins.put("SQR4", 4);
- } else {
- pins.put("RGB", mCommandsProto.SET_RGB1);
- pins.put("PGC", mCommandsProto.SET_RGB2);
- pins.put("SQ1", mCommandsProto.SET_RGB3);
- }
-
- if (!pins.containsKey(output)) {
- String outputPins = String.join(", ", pins.keySet());
- throw new IllegalArgumentException("Invalid output: " + output + ". output must be one of : " + outputPins);
- }
- pin = Objects.requireNonNull(pins.get(output));
-
- for (ArrayList color : colors) {
- if (color.size() != 3) {
- throw new IllegalArgumentException("Invalid colo; each color list must have three values.");
- }
- }
-
- order = order.toUpperCase(Locale.ROOT);
- char[] orderChars = order.toCharArray();
- Arrays.sort(orderChars);
- if (!Arrays.equals(orderChars, new char[]{'B', 'G', 'R'})) {
- throw new IllegalArgumentException("Invalid order: " + order + ". order must contain 'R', 'G', and 'B'.");
- }
-
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
-
- if (CommunicationHandler.PSLAB_VERSION == 6) {
- mPacketHandler.sendByte(mCommandsProto.SET_RGB_COMMON);
- } else {
- mPacketHandler.sendByte(pin);
- }
-
- mPacketHandler.sendByte(colors.size() * 3);
-
- for (ArrayList color : colors) {
- mPacketHandler.sendByte(color.get(order.indexOf('R')));
- mPacketHandler.sendByte(color.get(order.indexOf('G')));
- mPacketHandler.sendByte(color.get(order.indexOf('B')));
- }
-
- if (CommunicationHandler.PSLAB_VERSION == 6) {
- mPacketHandler.sendByte(pin);
- }
-
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void RGBLED(ArrayList colors, String output) {
- RGBLED(new ArrayList<>(Collections.singletonList(colors)), output, "GRB");
- }
-
- /* WAVEGEN SECTION */
-
- public void setWave(String channel, double frequency) {
- if ("SI1".equals(channel))
- this.setSI1(frequency, null);
- else if ("SI2".equals(channel))
- this.setSI2(frequency, null);
- }
-
- public double setSine1(double frequency) {
- return this.setSI1(frequency, "sine");
- }
-
- public double setSine2(double frequency) {
- return this.setSI2(frequency, "sine");
- }
-
- public double setSI1(double frequency, String waveType) {
- int HIGHRES, tableSize;
- if (frequency < 0.1) {
- Log.v(TAG, "frequency too low");
- return -1;
- } else if (frequency < 1100) {
- HIGHRES = 1;
- tableSize = 512;
- } else {
- HIGHRES = 0;
- tableSize = 32;
- }
- if (waveType != null) {
- if ("sine".equals(waveType) | "tria".equals(waveType)) {
- if (!(this.waveType.get("SI1").equals(waveType))) {
- this.loadEquation("SI1", waveType);
- }
- } else {
- Log.v(TAG, "Not a valid waveform. try sine or tria");
- }
- }
- int[] p = new int[]{1, 8, 64, 256};
- int prescalar = 0, wavelength = 0;
- while (prescalar <= 3) {
- wavelength = (int) (64e6 / frequency / p[prescalar] / tableSize);
- frequency = 64e6 / wavelength / p[prescalar] / tableSize;
- if (wavelength < 65525) break;
- prescalar++;
- }
- if (prescalar == 4) {
- Log.v(TAG, "Out of range");
- return -1;
- }
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SET_SINE1);
- mPacketHandler.sendByte(HIGHRES | (prescalar << 1));
- mPacketHandler.sendInt(wavelength - 1);
- mPacketHandler.getAcknowledgement();
- this.sin1Frequency = frequency;
- return this.sin1Frequency;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
-
- public double setSI2(double frequency, String waveType) {
- int HIGHRES;
- int tableSize;
- if (frequency < 0.1) {
- Log.v(TAG, "frequency too low");
- return -1;
- } else if (frequency < 1100) {
- HIGHRES = 1;
- tableSize = 512;
- } else {
- HIGHRES = 0;
- tableSize = 32;
- }
- if (waveType != null) {
- if ("sine".equals(waveType) | "tria".equals(waveType)) {
- if (!(this.waveType.get("SI2").equals(waveType))) {
- this.loadEquation("SI2", waveType);
- }
- } else {
- Log.v(TAG, "Not a valid waveform. try sine or tria");
- }
- }
- int[] p = new int[]{1, 8, 64, 256};
- int prescalar = 0, wavelength = 0;
- while (prescalar <= 3) {
- wavelength = (int) (64e6 / frequency / p[prescalar] / tableSize);
- frequency = 64e6 / wavelength / p[prescalar] / tableSize;
- if (wavelength < 65525) break;
- prescalar++;
- }
- if (prescalar == 4) {
- Log.v(TAG, "Out of range");
- return -1;
- }
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SET_SINE2);
- mPacketHandler.sendByte(HIGHRES | (prescalar << 1));
- mPacketHandler.sendInt(wavelength - 1);
- mPacketHandler.getAcknowledgement();
- this.sin2Frequency = frequency;
- return this.sin2Frequency;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
-
- public double readBackWaveform(String channel) {
- if ("SI1".equals(channel))
- return this.sin1Frequency;
- else if ("SI2".equals(channel))
- return this.sin2Frequency;
- else if ("SQR".startsWith(channel))
- return this.squareWaveFrequency.get(channel);
- return -1;
- }
-
- public double setWaves(double frequency, double phase, double frequency2) {
- // used frequency as double ( python code demanded ), maybe its taken in KHz or something ( Clarify )
- int HIGHRES, tableSize, HIGHRES2, tableSize2, wavelength = 0, wavelength2 = 0;
- if (frequency2 == -1) frequency2 = frequency;
- if (frequency < 0.1) {
- Log.v(TAG, "frequency 1 too low");
- return -1;
- } else if (frequency < 1100) {
- HIGHRES = 1;
- tableSize = 512;
- } else {
- HIGHRES = 0;
- tableSize = 32;
- }
- if (frequency2 < 0.1) {
- Log.v(TAG, "frequency 2 too low");
- return -1;
- } else if (frequency2 < 1100) {
- HIGHRES2 = 1;
- tableSize2 = 512;
- } else {
- HIGHRES2 = 0;
- tableSize2 = 32;
- }
- if (frequency < 1 || frequency2 < 1)
- Log.v(TAG, "extremely low frequencies will have reduced amplitudes due to AC coupling restrictions");
-
- int[] p = new int[]{1, 8, 64, 256};
- int prescalar = 0;
- double retFrequency = 0;
- while (prescalar <= 3) {
- wavelength = (int) (64e6 / frequency / p[prescalar] / tableSize);
- retFrequency = 64e6 / wavelength / p[prescalar] / tableSize;
- if (wavelength < 65525) break;
- prescalar++;
- }
- if (prescalar == 4) {
- Log.v(TAG, "#1 out of range");
- return -1;
- }
- int prescalar2 = 0;
- double retFrequency2 = 0;
- while (prescalar2 <= 3) {
- wavelength2 = (int) (64e6 / frequency2 / p[prescalar2] / tableSize2);
- retFrequency2 = 64e6 / wavelength2 / p[prescalar2] / tableSize2;
- if (wavelength2 < 65525) break;
- prescalar2++;
- }
- if (prescalar2 == 4) {
- Log.v(TAG, "#2 out of range");
- return -1;
- }
-
- int phaseCoarse = (int) (tableSize2 * (phase) / 360.);
- int phaseFine = (int) (wavelength2 * (phase - (phaseCoarse) * 360. / tableSize2) / (360. / tableSize2));
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SET_BOTH_WG);
- mPacketHandler.sendInt(wavelength - 1);
- mPacketHandler.sendInt(wavelength2 - 1);
- mPacketHandler.sendInt(phaseCoarse);
- mPacketHandler.sendInt(phaseFine);
- mPacketHandler.sendByte((prescalar2 << 4) | (prescalar << 2) | (HIGHRES2 << 1) | (HIGHRES));
- mPacketHandler.getAcknowledgement();
- this.sin1Frequency = retFrequency;
- this.sin2Frequency = retFrequency2;
- return retFrequency;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
-
- public void loadEquation(String channel, String function) {
- double[] span = new double[2];
- if ("sine".equals(function)) {
- span[0] = 0;
- span[1] = 2 * Math.PI;
- waveType.put(channel, "sine");
- } else if ("tria".equals(function)) {
- span[0] = 0;
- span[1] = 4;
- waveType.put(channel, "tria");
- } else {
- waveType.put(channel, "arbit");
- }
- double factor = (span[1] - span[0]) / 512;
- ArrayList x = new ArrayList<>();
- ArrayList y = new ArrayList<>();
- // for now using switch, proper way is to create an interface and pass it to loadEquation and call interface methods for calculation
- for (int i = 0; i < 512; i++) {
- x.add(span[0] + i * factor);
- switch (function) {
- case "sine":
- y.add(Math.sin(x.get(i)));
- break;
- case "tria":
- y.add(Math.abs(x.get(i) % 4 - 2));
- break;
- }
- }
- loadTable(channel, y, waveType.get(channel), -1);
- }
-
- private void loadTable(String channel, ArrayList y, String mode, double amp) {
- waveType.put(channel, mode);
- ArrayList channels = new ArrayList<>();
- ArrayList points = y;
- channels.add("SI1");
- channels.add("SI2");
- int num;
- if (channels.contains(channel)) {
- num = channels.indexOf(channel) + 1;
- } else {
- Log.e(TAG, "Channel does not exist. Try SI1 or SI2");
- return;
- }
-
- if (amp == -1) amp = 0.95;
- double LARGE_MAX = 511 * amp, SMALL_MAX = 63 * amp;
- double min = Collections.min(y);
- for (int i = 0; i < y.size(); i++) {
- y.set(i, y.get(i) - min);
- }
- double max = Collections.max(y);
- ArrayList yMod1 = new ArrayList<>();
- for (int i = 0; i < y.size(); i++) {
- double temp = 1 - (y.get(i) / max);
- yMod1.add((int) Math.round(LARGE_MAX - LARGE_MAX * temp));
- }
- y = new ArrayList();
-
-
- for (int i = 0; i < points.size(); i += 16) {
- y.add(points.get(i));
- }
- min = Collections.min(y);
- for (int i = 0; i < y.size(); i++) {
- y.set(i, y.get(i) - min);
- }
- max = Collections.max(y);
- ArrayList yMod2 = new ArrayList<>();
- for (int i = 0; i < y.size(); i++) {
- double temp = 1 - (y.get(i) / max);
- yMod2.add((int) Math.round(SMALL_MAX - SMALL_MAX * temp));
- }
-
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- switch (num) {
- case 1:
- mPacketHandler.sendByte(mCommandsProto.LOAD_WAVEFORM1);
- break;
- case 2:
- mPacketHandler.sendByte(mCommandsProto.LOAD_WAVEFORM2);
- break;
- }
- for (int a : yMod1)
- mPacketHandler.sendInt(a);
-
- for (int a : yMod2)
- mPacketHandler.sendByte(a);
-
- // sleep for 0.01
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- public double setSqr1(double frequency, double dutyCycle, boolean onlyPrepare) {
- if (dutyCycle == -1) dutyCycle = 50;
- if (frequency == 0 || dutyCycle == 0) return -1;
- if (frequency > 10e6) {
- Log.v(TAG, "Frequency is greater than 10MHz. Please use map_reference_clock for 16 & 32MHz outputs");
- return 0;
- }
- int[] p = new int[]{1, 8, 64, 256};
- int prescalar = 0;
- int wavelength = 0;
- while (prescalar <= 3) {
- wavelength = (int) (64e6 / frequency / p[prescalar]);
- if (wavelength < 65525) break;
- prescalar++;
- }
- if (prescalar == 4 || wavelength == 0) {
- Log.v(TAG, "Out of Range");
- return -1;
- }
- int highTime = (int) (wavelength * dutyCycle / 100);
- if (onlyPrepare) {
- Map args = new LinkedHashMap<>();
- args.put("SQR1", 0);
- this.setState(args);
- }
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SET_SQR1);
- mPacketHandler.sendInt(Math.round(wavelength));
- mPacketHandler.sendInt(Math.round(highTime));
- if (onlyPrepare) prescalar |= 0x4;
- mPacketHandler.sendByte(prescalar);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- this.squareWaveFrequency.put("SQR1", 64e6 / wavelength / p[prescalar & 0x3]);
- return this.squareWaveFrequency.get("SQR1");
- }
-
- public double setSqr2(double frequency, double dutyCycle) {
- int[] p = new int[]{1, 8, 64, 256};
- int prescalar = 0;
- int wavelength = 0;
- while (prescalar <= 3) {
- wavelength = (int) (64e6 / frequency / p[prescalar]);
- if (wavelength < 65525) break;
- prescalar++;
- }
- if (prescalar == 4 || wavelength == 0) {
- Log.v(TAG, "Out of Range");
- return -1;
- }
- int highTime = (int) (wavelength * dutyCycle / 100);
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SET_SQR2);
- mPacketHandler.sendInt(Math.round(wavelength));
- mPacketHandler.sendInt(Math.round(highTime));
- mPacketHandler.sendByte(prescalar);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- this.squareWaveFrequency.put("SQR2", 64e6 / wavelength / p[prescalar & 0x3]);
- return this.squareWaveFrequency.get("SQR2");
- }
-
- public void setSqrs(int wavelength, int phase, int highTime1, int highTime2, int prescalar) {
- if (prescalar == -1) prescalar = 1;
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SET_SQRS);
- mPacketHandler.sendInt(wavelength);
- mPacketHandler.sendInt(phase);
- mPacketHandler.sendInt(highTime1);
- mPacketHandler.sendInt(highTime2);
- mPacketHandler.sendByte(prescalar);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public double sqrPWM(double frequency, double h0, double p1, double h1, double p2, double h2, double p3, double h3, boolean pulse) {
- if (frequency == 0) return -1;
- if (h0 == 0) {
- h0 = 0.1;
- }
- if (h1 == 0) {
- h1 = 0.1;
- }
- if (h2 == 0) {
- h2 = 0.1;
- }
- if (h3 == 0) {
- h3 = 0.1;
- }
- if (frequency > 10e6) {
- Log.v(TAG, "Frequency is greater than 10MHz. Please use map_reference_clock for 16 & 32MHz outputs");
- return -1;
- }
- int[] p = new int[]{1, 8, 64, 256};
- int prescalar = 0, wavelength = 0;
- while (prescalar <= 3) {
- wavelength = (int) (64e6 / frequency / p[prescalar]);
- if (wavelength < 65525) break;
- prescalar++;
- }
- if (prescalar == 4 || wavelength == 0) {
- Log.v(TAG, "Out of Range");
- return -1;
- }
- if (!pulse) prescalar |= (1 << 5);
- int a1 = (int) (p1 % 1 * wavelength), b1 = (int) ((h1 + p1) % 1 * wavelength);
- int a2 = (int) (p2 % 1 * wavelength), b2 = (int) ((h2 + p2) % 1 * wavelength);
- int a3 = (int) (p3 % 1 * wavelength), b3 = (int) ((h3 + p3) % 1 * wavelength);
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SQR4);
- mPacketHandler.sendInt(wavelength - 1);
- mPacketHandler.sendInt((int) (wavelength * h0) - 1);
- mPacketHandler.sendInt(Math.max(0, a1 - 1));
- mPacketHandler.sendInt(Math.max(1, b1 - 1));
- mPacketHandler.sendInt(Math.max(0, a2 - 1));
- mPacketHandler.sendInt(Math.max(1, b2 - 1));
- mPacketHandler.sendInt(Math.max(0, a3 - 1));
- mPacketHandler.sendInt(Math.max(1, b3 - 1));
- mPacketHandler.sendByte(prescalar);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- for (String channel : new String[]{"SQR1", "SQR2", "SQR3", "SQR4"}) {
- this.squareWaveFrequency.put(channel, 64e6 / wavelength / p[prescalar & 0x3]);
- }
- return (int) (64e6 / wavelength / p[prescalar & 0x3]);
- }
-
- public void mapReferenceClock(ArrayList args, int scalar) {
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.MAP_REFERENCE);
- int channel = 0;
- if (args.contains("SQR1")) channel |= 1;
- if (args.contains("SQR2")) channel |= 2;
- if (args.contains("SQR3")) channel |= 4;
- if (args.contains("SQR4")) channel |= 8;
- if (args.contains("WAVEGEN")) channel |= 16;
- mPacketHandler.sendByte(channel);
- mPacketHandler.sendByte(scalar);
- if (args.contains("WAVEGEN")) {
- this.DDS_CLOCK = (int) 128e6 / (1 << scalar);
- }
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /* ANALOG OUTPUTS */
-
- public void setVoltage(String channel, float voltage) {
- DACChannel dacChannel = dacChannels.get(channel);
- int v = (int) (Math.round(dacChannel.VToCode.value(voltage)));
- try {
- mPacketHandler.sendByte(mCommandsProto.DAC);
- mPacketHandler.sendByte(mCommandsProto.SET_POWER);
- mPacketHandler.sendByte(dacChannel.channelCode);
- mPacketHandler.sendInt(v);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- values.put(channel, (double) voltage);
- }
-
- private void setCurrent(float current) {
- DACChannel dacChannel = dacChannels.get("PCS");
- int v = 3300 - (int) (Math.round(dacChannel.VToCode.value(current)));
- try {
- mPacketHandler.sendByte(mCommandsProto.DAC);
- mPacketHandler.sendByte(mCommandsProto.SET_POWER);
- mPacketHandler.sendByte(dacChannel.channelCode);
- mPacketHandler.sendInt(v);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- values.put("PCS", (double) current);
- }
-
- private double getVoltage(String channel) {
- return this.values.get(channel);
- }
-
- public void setPV1(float value) {
- this.setVoltage("PV1", value);
- }
-
- public void setPV2(float value) {
- this.setVoltage("PV2", value);
- }
-
- public void setPV3(float value) {
- this.setVoltage("PV3", value);
- }
-
- public void setPCS(float value) {
- this.setCurrent(value);
- }
-
- public double getPV1() {
- return this.getVoltage("PV1");
- }
-
- public double getPV2() {
- return this.getVoltage("PV2");
- }
-
- public double getPV3() {
- return this.getVoltage("PV3");
- }
-
- public double getPCS() {
- return this.getVoltage("PCS");
- }
-
- /* READ PROGRAM AND DATA ADDRESSES */
-
- public long deviceID() {
- long a = readProgramAddress(0x800FF8);
- long b = readProgramAddress(0x800FFA);
- long c = readProgramAddress(0x800FFC);
- long d = readProgramAddress(0x800FFE);
- long value = d | (c << 16) | (b << 32) | (a << 48);
- Log.v(TAG, "device ID : " + value);
- return value;
- }
-
- /**
- * Return the value stored at the specified address in program memory.
- *
- * @param address int
- * Address to read from. Refer to PIC24EP64GP204 programming manual.
- * @return data : int 16-bit wide value read from program memory.
- */
- public int readProgramAddress(int address) {
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.READ_PROGRAM_ADDRESS);
- mPacketHandler.sendInt(address & 0xffff);
- mPacketHandler.sendInt((address >> 16) & 0xffff);
- int data = mPacketHandler.getInt();
- mPacketHandler.getAcknowledgement();
- return data;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
-
- /**
- * Return the value stored at the specified address in RAM.
- *
- * @param address int
- * Address to read from. Refer to PIC24EP64GP204 programming manual.
- * @return data : int 16-bit wide value read from RAM.
- */
- public int readDataAddress(int address) {
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.READ_DATA_ADDRESS);
- mPacketHandler.sendInt(address & 0xffff);
- int data = mPacketHandler.getInt();
- mPacketHandler.getAcknowledgement();
- return data;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
-
- /**
- * Write a value to the specified address in RAM.
- *
- * @param address int
- * Address to write to. Refer to PIC24EP64GP204 programming manual.
- * @param value int
- * Value to write to RAM.
- */
- public void writeDataAddress(int address, int value) {
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.WRITE_DATA_ADDRESS);
- mPacketHandler.sendInt(address & 0xffff);
- mPacketHandler.sendInt(value);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Relay all data received by the device to TXD/RXD.
- *
- * If a period > 0.5 seconds elapses between two transmit/receive events,
- * the device resets and resumes normal mode. This timeout feature has
- * been implemented in lieu of a hard reset option.
- *
- *
- * Can be used to load programs into secondary microcontrollers with
- * bootloaders such as ATMEGA OR ESP8266.
- *
- *
- * @param baudrate int
- * Baudrate of the UART bus.
- * @param persist bool, optional
- * If set to True, the device will stay in passthrough mode until the
- * next power cycle. Otherwise(default scenario), the device will
- * return to normal operation if no data is sent/received for a period
- * greater than one second at a time.
- */
- public void enableUARTPassThrough(int baudrate, boolean persist) {
- try {
- mPacketHandler.sendByte(mCommandsProto.PASSTHROUGHS);
- mPacketHandler.sendByte(mCommandsProto.PASS_UART);
- if (persist)
- mPacketHandler.sendByte(1);
- else
- mPacketHandler.sendByte(0);
- mPacketHandler.sendInt((int) Math.round(((64e6 / baudrate) / 4) - 1));
- Log.v(TAG, "BRG2VAL: " + Math.round(((64e6 / baudrate) / 4) - 1));
- // sleep for 0.1 sec
- byte[] junk = new byte[100];
- mPacketHandler.read(junk, 100);
- // Log junk to see :D
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /* MOTOR SIGNALLING */
-
- public void servo4(double angle1, double angle2, double angle3, double angle4) {
- int params = (1 << 5) | 2;
- try {
- mPacketHandler.sendByte(mCommandsProto.WAVEGEN);
- mPacketHandler.sendByte(mCommandsProto.SQR4);
- mPacketHandler.sendInt(10000);
- mPacketHandler.sendInt(750 + (int) (angle1 * 1900 / 180));
- mPacketHandler.sendInt(0);
- mPacketHandler.sendInt(750 + (int) (angle2 * 1900 / 180));
- mPacketHandler.sendInt(0);
- mPacketHandler.sendInt(750 + (int) (angle3 * 1900 / 180));
- mPacketHandler.sendInt(0);
- mPacketHandler.sendInt(750 + (int) (angle4 * 1900 / 180));
- mPacketHandler.sendByte(params);
- mPacketHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Read hardware debug log.
- *
- * @return log : String Bytes read from the hardware debug log.
- */
- public String readLog() {
- String log = "";
- try {
- mPacketHandler.sendByte(mCommandsProto.COMMON);
- mPacketHandler.sendByte(mCommandsProto.READ_LOG);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return mPacketHandler.readLine();
- }
-
- public void disconnect() throws IOException {
- mCommunicationHandler.close();
- PacketHandler.version = "";
- }
-}
\ No newline at end of file
diff --git a/app/src/main/java/io/pslab/communication/SensorList.java b/app/src/main/java/io/pslab/communication/SensorList.java
deleted file mode 100644
index 2ff102ba5..000000000
--- a/app/src/main/java/io/pslab/communication/SensorList.java
+++ /dev/null
@@ -1,54 +0,0 @@
-package io.pslab.communication;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Created by viveksb007 on 30/3/17.
- */
-
-public class SensorList {
-
- public Map sensorList = new HashMap<>();
-
- public SensorList() {
- sensorList.put(0x00, new String[]{"Could be MLX90614. Try 0x5A"});
- sensorList.put(0x13, new String[]{"VCNL4000"});
- sensorList.put(0x3c, new String[]{"OLED SSD1306"});
- sensorList.put(0x3d, new String[]{"OLED SSD1306"});
- sensorList.put(0x48, new String[]{"PN532 RFID"});
- sensorList.put(0x29, new String[]{"TSL2561"});
- sensorList.put(0x39, new String[]{"TSL2561"});
- sensorList.put(0x49, new String[]{"TSL2561"});
- sensorList.put(0x1D, new String[]{"ADXL345", "MMA7455L", "LSM9DSO"});
- sensorList.put(0x53, new String[]{"ADXL345"});
- sensorList.put(0x5A, new String[]{"MLX90614 PIR temperature"});
- sensorList.put(0x1E, new String[]{"HMC5883L magnetometer", "LSM303 magnetometer"});
- sensorList.put(0x77, new String[]{"BMP180/GY-68 altimeter", "MS5607", "MS5611"});
- sensorList.put(0x68, new String[]{"MPU-6050/GY-521 accel+gyro+temp", "ITG3200", "DS1307", "DS3231"});
- sensorList.put(0x69, new String[]{"ITG3200"});
- sensorList.put(0x76, new String[]{"MS5607", "MS5611"});
- sensorList.put(0x6B, new String[]{"LSM9DSO gyro"});
- sensorList.put(0x19, new String[]{"LSM303 accel"});
- sensorList.put(0x20, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x21, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x22, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x23, new String[]{"BH1750", "MCP23008", "MCP23017"});
- sensorList.put(0x24, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x25, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x26, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x27, new String[]{"MCP23008", "MCP23017"});
- sensorList.put(0x40, new String[]{"SHT21(Temp/RH)"});
- sensorList.put(0x60, new String[]{"MCP4725A0 4 chan DAC (onBoard)"});
- sensorList.put(0x61, new String[]{"MCP4725A0 4 chan DAC"});
- sensorList.put(0x62, new String[]{"MCP4725A1 4 chan DAC"});
- sensorList.put(0x63, new String[]{"MCP4725A1 4 chan DAC", "Si4713"});
- sensorList.put(0x64, new String[]{"MCP4725A2 4 chan DAC"});
- sensorList.put(0x65, new String[]{"MCP4725A2 4 chan DAC"});
- sensorList.put(0x66, new String[]{"MCP4725A3 4 chan DAC"});
- sensorList.put(0x67, new String[]{"MCP4725A3 4 chan DAC"});
- sensorList.put(0x11, new String[]{"Si4713"});
- sensorList.put(0x38, new String[]{"FT6206 touch controller"});
- sensorList.put(0x41, new String[]{"STMPE610"});
- }
-}
diff --git a/app/src/main/java/io/pslab/communication/analogChannel/AnalogAquisitionChannel.java b/app/src/main/java/io/pslab/communication/analogChannel/AnalogAquisitionChannel.java
deleted file mode 100644
index 735766477..000000000
--- a/app/src/main/java/io/pslab/communication/analogChannel/AnalogAquisitionChannel.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package io.pslab.communication.analogChannel;
-
-import java.util.Arrays;
-
-/**
- * Created by viveksb007 on 24/3/17.
- */
-
-public class AnalogAquisitionChannel {
-
- private int resolution;
- private AnalogInputSource analogInputSource;
- private double gain;
- private String channel;
- public int bufferIndex;
- private double calibration_ref196;
- public int length;
- private double timebase;
- private double[] xAxis = new double[10000];
- public double[] yAxis = new double[10000];
-
- public AnalogAquisitionChannel(String channel) {
- gain = 0;
- this.channel = channel;
- calibration_ref196 = 1.;
- resolution = 10;
- length = 100;
- timebase = 1.;
- bufferIndex = 0;
- Arrays.fill(xAxis, 0);
- Arrays.fill(yAxis, 0);
- analogInputSource = new AnalogInputSource("CH1");
- }
-
- public double[] fixValue(double[] val) {
- double[] calcData = new double[val.length];
- if (resolution == 12)
- for (int i = 0; i < val.length; i++)
- calcData[i] = calibration_ref196 * (analogInputSource.calPoly12.value(val[i]));
- else
- for (int i = 0; i < val.length; i++)
- calcData[i] = calibration_ref196 * (analogInputSource.calPoly10.value(val[i]));
- return calcData;
- }
-
- void setYVal(int pos, int val) {
- yAxis[pos] = fixValue(new double[]{val})[0];
- }
-
- void setXVal(int pos, int val) {
- xAxis[pos] = fixValue(new double[]{val})[0];
- }
-
- public void setParams(String channel, int length, int bufferIndex, double timebase, int resolution, AnalogInputSource source, Double gain) {
- if (gain != null) this.gain = gain;
- if (source != null) this.analogInputSource = source;
- if (channel != null) this.channel = channel;
- if (resolution != -1) this.resolution = resolution;
- if (length != -1) this.length = length;
- if (bufferIndex != -1) this.bufferIndex = bufferIndex;
- if (timebase != -1) this.timebase = timebase;
- regenerateXAxis();
- }
-
- void regenerateXAxis() {
- for (int i = 0; i < length; i++) {
- xAxis[i] = timebase * i;
- }
- }
-
- public double[] getXAxis() {
- return Arrays.copyOfRange(xAxis, 0, length);
- }
-
- public double[] getYAxis() {
- return Arrays.copyOfRange(yAxis, 0, length);
- }
-
-}
diff --git a/app/src/main/java/io/pslab/communication/analogChannel/AnalogConstants.java b/app/src/main/java/io/pslab/communication/analogChannel/AnalogConstants.java
deleted file mode 100644
index 294998ca5..000000000
--- a/app/src/main/java/io/pslab/communication/analogChannel/AnalogConstants.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package io.pslab.communication.analogChannel;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Created by viveksb007 on 24/3/17.
- */
-
-public class AnalogConstants {
-
- public double[] gains = {1, 2, 4, 5, 8, 10, 16, 32, 1 / 11.};
- public String[] allAnalogChannels = {"CH1", "CH2", "CH3", "MIC", "CAP", "RES", "VOL"};
- public String[] biPolars = {"CH1", "CH2", "CH3", "MIC"};
- public Map inputRanges = new HashMap<>();
- public Map picADCMultiplex = new HashMap<>();
-
- public AnalogConstants() {
-
- inputRanges.put("CH1", new double[]{16.5, -16.5});
- inputRanges.put("CH2", new double[]{16.5, -16.5});
- inputRanges.put("CH3", new double[]{-3.3, 3.3});
- inputRanges.put("MIC", new double[]{-3.3, 3.3});
- inputRanges.put("CAP", new double[]{0, 3.3});
- inputRanges.put("RES", new double[]{0, 3.3});
- inputRanges.put("VOL", new double[]{0, 3.3});
-
- picADCMultiplex.put("CH1", 3);
- picADCMultiplex.put("CH2", 0);
- picADCMultiplex.put("CH3", 1);
- picADCMultiplex.put("MIC", 2);
- picADCMultiplex.put("AN4", 4);
- picADCMultiplex.put("RES", 7);
- picADCMultiplex.put("CAP", 5);
- picADCMultiplex.put("VOL", 8);
-
- }
-}
-
diff --git a/app/src/main/java/io/pslab/communication/analogChannel/AnalogInputSource.java b/app/src/main/java/io/pslab/communication/analogChannel/AnalogInputSource.java
deleted file mode 100644
index 21cfa9283..000000000
--- a/app/src/main/java/io/pslab/communication/analogChannel/AnalogInputSource.java
+++ /dev/null
@@ -1,136 +0,0 @@
-package io.pslab.communication.analogChannel;
-
-import android.util.Log;
-
-import org.apache.commons.lang3.ArrayUtils;
-import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class AnalogInputSource {
-
- private static String TAG = "AnalogInputSource";
-
- private double gainValues[], range[];
- public boolean gainEnabled = false, inverted = false, calibrationReady = false;
- private double gain = 0;
- public int gainPGA, CHOSA;
- private int inversion = 1;
- private int defaultOffsetCode = 0;
- private int scaling = 1;
- private String channelName;
- public PolynomialFunction calPoly10;
- public PolynomialFunction calPoly12;
- public PolynomialFunction voltToCode10;
- public PolynomialFunction voltToCode12;
- private List adc_shifts = new ArrayList<>();
- private List polynomials = new ArrayList<>(); //list of maps
-
- public AnalogInputSource(String channelName) {
- AnalogConstants analogConstants = new AnalogConstants();
- this.channelName = channelName;
- range = analogConstants.inputRanges.get(channelName);
- gainValues = analogConstants.gains;
- this.CHOSA = analogConstants.picADCMultiplex.get(channelName);
-
- calPoly10 = new PolynomialFunction(new double[]{0., 3.3 / 1023, 0.});
- calPoly12 = new PolynomialFunction(new double[]{0., 3.3 / 4095, 0.});
-
- if (range[1] - range[0] < 0) {
- inverted = true;
- inversion = -1;
- }
- if (channelName.equals("CH1")) {
- gainEnabled = true;
- gainPGA = 1;
- gain = 0;
- } else if (channelName.equals("CH2")) {
- gainEnabled = true;
- gainPGA = 2;
- gain = 0;
- }
- gain = 0;
- regenerateCalibration();
- }
-
- public Boolean setGain(int index) {
- if (!gainEnabled) {
- Log.e(channelName, "Analog gain is not available");
- return false;
- }
- gain = gainValues[index];
- regenerateCalibration();
- return true;
- }
-
- boolean inRange(double val) {
- double sum = voltToCode12.value(val);
- return sum >= 50 && sum <= 4095;
- }
-
- boolean conservativeInRange(double val) {
- double solution = voltToCode12.value(val);
- return solution >= 50 && solution <= 4000;
- }
-
- public List loadCalibrationTable(double[] table, double slope, double intercept) {
- for (double aTable : table) {
- adc_shifts.add(aTable * slope - intercept);
- }
- return adc_shifts;
- }
-
- public void ignoreCalibration() {
- calibrationReady = false;
- }
-
- public void loadPolynomials(ArrayList polys) {
- for (int i = 0; i < polys.size(); i++) {
- double[] temp = ArrayUtils.toPrimitive(polys.get(i));
- ArrayUtils.reverse(temp);
- polynomials.add(new PolynomialFunction(temp));
- }
- }
-
- public void regenerateCalibration() {
- double A, B, intercept, slope;
- B = range[1];
- A = range[0];
- if (gain >= 0 && gain<=8) {
- gain = gainValues[(int) gain];
- B /= gain;
- A /= gain;
- }
- slope = 2 * (B - A);
- intercept = 2 * A;
- if (!calibrationReady || gain == 8) {
- calPoly10 = new PolynomialFunction(new double[]{intercept, slope / 1023, 0.});
- calPoly12 = new PolynomialFunction(new double[]{intercept, slope / 4095, 0.});
- }//else cases need to be worked on!!!
-
- voltToCode10 = new PolynomialFunction(new double[]{-1023 * intercept / slope, 1023. / slope, 0.});
- voltToCode12 = new PolynomialFunction(new double[]{-4095 * intercept / slope, 4095., 0.});
- }
-
- public double[] cal12(double[] RAW) {
- double[] calcData = new double[RAW.length];
- for (int i = 0; i < RAW.length; i++) {
- double avg_shifts = (adc_shifts.get((int) Math.floor(RAW[i])) + adc_shifts.get((int) Math.ceil(RAW[i]))) / 2;
- RAW[i] -= 4095 * avg_shifts / 3.3;
- calcData[i] = (polynomials.get((int) gain).value(RAW[i]));
- }
- return calcData;
- }
-
- public double[] cal10(double[] RAW) {
- double[] calcData = new double[RAW.length];
- for (int i = 0; i < RAW.length; i++) {
- RAW[i] *= 4095 / 1023;
- double avg_shifts = (adc_shifts.get((int) Math.floor(RAW[i])) + adc_shifts.get((int) Math.ceil(RAW[i]))) / 2;
- RAW[i] -= 4095 * avg_shifts / 3.3;
- calcData[i] = (polynomials.get((int) gain).value(RAW[i]));
- }
- return calcData;
- }
-}
diff --git a/app/src/main/java/io/pslab/communication/digitalChannel/DigitalChannel.java b/app/src/main/java/io/pslab/communication/digitalChannel/DigitalChannel.java
deleted file mode 100644
index c934e998f..000000000
--- a/app/src/main/java/io/pslab/communication/digitalChannel/DigitalChannel.java
+++ /dev/null
@@ -1,156 +0,0 @@
-package io.pslab.communication.digitalChannel;
-
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-
-/**
- * Created by viveksb007 on 26/3/17.
- */
-
-public class DigitalChannel {
-
- public static final int EVERY_EDGE = 1;
- public static final int DISABLED = 0;
- private static final int EVERY_SIXTEENTH_RISING_EDGE = 5;
- private static final int EVERY_FOURTH_RISING_EDGE = 4;
- private static final int EVERY_RISING_EDGE = 3;
- private static final int EVERY_FALLING_EDGE = 2;
- public static String[] digitalChannelNames = {"LA1", "LA2", "LA3", "LA4", "RES", "EXT", "FRQ"};
- public String channelName, dataType;
- public int initialStateOverride, channelNumber, length, prescalar, trigger, dlength, plotLength, maxTime, mode;
- public double xAxis[], yAxis[], timestamps[];
- boolean initialState;
- double gain, maxT;
-
- public DigitalChannel(int channelNumber) {
- this.channelNumber = channelNumber;
- this.channelName = digitalChannelNames[channelNumber];
- this.gain = 0;
- this.xAxis = new double[20000];
- this.yAxis = new double[20000];
- this.timestamps = new double[10000];
- this.length = 100;
- this.initialState = false;
- this.prescalar = 0;
- this.dataType = "int";
- this.trigger = 0;
- this.dlength = 0;
- this.plotLength = 0;
- this.maxT = 0;
- this.maxTime = 0;
- this.initialStateOverride = 0;
- this.mode = EVERY_EDGE;
- }
-
- void setParams(String channelName, int channelNumber) {
- this.channelName = channelName;
- this.channelNumber = channelNumber;
- }
-
- void setPrescalar(int prescalar) {
- this.prescalar = prescalar;
- }
-
- public void loadData(LinkedHashMap initialStates, double[] timestamps) {
- if (initialStateOverride != 0) {
- this.initialState = (initialStateOverride - 1) == 1;
- this.initialStateOverride = 0;
- } else {
- final Integer s = initialStates.get(channelName);
- this.initialState = s != null && s == 1;
- }
- System.arraycopy(timestamps, 0, this.timestamps, 0, timestamps.length);
- this.dlength = timestamps.length; //
- double factor;
- switch (prescalar) {
- case 0:
- factor = 64;
- break;
- case 1:
- factor = 8;
- break;
- case 2:
- factor = 4;
- break;
- default:
- factor = 1;
- }
- for (int i = 0; i < this.timestamps.length; i++) this.timestamps[i] /= factor;
- if (dlength > 0)
- this.maxT = this.timestamps[this.timestamps.length - 1];
- else
- this.maxT = 0;
-
- }
-
- public void generateAxes() {
- int HIGH = 1, LOW = 0, state;
- if (initialState)
- state = LOW;
- else
- state = HIGH;
-
- if (this.mode == DISABLED) {
- xAxis[0] = 0;
- yAxis[0] = 0;
- this.plotLength = 1;
- } else if (this.mode == EVERY_EDGE) {
- xAxis[0] = 0;
- yAxis[0] = state;
- int i, j;
- for (i = 1, j = 1; i < this.dlength; i++, j++) {
- xAxis[j] = timestamps[i];
- yAxis[j] = state;
- if (state == HIGH)
- state = LOW;
- else
- state = HIGH;
- j++;
- xAxis[j] = timestamps[i];
- yAxis[j] = state;
- }
- plotLength = j;
- } else if (this.mode == EVERY_FALLING_EDGE) {
- xAxis[0] = 0;
- yAxis[0] = HIGH;
- int i, j;
- for (i = 1, j = 1; i < this.dlength; i++, j++) {
- xAxis[j] = timestamps[i];
- yAxis[j] = HIGH;
- j++;
- xAxis[j] = timestamps[i];
- yAxis[j] = LOW;
- j++;
- xAxis[j] = timestamps[i];
- yAxis[j] = HIGH;
- }
- state = HIGH;
- plotLength = j;
- } else if (this.mode == EVERY_RISING_EDGE || this.mode == EVERY_FOURTH_RISING_EDGE || this.mode == EVERY_SIXTEENTH_RISING_EDGE) {
- xAxis[0] = 0;
- yAxis[0] = LOW;
- int i, j;
- for (i = 1, j = 1; i < this.dlength; i++, j++) {
- xAxis[j] = timestamps[i];
- yAxis[j] = LOW;
- j++;
- xAxis[j] = timestamps[i];
- yAxis[j] = HIGH;
- j++;
- xAxis[j] = timestamps[i];
- yAxis[j] = LOW;
- }
- state = LOW;
- plotLength = j;
- }
-
- }
-
- public double[] getXAxis() {
- return Arrays.copyOfRange(this.xAxis, 0, plotLength);
- }
-
- public double[] getYAxis() {
- return Arrays.copyOfRange(this.yAxis, 0, plotLength);
- }
-}
diff --git a/app/src/main/java/io/pslab/communication/peripherals/DACChannel.java b/app/src/main/java/io/pslab/communication/peripherals/DACChannel.java
deleted file mode 100644
index 2a58fc2b1..000000000
--- a/app/src/main/java/io/pslab/communication/peripherals/DACChannel.java
+++ /dev/null
@@ -1,69 +0,0 @@
-package io.pslab.communication.peripherals;
-
-import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
-
-import java.util.ArrayList;
-import java.util.List;
-/**
- * Created by viveksb007 on 28/3/17.
- */
-
-public class DACChannel {
- private String name;
- int channum;
- private int offset;
- public double[] range;
- private double slope, intercept;
- public PolynomialFunction VToCode;
- public int channelCode;
- PolynomialFunction CodeToV;
- String calibrationEnabled;
- private List calibrationTable = new ArrayList<>();
-
- public DACChannel(String name, double[] span, int channum, int channelCode) {
- this.name = name;
- this.range = span;
- this.channum = channum;
- this.slope = span[1] - span[0];
- this.intercept = span[0];
- this.VToCode = new PolynomialFunction(new double[]{-3300. * intercept / slope, 3300. / slope});
- this.CodeToV = new PolynomialFunction(new double[]{intercept, slope / 3300.});
- this.calibrationEnabled = "false";
- this.slope = 1;
- this.offset = 0;
- this.channelCode = channelCode;
- }
-
- public void loadCalibrationTable(List table) {
- calibrationEnabled = "table";
- calibrationTable = table;
- }
-
- public void loadCalibrationTwopoint(double slope, int offset) {
- calibrationEnabled = "twopoint";
- this.slope = slope;
- this.offset = offset;
- }
-
- int applyCalibration(int v) {
- if (calibrationEnabled.equals("table")) {
- if (v + calibrationTable.get(v) <= 0) {
- return 0;
- } else if (v + calibrationTable.get(v) > 0 && v + calibrationTable.get(v) < 4095) {
- return ((int) (v + calibrationTable.get(v)));
- } else {
- return 4095;
- }
- } else if (calibrationEnabled.equals("twopoint")) {
- if (slope * v + offset <= 0) {
- return 0;
- } else if (slope * v + offset > 0 && slope * v + offset < 4095) {
- return ((int) (slope * v + offset));
- } else {
- return 4095;
- }
- } else {
- return v;
- }
- }
-}
diff --git a/app/src/main/java/io/pslab/communication/peripherals/I2C.java b/app/src/main/java/io/pslab/communication/peripherals/I2C.java
deleted file mode 100644
index 4f5802056..000000000
--- a/app/src/main/java/io/pslab/communication/peripherals/I2C.java
+++ /dev/null
@@ -1,284 +0,0 @@
-package io.pslab.communication.peripherals;
-
-import android.os.SystemClock;
-import android.util.Log;
-
-import io.pslab.communication.CommandsProto;
-import io.pslab.communication.PacketHandler;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * Created by viveksb007 on 28/3/17.
- */
-
-public class I2C {
-
- private static final String TAG = "I2C";
- private double[] buffer;
- private int frequency = 100000;
- private CommandsProto commandsProto;
- private PacketHandler packetHandler;
- private int totalBytes, channels, samples, timeGap;
-
- public I2C(PacketHandler packetHandler) {
- this.buffer = new double[10000];
- Arrays.fill(buffer, 0);
- this.packetHandler = packetHandler;
- this.commandsProto = new CommandsProto();
- }
-
- public void init() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_INIT);
- packetHandler.getAcknowledgement(); // can check success or failure by ack
- }
-
- public void enableSMBus() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_ENABLE_SMBUS);
- packetHandler.getAcknowledgement();
- }
-
- public void pullSCLLow(int uSec) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_PULLDOWN_SCL);
- packetHandler.sendInt(uSec);
- packetHandler.getAcknowledgement();
- }
-
- public void config(int frequency) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_CONFIG);
- int BRGVAL = (int) ((1 / frequency - 1 / 1e7) * 64e6 - 1);
- if (BRGVAL > 511) {
- BRGVAL = 511;
- Log.v(TAG, "Frequency too low. Setting to : " + String.valueOf(1 / ((BRGVAL + 1.0) / 64e6 + 1.0 / 1e7)));
- }
- packetHandler.sendInt(BRGVAL);
- packetHandler.getAcknowledgement();
- }
-
- public int start(int address, int rw) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_START);
- packetHandler.sendByte((address << 1) | rw & 0xff);
- return (packetHandler.getAcknowledgement() >> 4);
- }
-
- public void stop() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_STOP);
- packetHandler.getAcknowledgement();
- }
-
- public void _wait() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_WAIT);
- packetHandler.getAcknowledgement();
- }
-
- public int send(int data) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_SEND);
- packetHandler.sendByte(data);
- return (packetHandler.getAcknowledgement() >> 4);
- }
-
- public int restart(int address, int rw) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_RESTART);
- packetHandler.sendByte((address << 1) | rw & 0xff);
- return (packetHandler.getAcknowledgement() >> 4);
- }
-
- public ArrayList simpleRead(int address, int numBytes) throws IOException {
- this.start(address, 1);
- return this.read(numBytes);
- }
-
- public ArrayList read(int length) throws IOException {
- ArrayList data = new ArrayList<>();
- for (int i = 0; i < length - 1; i++) {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_READ_MORE);
- data.add(packetHandler.getByte());
- packetHandler.getAcknowledgement();
- }
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_READ_END);
- data.add(packetHandler.getByte());
- packetHandler.getAcknowledgement();
- return data;
- }
-
- public byte readRepeat() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_READ_MORE);
- byte val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return val;
- }
-
- public byte readEnd() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_READ_END);
- byte val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return val;
- }
-
- public int readStatus() throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_STATUS);
- int val = packetHandler.getInt();
- packetHandler.getAcknowledgement();
- return val;
- }
-
- public ArrayList readBulk(int deviceAddress, int registerAddress, int bytesToRead) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_READ_BULK);
- packetHandler.sendByte(deviceAddress);
- packetHandler.sendByte(registerAddress);
- packetHandler.sendByte(bytesToRead);
- byte[] data = new byte[bytesToRead + 1];
- packetHandler.read(data, bytesToRead + 1);
- ArrayList intData = new ArrayList<>();
- for (byte b : data) {
- intData.add((int) b);
- }
- return intData;
- }
-
- public ArrayList read(int deviceAddress, int bytesToRead, int registerAddress) throws IOException {
- return readBulk(deviceAddress, registerAddress, bytesToRead);
- }
-
- public int readByte(int deviceAddress, int registerAddress) throws IOException {
- return read(deviceAddress, 1, registerAddress).get(0);
- }
-
- public int readInt(int deviceAddress, int registerAddress) throws IOException {
- ArrayList data = read(deviceAddress, 2, registerAddress);
- return data.get(0) << 8 | data.get(1);
- }
-
- public long readLong(int deviceAddress, int registerAddress) throws IOException {
- ArrayList data = read(deviceAddress, 4, registerAddress);
- return data.get(0) << 24 | data.get(1) << 16 | data.get(2) << 8 | data.get(3);
- }
-
- public void writeBulk(int deviceAddress, int[] data) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_WRITE_BULK);
- packetHandler.sendByte(deviceAddress);
- packetHandler.sendByte(data.length);
- for (int aData : data) {
- packetHandler.sendByte(aData);
- }
- packetHandler.getAcknowledgement();
- }
-
- public void write(int deviceAddress, int[] data, int registerAddress) throws IOException {
- int[] finalData = new int[data.length + 1];
- finalData[0] = registerAddress;
- System.arraycopy(data, 0, finalData, 1, data.length);
- writeBulk(deviceAddress, finalData);
- }
-
- public void writeByte(int deviceAddress, int registerAddress, int data) throws IOException {
- write(deviceAddress, new int[]{data}, registerAddress);
- }
-
- public void writeInt(int deviceAddress, int registerAddress, int data) throws IOException {
- write(deviceAddress, new int[]{data & 0xff, (data >> 8) & 0xff}, registerAddress);
- }
-
- public void writeLong(int deviceAddress, int registerAddress, long data) throws IOException {
- write(deviceAddress, new int[]{(int) (data & 0xff), (int) ((data >> 8) & 0xff), (int) ((data >> 16) & 0xff), (int) ((data >> 24) & 0xff)}, registerAddress);
- }
-
- public ArrayList scan(Integer frequency) throws IOException {
- Integer freq = frequency;
- if (frequency == null) freq = 125000;
- config(freq);
- ArrayList addresses = new ArrayList<>();
- for (int i = 0; i < 128; i++) {
- int x = start(i, 0);
- if ((x & 1) == 0) {
- addresses.add(i);
- }
- stop();
- }
- return addresses;
- }
-
- public void sendBurst(int data) throws IOException {
- packetHandler.sendByte(commandsProto.I2C_HEADER);
- packetHandler.sendByte(commandsProto.I2C_SEND);
- packetHandler.sendByte(data);
- }
-
- public ArrayList retreiveBuffer() throws IOException {
- int totalIntSamples = totalBytes / 2;
- Log.v(TAG, "Fetching samples : " + totalIntSamples + ", split : " + commandsProto.DATA_SPLITTING);
- ArrayList listData = new ArrayList<>();
- for (int i = 0; i < (totalIntSamples / commandsProto.DATA_SPLITTING); i++) {
- packetHandler.sendByte(commandsProto.ADC);
- packetHandler.sendByte(commandsProto.GET_CAPTURE_CHANNEL);
- packetHandler.sendByte(0);
- packetHandler.sendInt(commandsProto.DATA_SPLITTING);
- packetHandler.sendInt(i * commandsProto.DATA_SPLITTING);
- int remaining = commandsProto.DATA_SPLITTING * 2 + 1;
- // reading in single go, change if create communication problem
- byte[] data = new byte[remaining];
- packetHandler.read(data, remaining);
- for (int j = 0; j < data.length - 1; j++)
- listData.add(data[j]);
- }
-
- if ((totalIntSamples % commandsProto.DATA_SPLITTING) != 0) {
- packetHandler.sendByte(commandsProto.ADC);
- packetHandler.sendByte(commandsProto.GET_CAPTURE_CHANNEL);
- packetHandler.sendByte(0);
- packetHandler.sendInt(totalIntSamples % commandsProto.DATA_SPLITTING);
- packetHandler.sendInt(totalIntSamples - totalIntSamples % commandsProto.DATA_SPLITTING);
- int remaining = 2 * (totalIntSamples % commandsProto.DATA_SPLITTING) + 1;
- byte[] data = new byte[remaining];
- packetHandler.read(data, remaining);
- for (int j = 0; j < data.length - 1; j++)
- listData.add(data[j]);
- }
-
- Log.v(TAG, "Final Pass : length = " + listData.size());
- return listData;
- }
-
- public Map dataProcessor(ArrayList data, Boolean inInt) {
- if (inInt) {
- for (int i = 0; i < (this.channels * this.samples) / 2; i++)
- this.buffer[i] = (data.get(i * 2) << 8) | (data.get(i * 2 + 1));
- } else {
- for (int i = 0; i < (this.channels * this.samples); i++)
- this.buffer[i] = data.get(i);
- }
- Map retData = new LinkedHashMap<>();
- ArrayList timeBase = new ArrayList<>();
- double factor = timeGap * (this.samples - 1) / this.samples;
- for (double i = 0; i < timeGap * (this.samples - 1); i += factor) timeBase.add(i);
- retData.put("time", timeBase);
- for (int i = 0; i < this.channels / 2; i++) {
- ArrayList yValues = new ArrayList<>();
- for (int j = i; j < this.samples * this.channels / 2; j += this.channels / 2) {
- yValues.add(buffer[j]);
- }
- retData.put("CH" + String.valueOf(i + 1), yValues);
- }
- return retData;
- }
-}
diff --git a/app/src/main/java/io/pslab/communication/peripherals/MCP4728.java b/app/src/main/java/io/pslab/communication/peripherals/MCP4728.java
deleted file mode 100644
index 20841c4cb..000000000
--- a/app/src/main/java/io/pslab/communication/peripherals/MCP4728.java
+++ /dev/null
@@ -1,81 +0,0 @@
-package io.pslab.communication.peripherals;
-
-import io.pslab.communication.PacketHandler;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Created by viveksb007 on 28/3/17.
- */
-
-public class MCP4728 {
-
- int defaultVDD = 3300;
- int RESET = 6;
- int WAKEUP = 9;
- int UPDATE = 8;
- int WRITEALL = 64;
- int WRITEONE = 88;
- int SEQWRITE = 80;
- int VREFWRITE = 128;
- int GAINWRITE = 192;
- int POWERDOWNWRITE = 160;
- int GENERALCALL = 0;
- private PacketHandler packetHandler;
- private double vref;
- private int devid;
- private I2C i2c;
- private List switchedOff;
- private List vRefs;
- private Map channelMap = new LinkedHashMap<>();
- private int addr;
-
- public MCP4728(PacketHandler packetHandler, I2C i2c) {
- this.packetHandler = packetHandler;
- this.vref = 3.3;
- this.devid = 0;
- switchedOff = new ArrayList<>(Arrays.asList(0, 0, 0, 0));
- vRefs = new ArrayList<>(Arrays.asList(0, 0, 0, 0));
- this.i2c = i2c;
- addr = 0x60 | devid;
- channelMap.put(0, "PCS");
- channelMap.put(1, "PV3");
- channelMap.put(2, "PV2");
- channelMap.put(3, "PV1");
-
- }
-
- public void writeAll(int v1, int v2, int v3, int v4) {
- try {
- i2c.start(addr, 0);
- i2c.send((v1 >> 8) & 0xF);
- i2c.send(v1 & 0xFF);
- i2c.send((v2 >> 8) & 0xF);
- i2c.send(v2 & 0xFF);
- i2c.send((v3 >> 8) & 0xF);
- i2c.send(v3 & 0xFF);
- i2c.send((v4 >> 8) & 0xF);
- i2c.send(v4 & 0xFF);
- i2c.stop();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void stat() {
- try {
- i2c.start(addr, 0);
- i2c.send(0x0);
- i2c.restart(addr, 1);
- i2c.read(24);
- i2c.stop();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-}
diff --git a/app/src/main/java/io/pslab/communication/peripherals/NRF24L01.java b/app/src/main/java/io/pslab/communication/peripherals/NRF24L01.java
deleted file mode 100644
index 3fc4e4b1a..000000000
--- a/app/src/main/java/io/pslab/communication/peripherals/NRF24L01.java
+++ /dev/null
@@ -1,530 +0,0 @@
-package io.pslab.communication.peripherals;
-
-import android.util.Log;
-
-import io.pslab.communication.CommandsProto;
-import io.pslab.communication.PacketHandler;
-import io.pslab.communication.SensorList;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * Created by viveksb007 on 28/3/17.
- */
-
-public class NRF24L01 {
-
- private static final String TAG = "NRF24L01";
- private int R_REG = 0x00;
- private int W_REG = 0x20;
- private int RX_PAYLOAD = 0x61;
- private int TX_PAYLOAD = 0xA0;
- private int ACK_PAYLOAD = 0xA8;
- private int FLUSH_TX = 0xE1;
- private int FLUSH_RX = 0xE2;
- private int ACTIVATE = 0x50;
- private int R_STATUS = 0xFF;
-
- private int NRF_CONFIG = 0x00;
- private int EN_AA = 0x01;
- private int EN_RXADDR = 0x02;
- private int SETUP_AW = 0x03;
- private int SETUP_RETR = 0x04;
- private int RF_CH = 0x05;
- private int RF_SETUP = 0x06;
- private int NRF_STATUS = 0x07;
- private int OBSERVE_TX = 0x08;
- private int CD = 0x09;
- private int RX_ADDR_P0 = 0x0A;
- private int RX_ADDR_P1 = 0x0B;
- private int RX_ADDR_P2 = 0x0C;
- private int RX_ADDR_P3 = 0x0D;
- private int RX_ADDR_P4 = 0x0E;
- private int RX_ADDR_P5 = 0x0F;
- private int TX_ADDR = 0x10;
- private int RX_PW_P0 = 0x11;
- private int RX_PW_P1 = 0x12;
- private int RX_PW_P2 = 0x13;
- private int RX_PW_P3 = 0x14;
- private int RX_PW_P4 = 0x15;
- private int RX_PW_P5 = 0x16;
- private int R_RX_PL_WID = 0x60;
- private int FIFO_STATUS = 0x17;
- private int DYNPD = 0x1C;
- private int FEATURE = 0x1D;
- private int PAYLOAD_SIZE = 0;
- private int ACK_PAYLOAD_SIZE = 0;
- private int READ_PAYLOAD_SIZE = 0;
-
- private int ADC_COMMANDS = 1;
- private int READ_ADC = 0 << 4;
-
- private int I2C_COMMANDS = 2;
- private int I2C_TRANSACTION = 0 << 4;
- private int I2C_WRITE = 1 << 4;
- private int I2C_SCAN = 2 << 4;
- private int PULL_SCL_LOW = 3 << 4;
- private int I2C_CONFIG = 4 << 4;
- private int I2C_READ = 5 << 4;
-
- private int NRF_COMMANDS = 3;
- private int NRF_READ_REGISTER = 0;
- private int NRF_WRITE_REGISTER = 1 << 4;
-
- public int CURRENT_ADDRESS = 0xAAAA01;
- private int nodePos = 0, status = 0;
- private int NODELIST_MAXLENGTH = 15;
- public boolean connected = false, ready = false;
-
- private Map sigs = new LinkedHashMap<>();
- private PacketHandler packetHandler;
- private CommandsProto commandsProto;
- private Map> nodeList = new LinkedHashMap<>();
-
- public NRF24L01(PacketHandler packetHandler) {
- this.packetHandler = packetHandler;
- this.commandsProto = new CommandsProto();
- sigs.put(CURRENT_ADDRESS, 1);
- if (packetHandler.isConnected()) {
- connected = init();
- }
- }
-
- private boolean init() {
- try {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_SETUP);
- packetHandler.getAcknowledgement();
- // add code for sleep for 15mSec
- status = getStatus();
- if ((status & 0x80) != 0) {
- Log.e(TAG, "Radio transceiver not installed/not found");
- return false;
- } else {
- ready = true;
- }
- selectAddress(CURRENT_ADDRESS);
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
-
- public void selectAddress(int address) {
- try {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_WRITEADDRESS);
- packetHandler.sendByte(address & 0xff);
- packetHandler.sendByte((address >> 8) & 0xff);
- packetHandler.sendByte((address >> 16) & 0xff);
- packetHandler.getAcknowledgement();
- this.CURRENT_ADDRESS = address;
- if (!sigs.containsKey(address)) {
- sigs.put(address, 1);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void writeAddress(int register, int address) {
- try {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_WRITEADDRESSES);
- packetHandler.sendByte(register);
- packetHandler.sendByte(address & 0xff);
- packetHandler.sendByte((address >> 8) & 0xff);
- packetHandler.sendByte((address >> 16) & 0xff);
- packetHandler.getAcknowledgement();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public int getStatus() {
- int val = -1;
- try {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_GETSTATUS);
- val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return val;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return val;
- }
-
- public void rxMode() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_RXMODE);
- packetHandler.getAcknowledgement();
- }
-
- public void txMode() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_TXMODE);
- packetHandler.getAcknowledgement();
- }
-
- public void powerDown() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_POWER_DOWN);
- packetHandler.getAcknowledgement();
- }
-
- public char rxChar() throws IOException {
- int val = -1;
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_RXCHAR);
- val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return ((char) (val & 0xff));
- }
-
- public int txChar(char character) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_TXCHAR);
- packetHandler.sendByte(character);
- return packetHandler.getAcknowledgement() >> 4;
- }
-
- public int hasData() throws IOException {
- int val = -1;
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_HASDATA);
- val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return val;
- }
-
- public void flush() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_FLUSH);
- packetHandler.getAcknowledgement();
- }
-
- public void writeRegister(int address, int value) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_WRITEREG);
- packetHandler.sendByte(address);
- packetHandler.sendByte(value);
- packetHandler.getAcknowledgement();
- }
-
- public byte readRegister(int address) throws IOException {
- byte val = -1;
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_READREG);
- packetHandler.sendByte(address);
- val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return val;
- }
-
- public void writeCommand(int command) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_WRITECOMMAND);
- packetHandler.sendByte(command);
- packetHandler.getAcknowledgement();
- }
-
- public ArrayList readPayload(int numBytes) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_READPAYLOAD);
- packetHandler.sendByte(numBytes);
- byte[] data = new byte[numBytes];
- packetHandler.read(data, numBytes);
- packetHandler.getAcknowledgement();
- ArrayList charData = new ArrayList<>();
- for (int i = 0; i < numBytes; i++) {
- charData.add((char) data[i]);
- }
- return charData;
- }
-
- public int writePayload(int[] data, boolean rxMode) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_WRITEPAYLOAD);
- int numBytes = data.length | 0x80;
- if (rxMode) numBytes |= 0x40;
- packetHandler.sendByte(numBytes);
- packetHandler.sendByte(TX_PAYLOAD);
- for (int _data : data) {
- packetHandler.sendByte(_data);
- }
- int val = packetHandler.getAcknowledgement() >> 4;
- if ((val & 0x2) != 0)
- Log.e(TAG, "NRF radio not found. Connect one to the add-on port");
- else if ((val & 0x1) != 0)
- Log.e(TAG, "Node probably dead/out of range. It failed to acknowledge");
- return val;
- }
-
- public void startTokenManager() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_START_TOKEN_MANAGER);
- packetHandler.getAcknowledgement();
- }
-
- public void stopTokenManager() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_STOP_TOKEN_MANAGER);
- packetHandler.getAcknowledgement();
- }
-
- public int totalTokens() throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_TOTAL_TOKENS);
- int val = packetHandler.getByte();
- packetHandler.getAcknowledgement();
- return val;
- }
-
- public ArrayList fetchReport(int num) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_REPORTS);
- packetHandler.sendByte(num);
- ArrayList data = new ArrayList<>();
- for (int i = 0; i < 20; i++) {
- data.add(packetHandler.getByte());
- }
- packetHandler.getAcknowledgement();
- return data;
- }
-
- public ArrayList decodeI2CList(int[] data) {
- int sum = 0;
- ArrayList addressList = new ArrayList<>();
- for (int _data : data) {
- sum += _data;
- }
- if (sum == 0) return addressList;
- for (int i = 0; i < data.length; i++) {
- if ((data[i] ^ 255) != 0) {
- for (int j = 0; j < 8; j++) {
- if ((data[i] & (0x80 >> j)) == 0) {
- addressList.add(8 * i + j);
- }
- }
- }
- }
- return addressList;
- }
-
- public Map> getNodeList() throws IOException {
- int total = totalTokens();
- if (this.nodePos != total) {
- for (int i = 0; i < this.NODELIST_MAXLENGTH; i++) {
- ArrayList data = fetchReport(i);
- int txrx = (data.get(0)) | (data.get(1) << 8) | (data.get(2) << 16);
- if (txrx == 0) continue;
- int[] tempData = new int[17];
- for (int j = 3; j < 20; j++)
- tempData[j - 3] = data.get(j);
- nodeList.put(txrx, decodeI2CList(tempData));
- this.nodePos = total;
- }
- }
- Map> filteredList = new LinkedHashMap<>();
- for (Map.Entry> entry : nodeList.entrySet()) {
- if (isAlive(entry.getKey()) != null) {
- filteredList.put(entry.getKey(), entry.getValue());
- }
- }
- return filteredList;
- }
-
- public ArrayList isAlive(int address) throws IOException {
- selectAddress(address);
- return transaction(new int[]{NRF_COMMANDS | NRF_READ_REGISTER, R_STATUS}, 0, 100);
- }
-
- public ArrayList transaction(int[] data, int listen, int timeout) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_TRANSACTION);
- packetHandler.sendByte(data.length);
- packetHandler.sendInt(timeout);
- for (int _data : data) {
- packetHandler.sendByte(_data);
- }
- ArrayList characterData = new ArrayList<>();
- int numBytes = packetHandler.getByte();
- byte[] readData;
- if (numBytes != -1) {
- readData = new byte[numBytes];
- packetHandler.read(readData, numBytes);
- } else {
- readData = null;
- }
- int val = packetHandler.getAcknowledgement() >> 4;
- if ((val & 0x1) != 0) Log.e(TAG, "Node not found " + CURRENT_ADDRESS);
- if ((val & 0x2) != 0) Log.e(TAG, "NRF on-board transmitter not found " + CURRENT_ADDRESS);
- if ((val & 0x4) != 0 & (listen == 1))
- Log.e(TAG, "Node received command but did not reply " + CURRENT_ADDRESS);
-
- if ((val & 0x7) != 0) {
- flush();
- sigs.put(CURRENT_ADDRESS, sigs.get(CURRENT_ADDRESS) * 50 / 51);
- return null;
- }
- sigs.put(CURRENT_ADDRESS, (sigs.get(CURRENT_ADDRESS) * 50 + 1) / 51);
- if (readData == null) return characterData;
- for (int i = 0; i < numBytes; i++) {
- characterData.add((char) readData[i]);
- }
- return characterData;
- }
-
- public ArrayList transactionWithRetries(int[] data, int retries) throws IOException {
- if (retries == -1) retries = 5;
- ArrayList reply = null;
- while (retries > 0) {
- reply = transaction(data, 0, 200);
- if (reply != null) {
- break;
- }
- retries--;
- }
- return reply;
- }
-
- public void deleteRegisteredNode(int num) throws IOException {
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_DELETE_REPORT_ROW);
- packetHandler.sendByte(num);
- packetHandler.getAcknowledgement();
- }
-
- public void deleteAllRegisteredNodes() throws IOException {
- while (totalTokens() != 0) {
- deleteRegisteredNode(0);
- }
- }
-
- public void initShockBurstTransmitter(int payloadSize, int myAddress, int sendAddress) throws IOException {
- if (payloadSize != -1) PAYLOAD_SIZE = payloadSize;
- if (myAddress != -1) myAddress = 0xAAAA01;
- if (sendAddress != -1) sendAddress = 0xAAAA01;
-
- init();
- writeAddress(RX_ADDR_P0, myAddress);
- writeAddress(TX_ADDR, sendAddress);
- writeRegister(RX_PW_P0, PAYLOAD_SIZE);
- rxMode();
- // Add code for sleep 0.1 sec
- flush();
- }
-
- public void initShockBurstReceiver(int payloadSize, int[] myAddress) throws IOException {
- if (payloadSize != -1) {
- PAYLOAD_SIZE = payloadSize;
- }
- if (myAddress[0] != -1) {
- myAddress[0] = 0xA523B5;
- }
- init();
- writeRegister(RF_SETUP, 0x26);
- int enabledPipes = 0;
- for (int i = 0; i < 6; i++) {
- if (myAddress[i] != -1) {
- enabledPipes |= (1 << i);
- writeAddress(RX_ADDR_P0 + i, myAddress[i]);
- }
- }
- if (myAddress[1] != -1)
- writeAddress(RX_ADDR_P1, myAddress[1]);
-
- writeRegister(EN_RXADDR, enabledPipes);
- writeRegister(EN_AA, enabledPipes);
- writeRegister(DYNPD, enabledPipes);
- writeRegister(FEATURE, 0x06);
-
- rxMode();
- // Add code for sleep 0.1 sec
- flush();
- }
-
- public void triggerAll(int val) throws IOException {
- txMode();
- selectAddress(0x111111);
- writeRegister(EN_AA, 0x00);
- writePayload(new int[]{val}, true);
- writeRegister(EN_AA, 0x01);
- }
-
- public int writeAckPayload(int[] data, int pipe) throws IOException {
- if (data.length != ACK_PAYLOAD_SIZE) {
- ACK_PAYLOAD_SIZE = data.length;
- if (ACK_PAYLOAD_SIZE > 15) {
- Log.v(TAG, "too large. Truncating");
- ACK_PAYLOAD_SIZE = 15;
- data = Arrays.copyOf(data, 15);
- } else {
- Log.v(TAG, "Ack payload size " + ACK_PAYLOAD_SIZE);
- }
- }
- packetHandler.sendByte(commandsProto.NRFL01);
- packetHandler.sendByte(commandsProto.NRF_WRITEPAYLOAD);
- packetHandler.sendByte(data.length);
- packetHandler.sendByte(ACK_PAYLOAD | pipe);
- for (int _data : data) {
- packetHandler.sendByte(_data);
- }
- return packetHandler.getAcknowledgement() >> 4;
- }
-
- public ArrayList i2CScan() throws IOException {
- ArrayList addresses = new ArrayList<>();
- ArrayList temp = transaction(new int[]{I2C_COMMANDS | I2C_SCAN | 0x80}, 0, 500);
- if (temp == null) return addresses;
- int sum = 0;
- for (int i = 0; i < temp.size(); i++) {
- sum += (int) temp.get(i);
- }
- if (sum == 0) return addresses;
-
- for (int i = 0; i < 16; i++) {
- if ((temp.get(i) ^ 255) != 0) {
- for (int j = 0; j < 8; j++) {
- if ((temp.get(i) & (0x80 >> j)) == 0) {
- addresses.add(8 * i + j);
- }
- }
- }
- }
- return addresses;
- }
-
- public ArrayList guessingScan() throws IOException {
- ArrayList addresses = new ArrayList<>();
- ArrayList temp = transaction(new int[]{I2C_COMMANDS | I2C_SCAN | 0x80}, 0, 500);
- if (temp == null) return addresses;
- int sum = 0;
- for (int i = 0; i < temp.size(); i++) {
- sum += (int) temp.get(i);
- }
- if (sum == 0) return addresses;
- Log.v(TAG, "Address \t Possible Devices");
- SensorList sensorList = new SensorList();
- for (int i = 0; i < 16; i++) {
- if ((temp.get(i) ^ 255) != 0) {
- for (int j = 0; j < 8; j++) {
- if ((temp.get(i) & (0x80 >> j)) == 0) {
- int address = 8 * i + j;
- addresses.add(address);
- Log.v(TAG, Integer.toHexString(address) + "\t" + Arrays.toString(sensorList.sensorList.get(address)));
- }
- }
- }
- }
-
- return addresses;
- }
-
-}
\ No newline at end of file
diff --git a/app/src/main/java/io/pslab/communication/peripherals/RadioLink.java b/app/src/main/java/io/pslab/communication/peripherals/RadioLink.java
deleted file mode 100644
index 9a0d5be7d..000000000
--- a/app/src/main/java/io/pslab/communication/peripherals/RadioLink.java
+++ /dev/null
@@ -1,184 +0,0 @@
-package io.pslab.communication.peripherals;
-
-import android.util.Log;
-
-import io.pslab.communication.SensorList;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-
-/**
- * Created by viveksb007 on 28/3/17.
- */
-
-public class RadioLink {
-
- private static final String TAG = "RadioLink";
- private int ADC_COMMANDS = 1;
- private int READ_ADC = 0 << 4;
-
- private int I2C_COMMANDS = 2;
- private int I2C_TRANSACTION = 0 << 4;
- private int I2C_WRITE = 1 << 4;
- private int I2C_SCAN = 2 << 4;
- private int PULL_SCL_LOW = 3 << 4;
- private int I2C_CONFIG = 4 << 4;
- private int I2C_READ = 5 << 4;
-
- private int NRF_COMMANDS = 3;
- private int NRF_READ_REGISTER = 0 << 4;
- private int NRF_WRITE_REGISTER = 1 << 4;
-
- private int MISC_COMMANDS = 4;
- private int WS2812B_CMD = 0 << 4;
-
- private NRF24L01 nrf24L01;
- private int ADDRESS = 0x010101;
- private int timeout = 200;
-
- public RadioLink(NRF24L01 nrf24L01, int address) {
- this.nrf24L01 = nrf24L01;
- if (address != -1) ADDRESS = address;
-
- }
-
- private void selectMe() {
- if (this.nrf24L01.CURRENT_ADDRESS != this.ADDRESS) {
- this.nrf24L01.selectAddress(this.ADDRESS);
- }
- }
-
- public ArrayList writeI2C(int I2CAddress, int regAddress, int[] data) throws IOException {
- selectMe();
- int[] newData = new int[3 + data.length];
- newData[0] = I2C_COMMANDS | I2C_WRITE;
- newData[1] = I2CAddress;
- newData[2] = regAddress;
- System.arraycopy(data, 0, newData, 3, data.length);
- return this.nrf24L01.transaction(newData, 0, timeout);
- }
-
- public ArrayList readI2C(int I2CAddress, int regAddress, int numBytes) throws IOException {
- selectMe();
- return this.nrf24L01.transaction(new int[]{I2C_COMMANDS | I2C_TRANSACTION, I2CAddress, regAddress, numBytes}, 0, timeout);
- }
-
- public ArrayList writeBulk(int I2CAddress, int[] data) throws IOException {
- selectMe();
- int[] newData = new int[2 + data.length];
- newData[0] = I2C_COMMANDS | I2C_WRITE;
- newData[1] = I2CAddress;
- System.arraycopy(data, 0, newData, 2, data.length);
- return this.nrf24L01.transaction(newData, 0, timeout);
- }
-
- public ArrayList