1033 lines
31 KiB
Python
1033 lines
31 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit, QPlainTextEdit, QComboBox, QMessageBox
|
|
from PyQt5.QtCore import QProcess, QSize
|
|
from everywhereml.arduino import Sketch, Ino, H
|
|
import platform
|
|
import os
|
|
|
|
class LogView(QPlainTextEdit):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.setReadOnly(True)
|
|
self._process = QProcess()
|
|
self._process.readyReadStandardOutput.connect(self.handle_stdout)
|
|
self._process.readyReadStandardError.connect(self.handle_stderr)
|
|
|
|
def start_log(self, program, arguments=None):
|
|
if arguments is None:
|
|
arguments = []
|
|
self._process.start(program, arguments)
|
|
|
|
def add_log(self, message):
|
|
self.appendPlainText(message.rstrip())
|
|
|
|
def handle_stdout(self):
|
|
message = self._process.readAllStandardOutput().data().decode()
|
|
self.add_log(message)
|
|
|
|
def handle_stderr(self):
|
|
message = self._process.readAllStandardError().data().decode()
|
|
self.add_log(message)
|
|
|
|
|
|
def flash(log_view, led1, led2, port):
|
|
"""
|
|
Create a sketch object.
|
|
A sketch is defined by:
|
|
- a name (required)
|
|
- a folder (optional)
|
|
|
|
If you leave the folder empty, the current working directory will be used.
|
|
You can use the special name ':system:' to use the default Arduino sketches folder
|
|
(as reported by the command `arduino-cli config dump`)
|
|
"""
|
|
|
|
showdialog()
|
|
|
|
sketch = Sketch(name="hemocube", folder=":system:")
|
|
|
|
"""
|
|
Then you can add files to the project (either the .ino main file or
|
|
C++ header files)
|
|
"""
|
|
sketch += Ino("""
|
|
#include "ADS1X15.h"
|
|
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include <math.h>
|
|
|
|
const int buttonPin = 12; // Button pin
|
|
const int PIN_CS = 10;
|
|
const int GAIN_1 = 0x1;
|
|
const int GAIN_2 = 0x0;
|
|
const unsigned int steps = 512;
|
|
unsigned int sines_of_steps[steps];
|
|
char data; //Variable to store the data
|
|
|
|
ADS1115 ADS(0x48);
|
|
|
|
int blue_dac = {led1dac}; //2220; //390 2940
|
|
int green_dac = {led2dac}; //1880; //2320 2860
|
|
|
|
int green_sample_size = 100;
|
|
int blue_sample_size = 100;
|
|
|
|
int loopcounter = 0;
|
|
unsigned long long intensity_storage = 0;
|
|
int temp_counter = 0;
|
|
|
|
float avg_storage = 0;
|
|
float avg_blueS = 0;
|
|
float avg_blueS1 = 0;
|
|
float avg_blueS2 = 0;
|
|
float avg_blueS3 = 0;
|
|
float avg_greenS = 0;
|
|
float avg_greenS1 = 0;
|
|
float avg_greenS2 = 0;
|
|
float avg_greenS3 = 0;
|
|
float avg_blueB = 0;
|
|
float avg_blueB1 = 0;
|
|
float avg_blueB2 = 0;
|
|
float avg_blueB3 = 0;
|
|
float avg_greenB = 0;
|
|
float avg_greenB1 = 0;
|
|
float avg_greenB2 = 0;
|
|
float avg_greenB3 = 0;
|
|
float old_blue = 0;
|
|
float old_green = 0;
|
|
float avg_green = 0;
|
|
float avg_blue = 0;
|
|
//
|
|
bool buttonState = false; // Current button state
|
|
bool lastButtonState = false; // Previous button state
|
|
//int taskNumber = 1; // Current task number
|
|
bool taskStarted = false;
|
|
//int buttonPressCount = 0;
|
|
int bs;
|
|
int split_counter = 0;
|
|
int intensity_display = 1;
|
|
int plotter = 0;
|
|
int blue_stable_time = 50;
|
|
int green_stable_time = 50;
|
|
|
|
int blue_samples = 10;
|
|
int green_samples = 10;
|
|
|
|
int moving_average_array[] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
|
|
int sorting_array[] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
|
|
int moving_average = 0;
|
|
int j = 0;
|
|
int temp_sort = 0;
|
|
|
|
void setup() {{
|
|
|
|
Serial.begin(115200);
|
|
Serial.println("SN HCV1001");
|
|
pinMode(PIN_CS, OUTPUT);
|
|
pinMode(buttonPin, INPUT_PULLUP);
|
|
SPI.begin();
|
|
SPI.setClockDivider(SPI_CLOCK_DIV2);
|
|
ADS.begin();
|
|
}}
|
|
|
|
void setOutput(byte channel, byte gain, byte shutdown, unsigned int val)
|
|
{{
|
|
byte lowByte = val & 0xff;
|
|
byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;
|
|
PORTB &= 0xfb;
|
|
SPI.transfer(highByte);
|
|
SPI.transfer(lowByte);
|
|
PORTB |= 0x4;
|
|
}}
|
|
|
|
void loop() {{
|
|
loopcounter = 0;
|
|
avg_storage = 0;
|
|
split_counter = 0;
|
|
|
|
if (Serial.available()) {{
|
|
char data = Serial.read();
|
|
|
|
if (data == 'B') {{
|
|
taskStarted = false;
|
|
Serial.println("#Place Buffer");
|
|
|
|
while (!taskStarted) {{
|
|
buttonState = digitalRead(buttonPin);
|
|
|
|
if (buttonState == HIGH && lastButtonState == LOW) {{
|
|
lastButtonState = buttonState;
|
|
taskStarted = true;
|
|
startTask1();
|
|
}}
|
|
|
|
lastButtonState = buttonState;
|
|
}}
|
|
|
|
taskStarted = false;
|
|
while (!taskStarted) {{
|
|
buttonState = digitalRead(buttonPin);
|
|
|
|
if (buttonState == HIGH && lastButtonState == LOW) {{
|
|
lastButtonState = buttonState;
|
|
taskStarted = true;
|
|
startTask2();
|
|
}}
|
|
|
|
lastButtonState = buttonState;
|
|
}}
|
|
}}
|
|
else if (data == 'S') {{
|
|
Serial.println("Place Sample");
|
|
while (digitalRead(buttonPin) == LOW) {{
|
|
delay(10);
|
|
}}
|
|
startTask2();
|
|
}}
|
|
|
|
else if (data == 'P') {{ //Check if the received character is 1
|
|
Serial.print("RESULT");
|
|
Serial.print(" "); Serial.print("SN HCV1001"); Serial.print(" "); Serial.print(avg_greenB); Serial.print(" "); Serial.print(avg_blueB); Serial.print(" "); Serial.print(avg_greenS); Serial.print(" "); Serial.print(avg_blueS); Serial.print(" ");Serial.print("REND");Serial.println(" ");
|
|
|
|
delay(100);
|
|
}}
|
|
|
|
}}
|
|
}}
|
|
void startTask1() {{
|
|
Serial.println("#Buffer Started");
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
setOutput(0, GAIN_1, 1, green_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
float f = ADS.toVoltage(1); // voltage factor
|
|
for (int i = 0; i < green_samples + green_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
if (i >= green_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Green_Resistor_drop: "); Serial.print(val_2);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_greenB1 = intensity_storage / temp_counter;
|
|
delay(500);
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, blue_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
for (int i = 0; i < blue_samples + blue_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
//Serial.println(moving_average_array[0]);
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
//Serial.println(sorting_array[0]);
|
|
if (i >= blue_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Blue_Resistor_drop: "); Serial.print(val_3);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_blueB1 = intensity_storage / temp_counter;
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
delay (100);
|
|
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
setOutput(0, GAIN_1, 1, green_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
f = ADS.toVoltage(1); // voltage factor
|
|
for (int i = 0; i < green_samples + green_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
if (i >= green_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Green_Resistor_drop: "); Serial.print(val_2);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_greenB2 = intensity_storage / temp_counter;
|
|
delay(500);
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, blue_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
for (int i = 0; i < blue_samples + blue_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
//Serial.println(moving_average_array[0]);
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
//Serial.println(sorting_array[0]);
|
|
if (i >= blue_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Blue_Resistor_drop: "); Serial.print(val_3);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_blueB2 = intensity_storage / temp_counter;
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
delay (100);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
setOutput(0, GAIN_1, 1, green_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
f = ADS.toVoltage(1); // voltage factor
|
|
for (int i = 0; i < green_samples + green_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
if (i >= green_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Green_Resistor_drop: "); Serial.print(val_2);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_greenB3 = intensity_storage / temp_counter;
|
|
delay(500);
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, blue_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
for (int i = 0; i < blue_samples + blue_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
//Serial.println(moving_average_array[0]);
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
//Serial.println(sorting_array[0]);
|
|
if (i >= blue_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Blue_Resistor_drop: "); Serial.print(val_3);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_blueB3 = intensity_storage / temp_counter;
|
|
delay(100);
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
avg_blueB = (avg_blueB1 + avg_blueB2 + avg_blueB3) / 3;
|
|
avg_greenB = (avg_greenB1 + avg_greenB2 + avg_greenB3) / 3;
|
|
Serial.print("ok Green B Intensity: "); Serial.println(avg_greenB);
|
|
Serial.print("ok Blue B Intensity: "); Serial.println(avg_blueB);
|
|
Serial.println("#Buffer Completed");
|
|
Serial.println("#Place sample");
|
|
delay(200);
|
|
|
|
}}
|
|
void startTask2() {{
|
|
Serial.println("#Sample started");
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
setOutput(0, GAIN_1, 1, green_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
float f = ADS.toVoltage(1); // voltage factor
|
|
for (int i = 0; i < green_samples + green_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
if (i >= green_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Green_Resistor_drop: "); Serial.print(val_2);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_greenS1 = intensity_storage / temp_counter;
|
|
delay(500);
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, blue_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
for (int i = 0; i < blue_samples + blue_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
//Serial.println(moving_average_array[0]);
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
//Serial.println(sorting_array[0]);
|
|
if (i >= blue_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Blue_Resistor_drop: "); Serial.print(val_3);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_blueS1 = intensity_storage / temp_counter;
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
delay(100);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
setOutput(0, GAIN_1, 1, green_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
f = ADS.toVoltage(1); // voltage factor
|
|
for (int i = 0; i < green_samples + green_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
if (i >= green_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Green_Resistor_drop: "); Serial.print(val_2);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_greenS2 = intensity_storage / temp_counter;
|
|
delay(500);
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, blue_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
for (int i = 0; i < blue_samples + blue_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
//Serial.println(moving_average_array[0]);
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
//Serial.println(sorting_array[0]);
|
|
if (i >= blue_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Blue_Resistor_drop: "); Serial.print(val_3);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_blueS2 = intensity_storage / temp_counter;
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
delay(100);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
setOutput(0, GAIN_1, 1, green_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
f = ADS.toVoltage(1); // voltage factor
|
|
for (int i = 0; i < green_samples + green_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
if (i >= green_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Green_Resistor_drop: "); Serial.print(val_2);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_greenS3 = intensity_storage / temp_counter;
|
|
delay(500);
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, blue_dac);
|
|
memset (moving_average_array, 0, 10);
|
|
temp_counter = 0;
|
|
intensity_storage = 0;
|
|
for (int i = 0; i < blue_samples + blue_stable_time; i += 1)
|
|
{{
|
|
|
|
int16_t val_0 = ADS.readADC(0);
|
|
int16_t val_1 = ADS.readADC(1);
|
|
int16_t val_2 = ADS.readADC(2);
|
|
int16_t val_3 = ADS.readADC(3);
|
|
moving_average_array[i % 10] = val_1;
|
|
//Serial.println(moving_average_array[0]);
|
|
for (int j = 0; j < 10; j++)
|
|
{{
|
|
sorting_array[j] = moving_average_array[j];
|
|
}}
|
|
//Serial.println(sorting_array[0]);
|
|
if (i >= blue_stable_time)
|
|
{{
|
|
for (int j = 0; j < 6; j++)
|
|
{{
|
|
for (int k = 0; k < (9 - j); k++)
|
|
{{
|
|
if (sorting_array[k] > sorting_array[k + 1])
|
|
{{
|
|
temp_sort = sorting_array[k];
|
|
sorting_array[k] = sorting_array[k + 1];
|
|
sorting_array[k + 1] = temp_sort;
|
|
}}
|
|
}}
|
|
}}
|
|
moving_average = (sorting_array[4] / 2) + (sorting_array[5] / 2);
|
|
if ((val_1 > (moving_average * 0.9)) and (val_1 < (moving_average * 1.1)))
|
|
{{
|
|
intensity_storage += val_1;
|
|
|
|
temp_counter++;
|
|
}}
|
|
}}
|
|
if (intensity_display == 1)
|
|
{{
|
|
// Serial.print("DAC_value : "); Serial.print(i); Serial.print('\t'); Serial.print('\t'); Serial.print("Blue_Resistor_drop: "); Serial.print(val_3);
|
|
// Serial.print('\t'); Serial.print('\t'); Serial.print("PD_output: "); Serial.print(val_1); Serial.print('\t'); Serial.print('\t'); Serial.print("PD_2X_output: "); Serial.println(val_0);
|
|
}}
|
|
if (plotter == 1)
|
|
{{
|
|
Serial.println(val_1);
|
|
}}
|
|
//delay(200);
|
|
}}
|
|
avg_blueS3 = intensity_storage / temp_counter;
|
|
|
|
setOutput(0, GAIN_1, 1, 0);
|
|
setOutput(1, GAIN_1, 1, 0);
|
|
avg_greenS = (avg_greenS1 + avg_greenS2 + avg_greenS3) / 3;
|
|
avg_blueS = (avg_blueS1 + avg_blueS2 + avg_blueS3) / 3;
|
|
Serial.print("ok Green Intensity: "); Serial.println(avg_greenS);
|
|
Serial.print("ok Blue Intensity: "); Serial.println(avg_blueS);
|
|
Serial.println("#Sample Completed");
|
|
delay(300);
|
|
}}
|
|
|
|
|
|
|
|
""".format(led1dac=led1, led2dac=led2))
|
|
|
|
sketch += H("hello.h", """
|
|
void hello() {
|
|
Serial.println("HemoCube QC 1");
|
|
}
|
|
""")
|
|
|
|
"""
|
|
Compile sketch for Arduino Nano 33 BLE board.
|
|
The board you target must appear in the `arduino-cli board listall` command.
|
|
If you know the FQBN (Fully Qualified Board Name), you can use that too.
|
|
"""
|
|
if sketch.compile(board='arduino:avr:nano:cpu=atmega328old').is_successful:
|
|
# log_view.add_log("Log: \n\n" + sketch.output)
|
|
# log_view.add_log("Sketch stats: \n\n" + sketch.stats)
|
|
print('Log', sketch.output)
|
|
print('Sketch stats', sketch.stats)
|
|
else:
|
|
log_view.add_log("ERROR: \n\n" + sketch.output)
|
|
print('ERROR', sketch.output)
|
|
|
|
"""
|
|
You can specify the exact port
|
|
"""
|
|
# sketch.upload(port='/dev/cu.usbmodem14201')
|
|
|
|
sketch.upload(port=port)
|
|
|
|
# """
|
|
# Or even part of it.
|
|
# The library will look for the best match.
|
|
# """
|
|
# sketch.upload(port='ttyUSB')
|
|
# sketch.upload(port='/dev/cu.usbserial-1420') #/dev/cu.usbmodem
|
|
|
|
log_view.add_log("uploading... \n\n" + sketch.output)
|
|
print(sketch.output)
|
|
|
|
def showdialog():
|
|
msg = QMessageBox()
|
|
msg.setIcon(QMessageBox.Information)
|
|
|
|
msg.setText("HemoCube flashing")
|
|
msg.setInformativeText("HemoCube device will be flashed with new values")
|
|
msg.setWindowTitle("Flashing")
|
|
msg.setDetailedText("The LED dac values will be set. Wait for sometime.")
|
|
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
|
|
msg.buttonClicked.connect(msgbtn)
|
|
|
|
retval = msg.exec_()
|
|
print("value of pressed message box button:", retval)
|
|
|
|
def msgbtn(i):
|
|
print("Button pressed is:",i.text())
|
|
|
|
def get_port():
|
|
ports = []
|
|
if platform.system() == 'Darwin':
|
|
ports = list(filter(lambda x: "cu" in x, os.listdir("/dev")))
|
|
else:
|
|
import serial.tools.list_ports
|
|
# ports = ['COM%s' % (i + 1) for i in range(256)]
|
|
serial_ports = list(serial.tools.list_ports.comports())
|
|
for port, desc, hwid in sorted(serial_ports):
|
|
ports.append(port)
|
|
return ports
|
|
|
|
# 1. Import QApplication and all the required widgets
|
|
# from PyQt5.QtWidgets import QApplication, QLabel, QWidget
|
|
|
|
def addLabel(layout, text):
|
|
layout.addWidget(QLabel(text))
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
|
|
window = QWidget()
|
|
layout = QVBoxLayout(window)
|
|
|
|
# Create a label Widget and add it to the layout
|
|
labelLed1 = QLabel('Enter LED1 value (1500-3000)')
|
|
layout.addWidget(labelLed1)
|
|
|
|
line_edit_led1 = QLineEdit()
|
|
line_edit_led1.setFixedSize(QSize(150, 30))
|
|
layout.addWidget(line_edit_led1)
|
|
|
|
labelLed2 = QLabel('Enter LED2 value (1500-3000)')
|
|
left = 0
|
|
top = 25
|
|
right = 0
|
|
bottom = 0
|
|
labelLed2.setContentsMargins(left, top, right, bottom)
|
|
layout.addWidget(labelLed2)
|
|
|
|
line_edit_led2 = QLineEdit()
|
|
line_edit_led2.setFixedSize(QSize(150, 30))
|
|
layout.addWidget(line_edit_led2)
|
|
|
|
|
|
labelPort = QLabel('Select a port')
|
|
layout.addWidget(labelPort)
|
|
|
|
cb = QComboBox()
|
|
cb.setFixedSize(QSize(150, 30))
|
|
ports = get_port()
|
|
cb.addItem("Select")
|
|
for port in ports:
|
|
cb.addItem(port)
|
|
layout.addWidget(cb)
|
|
|
|
flash_port = ""
|
|
def selectionchange(i):
|
|
flash_port = cb.currentText()
|
|
print("flash_port", flash_port)
|
|
cb.currentIndexChanged.connect(selectionchange)
|
|
|
|
# Create a QPushButton object with a caption on it
|
|
qbtn = QPushButton('Flash')
|
|
qbtn.setFixedSize(QSize(150, 40))
|
|
|
|
# Add the QPushButton to the layout
|
|
layout.addWidget(qbtn)
|
|
|
|
w = LogView()
|
|
w.resize(640, 480)
|
|
# w.show()
|
|
# w.start_log("adb", ["logcat", "*:I"])
|
|
# w.start_log("arduino-cli", ["monitor", "-p /dev/cu.Bluetooth-Incoming-Port"])
|
|
# w.start_log("arduino-cli", ["lib", "install", "ADS1X15"])
|
|
w.handle_stdout()
|
|
layout.addWidget(w)
|
|
|
|
# Close the application when the button is pressed
|
|
# Here I am using slots & signals, which I will demonstrate later in this tutorial
|
|
# qbtn.clicked.connect(lambda:addLabel(layout, "Flashing..."))
|
|
qbtn.clicked.connect(lambda: flash(w, line_edit_led1.text(), line_edit_led2.text(), cb.currentText()))
|
|
|
|
window.setWindowTitle("HemoCube QC")
|
|
window.setGeometry(400, 400, 800, 600)
|
|
# helloMsg = QLabel("<h1>Hello, World!</h1>", parent=window)
|
|
# helloMsg.move(60, 15)
|
|
|
|
window.show()
|
|
|
|
# 5. Run your application's event loop
|
|
sys.exit(app.exec())
|