Skip to content

Commit

Permalink
data reading OK
Browse files Browse the repository at this point in the history
  • Loading branch information
MarreTeint authored Oct 14, 2022
1 parent 70a226f commit 3a6dd21
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 7 deletions.
39 changes: 39 additions & 0 deletions monochromateur.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_17">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/jSerialComm-2.9.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-controls:win:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:win:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-base:win:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:win:17.0.1" level="project" />
<orderEntry type="library" name="Maven: org.controlsfx:controlsfx:11.1.0" level="project" />
<orderEntry type="library" name="Maven: org.kordamp.bootstrapfx:bootstrapfx-core:0.4.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.8.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.8.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.8.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.8.1" level="project" />
</component>
</module>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.example.monchromateur/com.monchromateur.Launcher</mainClass>
<mainClass>com.example.monchromateur/com.monochromateur.Launcher</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.monchromateur.Launcher
Main-Class: com.monochromateur.Launcher

163 changes: 163 additions & 0 deletions src/main/java/com/monochromateur/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.monochromateur;

import javafx.fxml.FXML;
import javafx.scene.chart.*;
import com.fazecast.jSerialComm.*;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;


import java.util.ArrayList;
import java.util.List;

import static java.lang.Integer.parseInt;


public class Controller {


public Button button;
public ComboBox COMPort;
public Slider slide;
public Label slideY;
public Label slideX;
public HBox displayData;
@FXML
private LineChart result;
@FXML
private ProgressIndicator progress;
private String port;

private final int[] Y = new int[401];

@FXML
protected void plotDatas() {
if(port!=null) {
button.setVisible(false);
progress.setVisible(true);
new Thread(() -> {
SerialPort comPort = SerialPort.getCommPort(port);
comPort.setComPortParameters(2000000, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
comPort.openPort();
//Arduino restart when openPort
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

//Starting message to arduino
byte[] WriteByte = new byte[1];
WriteByte[0] = 65; //send A
if (comPort.writeBytes(WriteByte, 1) == 1) {
System.out.println("Start command sent on " + port);
} else {

System.out.println("Error sending start command");
progress.setVisible(false);
button.setVisible(true);
comPort.closePort();
//TODO add an error message on the UI
return;
}


NumberAxis xAxis = new NumberAxis();
xAxis.setLabel("Years");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("No.of schools");
XYChart.Series series = new XYChart.Series();
series.setName("Absorbance en fonction de la longueur d'onde");

//read data from arduino
//int Y[] = new int[401];
int maxat = 0;
for (int i = 0; i < 401; i++) {
StringBuilder data = new StringBuilder();
byte[] readBuffer = new byte[1];
comPort.readBytes(readBuffer, readBuffer.length);
while (readBuffer[0] != 10 && data.length() < 3 && data.length() >= 0) {
if (readBuffer[0] == 0) {
} else if (readBuffer[0] >= 48 && readBuffer[0] <= 57) {
String d = String.valueOf((char) readBuffer[0]);
data.append(d);
}
comPort.readBytes(readBuffer, readBuffer.length);
}
if (data.length() > 0) {
Y[i] = parseInt(data.toString());
if (Y[i] > Y[maxat]) {
maxat = i;
}
System.out.println(i + " Data received: " + Y[i]);
int finalI = i;
javafx.application.Platform.runLater(() -> {
progress.setProgress((double) finalI / 400);
});
}
}

for (int i = 400; i <= 800; i += 1) {
series.getData().add(new XYChart.Data(i, Y[i - 400]));
}

//send series to main thread
int finalMaxat = maxat;
javafx.application.Platform.runLater(() -> {
result.getData().clear();
result.getData().add(series);
result.setCreateSymbols(false);
progress.setVisible(false);
result.setTitle("Résultat");
button.setText("Refaire une mesure");
button.setVisible(true);
progress.setProgress(0);
slide.setVisible(true);
displayData.setVisible(true);
sliderMoved();
});
//close serial port
comPort.closePort();
}).start();
}
else {
System.out.println("No COM port selected");
}
}

@FXML
protected void sliderMoved() {
new Thread(() -> {
XYChart.Series series = new XYChart.Series();
series.getData().add(new XYChart.Data((int)slide.getValue(), 0));
series.getData().add(new XYChart.Data((int)slide.getValue(), 100));
series.setName("Curseur");
javafx.application.Platform.runLater(() -> {
result.setAnimated(false);
if(result.getData().size()>1) {
result.getData().remove(1);
}
result.getData().add(series);
slideX.setText(String.valueOf((int)slide.getValue()));
slideY.setText(String.valueOf(Y[(int)slide.getValue()-400]));

});
}).start();
}

@FXML
protected void initialize() {
List<String> ports = new ArrayList<>();
for (SerialPort port : SerialPort.getCommPorts()) {
ports.add(port.getSystemPortName());
}
COMPort.getItems().addAll(ports);
COMPort.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> {
port = newValue.toString();
System.out.println(port);
});
}
}


23 changes: 23 additions & 0 deletions src/main/java/com/monochromateur/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.monochromateur;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Launcher extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Launcher.class.getResource("View.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1600, 900);
stage.setTitle("Monochromateur APP4 - 2022/2023");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/monochromateur/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.monochromateur;

public class Main {
public static void main(String[] args) {
Launcher.main(args);
}
}
6 changes: 3 additions & 3 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module com.example.monchromateur {
module com.example.monochromateur {
requires javafx.controls;
requires javafx.fxml;

requires org.controlsfx.controls;
requires org.kordamp.bootstrapfx.core;
requires com.fazecast.jSerialComm;

opens com.monchromateur to javafx.fxml;
exports com.monchromateur;
opens com.monochromateur to javafx.fxml;
exports com.monochromateur;
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.monchromateur.Main
Main-Class: com.monochromateur.Main

39 changes: 39 additions & 0 deletions src/main/resources/com/monochromateur/View.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox alignment="CENTER" prefHeight="602.0" prefWidth="701.0" spacing="20.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.monochromateur.Controller">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<LineChart fx:id="result" prefHeight="529.0" prefWidth="719.0">
<xAxis>
<NumberAxis autoRanging="false" label="Longeur d'onde" lowerBound="400" side="BOTTOM" tickLength="10.0" tickUnit="10.0" upperBound="800" />
</xAxis>
<yAxis>
<NumberAxis autoRanging="false" label="Absorbance" side="LEFT" />
</yAxis>
</LineChart>
<HBox alignment="CENTER" prefHeight="64.0" prefWidth="661.0">
<children>
<ComboBox fx:id="COMPort" prefHeight="25.0" prefWidth="101.0" promptText="COM Port" />
<Separator orientation="VERTICAL" prefHeight="25.0" prefWidth="28.0" />
<Button fx:id="button" onMouseClicked="#plotDatas" text="Faire une mesure" />
<ProgressIndicator fx:id="progress" prefHeight="25.0" prefWidth="64.0" progress="0.0" visible="false" />
</children>
</HBox>
<Slider fx:id="slide" majorTickUnit="1" max="800.0" min="400.0" onMouseDragged="#sliderMoved" showTickLabels="true" showTickMarks="true" snapToTicks="true" visible="false" />
<HBox fx:id="displayData" alignment="CENTER" prefHeight="43.0" prefWidth="661.0" visible="false">
<children>
<Label text="Longeur d'onde : " />
<Label fx:id="slideX" text="" />
<Separator orientation="VERTICAL" prefHeight="17.0" prefWidth="23.0" />
<Label text=" Absorbance : " />
<Label fx:id="slideY" text="" />

</children>
</HBox>
</VBox>
2 changes: 1 addition & 1 deletion target/classes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.monchromateur.Main
Main-Class: com.monochromateur.Main

Binary file not shown.
Binary file added target/classes/com/monochromateur/Launcher.class
Binary file not shown.
Binary file added target/classes/com/monochromateur/Main.class
Binary file not shown.
39 changes: 39 additions & 0 deletions target/classes/com/monochromateur/View.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox alignment="CENTER" prefHeight="602.0" prefWidth="701.0" spacing="20.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.monochromateur.Controller">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<LineChart fx:id="result" prefHeight="529.0" prefWidth="719.0">
<xAxis>
<NumberAxis autoRanging="false" label="Longeur d'onde" lowerBound="400" side="BOTTOM" tickLength="10.0" tickUnit="10.0" upperBound="800" />
</xAxis>
<yAxis>
<NumberAxis autoRanging="false" label="Absorbance" side="LEFT" />
</yAxis>
</LineChart>
<HBox alignment="CENTER" prefHeight="64.0" prefWidth="661.0">
<children>
<ComboBox fx:id="COMPort" prefHeight="25.0" prefWidth="101.0" promptText="COM Port" />
<Separator orientation="VERTICAL" prefHeight="25.0" prefWidth="28.0" />
<Button fx:id="button" onMouseClicked="#plotDatas" text="Faire une mesure" />
<ProgressIndicator fx:id="progress" prefHeight="25.0" prefWidth="64.0" progress="0.0" visible="false" />
</children>
</HBox>
<Slider fx:id="slide" majorTickUnit="1" max="800.0" min="400.0" onMouseDragged="#sliderMoved" showTickLabels="true" showTickMarks="true" snapToTicks="true" visible="false" />
<HBox fx:id="displayData" alignment="CENTER" prefHeight="43.0" prefWidth="661.0" visible="false">
<children>
<Label text="Longeur d'onde : " />
<Label fx:id="slideX" text="" />
<Separator orientation="VERTICAL" prefHeight="17.0" prefWidth="23.0" />
<Label text=" Absorbance : " />
<Label fx:id="slideY" text="" />

</children>
</HBox>
</VBox>
Binary file modified target/classes/module-info.class
Binary file not shown.

0 comments on commit 3a6dd21

Please sign in to comment.