Restructure into src/ layout under pygui package
Move all application source under src/pygui/ and rewire imports, build config, and QML module path to match. - Relocate backend/, serialcomm/, and the ISC QML module into src/pygui/; convert main.py into pygui/__main__.py with a main() entry point (run via `python -m pygui` or the new `isc` script) - Rewrite absolute imports: backend.* -> pygui.backend.*, serialcomm.* -> pygui.serialcomm.* (source + tests) - Move app icons (isc.ico/icns) into packaging/ - Update README and ISC.qmlproject to the new paths
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
import ISC
|
||||
|
||||
// ===== Home Workspace Shell =====
|
||||
Rectangle {
|
||||
id: root
|
||||
anchors.fill: parent
|
||||
color: Theme.pageBackground
|
||||
clip: true
|
||||
border.color: Theme.outerFrameBorder
|
||||
border.width: Theme.borderStrong
|
||||
|
||||
// ===== Navigation Model =====
|
||||
// Primary navigation shown in the left rail.
|
||||
property var sideActions: ["DETECT WAFER", "READ MEMORY", "OPEN CSV IN EXCEL", "ERASE MEMORY", "IMPORT DATA", "STORED DATA"]
|
||||
|
||||
// ===== Footer Tab Model =====
|
||||
// Footer tabs drive the active workspace section.
|
||||
property var bottomTabs: ["Status", "Graph", "Data", "Wafer Map", "Compare", "Split", "Settings", "About"]
|
||||
|
||||
// ===== View State =====
|
||||
property int selectedTabIndex: 0
|
||||
property int selectedSideActionIndex: -1 // nothing active on startup
|
||||
|
||||
// ===== Main Two-Column Layout =====
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
spacing: 0
|
||||
|
||||
// ===== Left Action Rail =====
|
||||
// Left control rail.
|
||||
Rectangle {
|
||||
id: sideRail
|
||||
Layout.preferredWidth: Theme.sideRailWidth
|
||||
Layout.fillHeight: true
|
||||
color: Theme.sideRailBackground
|
||||
border.color: Theme.workspaceBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
readonly property int actionCount: root.sideActions.length
|
||||
readonly property real computedButtonHeight: Math.min(Theme.sideButtonHeight, (height - (Theme.panelPadding * 2) - (Theme.sideRailSpacing * Math.max(0, actionCount - 1))) / Math.max(1, actionCount))
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.panelPadding
|
||||
spacing: Theme.sideRailSpacing
|
||||
|
||||
Repeater {
|
||||
model: root.sideActions
|
||||
|
||||
Button {
|
||||
id: control
|
||||
text: modelData
|
||||
property bool isActive: index === root.selectedSideActionIndex
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: Math.max(Theme.sideButtonMinHeight, sideRail.computedButtonHeight)
|
||||
hoverEnabled: true
|
||||
onClicked: {
|
||||
root.selectedSideActionIndex = index
|
||||
root.selectedTabIndex = 0 // always jump to Status tab
|
||||
if (index === 0) deviceController.detectWafer()
|
||||
else if (index === 1) deviceController.readMemoryAsync()
|
||||
else if (index === 2) deviceController.openCsvFile()
|
||||
}
|
||||
|
||||
background: Rectangle {
|
||||
color: {
|
||||
if (control.down) {
|
||||
return Theme.buttonPressed;
|
||||
}
|
||||
if (control.isActive) {
|
||||
return Theme.sideActiveBackground;
|
||||
}
|
||||
return "transparent";
|
||||
}
|
||||
border.color: control.isActive ? Theme.cardBorder : "transparent"
|
||||
border.width: 1
|
||||
radius: Theme.radiusSm
|
||||
|
||||
Rectangle {
|
||||
anchors.left: parent.left
|
||||
anchors.top: parent.top
|
||||
anchors.bottom: parent.bottom
|
||||
width: 3
|
||||
visible: control.isActive
|
||||
color: Theme.primaryAccent
|
||||
radius: Theme.radiusXs
|
||||
}
|
||||
}
|
||||
|
||||
contentItem: Text {
|
||||
text: control.text
|
||||
color: control.isActive ? Theme.headingColor : Theme.bodyColor
|
||||
font.bold: control.isActive
|
||||
font.pixelSize: 18
|
||||
wrapMode: Text.WordWrap
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Main workspace and footer navigation.
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
color: Theme.workspaceBackground
|
||||
border.color: Theme.workspaceBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.mainAreaPadding
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
// ===== Active Tab Content Area =====
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
Layout.minimumHeight: 220
|
||||
color: Theme.responseBackground
|
||||
border.color: Theme.responseBorder
|
||||
border.width: 1
|
||||
radius: Theme.radiusMd
|
||||
|
||||
StackLayout {
|
||||
anchors.fill: parent
|
||||
currentIndex: root.selectedTabIndex
|
||||
|
||||
// ===== Tab Content Routing =====
|
||||
Repeater {
|
||||
model: root.bottomTabs
|
||||
|
||||
Item {
|
||||
property string tabName: modelData
|
||||
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
active: parent.tabName === "Settings"
|
||||
source: parent.tabName === "Settings" ? "Tabs/SettingsTab.qml" : ""
|
||||
}
|
||||
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
active: parent.tabName === "Status"
|
||||
source: parent.tabName === "Status" ? "Tabs/StatusTab.qml" : ""
|
||||
}
|
||||
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
active: parent.tabName === "Data"
|
||||
source: parent.tabName === "Data" ? "Tabs/DataTab.qml" : ""
|
||||
}
|
||||
|
||||
Label {
|
||||
anchors.centerIn: parent
|
||||
visible: parent.tabName !== "Settings" && parent.tabName !== "Status" && parent.tabName !== "Data"
|
||||
text: parent.tabName + " content"
|
||||
color: Theme.bodyColor
|
||||
font.pixelSize: 20
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep the tab strip evenly distributed across the footer.
|
||||
// ===== Footer Tab Strip =====
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: Theme.tabBarHeight + (Theme.tabBarPadding * 2)
|
||||
color: Theme.tabBarBackground
|
||||
border.color: Theme.workspaceBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.tabBarPadding
|
||||
spacing: Theme.tabSpacing
|
||||
|
||||
Repeater {
|
||||
model: root.bottomTabs
|
||||
|
||||
Button {
|
||||
id: botTabBtn
|
||||
property bool isActive: index === root.selectedTabIndex
|
||||
|
||||
text: modelData
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: Theme.tabBarHeight
|
||||
Layout.minimumWidth: Theme.tabButtonMinWidth
|
||||
onClicked: root.selectedTabIndex = index
|
||||
hoverEnabled: true
|
||||
|
||||
background: Rectangle {
|
||||
color: botTabBtn.isActive ? Theme.tabActiveBackground : (botTabBtn.hovered ? Theme.tabHoverBackground : Theme.tabBackground)
|
||||
border.color: Theme.tabBorder
|
||||
border.width: 1
|
||||
radius: Theme.tabRadius
|
||||
}
|
||||
|
||||
contentItem: Text {
|
||||
text: botTabBtn.text
|
||||
color: botTabBtn.isActive ? Theme.tabActiveText : Theme.tabText
|
||||
font.pixelSize: Theme.tabFontSize
|
||||
font.bold: botTabBtn.isActive
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import QtQuick.Window
|
||||
import ISC
|
||||
|
||||
// ===== App Window Shell =====
|
||||
Window {
|
||||
// ===== Window Dimensions =====
|
||||
width: 1400
|
||||
height: 820
|
||||
minimumWidth: 1100
|
||||
minimumHeight: 700
|
||||
visible: true
|
||||
title: qsTr("ISenseCloud")
|
||||
|
||||
// Keep the window file very small.
|
||||
// Styling and layout now live in HomePage.qml.
|
||||
// ===== Root Page Mount =====
|
||||
HomePage {
|
||||
anchors.fill: parent
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
import ISC
|
||||
|
||||
// ===== Data Tab =====
|
||||
// Displays parsed temperature data in a scrollable table.
|
||||
// Column 0 = Row index, remaining columns = Sensor1, Sensor2, ...
|
||||
Item {
|
||||
id: root
|
||||
anchors.fill: parent
|
||||
|
||||
readonly property int rowCount: deviceController.dataRowCount
|
||||
|
||||
// Re-layout columns whenever the model is reset
|
||||
Connections {
|
||||
target: deviceController
|
||||
function onParsedDataReady(result) {
|
||||
if (result && result.success) {
|
||||
dataTable.forceLayout()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Empty State =====
|
||||
Column {
|
||||
anchors.centerIn: parent
|
||||
spacing: 16
|
||||
visible: root.rowCount === 0
|
||||
|
||||
Label {
|
||||
text: "No Data"
|
||||
font.pixelSize: 24
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
|
||||
Label {
|
||||
text: "Read and parse wafer data to see temperature readings here."
|
||||
font.pixelSize: 14
|
||||
color: Theme.bodyColor
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
wrapMode: Text.WordWrap
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Data Table =====
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.panelPadding
|
||||
spacing: Theme.rightPaneGap
|
||||
visible: root.rowCount > 0
|
||||
|
||||
// --- Header row ---
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
Label {
|
||||
text: "Temperature Data"
|
||||
font.pixelSize: 18
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
Label {
|
||||
text: deviceController.dataRowCount + " rows × " +
|
||||
deviceController.dataColCount + " sensors"
|
||||
font.pixelSize: 13
|
||||
color: Theme.bodyColor
|
||||
}
|
||||
}
|
||||
|
||||
// --- Table container ---
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
color: Theme.panelBackground
|
||||
border.color: Theme.cardBorder
|
||||
border.width: Theme.borderThin
|
||||
radius: Theme.radiusSm
|
||||
clip: true
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: 1
|
||||
spacing: 0
|
||||
|
||||
// Column header strip
|
||||
HorizontalHeaderView {
|
||||
id: headerView
|
||||
Layout.fillWidth: true
|
||||
syncView: dataTable
|
||||
clip: true
|
||||
|
||||
delegate: Rectangle {
|
||||
implicitHeight: 28
|
||||
color: Theme.cardBackground
|
||||
border.color: Theme.cardBorder
|
||||
border.width: 1
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
// 'display' is the Qt.DisplayRole value from headerData()
|
||||
text: display !== undefined ? display : ""
|
||||
color: Theme.headingColor
|
||||
font.pixelSize: 11
|
||||
font.bold: true
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Data rows
|
||||
TableView {
|
||||
id: dataTable
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
model: deviceController.dataModel
|
||||
clip: true
|
||||
reuseItems: true
|
||||
|
||||
columnWidthProvider: function(col) {
|
||||
if (col === 0) return 52 // row index column
|
||||
const sensorCols = Math.max(1, dataTable.columns - 1)
|
||||
const available = dataTable.width - 52
|
||||
return Math.max(52, Math.min(90, available / sensorCols))
|
||||
}
|
||||
|
||||
rowHeightProvider: function() { return 24 }
|
||||
|
||||
delegate: Rectangle {
|
||||
color: row % 2 === 0 ? Theme.panelBackground : Theme.cardBackground
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: display !== undefined ? display : ""
|
||||
color: Theme.bodyColor
|
||||
font.pixelSize: 11
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
}
|
||||
|
||||
ScrollBar.horizontal: ScrollBar { policy: ScrollBar.AsNeeded }
|
||||
ScrollBar.vertical: ScrollBar { policy: ScrollBar.AsNeeded }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,603 @@
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
import Qt.labs.qmlmodels
|
||||
import ISC
|
||||
|
||||
Dialog {
|
||||
id: root
|
||||
title: "Select File"
|
||||
width: 980
|
||||
height: 640
|
||||
modal: true
|
||||
padding: 16
|
||||
topPadding: 0
|
||||
parent: Overlay.overlay
|
||||
x: Math.round(((parent ? parent.width : width) - width) / 2)
|
||||
y: Math.round(((parent ? parent.height : height) - height) / 2)
|
||||
|
||||
signal fileChosen(string path)
|
||||
|
||||
// ===== Dialog State =====
|
||||
property string selectedPath: ""
|
||||
property var tableModel: []
|
||||
property int selectedRow: -1
|
||||
property int metadataEditRow: -1
|
||||
readonly property string masterTypeFieldText: {
|
||||
if (metadataEditRow < 0) return "";
|
||||
const rec = tableModel[metadataEditRow];
|
||||
if (!rec) return "";
|
||||
return rec.masterType ?? "";
|
||||
}
|
||||
|
||||
function openMetadataEditor(row) {
|
||||
metadataEditRow = row;
|
||||
const rec = tableModel[row];
|
||||
if (!rec) {
|
||||
metadataEditDialog.close();
|
||||
return;
|
||||
}
|
||||
waferField.text = rec.wafer ?? "";
|
||||
dateField.text = rec.date ?? "";
|
||||
chamberField.text = rec.chamber ?? "";
|
||||
if (!chamberField.text && settingsModel.chamberId)
|
||||
chamberField.text = settingsModel.chamberId;
|
||||
notesField.text = rec.notes ?? "";
|
||||
filePathLabel.text = rec.fileName ?? "";
|
||||
metadataEditDialog.open();
|
||||
}
|
||||
|
||||
function applyMetadataFromEditor() {
|
||||
if (metadataEditRow < 0)
|
||||
return;
|
||||
const rec = tableModel[metadataEditRow];
|
||||
if (!rec) {
|
||||
metadataEditDialog.close();
|
||||
return;
|
||||
}
|
||||
file_browser.saveMetadata(rec.fileName ?? "", waferField.text, dateField.text, chamberField.text, notesField.text, false, "");
|
||||
metadataEditDialog.close();
|
||||
metadataEditRow = -1;
|
||||
}
|
||||
|
||||
readonly property var headerTitles: ["Master", "Wafer", "Date", "Chamber", "Notes", "File", "Edit"]
|
||||
// Build a path→family reverse map from settingsModel.masters.
|
||||
// settingsModel is a QML context property; accessing it here makes the
|
||||
// binding reactive — any mastersChanged signal re-evaluates normalizedRows.
|
||||
function masterFamilyForPath(filePath) {
|
||||
const m = settingsModel.masters;
|
||||
for (var fam in m) {
|
||||
if (m[fam] && m[fam] === filePath)
|
||||
return fam;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
readonly property var normalizedRows: {
|
||||
// Touch settingsModel.masters so QML tracks this dependency.
|
||||
const _ = settingsModel.masters;
|
||||
return (tableModel || []).map(function(row) {
|
||||
row = row || {};
|
||||
// settingsModel is the authoritative source; fall back to sidecar masterType.
|
||||
const masterType = root.masterFamilyForPath(row.fileName ?? "") || row.masterType || "";
|
||||
return {
|
||||
masterType: masterType,
|
||||
wafer: row.wafer ?? "",
|
||||
date: row.date ?? "",
|
||||
chamber: row.chamber ?? "",
|
||||
edit: "",
|
||||
notes: row.notes ?? "",
|
||||
fileName: row.fileName ?? "",
|
||||
highlight: row.highlight ?? false
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// ===== Reusable Controls =====
|
||||
component DialogActionButton: Button {
|
||||
id: dialogButton
|
||||
hoverEnabled: true
|
||||
implicitHeight: 36
|
||||
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusSm
|
||||
color: dialogButton.down ? Theme.buttonNeutralPressed : dialogButton.hovered ? Theme.buttonNeutralHover : Theme.buttonNeutralBackground
|
||||
border.width: Theme.borderThin
|
||||
border.color: Theme.fieldBorder
|
||||
}
|
||||
|
||||
contentItem: Text {
|
||||
text: dialogButton.text
|
||||
color: Theme.buttonNeutralText
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
elide: Text.ElideRight
|
||||
font.bold: true
|
||||
font.pixelSize: 13
|
||||
}
|
||||
}
|
||||
|
||||
component DialogTextInput: TextField {
|
||||
id: dialogTextInput
|
||||
color: Theme.fieldText
|
||||
placeholderTextColor: Theme.fieldPlaceholder
|
||||
selectedTextColor: Theme.fieldBackground
|
||||
selectionColor: Theme.fieldText
|
||||
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusXs
|
||||
color: Theme.fieldBackground
|
||||
border.width: dialogTextInput.activeFocus ? Theme.borderStrong : Theme.borderThin
|
||||
border.color: dialogTextInput.activeFocus ? Theme.fieldBorderFocus : Theme.fieldBorder
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Dialog Chrome =====
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusLg
|
||||
color: Theme.panelBackground
|
||||
border.color: Theme.outerFrameBorder
|
||||
border.width: Theme.borderStrong
|
||||
}
|
||||
|
||||
// Custom header — replaces the native dark title bar
|
||||
header: Rectangle {
|
||||
width: root.width
|
||||
height: 48
|
||||
radius: Theme.radiusLg
|
||||
color: Theme.subtleSectionBackground
|
||||
border.color: Theme.innerFrameBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
// Square off the bottom corners
|
||||
Rectangle {
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
anchors.bottom: parent.bottom
|
||||
height: parent.radius
|
||||
color: parent.color
|
||||
}
|
||||
|
||||
Label {
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 16
|
||||
text: root.title
|
||||
font.pixelSize: 15
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
}
|
||||
|
||||
// Close button
|
||||
DialogActionButton {
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 12
|
||||
text: "✕"
|
||||
implicitWidth: 32
|
||||
implicitHeight: 32
|
||||
onClicked: root.close()
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Content =====
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: 10
|
||||
|
||||
// ===== Browser Toolbar =====
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.topMargin: 12
|
||||
spacing: 8
|
||||
|
||||
DialogActionButton {
|
||||
text: "Choose Folder"
|
||||
Layout.preferredWidth: 140
|
||||
onClicked: file_browser.chooseDirectory()
|
||||
}
|
||||
|
||||
DialogActionButton {
|
||||
text: "Refresh"
|
||||
Layout.preferredWidth: 100
|
||||
onClicked: file_browser.refreshFiles()
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 36
|
||||
Layout.minimumWidth: 80
|
||||
clip: true
|
||||
radius: Theme.radiusXs
|
||||
color: Theme.fieldBackground
|
||||
border.color: Theme.fieldBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
Text {
|
||||
anchors.fill: parent
|
||||
anchors.leftMargin: 10
|
||||
anchors.rightMargin: 10
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
text: file_browser.currentDirectory
|
||||
color: Theme.fieldText
|
||||
elide: Text.ElideMiddle
|
||||
font.pixelSize: 13
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== File Table =====
|
||||
HorizontalHeaderView {
|
||||
id: headerView
|
||||
syncView: tableView
|
||||
Layout.fillWidth: true
|
||||
|
||||
delegate: Rectangle {
|
||||
implicitHeight: 34
|
||||
color: Theme.subtleSectionBackground
|
||||
border.color: Theme.innerFrameBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: root.headerTitles[index] ?? ""
|
||||
font.bold: true
|
||||
font.pixelSize: 12
|
||||
color: Theme.headingColor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TableView {
|
||||
id: tableView
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
clip: true
|
||||
columnSpacing: 0
|
||||
rowSpacing: 0
|
||||
flickableDirection: Flickable.VerticalFlick
|
||||
|
||||
columnWidthProvider: function (col) {
|
||||
// [master, wafer, date, chamber, notes, file (variable), edit]
|
||||
const fixed = {
|
||||
0: 48,
|
||||
1: 90,
|
||||
2: 170,
|
||||
3: 120,
|
||||
4: 180,
|
||||
6: 56
|
||||
};
|
||||
if (col in fixed)
|
||||
return fixed[col];
|
||||
// col 5 (File) takes remaining width
|
||||
const w = Math.max(400, tableView.width);
|
||||
const used = 48 + 90 + 170 + 120 + 180 + 56;
|
||||
return Math.max(120, w - used);
|
||||
}
|
||||
|
||||
model: TableModel {
|
||||
TableModelColumn {
|
||||
display: "masterType"
|
||||
}
|
||||
TableModelColumn {
|
||||
display: "wafer"
|
||||
}
|
||||
TableModelColumn {
|
||||
display: "date"
|
||||
}
|
||||
TableModelColumn {
|
||||
display: "chamber"
|
||||
}
|
||||
TableModelColumn {
|
||||
display: "notes"
|
||||
}
|
||||
TableModelColumn {
|
||||
display: "fileName"
|
||||
}
|
||||
TableModelColumn {
|
||||
display: "edit"
|
||||
}
|
||||
rows: root.normalizedRows
|
||||
}
|
||||
|
||||
delegate: Rectangle {
|
||||
required property bool current
|
||||
required property int row
|
||||
required property int column
|
||||
|
||||
implicitHeight: 40
|
||||
border.color: Theme.softBorder
|
||||
border.width: Theme.borderThin
|
||||
color: {
|
||||
if (root.selectedRow === row)
|
||||
return Theme.sideActiveBackground;
|
||||
if (root.tableModel[row]?.highlight)
|
||||
return Theme.subtleSectionBackground;
|
||||
return row % 2 === 0 ? Theme.cardBackground : Theme.tone100;
|
||||
}
|
||||
|
||||
// Master type indicator (col 0)
|
||||
// Reads normalizedRows (which merges settingsModel.masters + sidecar).
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
active: column === 0
|
||||
sourceComponent: Text {
|
||||
anchors.fill: parent
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
text: {
|
||||
const mt = root.normalizedRows[row]?.masterType ?? "";
|
||||
return mt || "—";
|
||||
}
|
||||
font.pixelSize: 14
|
||||
font.bold: true
|
||||
color: {
|
||||
const mt = root.normalizedRows[row]?.masterType ?? "";
|
||||
return mt ? Theme.primaryAccent : Theme.fieldPlaceholder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Edit button (col 6)
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
anchors.margins: 4
|
||||
active: column === 6
|
||||
sourceComponent: Button {
|
||||
hoverEnabled: true
|
||||
text: "Edit…"
|
||||
font.pixelSize: 12
|
||||
onClicked: root.openMetadataEditor(row)
|
||||
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusXs
|
||||
color: parent.down ? Theme.buttonNeutralPressed : parent.hovered ? Theme.buttonNeutralHover : Theme.buttonNeutralBackground
|
||||
border.width: Theme.borderThin
|
||||
border.color: Theme.fieldBorder
|
||||
}
|
||||
|
||||
contentItem: Text {
|
||||
text: parent.text
|
||||
color: Theme.buttonNeutralText
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
font.pixelSize: 12
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Text columns: Wafer (1), Date (2), File (5)
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
active: column === 1 || column === 2 || column === 5
|
||||
sourceComponent: Text {
|
||||
anchors.fill: parent
|
||||
anchors.leftMargin: 8
|
||||
anchors.rightMargin: 8
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
text: {
|
||||
const record = root.tableModel[row] || {};
|
||||
if (column === 1)
|
||||
return record.wafer ?? "";
|
||||
if (column === 2)
|
||||
return record.date ?? "";
|
||||
if (column === 5) {
|
||||
const p = record.fileName ?? "";
|
||||
return p.split("/").pop();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
elide: Text.ElideRight
|
||||
font.pixelSize: 13
|
||||
color: Theme.bodyColor
|
||||
}
|
||||
}
|
||||
|
||||
// Text columns: Chamber (3), Notes (4)
|
||||
Loader {
|
||||
anchors.fill: parent
|
||||
active: column === 3 || column === 4
|
||||
sourceComponent: Text {
|
||||
anchors.fill: parent
|
||||
anchors.leftMargin: 8
|
||||
anchors.rightMargin: 8
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
text: {
|
||||
const record = root.tableModel[row] || {};
|
||||
if (column === 3)
|
||||
return record.chamber ?? "";
|
||||
return record.notes ?? "";
|
||||
}
|
||||
elide: Text.ElideRight
|
||||
font.pixelSize: 13
|
||||
color: Theme.bodyColor
|
||||
}
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
enabled: column !== 6
|
||||
onClicked: {
|
||||
root.selectedRow = row;
|
||||
root.selectedPath = root.tableModel[row]?.fileName ?? "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Bottom Bar =====
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 8
|
||||
|
||||
DialogActionButton {
|
||||
text: "OK"
|
||||
Layout.preferredWidth: 90
|
||||
onClicked: {
|
||||
root.fileChosen(root.selectedPath);
|
||||
root.close();
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 32
|
||||
Layout.minimumWidth: 120
|
||||
clip: true
|
||||
radius: Theme.radiusXs
|
||||
color: Theme.fieldBackground
|
||||
border.color: Theme.fieldBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
Text {
|
||||
anchors.fill: parent
|
||||
anchors.leftMargin: 10
|
||||
anchors.rightMargin: 10
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
text: root.selectedPath
|
||||
elide: Text.ElideLeft
|
||||
font.pixelSize: 13
|
||||
color: root.selectedPath ? Theme.fieldText : Theme.fieldPlaceholder
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Metadata Editor Dialog =====
|
||||
Dialog {
|
||||
id: metadataEditDialog
|
||||
parent: Overlay.overlay
|
||||
modal: true
|
||||
title: qsTr("Edit Metadata")
|
||||
width: 520
|
||||
height: 520
|
||||
padding: 16
|
||||
topPadding: 0
|
||||
x: Math.round(((parent ? parent.width : width) - width) / 2)
|
||||
y: Math.round(((parent ? parent.height : height) - height) / 2)
|
||||
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusLg
|
||||
color: Theme.panelBackground
|
||||
border.color: Theme.outerFrameBorder
|
||||
border.width: Theme.borderStrong
|
||||
}
|
||||
|
||||
header: Rectangle {
|
||||
width: metadataEditDialog.width
|
||||
height: 48
|
||||
radius: Theme.radiusLg
|
||||
color: Theme.subtleSectionBackground
|
||||
border.color: Theme.innerFrameBorder
|
||||
border.width: Theme.borderThin
|
||||
|
||||
Rectangle {
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
anchors.bottom: parent.bottom
|
||||
height: parent.radius
|
||||
color: parent.color
|
||||
}
|
||||
|
||||
Label {
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 16
|
||||
text: metadataEditDialog.title
|
||||
font.pixelSize: 15
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
}
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: Theme.settingsGridSpacing
|
||||
|
||||
Label {
|
||||
text: qsTr("File")
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
Layout.topMargin: 8
|
||||
}
|
||||
|
||||
Text {
|
||||
id: filePathLabel
|
||||
Layout.fillWidth: true
|
||||
Layout.maximumHeight: 56
|
||||
Layout.bottomMargin: 16
|
||||
wrapMode: Text.WrapAnywhere
|
||||
maximumLineCount: 3
|
||||
elide: Text.ElideRight
|
||||
color: Theme.bodyColor
|
||||
font.pixelSize: 12
|
||||
}
|
||||
|
||||
Label {
|
||||
text: qsTr("Wafer")
|
||||
color: Theme.headingColor
|
||||
}
|
||||
DialogTextInput {
|
||||
id: waferField
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 36
|
||||
}
|
||||
|
||||
Label {
|
||||
text: qsTr("Date")
|
||||
color: Theme.headingColor
|
||||
}
|
||||
DialogTextInput {
|
||||
id: dateField
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 36
|
||||
}
|
||||
|
||||
Label {
|
||||
text: qsTr("Chamber")
|
||||
color: Theme.headingColor
|
||||
}
|
||||
DialogTextInput {
|
||||
id: chamberField
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 36
|
||||
}
|
||||
|
||||
Label {
|
||||
text: qsTr("Notes")
|
||||
color: Theme.headingColor
|
||||
}
|
||||
DialogTextInput {
|
||||
id: notesField
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 36
|
||||
}
|
||||
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.topMargin: 16
|
||||
Layout.bottomMargin: 16
|
||||
spacing: 8
|
||||
|
||||
Item {
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
|
||||
DialogActionButton {
|
||||
text: qsTr("Cancel")
|
||||
Layout.preferredWidth: 90
|
||||
onClicked: {
|
||||
root.metadataEditRow = -1;
|
||||
metadataEditDialog.close();
|
||||
}
|
||||
}
|
||||
|
||||
DialogActionButton {
|
||||
text: qsTr("Save")
|
||||
Layout.preferredWidth: 90
|
||||
onClicked: root.applyMetadataFromEditor()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Dialogs
|
||||
import ISC
|
||||
|
||||
Item {
|
||||
id: root
|
||||
anchors.fill: parent
|
||||
|
||||
// ===== CSV metadata dialog (must not live inside RowLayout: implicit size breaks the tab) =====
|
||||
SelectFileDialog {
|
||||
id: selectFileDialog
|
||||
tableModel: file_browser.files
|
||||
onFileChosen: function (path) {
|
||||
root.handleCsvSelected(path);
|
||||
}
|
||||
}
|
||||
|
||||
function handleEditCsvMetadata() {
|
||||
file_browser.refreshFiles();
|
||||
selectFileDialog.open();
|
||||
}
|
||||
|
||||
function handleCsvSelected(filePath) {
|
||||
// Selection confirmed — metadata editing is handled inline inside SelectFileDialog.
|
||||
}
|
||||
|
||||
// ===== Settings Data Helpers =====
|
||||
readonly property var masterFamilies: ["A", "B", "C", "D", "E", "F", "P", "X", "Z"]
|
||||
|
||||
function masterPath(family) {
|
||||
const table = settingsModel.masters;
|
||||
return table && table[family] ? table[family] : "";
|
||||
}
|
||||
|
||||
function statusColor() {
|
||||
if (!settingsModel.isValid) {
|
||||
return Theme.statusWarningColor;
|
||||
}
|
||||
return settingsModel.saveStatus.indexOf("error:") === 0 ? Theme.statusErrorColor : Theme.statusSuccessColor;
|
||||
}
|
||||
|
||||
// ===== Reusable Settings Components =====
|
||||
component SettingsGroupBox: GroupBox {
|
||||
id: settingsGroup
|
||||
|
||||
background: Rectangle {
|
||||
y: settingsGroup.topPadding - settingsGroup.bottomPadding
|
||||
width: settingsGroup.width
|
||||
height: settingsGroup.height - settingsGroup.topPadding + settingsGroup.bottomPadding
|
||||
radius: Theme.radiusMd
|
||||
color: Theme.panelBackground
|
||||
border.color: Theme.innerFrameBorder
|
||||
border.width: Theme.borderThin
|
||||
}
|
||||
|
||||
label: Label {
|
||||
text: settingsGroup.title
|
||||
color: Theme.panelTitleText
|
||||
font.pixelSize: 16
|
||||
font.bold: true
|
||||
}
|
||||
}
|
||||
|
||||
component SettingsTextInput: TextField {
|
||||
id: textInput
|
||||
color: Theme.fieldText
|
||||
placeholderTextColor: Theme.fieldPlaceholder
|
||||
selectedTextColor: Theme.fieldBackground
|
||||
selectionColor: Theme.fieldText
|
||||
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusXs
|
||||
color: Theme.fieldBackground
|
||||
border.width: textInput.activeFocus ? Theme.borderStrong : Theme.borderThin
|
||||
border.color: textInput.activeFocus ? Theme.fieldBorderFocus : Theme.fieldBorder
|
||||
}
|
||||
}
|
||||
|
||||
component SettingsActionButton: Button {
|
||||
id: actionButton
|
||||
hoverEnabled: true
|
||||
|
||||
background: Rectangle {
|
||||
radius: Theme.radiusSm
|
||||
color: actionButton.down ? Theme.buttonNeutralPressed : (actionButton.hovered ? Theme.buttonNeutralHover : Theme.buttonNeutralBackground)
|
||||
border.width: Theme.borderThin
|
||||
border.color: Theme.fieldBorder
|
||||
}
|
||||
|
||||
contentItem: Text {
|
||||
text: actionButton.text
|
||||
color: Theme.buttonNeutralText
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
elide: Text.ElideRight
|
||||
font.bold: true
|
||||
}
|
||||
}
|
||||
|
||||
component SettingsToggle: CheckBox {
|
||||
id: toggle
|
||||
indicator: Rectangle {
|
||||
implicitWidth: 20
|
||||
implicitHeight: 20
|
||||
x: toggle.leftPadding
|
||||
y: parent.height / 2 - height / 2
|
||||
radius: Theme.radiusXs
|
||||
color: toggle.checked ? Theme.primaryAccent : Theme.fieldBackground
|
||||
border.width: Theme.borderThin
|
||||
border.color: toggle.checked ? Theme.primaryAccent : Theme.fieldBorder
|
||||
|
||||
Rectangle {
|
||||
anchors.centerIn: parent
|
||||
width: 9
|
||||
height: 9
|
||||
radius: Theme.radiusXs
|
||||
visible: toggle.checked
|
||||
color: Theme.fieldBackground
|
||||
}
|
||||
}
|
||||
|
||||
contentItem: Text {
|
||||
text: toggle.text
|
||||
color: Theme.checkboxText
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
leftPadding: toggle.indicator.width + toggle.spacing
|
||||
}
|
||||
}
|
||||
|
||||
// ===== File Picker Dialog =====
|
||||
FileDialog {
|
||||
id: masterPicker
|
||||
title: "Choose Master CSV"
|
||||
nameFilters: ["CSV files (*.csv)"]
|
||||
property string targetFamily: ""
|
||||
onAccepted: settingsModel.setMaster(targetFamily, String(selectedFile).replace(/^file:\/\//, ""))
|
||||
}
|
||||
|
||||
// ===== Settings Page Layout =====
|
||||
Item {
|
||||
anchors.fill: parent
|
||||
|
||||
ScrollView {
|
||||
id: settingsScroll
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.settingsOuterMargin
|
||||
|
||||
contentWidth: availableWidth
|
||||
contentHeight: container.implicitHeight
|
||||
|
||||
ScrollBar.vertical: ScrollBar {
|
||||
parent: settingsScroll
|
||||
anchors.top: settingsScroll.top
|
||||
anchors.right: settingsScroll.right
|
||||
anchors.bottom: settingsScroll.bottom
|
||||
policy: ScrollBar.AsNeeded
|
||||
|
||||
contentItem: Rectangle {
|
||||
implicitWidth: 8
|
||||
implicitHeight: 8
|
||||
radius: Theme.radiusSm
|
||||
color: Theme.trackBackground
|
||||
}
|
||||
|
||||
background: Rectangle {
|
||||
implicitWidth: 8
|
||||
color: "transparent"
|
||||
}
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
id: container
|
||||
width: Math.min(settingsScroll.availableWidth, Theme.settingsPanelMaxWidth)
|
||||
x: (settingsScroll.availableWidth - width) / 2
|
||||
spacing: Theme.settingsSectionSpacing
|
||||
|
||||
// ===== Page Title =====
|
||||
Label {
|
||||
text: "Settings"
|
||||
font.pixelSize: 30
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
}
|
||||
|
||||
// ===== Chamber Settings =====
|
||||
SettingsGroupBox {
|
||||
title: "Chamber"
|
||||
Layout.fillWidth: true
|
||||
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
spacing: Theme.settingsRowSpacing
|
||||
|
||||
Label {
|
||||
text: "Chamber ID"
|
||||
color: Theme.headingColor
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
}
|
||||
|
||||
SettingsTextInput {
|
||||
id: chamberField
|
||||
Layout.fillWidth: true
|
||||
Layout.minimumWidth: Theme.settingsFieldMinWidth
|
||||
placeholderText: "Enter chamber ID"
|
||||
text: settingsModel.chamberId
|
||||
|
||||
Connections {
|
||||
target: settingsModel
|
||||
function onChamberIdChanged() {
|
||||
if (!chamberField.activeFocus) {
|
||||
chamberField.text = settingsModel.chamberId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SettingsActionButton {
|
||||
text: "Set"
|
||||
Layout.preferredWidth: 90
|
||||
Layout.preferredHeight: Theme.settingsButtonHeight
|
||||
onClicked: {
|
||||
settingsModel.setChamberId(chamberField.text);
|
||||
settingsModel.saveSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Master CSV Mapping =====
|
||||
SettingsGroupBox {
|
||||
title: "Master Files"
|
||||
Layout.fillWidth: true
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: Theme.settingsGridSpacing
|
||||
|
||||
Repeater {
|
||||
model: root.masterFamilies
|
||||
|
||||
RowLayout {
|
||||
property string family: modelData
|
||||
|
||||
Layout.fillWidth: true
|
||||
spacing: Theme.settingsRowSpacing
|
||||
|
||||
Label {
|
||||
text: family
|
||||
font.bold: true
|
||||
color: Theme.headingColor
|
||||
Layout.preferredWidth: 24
|
||||
}
|
||||
|
||||
SettingsTextInput {
|
||||
Layout.fillWidth: true
|
||||
readOnly: true
|
||||
text: root.masterPath(family)
|
||||
placeholderText: "No master selected"
|
||||
color: Theme.bodyColor
|
||||
}
|
||||
|
||||
SettingsActionButton {
|
||||
text: "Browse"
|
||||
Layout.preferredHeight: Theme.settingsButtonHeight
|
||||
onClicked: {
|
||||
masterPicker.targetFamily = family;
|
||||
masterPicker.open();
|
||||
}
|
||||
}
|
||||
|
||||
SettingsActionButton {
|
||||
text: "Clear"
|
||||
Layout.preferredHeight: Theme.settingsButtonHeight
|
||||
onClicked: settingsModel.clearMaster(family)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Wafer Behavior =====
|
||||
SettingsGroupBox {
|
||||
title: "Wafer Behavior"
|
||||
Layout.fillWidth: true
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: Theme.settingsGridSpacing
|
||||
|
||||
SettingsToggle {
|
||||
text: "Reverse Z Wafer"
|
||||
checked: settingsModel.reverseZWafer
|
||||
onToggled: settingsModel.setReverseZWafer(checked)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Appearance =====
|
||||
SettingsGroupBox {
|
||||
title: "Appearance"
|
||||
Layout.fillWidth: true
|
||||
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
spacing: Theme.settingsRowSpacing
|
||||
|
||||
// Custom pill toggle
|
||||
Rectangle {
|
||||
id: toggleTrack
|
||||
implicitWidth: 52
|
||||
implicitHeight: 28
|
||||
radius: height / 2
|
||||
color: Theme.trackBackground
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
Layout.preferredWidth: implicitWidth
|
||||
Layout.preferredHeight: implicitHeight
|
||||
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 250
|
||||
easing.type: Easing.InOutQuad
|
||||
}
|
||||
}
|
||||
|
||||
// Thumb
|
||||
Rectangle {
|
||||
id: toggleThumb
|
||||
width: 22
|
||||
height: 22
|
||||
radius: height / 2
|
||||
color: "white"
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
x: Theme.isDarkMode ? parent.width - width - 3 : 3
|
||||
|
||||
Behavior on x {
|
||||
NumberAnimation {
|
||||
duration: 250
|
||||
easing.type: Easing.InOutQuad
|
||||
}
|
||||
}
|
||||
|
||||
// Drop shadow effect
|
||||
layer.enabled: true
|
||||
layer.effect: null // swap for DropShadow if Qt.labs.effects is available
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: Theme.isDarkMode = !Theme.isDarkMode
|
||||
}
|
||||
}
|
||||
|
||||
// Label
|
||||
Label {
|
||||
text: Theme.isDarkMode ? "Dark" : "Light"
|
||||
color: Theme.bodyColor
|
||||
font.pixelSize: 13
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
|
||||
Behavior on color {
|
||||
ColorAnimation {
|
||||
duration: 200
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Item {
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Adjustment Entry Point =====
|
||||
RowLayout {
|
||||
id: adjustmentRow
|
||||
Layout.fillWidth: true
|
||||
spacing: Theme.settingsRowSpacing
|
||||
|
||||
SettingsActionButton {
|
||||
text: "Edit CSV Metadata"
|
||||
Layout.preferredWidth: Theme.settingsActionWidth
|
||||
Layout.preferredHeight: Theme.settingsButtonHeight
|
||||
onClicked: root.handleEditCsvMetadata()
|
||||
}
|
||||
|
||||
Item {
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
}
|
||||
|
||||
// Bottom spacer so last group isn't flush with scroll edge
|
||||
Item {
|
||||
Layout.preferredHeight: Theme.settingsSectionSpacing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Scroll hint overlay (fades out when scrolled) =====
|
||||
Rectangle {
|
||||
id: scrollHint
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.left: parent.left
|
||||
anchors.right: parent.right
|
||||
height: 48
|
||||
gradient: Gradient {
|
||||
GradientStop {
|
||||
position: 0.0
|
||||
color: Theme.isDarkMode ? 'rgba(30,30,35,1)' : 'rgba(255,255,255,1)'
|
||||
}
|
||||
GradientStop {
|
||||
position: 1.0
|
||||
color: Qt.rgba(0, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
Label {
|
||||
anchors.centerIn: parent
|
||||
text: "↓ Scroll for more"
|
||||
font.pixelSize: 13
|
||||
font.bold: true
|
||||
color: Theme.bodyColor
|
||||
opacity: (settingsScroll.ScrollBar.vertical.position <= 0 && settingsScroll.contentHeight > settingsScroll.height) ? (scrollHintTimer.running ? 1 : 0) : 0
|
||||
|
||||
Behavior on opacity {
|
||||
NumberAnimation {
|
||||
duration: 400
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Auto-hide scroll hint after first interaction =====
|
||||
Timer {
|
||||
id: scrollHintTimer
|
||||
running: true
|
||||
repeat: false
|
||||
onTriggered: scrollHint.visible = false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import QtQuick.Controls
|
||||
import ISC
|
||||
|
||||
// ===== Status Tab =====
|
||||
// Blank on startup. Fills in after Detect Wafer fires from the left rail.
|
||||
ColumnLayout {
|
||||
id: root
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.panelPadding
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
property alias waferFamilyCode: waferInfoFamily.text
|
||||
property alias waferSerialNumber: waferSerial.text
|
||||
property alias waferSensorCount: waferSensors.text
|
||||
property alias waferRuntime: waferRuntime.text
|
||||
property alias waferCycles: waferCycles.text
|
||||
property bool waferDetected: false
|
||||
|
||||
// Latches true once any operation starts (or a previous session is
|
||||
// restored) and never resets, so the status panel stays visible even
|
||||
// when a detect finds no wafer.
|
||||
property bool statusActive: false
|
||||
|
||||
// Data summary after parse
|
||||
property int dataRows: 0
|
||||
property int dataCols: 0
|
||||
property string csvPath: ""
|
||||
property bool dataParsed: false
|
||||
|
||||
// ===== Empty state — nothing shown until detect fires =====
|
||||
Item {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
visible: !root.statusActive
|
||||
}
|
||||
|
||||
// ===== Results area (shown once detect/operation runs) =====
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
visible: root.statusActive
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
// --- Connection Status ---
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 86
|
||||
color: {
|
||||
if (deviceController.connectionStatus === "Connected") return Theme.statusSuccessColor + "22"
|
||||
if (deviceController.connectionStatus === "Disconnected") return Theme.statusErrorColor + "22"
|
||||
return Theme.cardBackground
|
||||
}
|
||||
border.color: {
|
||||
if (deviceController.connectionStatus === "Connected") return Theme.statusSuccessColor
|
||||
if (deviceController.connectionStatus === "Disconnected") return Theme.statusErrorColor
|
||||
return Theme.cardBorder
|
||||
}
|
||||
radius: Theme.radiusSm
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.panelPadding
|
||||
spacing: 4
|
||||
|
||||
Text {
|
||||
text: deviceController.connectionStatus
|
||||
color: {
|
||||
if (deviceController.connectionStatus === "Connected") return Theme.statusSuccessColor
|
||||
if (deviceController.connectionStatus === "Disconnected") return Theme.statusErrorColor
|
||||
return Theme.bodyColor
|
||||
}
|
||||
font.bold: true
|
||||
font.pixelSize: 16
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
|
||||
Text {
|
||||
text: "Port: " + (deviceController.selectedPort || "None selected")
|
||||
color: Theme.bodyColor
|
||||
font.pixelSize: 14
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
|
||||
Text {
|
||||
text: "Save dir: " + deviceController.saveDataDir
|
||||
color: Theme.bodyColor
|
||||
font.pixelSize: 12
|
||||
opacity: 0.7
|
||||
elide: Text.ElideMiddle
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Wafer Info ---
|
||||
GroupBox {
|
||||
title: "Wafer Information"
|
||||
visible: root.waferDetected
|
||||
Layout.fillWidth: true
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.panelPadding
|
||||
spacing: 8
|
||||
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Family Code"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { id: waferInfoFamily; text: "—"; color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Serial Number"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { id: waferSerial; text: "—"; color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Sensor Count"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { id: waferSensors; text: "—"; color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
}
|
||||
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Runtime"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { id: waferRuntime; text: "—"; color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Cycles"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { id: waferCycles; text: "—"; color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Data Summary (shown after parse) ---
|
||||
GroupBox {
|
||||
title: "Data Summary"
|
||||
visible: root.dataParsed
|
||||
Layout.fillWidth: true
|
||||
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: Theme.panelPadding
|
||||
spacing: Theme.rightPaneGap
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Rows"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { text: String(root.dataRows); color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "Sensors"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { text: String(root.dataCols); color: Theme.bodyColor; font.pixelSize: 16; font.bold: true }
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 4
|
||||
Text { text: "CSV"; color: Theme.subheadingColor; font.pixelSize: 12 }
|
||||
Text { text: root.csvPath; color: Theme.bodyColor; font.pixelSize: 12; elide: Text.ElideMiddle }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Activity Log ---
|
||||
GroupBox {
|
||||
title: "Activity Log"
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
|
||||
ScrollView {
|
||||
anchors.fill: parent
|
||||
clip: true
|
||||
|
||||
TextArea {
|
||||
id: activityLog
|
||||
width: parent.width
|
||||
text: ""
|
||||
readOnly: true
|
||||
font.family: "monospace"
|
||||
font.pixelSize: 12
|
||||
wrapMode: TextArea.WordWrap
|
||||
leftPadding: 4
|
||||
rightPadding: 4
|
||||
color: Theme.bodyColor
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: deviceController
|
||||
function onActivityLogUpdated(newLog) {
|
||||
activityLog.text = newLog
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Signal Handlers ---
|
||||
Connections {
|
||||
target: deviceController
|
||||
// Any operation start (detect/read/erase/debug) latches the panel on.
|
||||
function onPortsUpdated() {
|
||||
if (deviceController.operationInProgress)
|
||||
root.statusActive = true
|
||||
}
|
||||
|
||||
function onDetectResult(result) {
|
||||
root.statusActive = true
|
||||
if (result && result.familyCode) {
|
||||
root.waferDetected = true
|
||||
waferInfoFamily.text = result.familyCode
|
||||
waferSerial.text = result.serialNumber
|
||||
waferSensors.text = String(result.sensorCount)
|
||||
waferRuntime.text = result.runtime + "s"
|
||||
waferCycles.text = String(result.cycleCount)
|
||||
}
|
||||
// On failure, keep the last displayed wafer info and do not change waferDetected.
|
||||
}
|
||||
|
||||
function onStatusRestored() {
|
||||
// Show restored wafer info if available
|
||||
var info = deviceController.lastWaferInfo
|
||||
if (info && info.length > 0) {
|
||||
root.statusActive = true
|
||||
root.waferDetected = true
|
||||
waferInfoFamily.text = info[0] || "—"
|
||||
waferSerial.text = info[1] || "—"
|
||||
waferSensors.text = String(info[2] || 0)
|
||||
waferRuntime.text = (info[3] || 0) + "s"
|
||||
waferCycles.text = String(info[4] || 0)
|
||||
}
|
||||
// Show data summary if data was previously parsed
|
||||
if (deviceController.dataRowCount > 0) {
|
||||
root.statusActive = true
|
||||
root.dataParsed = true
|
||||
root.dataRows = deviceController.dataRowCount
|
||||
root.dataCols = deviceController.dataColCount
|
||||
root.csvPath = "" // CSV path not persisted as full path, just show it was parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: deviceController
|
||||
function onReadResult(result) {
|
||||
root.dataParsed = false
|
||||
root.dataRows = 0
|
||||
root.dataCols = 0
|
||||
root.csvPath = ""
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: deviceController
|
||||
function onParsedDataReady(result) {
|
||||
if (result && result.success) {
|
||||
root.dataParsed = true
|
||||
root.dataRows = result.rows
|
||||
root.dataCols = result.cols
|
||||
root.csvPath = result.csv_path || ""
|
||||
} else {
|
||||
root.dataParsed = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# ===== ISC Tabs Module =====
|
||||
module ISC.Tabs
|
||||
|
||||
# ===== Tab Components =====
|
||||
SettingsTab 1.0 SettingsTab.qml
|
||||
@@ -0,0 +1,136 @@
|
||||
pragma Singleton
|
||||
|
||||
import QtQuick
|
||||
|
||||
// =============================================================================
|
||||
// Theme — single source of truth for colors and sizing
|
||||
// =============================================================================
|
||||
// Sections:
|
||||
// 1. Mode flag
|
||||
// 2. Tone palette (raw tones; everything else derives from these)
|
||||
// 3. Surfaces (page / card / panel / workspace backgrounds)
|
||||
// 4. Borders
|
||||
// 5. Text (headings, body, panel titles)
|
||||
// 6. Controls (fields, buttons, checkbox)
|
||||
// 7. Tracks (pill toggle + scrollbar)
|
||||
// 8. Tabs (footer tab bar)
|
||||
// 9. Side rail
|
||||
// 10. Status (success / warning / error)
|
||||
// 11. Geometry (radius / border width / spacing / sizing)
|
||||
// =============================================================================
|
||||
|
||||
QtObject {
|
||||
// ── 1. Mode ──────────────────────────────────────────────────────────────
|
||||
property bool isDarkMode: false
|
||||
|
||||
// ── 2. Tone palette (base values; consume via semantic tokens below) ─────
|
||||
readonly property color tone100: isDarkMode ? "#111111" : "#FAFAFA"
|
||||
readonly property color tone200: isDarkMode ? "#1A1A1A" : "#F2F2F2"
|
||||
readonly property color tone300: isDarkMode ? "#242424" : "#E8E8E8"
|
||||
readonly property color toneText: isDarkMode ? "#F2F2F2" : "#111111"
|
||||
readonly property color toneMute: isDarkMode ? "#A8A8A8" : "#8A8A8A"
|
||||
readonly property color toneBorder: isDarkMode ? "#2B2B2B" : "#D8D8D8"
|
||||
|
||||
// ── 3. Surfaces ──────────────────────────────────────────────────────────
|
||||
readonly property color pageBackground: tone100
|
||||
readonly property color cardBackground: tone200
|
||||
readonly property color panelBackground: tone200
|
||||
readonly property color workspaceBackground: tone200
|
||||
readonly property color responseBackground: tone200
|
||||
readonly property color subtleSectionBackground: tone300
|
||||
|
||||
// ── 4. Borders ───────────────────────────────────────────────────────────
|
||||
readonly property color cardBorder: toneBorder
|
||||
readonly property color responseBorder: toneBorder
|
||||
readonly property color workspaceBorder: toneBorder
|
||||
readonly property color innerFrameBorder: toneBorder
|
||||
readonly property color outerFrameBorder: isDarkMode ? "#343434" : "#CECECE"
|
||||
readonly property color softBorder: isDarkMode ? "#222222" : "#E2E2E2"
|
||||
|
||||
// ── 5. Text ──────────────────────────────────────────────────────────────
|
||||
readonly property color headingColor: toneText
|
||||
readonly property color bodyColor: toneMute
|
||||
readonly property color subheadingColor: toneMute
|
||||
readonly property color panelTitleText: toneMute
|
||||
readonly property color disabledText: toneMute
|
||||
|
||||
// ── 6. Controls ──────────────────────────────────────────────────────────
|
||||
// Fields
|
||||
readonly property color fieldBackground: tone100
|
||||
readonly property color fieldText: toneText
|
||||
readonly property color fieldPlaceholder: toneMute
|
||||
readonly property color fieldBorder: toneBorder
|
||||
readonly property color fieldBorderFocus: toneText
|
||||
// Neutral buttons
|
||||
readonly property color buttonNeutralBackground: tone300
|
||||
readonly property color buttonNeutralHover: isDarkMode ? "#303030" : "#DDDDDD"
|
||||
readonly property color buttonNeutralPressed: tone100
|
||||
readonly property color buttonNeutralText: toneText
|
||||
readonly property color buttonPressed: tone300
|
||||
readonly property color primaryAccent: toneText
|
||||
// Checkbox
|
||||
readonly property color checkboxText: toneText
|
||||
|
||||
// ── 7. Tracks (pill toggle, scrollbar thumb) ─────────────────────────────
|
||||
readonly property color trackBackground: isDarkMode ? "#25252B" : "#D1D5DB"
|
||||
|
||||
// ── 8. Tabs (footer tab bar) ─────────────────────────────────────────────
|
||||
readonly property color tabBarBackground: tone200
|
||||
readonly property color tabBackground: "transparent"
|
||||
readonly property color tabActiveBackground: "transparent"
|
||||
readonly property color tabHoverBackground: tone300
|
||||
readonly property color tabBorder: "transparent"
|
||||
readonly property color tabText: toneMute
|
||||
readonly property color tabActiveText: toneText
|
||||
|
||||
// ── 9. Side rail ─────────────────────────────────────────────────────────
|
||||
readonly property color sideRailBackground: tone200
|
||||
readonly property color sideActiveBackground: tone300
|
||||
|
||||
// ── 10. Status ───────────────────────────────────────────────────────────
|
||||
readonly property color statusSuccessColor: isDarkMode ? "#63D471" : "#2E9E44"
|
||||
readonly property color statusWarningColor: isDarkMode ? "#F5C15C" : "#C88A18"
|
||||
readonly property color statusErrorColor: isDarkMode ? "#FF6B6B" : "#D64545"
|
||||
|
||||
// -- 10b. Sensor bands (wafer map dots)
|
||||
readonly property color sensorInRange: statusSuccessColor
|
||||
readonly property color sensorHigh: statusErrorColor
|
||||
readonly property color sensorlow: isDarkMode ? "#589DF5" : "#2F6FE0"
|
||||
readonly property color waferRingColor: toneBorder
|
||||
readonly property color waferAxisColor: softBorder
|
||||
|
||||
// ── 11. Geometry ─────────────────────────────────────────────────────────
|
||||
// Radius
|
||||
readonly property int radiusXs: 4 // fields, tight elements
|
||||
readonly property int radiusSm: 6 // buttons
|
||||
readonly property int radiusMd: 10 // cards, group boxes
|
||||
readonly property int radiusLg: 14 // large panels / dialogs
|
||||
// Border width
|
||||
readonly property int borderThin: 1
|
||||
readonly property int borderStrong: 2
|
||||
// Main panel layout
|
||||
readonly property int mainAreaPadding: 10
|
||||
readonly property int rightPaneGap: 6
|
||||
readonly property int panelPadding: 12
|
||||
// Side rail layout
|
||||
readonly property int sideRailWidth: 190
|
||||
readonly property int sideRailSpacing: 14
|
||||
readonly property int sideButtonHeight: 146
|
||||
readonly property int sideButtonMinHeight: 88
|
||||
// Tab bar layout
|
||||
readonly property int tabBarHeight: 34
|
||||
readonly property int tabBarPadding: 6
|
||||
readonly property int tabSpacing: 2
|
||||
readonly property int tabRadius: 2
|
||||
readonly property int tabFontSize: 12
|
||||
readonly property int tabButtonMinWidth: 80
|
||||
// Settings page layout
|
||||
readonly property int settingsPanelMaxWidth: 760
|
||||
readonly property int settingsOuterMargin: 40
|
||||
readonly property int settingsSectionSpacing: 20
|
||||
readonly property int settingsRowSpacing: 16
|
||||
readonly property int settingsGridSpacing: 12
|
||||
readonly property int settingsButtonHeight: 40
|
||||
readonly property int settingsActionWidth: 200
|
||||
readonly property int settingsFieldMinWidth: 160
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# ===== ISC QML Module =====
|
||||
module ISC
|
||||
|
||||
# ===== Root Components =====
|
||||
Main 1.0 Main.qml
|
||||
HomePage 1.0 HomePage.qml
|
||||
|
||||
# ===== Singleton Theme =====
|
||||
singleton Theme 1.0 Theme.qml
|
||||
@@ -0,0 +1,52 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtQml import QQmlApplicationEngine
|
||||
from PySide6.QtQuickControls2 import QQuickStyle
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from pygui.backend.device_controller import DeviceController
|
||||
from pygui.backend.local_settings import LocalSettings
|
||||
from pygui.backend.local_settings_model import LocalSettingsModel
|
||||
from pygui.backend.file_browser import FileBrowser
|
||||
|
||||
|
||||
# ===== Application Entry Point =====
|
||||
def main() -> int:
|
||||
# ===== UI Style Setup =====
|
||||
# Use a non-native controls style so our custom QML button backgrounds are supported.
|
||||
QQuickStyle.setStyle("Basic")
|
||||
|
||||
# ===== Qt Bootstrap =====
|
||||
# Minimal Qt bootstrap: load the ISC QML module and exit if it fails.
|
||||
app = QApplication(sys.argv)
|
||||
engine = QQmlApplicationEngine()
|
||||
|
||||
# ===== Shared Models =====
|
||||
settings_model = LocalSettingsModel()
|
||||
select_file_dialog_model = FileBrowser()
|
||||
settings_model.loadSettings()
|
||||
engine.rootContext().setContextProperty("settingsModel", settings_model)
|
||||
engine.rootContext().setContextProperty("file_browser", select_file_dialog_model)
|
||||
|
||||
# ===== Device Controller (serial comm) =====
|
||||
data_dir = str(settings_model._data_dir)
|
||||
raw_settings = LocalSettings.read_settings(data_dir)
|
||||
device_controller = DeviceController(raw_settings, data_dir)
|
||||
engine.rootContext().setContextProperty("deviceController", device_controller)
|
||||
|
||||
# ===== QML Startup =====
|
||||
# The "ISC" QML module lives alongside this file (src/pygui/ISC), so the
|
||||
# package directory is the import path the engine searches for qmldir.
|
||||
engine.addImportPath(Path(__file__).parent)
|
||||
engine.loadFromModule("ISC", "Main")
|
||||
|
||||
# ===== Exit Handling =====
|
||||
if not engine.rootObjects():
|
||||
return -1
|
||||
|
||||
return app.exec()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1 @@
|
||||
# ===== Backend Package Marker =====
|
||||
@@ -0,0 +1,26 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List
|
||||
|
||||
|
||||
# ===== Single Contour Segment =====
|
||||
@dataclass
|
||||
class ContourSegment:
|
||||
start_x: float
|
||||
start_y: float
|
||||
end_x: float
|
||||
end_y: float
|
||||
|
||||
@property
|
||||
def start(self):
|
||||
return (self.start_x, self.start_y)
|
||||
|
||||
@property
|
||||
def end(self):
|
||||
return (self.end_x, self.end_y)
|
||||
|
||||
|
||||
# ===== Contour Line =====
|
||||
@dataclass
|
||||
class ContourLine:
|
||||
level: float
|
||||
segments: List[ContourSegment] = field(default_factory=list)
|
||||
@@ -0,0 +1,90 @@
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
|
||||
|
||||
# ===== Crypto Utilities =====
|
||||
class CryptoHelper:
|
||||
"""Cryptographic utilities for wafer data encryption/decryption."""
|
||||
|
||||
# ===== Hex/Byte Conversion =====
|
||||
@staticmethod
|
||||
def hex_string_to_bytearray(hex_str: str) -> bytes:
|
||||
"""Converts a hex string to bytes (e.g., '48656C6C6F' → b'Hello')."""
|
||||
if len(hex_str) % 2 != 0:
|
||||
raise ValueError("Hex string must have even length")
|
||||
try:
|
||||
return bytes.fromhex(hex_str)
|
||||
except ValueError as e:
|
||||
raise ValueError(f"Invalid hex string: {e}")
|
||||
|
||||
@staticmethod
|
||||
def bytearray_to_hex_string(data: Union[bytes, bytearray]) -> str:
|
||||
"""Converts bytes to lowercase hex string."""
|
||||
return data.hex()
|
||||
|
||||
# ===== Random Data =====
|
||||
@staticmethod
|
||||
def generate_random_bytes(length: int) -> bytes:
|
||||
"""Generates cryptographically secure random bytes."""
|
||||
if length < 0:
|
||||
raise ValueError("Length must be non-negative")
|
||||
return secrets.token_bytes(length)
|
||||
|
||||
# ===== AES Decryption =====
|
||||
@staticmethod
|
||||
def decrypt_aes(data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||
"""
|
||||
Decrypts AES-128-CBC data with PKCS7 padding.
|
||||
|
||||
Args:
|
||||
data: Encrypted ciphertext (must be multiple of 16 bytes)
|
||||
key: 16-byte AES key
|
||||
iv: 16-byte IV
|
||||
|
||||
Returns:
|
||||
Decrypted plaintext
|
||||
|
||||
Raises:
|
||||
ValueError: If inputs are invalid
|
||||
cryptography.exceptions.InvalidSignature: On decryption failure
|
||||
"""
|
||||
if len(key) != 16:
|
||||
raise ValueError("Key must be 16 bytes (AES-128)")
|
||||
if len(iv) != 16:
|
||||
raise ValueError("IV must be 16 bytes")
|
||||
if len(data) == 0:
|
||||
return b""
|
||||
if len(data) % 16 != 0:
|
||||
raise ValueError("Ciphertext length must be multiple of 16 bytes")
|
||||
|
||||
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
|
||||
decrypt_ctx = cipher.decryptor()
|
||||
plaintext_padded = decrypt_ctx.update(data) + decrypt_ctx.finalize()
|
||||
|
||||
# Remove PKCS7 padding
|
||||
pad_len = plaintext_padded[-1]
|
||||
if pad_len > 16 or pad_len == 0:
|
||||
raise ValueError("Invalid padding")
|
||||
return plaintext_padded[:-pad_len]
|
||||
|
||||
# ===== Binary File I/O =====
|
||||
@staticmethod
|
||||
def save_to_binary_file(file_path: Union[str, Path], data: bytes) -> None:
|
||||
"""Writes bytes to a binary file."""
|
||||
Path(file_path).write_bytes(data)
|
||||
|
||||
@staticmethod
|
||||
def read_from_binary_file(file_path: Union[str, Path]) -> bytes:
|
||||
"""Reads bytes from a binary file."""
|
||||
return Path(file_path).read_bytes()
|
||||
|
||||
# ===== Convenience Helpers =====
|
||||
@classmethod
|
||||
def read_hex_string_from_binary_file(cls, file_path: Union[str, Path]) -> str:
|
||||
"""Reads binary file and returns hex string."""
|
||||
data = cls.read_from_binary_file(file_path)
|
||||
return cls.bytearray_to_hex_string(data)
|
||||
@@ -0,0 +1,51 @@
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# ===== CSV Metadata Model =====
|
||||
class CSVFileMetadata:
|
||||
# ===== Lifecycle =====
|
||||
def __init__(self, wafer="", date="", chamber="", notes="", master_type="", filename="", columns=0):
|
||||
self.wafer = wafer
|
||||
self.date = date
|
||||
self.chamber = chamber
|
||||
self.notes = notes
|
||||
self.master_type = master_type
|
||||
|
||||
self.filename = filename
|
||||
self.columns = columns
|
||||
|
||||
# ===== Accessors =====
|
||||
def get_wafer_type(self) -> str:
|
||||
"""Return the first character of the wafer string, or empty if none"""
|
||||
return self.wafer[0] if self.wafer else ""
|
||||
|
||||
def get_date(self) -> datetime:
|
||||
"""Parsifes the date string. Returns current time if parsing fails"""
|
||||
date_format = "%Y-%m-%d %H:%M:%S"
|
||||
try:
|
||||
return datetime.strptime(self.date, date_format)
|
||||
except (ValueError, TypeError):
|
||||
# If format is wrong or date is None, return current time
|
||||
return datetime.now()
|
||||
|
||||
# ===== Formatting =====
|
||||
@staticmethod
|
||||
def format_date(dt: datetime) -> str:
|
||||
"""Formats a datetime object into the standard ISO-like string."""
|
||||
if not isinstance(dt, datetime):
|
||||
return ""
|
||||
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# ===== Serialization =====
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"wafer": self.wafer,
|
||||
"date": self.string_date_format(), # Ensure we save as string
|
||||
"chamber": self.chamber,
|
||||
"notes": self.notes,
|
||||
"masterType": self.master_type,
|
||||
}
|
||||
|
||||
def string_date_format(self) -> str:
|
||||
"""Helper to ensure the date is always a string for JSON."""
|
||||
return self.format_date(self.get_date())
|
||||
@@ -0,0 +1,84 @@
|
||||
"""QAbstractTableModel for displaying parsed wafer temperature data in QML."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from PySide6.QtCore import QAbstractTableModel, Qt
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Column roles for the temperature data table
|
||||
ROW_ROLE = Qt.ItemDataRole.UserRole + 1
|
||||
COL_ROLE = Qt.ItemDataRole.UserRole + 2
|
||||
|
||||
|
||||
class TemperatureTableModel(QAbstractTableModel):
|
||||
"""Table model for parsed wafer temperature data.
|
||||
|
||||
Exposes a 2D list of temperature strings to QML TableView.
|
||||
Column 0 = row index, remaining columns = Sensor1, Sensor2, ...
|
||||
"""
|
||||
|
||||
def __init__(self, parent: Any = None) -> None:
|
||||
super().__init__(parent)
|
||||
self._data: list[list[str]] = [] # 2D array of temperature strings
|
||||
self._col_count: int = 0 # Number of sensor columns (excludes row index)
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Clear all data."""
|
||||
self.beginResetModel()
|
||||
self._data = []
|
||||
self._col_count = 0
|
||||
self.endResetModel()
|
||||
|
||||
def load_data(self, data: list[list[str]], col_count: int) -> None:
|
||||
"""Load parsed temperature data into the model.
|
||||
|
||||
Args:
|
||||
data: 2D list of temperature strings (e.g. [["25.30", "24.80", ...], ...]).
|
||||
col_count: Number of sensor columns (may differ from data[0] length).
|
||||
"""
|
||||
self.beginResetModel()
|
||||
self._data = data
|
||||
self._col_count = col_count
|
||||
self.endResetModel()
|
||||
log.info("Loaded %d rows × %d cols into model", len(data), col_count)
|
||||
|
||||
# ---- QAbstractTableModel interface ----
|
||||
|
||||
def rowCount(self, parent: Any = None) -> int:
|
||||
return len(self._data)
|
||||
|
||||
def columnCount(self, parent: Any = None) -> int:
|
||||
# Column 0 = row index, columns 1..N = sensor values
|
||||
return self._col_count + 1
|
||||
|
||||
def data(self, index: Any, role: int = ...) -> Any:
|
||||
if not index.isValid():
|
||||
return None
|
||||
|
||||
row = index.row()
|
||||
col = index.column()
|
||||
|
||||
if role == Qt.ItemDataRole.DisplayRole:
|
||||
if col == 0:
|
||||
return str(row + 1) # 1-based row index
|
||||
sensor_col = col - 1
|
||||
if row < len(self._data) and sensor_col < len(self._data[row]):
|
||||
return self._data[row][sensor_col]
|
||||
return "0"
|
||||
|
||||
return None
|
||||
|
||||
def headerData(
|
||||
self, section: int, orientation: int, role: int = ...
|
||||
) -> Any:
|
||||
if role != Qt.ItemDataRole.DisplayRole:
|
||||
return None
|
||||
if orientation == Qt.Orientation.Horizontal:
|
||||
if section == 0:
|
||||
return "Row"
|
||||
return f"Sensor{section}"
|
||||
return str(section + 1)
|
||||
@@ -0,0 +1,55 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
# ===== Time-Indexed Data Segment =====
|
||||
class DataSegment:
|
||||
# ===== Lifecycle =====
|
||||
def __init__(
|
||||
self,
|
||||
full_data: list[float],
|
||||
start_time: datetime,
|
||||
start_index: int,
|
||||
end_index: int,
|
||||
):
|
||||
if not isinstance(start_index, int):
|
||||
raise TypeError("start_index must be an integer")
|
||||
if not isinstance(end_index, int):
|
||||
raise TypeError("end_index must be an integer")
|
||||
"""
|
||||
full_data: The complete list of sensor readings.
|
||||
start_time: The timestamp of the very first reading in full_data.
|
||||
start_index: The offset (in seconds) from start_time to the beginning of this segment.
|
||||
end_index: The offset (in seconds) from start_time to the end of this segment.
|
||||
"""
|
||||
self.full_data = full_data
|
||||
self._base_start_time = start_time
|
||||
self._start_index = start_index
|
||||
self._end_index = end_index
|
||||
self.chamber = ""
|
||||
self.notes = ""
|
||||
|
||||
# ===== Index Properties =====
|
||||
@property
|
||||
def start_index(self) -> int:
|
||||
return self._start_index
|
||||
|
||||
@start_index.setter
|
||||
def start_index(self, value: int):
|
||||
self._start_index = value
|
||||
|
||||
@property
|
||||
def end_index(self) -> int:
|
||||
return self._end_index
|
||||
|
||||
@end_index.setter
|
||||
def end_index(self, value: int):
|
||||
self._end_index = value
|
||||
|
||||
# ===== Derived Time Properties =====
|
||||
@property
|
||||
def start_time(self) -> datetime:
|
||||
return self._base_start_time + timedelta(seconds=self.start_index)
|
||||
|
||||
@property
|
||||
def end_time(self) -> datetime:
|
||||
return self._base_start_time + timedelta(seconds=self.end_index)
|
||||
@@ -0,0 +1,521 @@
|
||||
"""QML-exposed controller for wafer device communication.
|
||||
|
||||
Bridges DeviceService (serialcomm) to QML via @Slot methods and signals.
|
||||
All hardware operations run synchronously on the main thread (matching
|
||||
C# Form1.cs per-operation open/close pattern). For long operations
|
||||
(erase ~15s, read up to 120s) the caller should show a loading state.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from datetime import datetime
|
||||
from typing import Any, Optional
|
||||
|
||||
from PySide6.QtCore import QObject, Property, Qt, Signal, Slot
|
||||
|
||||
from pygui.backend.data_model import TemperatureTableModel
|
||||
from pygui.backend.graph_view import GraphView
|
||||
from pygui.backend.local_settings import LocalSettings
|
||||
from pygui.serialcomm.data_parser import (
|
||||
convert_to_temperatures,
|
||||
parse_binary_data,
|
||||
remove_trailing_zeros,
|
||||
save_to_csv,
|
||||
)
|
||||
from pygui.serialcomm.device_service import DeviceService
|
||||
from pygui.serialcomm.serial_port import WaferInfo
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DeviceController(QObject):
|
||||
"""Controls serial communication with the temperature-sensing wafer.
|
||||
|
||||
Exposed to QML as ``deviceController`` context property.
|
||||
"""
|
||||
|
||||
# ---- public signals ----
|
||||
portsUpdated = Signal(list)
|
||||
detectResult = Signal(object) # WaferInfo dict or None
|
||||
readResult = Signal(object) # {"success": bool, "bytes": int} or {"error": str}
|
||||
eraseResult = Signal(object) # {"success": bool}
|
||||
debugResult = Signal(object) # {"success": bool, "sensor_bytes": int, "debug_bytes": int}
|
||||
parsedDataReady = Signal(object) # {"success": bool, "rows": int, "cols": int, "data": ..., "csv_path": str}
|
||||
statusRestored = Signal() # Emitted when status is restored from previous session
|
||||
logMessage = Signal(str)
|
||||
activityLogUpdated = Signal(str)
|
||||
|
||||
# ---- private signals (marshal worker-thread results back to main thread) ----
|
||||
_detectFinished = Signal(object, object) # (port_or_None, WaferInfo_or_None)
|
||||
_readFinished = Signal(str, str, object) # (port, family_code, bytes_or_None)
|
||||
|
||||
def __init__(self, settings: LocalSettings, data_dir: str, parent: QObject | None = None) -> None:
|
||||
super().__init__(parent)
|
||||
self._settings = settings
|
||||
self._data_dir = data_dir
|
||||
self._service = DeviceService(settings)
|
||||
|
||||
# Initialize status from persisted settings (with defaults for new installations)
|
||||
self._connection_status = getattr(settings, 'connection_status', "Disconnected")
|
||||
self._operation_in_progress = False
|
||||
self._activity_log: list[str] = getattr(settings, 'activity_log', [])
|
||||
self._raw_bytes: Optional[bytes] = None
|
||||
# Default save dir lives next to the settings file (~/Documents/isc_data/csv)
|
||||
from pathlib import Path
|
||||
self._save_data_dir: str = getattr(settings, 'save_data_dir', str(Path.home() / "Documents" / "isc_data" / "csv"))
|
||||
self._last_wafer_info: dict[str, Any] = getattr(settings, 'last_wafer_info', {})
|
||||
self._last_csv_path: str = getattr(settings, 'last_csv_path', "")
|
||||
self._selected_port: str = getattr(settings, 'selected_port', "")
|
||||
self._data_row_count: int = getattr(settings, 'data_row_count', 0)
|
||||
self._data_col_count: int = getattr(settings, 'data_col_count', 0)
|
||||
self._data_model = TemperatureTableModel(self)
|
||||
self._graph_view = GraphView(self)
|
||||
|
||||
# If we have persisted activity log, emit it to QML
|
||||
if self._activity_log:
|
||||
self.activityLogUpdated.emit("\n".join(self._activity_log))
|
||||
|
||||
# Log status restoration and emit signal for UI updates
|
||||
if self._connection_status != "Disconnected" or self._selected_port or self._last_wafer_info:
|
||||
self._append_log("Restored previous session status")
|
||||
if self._selected_port:
|
||||
self._append_log(f"Last selected port: {self._selected_port}")
|
||||
if self._last_wafer_info:
|
||||
info = self._last_wafer_info
|
||||
self._append_log(f"Last wafer: {info.get('serialNumber', 'Unknown')} ({info.get('familyCode', 'Unknown')})")
|
||||
self.statusRestored.emit()
|
||||
|
||||
# Marshal worker-thread results onto the Qt main thread.
|
||||
self._detectFinished.connect(self._handle_detect_finished, Qt.QueuedConnection)
|
||||
self._readFinished.connect(self._handle_read_finished, Qt.QueuedConnection)
|
||||
|
||||
# ---- Properties ----
|
||||
@Property(list, notify=portsUpdated)
|
||||
def availablePorts(self) -> list[str]:
|
||||
"""Currently available serial port names."""
|
||||
return self._service.enumerate_ports()
|
||||
|
||||
@Property(str, notify=portsUpdated)
|
||||
def connectionStatus(self) -> str:
|
||||
"""Current connection status string for display."""
|
||||
return self._connection_status
|
||||
|
||||
@Property(bool, notify=portsUpdated)
|
||||
def operationInProgress(self) -> bool:
|
||||
"""Whether a hardware operation is currently running."""
|
||||
return self._operation_in_progress
|
||||
|
||||
@Property(str, notify=activityLogUpdated)
|
||||
def saveDataDir(self) -> str:
|
||||
"""Directory where parsed CSV data files are saved."""
|
||||
return self._save_data_dir
|
||||
|
||||
@Slot(str)
|
||||
def setSaveDataDir(self, path: str) -> None:
|
||||
"""Set the directory for saving parsed CSV data files."""
|
||||
self._save_data_dir = path
|
||||
self._append_log(f"Save data dir set to: {path}")
|
||||
self._save_status()
|
||||
|
||||
@Property(str, notify=portsUpdated)
|
||||
def selectedPort(self) -> str:
|
||||
"""Currently selected serial port shared across the UI."""
|
||||
return self._selected_port
|
||||
|
||||
@Slot(str)
|
||||
def setSelectedPort(self, port: str) -> None:
|
||||
"""Set the active serial port from the port selector."""
|
||||
self._selected_port = port
|
||||
self._save_status()
|
||||
|
||||
@Property(int, notify=parsedDataReady)
|
||||
def dataRowCount(self) -> int:
|
||||
"""Number of rows in the parsed temperature dataset."""
|
||||
return self._data_row_count
|
||||
|
||||
@Property(int, notify=parsedDataReady)
|
||||
def dataColCount(self) -> int:
|
||||
"""Number of sensor columns in the parsed temperature dataset."""
|
||||
return self._data_col_count
|
||||
|
||||
@Property(list, notify=detectResult)
|
||||
def lastWaferInfo(self) -> list:
|
||||
"""Last detected wafer info as a flat list for QML bindings.
|
||||
|
||||
Returns [familyCode, serialNumber, sensorCount, mfgDateHex, runtime, cycleCount].
|
||||
"""
|
||||
info = self._last_wafer_info
|
||||
return [
|
||||
info.get("familyCode", ""),
|
||||
info.get("serialNumber", ""),
|
||||
info.get("sensorCount", 0),
|
||||
info.get("mfgDateHex", ""),
|
||||
info.get("runtime", 0),
|
||||
info.get("cycleCount", 0),
|
||||
]
|
||||
|
||||
@Property(object, notify=detectResult)
|
||||
def dataModel(self) -> TemperatureTableModel:
|
||||
"""QAbstractTableModel for the parsed temperature data table."""
|
||||
return self._data_model
|
||||
|
||||
@Property(object, notify=debugResult)
|
||||
def graphView(self) -> GraphView:
|
||||
"""GraphView for rendering sensor temperature line charts."""
|
||||
return self._graph_view
|
||||
|
||||
@Slot(result=object)
|
||||
def getChartData(self) -> dict[str, Any]:
|
||||
"""Extract chart-ready data from the current model.
|
||||
|
||||
Returns a dict with sensor names and per-sensor value lists
|
||||
suitable for GraphView.updateChart().
|
||||
"""
|
||||
if not self._data_model or self._data_model.rowCount() == 0:
|
||||
return {"success": False}
|
||||
|
||||
num_sensors = self._data_model.columnCount() - 1 # Exclude row index
|
||||
sensor_names = [f"Sensor{i+1}" for i in range(num_sensors)]
|
||||
|
||||
# Extract per-sensor values from the model
|
||||
series_data = []
|
||||
for col in range(1, self._data_model.columnCount()):
|
||||
sensor_values = []
|
||||
for row in range(self._data_model.rowCount()):
|
||||
idx = self._data_model.index(row, col)
|
||||
val = self._data_model.data(idx)
|
||||
sensor_values.append(val if val else "0")
|
||||
series_data.append(sensor_values)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"sensor_names": sensor_names,
|
||||
"series": series_data,
|
||||
}
|
||||
|
||||
def _append_log(self, message: str) -> None:
|
||||
"""Append a message to the activity log and emit updated log."""
|
||||
ts = datetime.now().strftime("%H:%M:%S")
|
||||
self._activity_log.append(f"[{ts}] {message}")
|
||||
# Keep only last 200 lines
|
||||
if len(self._activity_log) > 200:
|
||||
self._activity_log = self._activity_log[-200:]
|
||||
self.activityLogUpdated.emit("\n".join(self._activity_log))
|
||||
|
||||
def _set_operation_progress(self, in_progress: bool) -> None:
|
||||
"""Set operation progress state and notify QML.
|
||||
|
||||
portsUpdated is the shared notify signal for availablePorts,
|
||||
connectionStatus, and operationInProgress — emitting it forces
|
||||
QML to re-evaluate all three bindings.
|
||||
"""
|
||||
self._operation_in_progress = in_progress
|
||||
self.portsUpdated.emit(self._service.enumerate_ports())
|
||||
|
||||
# ---- Public Slots ----
|
||||
|
||||
@Slot()
|
||||
def refreshPorts(self) -> None:
|
||||
"""Scan for available serial ports and emit updated list."""
|
||||
ports = self._service.enumerate_ports()
|
||||
self._connection_status = "Disconnected"
|
||||
self.portsUpdated.emit(ports)
|
||||
self._append_log(f"Scanned {len(ports)} serial port(s)")
|
||||
self._save_status()
|
||||
|
||||
@Slot()
|
||||
def detectWafer(self) -> None:
|
||||
"""Scan all serial ports for a wafer (runs in background thread).
|
||||
|
||||
Mirrors C# chkConnection(): tries every available port with s0.
|
||||
Stores the found port in _selected_port for subsequent read/erase.
|
||||
Emits detectResult with WaferInfo dict on success, or None.
|
||||
"""
|
||||
if self._operation_in_progress:
|
||||
self._append_log("Already busy — ignoring detect request")
|
||||
return
|
||||
|
||||
self._set_operation_progress(True)
|
||||
self._connection_status = "Detecting..."
|
||||
self.portsUpdated.emit(self._service.enumerate_ports())
|
||||
self._append_log("Scanning all ports for wafer ...")
|
||||
|
||||
# Spawn a daemon worker — UI stays responsive during the scan.
|
||||
threading.Thread(target=self._detect_worker, daemon=True).start()
|
||||
|
||||
def _detect_worker(self) -> None:
|
||||
"""Background-thread body for detectWafer."""
|
||||
try:
|
||||
port, info = self._service.detect_all_ports()
|
||||
except Exception as exc:
|
||||
log.exception("Detect worker crashed: %s", exc)
|
||||
port, info = None, None
|
||||
self._detectFinished.emit(port, info)
|
||||
|
||||
@Slot(object, object)
|
||||
def _handle_detect_finished(
|
||||
self, port: Optional[str], info: Optional[WaferInfo]
|
||||
) -> None:
|
||||
"""Main-thread completion handler for detectWafer."""
|
||||
if info is not None:
|
||||
self._selected_port = port or ""
|
||||
self._connection_status = "Connected"
|
||||
self._append_log(
|
||||
f"Detected: {info.serial_number} (family={info.family_code}, "
|
||||
f"port={port}, runtime={info.runtime}s, cycles={info.cycle_count})"
|
||||
)
|
||||
wafer_dict = self._wafer_info_to_dict(info)
|
||||
self._last_wafer_info = wafer_dict
|
||||
self.detectResult.emit(wafer_dict)
|
||||
self._save_status()
|
||||
else:
|
||||
self._selected_port = ""
|
||||
self._connection_status = "Disconnected"
|
||||
self._append_log("No wafer detected on any port")
|
||||
self._last_wafer_info = {}
|
||||
self.detectResult.emit(None)
|
||||
self._set_operation_progress(False)
|
||||
|
||||
@Slot()
|
||||
def readMemoryAsync(self) -> None:
|
||||
"""Read wafer memory in a background thread.
|
||||
|
||||
Uses the family code and port from the last detect.
|
||||
Emits readResult on completion, then auto-chains to parseAndSaveData.
|
||||
"""
|
||||
if self._operation_in_progress:
|
||||
self._append_log("Already busy — ignoring read request")
|
||||
return
|
||||
|
||||
if not self._selected_port:
|
||||
self._append_log("No wafer detected — run Detect Wafer first")
|
||||
self.readResult.emit({"error": "No port — detect wafer first"})
|
||||
return
|
||||
|
||||
port = self._selected_port
|
||||
family_code = self._last_wafer_info.get("familyCode", "")
|
||||
|
||||
self._set_operation_progress(True)
|
||||
self._connection_status = "Reading..."
|
||||
self.portsUpdated.emit(self._service.enumerate_ports())
|
||||
self._append_log(
|
||||
f"Reading memory on {port} (family={family_code or 'auto'}) ..."
|
||||
)
|
||||
|
||||
threading.Thread(
|
||||
target=self._read_worker,
|
||||
args=(port, family_code),
|
||||
daemon=True,
|
||||
).start()
|
||||
|
||||
def _read_worker(self, port: str, family_code: str) -> None:
|
||||
"""Background-thread body for readMemoryAsync."""
|
||||
try:
|
||||
data = self._service.read_wafer_data(port, family_code)
|
||||
except Exception as exc:
|
||||
log.exception("Read worker crashed: %s", exc)
|
||||
data = None
|
||||
self._readFinished.emit(port, family_code, data)
|
||||
|
||||
@Slot(str, str, object)
|
||||
def _handle_read_finished(
|
||||
self, port: str, family_code: str, data: Optional[bytes]
|
||||
) -> None:
|
||||
"""Main-thread completion handler for readMemoryAsync."""
|
||||
if data is not None:
|
||||
self._connection_status = "Connected"
|
||||
self._append_log(f"Read {len(data)} bytes")
|
||||
self._raw_bytes = data
|
||||
self.readResult.emit({"success": True, "bytes": len(data)})
|
||||
self._set_operation_progress(False)
|
||||
# Auto-chain: read → parse → save (matches C# behavior).
|
||||
# Parse runs on main thread — it's CPU-bound but bounded (~1s).
|
||||
self.parseAndSaveData(family_code, port)
|
||||
self._save_status()
|
||||
return
|
||||
self._connection_status = "Disconnected"
|
||||
self._append_log("Read returned no data")
|
||||
self._raw_bytes = None
|
||||
self.readResult.emit({"error": "Read returned no data"})
|
||||
self._set_operation_progress(False)
|
||||
self._save_status()
|
||||
|
||||
@Slot(str)
|
||||
def eraseMemory(self, port: str) -> None:
|
||||
"""Send p1 erase command."""
|
||||
self._set_operation_progress(True)
|
||||
self._connection_status = "Erasing..."
|
||||
self.portsUpdated.emit(self._service.enumerate_ports())
|
||||
self._append_log(f"Erase command sent to {port} (wafer takes ~15s) ...")
|
||||
|
||||
ok = self._service.erase_wafer(port)
|
||||
if ok:
|
||||
self._connection_status = "Connected"
|
||||
self._append_log("Erase command accepted — wait ~15 seconds")
|
||||
else:
|
||||
self._connection_status = "Disconnected"
|
||||
self._append_log("Erase command failed")
|
||||
self.eraseResult.emit({"success": ok})
|
||||
self._set_operation_progress(False)
|
||||
|
||||
@Slot(str)
|
||||
def readDebug(self, port: str) -> None:
|
||||
"""Read both D1 (sensor) and F1 (debug/cold-junction) data.
|
||||
|
||||
Emits debugResult with counts on success.
|
||||
"""
|
||||
self._set_operation_progress(True)
|
||||
self._connection_status = "Reading debug..."
|
||||
self.portsUpdated.emit(self._service.enumerate_ports())
|
||||
self._append_log(f"Reading debug data on {port} ...")
|
||||
|
||||
# Step 1: D1 sensor data
|
||||
sensor_data = self._service.read_wafer_data(port, family_code="")
|
||||
if sensor_data is None:
|
||||
self._connection_status = "Disconnected"
|
||||
self._append_log("D1 read failed — aborting debug read")
|
||||
self.debugResult.emit({"error": "D1 read failed"})
|
||||
self._set_operation_progress(False)
|
||||
return
|
||||
|
||||
self._append_log(f"D1 read: {len(sensor_data)} bytes")
|
||||
|
||||
# Step 2: F1 debug data
|
||||
debug_data = self._service.read_wafer_data(port, family_code="", cmd="F1")
|
||||
if debug_data is None:
|
||||
self._connection_status = "Disconnected"
|
||||
self._append_log("F1 read failed")
|
||||
self.debugResult.emit({"error": "F1 read failed"})
|
||||
self._set_operation_progress(False)
|
||||
return
|
||||
|
||||
self._connection_status = "Connected"
|
||||
self._append_log(
|
||||
f"Debug read complete: {len(sensor_data)} sensor bytes, "
|
||||
f"{len(debug_data)} debug bytes"
|
||||
)
|
||||
self.debugResult.emit({
|
||||
"success": True,
|
||||
"sensor_bytes": len(sensor_data),
|
||||
"debug_bytes": len(debug_data),
|
||||
})
|
||||
self._set_operation_progress(False)
|
||||
|
||||
@Slot(str, str)
|
||||
def parseAndSaveData(self, family_code: str = "", port: str = "") -> None:
|
||||
"""Parse raw bytes into temperatures and save to CSV.
|
||||
|
||||
Uses the bytes from the most recent readMemoryAsync call.
|
||||
Emits parsedDataReady with the parsed result.
|
||||
"""
|
||||
if not self._raw_bytes:
|
||||
self._append_log("No raw bytes to parse (read first)")
|
||||
self.parsedDataReady.emit({
|
||||
"success": False,
|
||||
"error": "No raw bytes available",
|
||||
})
|
||||
return
|
||||
|
||||
if not self._save_data_dir:
|
||||
self._append_log("No save data directory set")
|
||||
self.parsedDataReady.emit({
|
||||
"success": False,
|
||||
"error": "No save data directory set",
|
||||
})
|
||||
return
|
||||
|
||||
fc = family_code or (
|
||||
self._last_wafer_info.get("familyCode", "")
|
||||
if self._last_wafer_info
|
||||
else ""
|
||||
)
|
||||
serial = (
|
||||
self._last_wafer_info.get("serialNumber", "UNKNOWN")
|
||||
if self._last_wafer_info
|
||||
else "UNKNOWN"
|
||||
)
|
||||
|
||||
self._append_log(f"Parsing {len(self._raw_bytes)} bytes (family={fc or 'auto'}) ...")
|
||||
|
||||
# Step 1: Parse binary → hex strings
|
||||
hex_data = parse_binary_data(self._raw_bytes, fc)
|
||||
if hex_data is None:
|
||||
self._append_log("Binary parse failed")
|
||||
self.parsedDataReady.emit({
|
||||
"success": False,
|
||||
"error": "Binary parse failed",
|
||||
})
|
||||
return
|
||||
|
||||
rows = len(hex_data)
|
||||
cols = len(hex_data[0]) if hex_data else 0
|
||||
self._append_log(f"Parsed: {rows} rows × {cols} sensors")
|
||||
|
||||
# Step 2: Convert hex → temperatures
|
||||
temp_data = convert_to_temperatures(hex_data, fc)
|
||||
|
||||
# Step 3: Remove trailing zero rows
|
||||
remove_trailing_zeros(temp_data)
|
||||
self._append_log(f"After trim: {len(temp_data)} rows")
|
||||
|
||||
# Step 4: Save to CSV
|
||||
csv_path = save_to_csv(temp_data, fc, serial, self._save_data_dir)
|
||||
if csv_path is None:
|
||||
self._append_log("CSV save failed")
|
||||
self.parsedDataReady.emit({
|
||||
"success": False,
|
||||
"error": "CSV save failed",
|
||||
})
|
||||
return
|
||||
|
||||
self._append_log(f"Saved CSV: {csv_path}")
|
||||
|
||||
# Load data into the QAbstractTableModel
|
||||
self._data_model.load_data(temp_data, cols)
|
||||
self._data_row_count = len(temp_data)
|
||||
self._data_col_count = cols
|
||||
|
||||
# Emit parsed data to QML
|
||||
self.parsedDataReady.emit({
|
||||
"success": True,
|
||||
"rows": len(temp_data),
|
||||
"cols": cols,
|
||||
"data": temp_data,
|
||||
"csv_path": csv_path,
|
||||
"familyCode": fc,
|
||||
"serialNumber": serial,
|
||||
})
|
||||
self._save_status()
|
||||
|
||||
# ---- Status Persistence ----
|
||||
|
||||
def _save_status(self) -> None:
|
||||
"""Persist current operational status to settings."""
|
||||
# Update settings with current status values
|
||||
self._settings.connection_status = self._connection_status
|
||||
self._settings.selected_port = self._selected_port
|
||||
self._settings.last_wafer_info = self._last_wafer_info.copy()
|
||||
self._settings.save_data_dir = self._save_data_dir
|
||||
self._settings.activity_log = self._activity_log.copy()
|
||||
self._settings.data_row_count = self._data_row_count
|
||||
self._settings.data_col_count = self._data_col_count
|
||||
self._settings.last_csv_path = self._last_csv_path
|
||||
|
||||
# Save to disk
|
||||
LocalSettings.save_settings(LocalSettings._settings_path(self._data_dir), self._settings)
|
||||
|
||||
# ---- Helpers ----
|
||||
|
||||
@staticmethod
|
||||
def _wafer_info_to_dict(info: WaferInfo) -> dict[str, Any]:
|
||||
"""Convert WaferInfo dataclass to JSON-serialisable dict for QML."""
|
||||
return {
|
||||
"familyCode": info.family_code,
|
||||
"serialNumber": info.serial_number,
|
||||
"sensorCount": info.sensor_count,
|
||||
"mfgDateHex": info.mfg_date_hex,
|
||||
"runtime": info.runtime,
|
||||
"cycleCount": info.cycle_count,
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QObject, Property, QStandardPaths, Signal, Slot
|
||||
from PySide6.QtWidgets import QFileDialog, QMessageBox
|
||||
|
||||
from pygui.backend.csv_file_metadata import CSVFileMetadata
|
||||
from pygui.backend.zwafer_parser import ZWaferParser
|
||||
|
||||
|
||||
# ===== File Browser Model =====
|
||||
class FileBrowser(QObject):
|
||||
# ===== Change Signals =====
|
||||
filesChanged = Signal()
|
||||
currentDirectoryChanged = Signal()
|
||||
|
||||
# ===== Lifecycle =====
|
||||
def __init__(self, parent: QObject | None = None) -> None:
|
||||
super().__init__(parent)
|
||||
self._parser = ZWaferParser()
|
||||
self._files: list[dict[str, object]] = []
|
||||
self._current_directory = self._resolve_default_directory()
|
||||
self._refresh_files(show_empty_message=False)
|
||||
|
||||
# ===== Exposed Properties =====
|
||||
@Property("QVariantList", notify=filesChanged)
|
||||
def files(self) -> list[dict[str, object]]:
|
||||
return [dict(row) for row in self._files]
|
||||
|
||||
@Property(str, notify=currentDirectoryChanged)
|
||||
def currentDirectory(self) -> str:
|
||||
return str(self._current_directory)
|
||||
|
||||
# ===== Directory Selection =====
|
||||
@Slot()
|
||||
def chooseDirectory(self) -> None:
|
||||
selected_dir = QFileDialog.getExistingDirectory(
|
||||
None,
|
||||
"Select CSV Folder",
|
||||
self.currentDirectory,
|
||||
)
|
||||
if not selected_dir:
|
||||
return
|
||||
|
||||
self._set_current_directory(Path(selected_dir))
|
||||
self._refresh_files(show_empty_message=True)
|
||||
|
||||
@Slot()
|
||||
def refreshFiles(self) -> None:
|
||||
self._refresh_files(show_empty_message=False)
|
||||
|
||||
# ===== Metadata Persistence =====
|
||||
@Slot(str, str, str, str, str, bool, str)
|
||||
def saveMetadata(
|
||||
self,
|
||||
file_path: str,
|
||||
wafer: str,
|
||||
date: str,
|
||||
chamber: str,
|
||||
notes: str,
|
||||
selected: bool,
|
||||
master_type: str = "",
|
||||
) -> None:
|
||||
csv_path = Path(file_path)
|
||||
if not csv_path.exists():
|
||||
self._show_error(f"CSV file was not found:\n{file_path}")
|
||||
return
|
||||
|
||||
row = self._find_row(file_path)
|
||||
if row is None:
|
||||
self._show_error("The selected CSV row is no longer available.")
|
||||
return
|
||||
|
||||
row["wafer"] = wafer
|
||||
row["date"] = date
|
||||
row["chamber"] = chamber
|
||||
row["notes"] = notes
|
||||
row["selected"] = selected
|
||||
row["masterType"] = master_type
|
||||
|
||||
payload = {
|
||||
"wafer": wafer,
|
||||
"date": date,
|
||||
"chamber": chamber,
|
||||
"notes": notes,
|
||||
"masterType": master_type,
|
||||
}
|
||||
|
||||
try:
|
||||
sidecar_path = self._sidecar_path(csv_path)
|
||||
sidecar_path.write_text(json.dumps(payload, indent=4), encoding="utf-8")
|
||||
except Exception as exc: # pragma: no cover - defensive UI path
|
||||
self._show_error(f"Failed to save metadata:\n{exc}")
|
||||
return
|
||||
|
||||
self.filesChanged.emit()
|
||||
self._show_info(f"Saved metadata for:\n{csv_path.name}")
|
||||
|
||||
# ===== Internal Data Loading =====
|
||||
def _refresh_files(self, show_empty_message: bool) -> None:
|
||||
selected_state = {
|
||||
str(row.get("fileName", "")): bool(row.get("selected", False))
|
||||
for row in self._files
|
||||
}
|
||||
master_state = {
|
||||
str(row.get("fileName", "")): row.get("masterType", "") or ""
|
||||
for row in self._files
|
||||
}
|
||||
|
||||
self._files = []
|
||||
|
||||
if not self._current_directory.exists():
|
||||
self.filesChanged.emit()
|
||||
return
|
||||
|
||||
for csv_path in sorted(self._current_directory.glob("*.csv")):
|
||||
try:
|
||||
metadata = self._load_metadata(csv_path)
|
||||
self._files.append(
|
||||
{
|
||||
"selected": selected_state.get(str(csv_path), False),
|
||||
"wafer": metadata.wafer,
|
||||
"date": metadata.string_date_format(),
|
||||
"chamber": metadata.chamber,
|
||||
"notes": metadata.notes,
|
||||
"masterType": master_state.get(str(csv_path), "") or metadata.master_type,
|
||||
"fileName": str(csv_path),
|
||||
"highlight": metadata.get_wafer_type() in {"A", "B", "C"},
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
self._files.append(
|
||||
{
|
||||
"selected": selected_state.get(str(csv_path), False),
|
||||
"wafer": csv_path.stem,
|
||||
"date": "",
|
||||
"chamber": "",
|
||||
"notes": "Unable to parse metadata",
|
||||
"masterType": master_state.get(str(csv_path), ""),
|
||||
"fileName": str(csv_path),
|
||||
"highlight": False,
|
||||
}
|
||||
)
|
||||
|
||||
self.filesChanged.emit()
|
||||
|
||||
if show_empty_message and not self._files:
|
||||
self._show_info("No CSV files were found in the selected folder.")
|
||||
|
||||
def _load_metadata(self, csv_path: Path) -> CSVFileMetadata:
|
||||
sidecar_path = self._sidecar_path(csv_path)
|
||||
if sidecar_path.exists():
|
||||
try:
|
||||
payload = json.loads(sidecar_path.read_text(encoding="utf-8"))
|
||||
return CSVFileMetadata(
|
||||
wafer=str(payload.get("wafer", "")),
|
||||
date=str(payload.get("date", "")),
|
||||
chamber=str(payload.get("chamber", "")),
|
||||
notes=str(payload.get("notes", "")),
|
||||
master_type=str(payload.get("masterType", "")),
|
||||
filename=str(csv_path),
|
||||
)
|
||||
except Exception:
|
||||
# Fall back to CSV/header parsing if sidecar is malformed.
|
||||
pass
|
||||
|
||||
parser_data, _ = self._parser.parse(str(csv_path))
|
||||
if parser_data is not None:
|
||||
wafer = parser_data.serial or ""
|
||||
date_text = ""
|
||||
if parser_data.date != datetime.min:
|
||||
date_text = CSVFileMetadata.format_date(parser_data.date)
|
||||
return CSVFileMetadata(
|
||||
wafer=wafer,
|
||||
date=date_text,
|
||||
chamber="",
|
||||
notes="",
|
||||
filename=str(csv_path),
|
||||
)
|
||||
|
||||
return self._metadata_from_filename(csv_path)
|
||||
|
||||
def _metadata_from_filename(self, csv_path: Path) -> CSVFileMetadata:
|
||||
match = re.match(
|
||||
r"^(?P<wafer>[^-]+)-(?P<date>\d{8})(?:_(?P<run>\d+))?$",
|
||||
csv_path.stem,
|
||||
)
|
||||
if not match:
|
||||
return CSVFileMetadata(filename=str(csv_path))
|
||||
|
||||
date_text = ""
|
||||
try:
|
||||
parsed = datetime.strptime(match.group("date"), "%Y%m%d")
|
||||
date_text = CSVFileMetadata.format_date(parsed)
|
||||
except ValueError:
|
||||
date_text = ""
|
||||
|
||||
return CSVFileMetadata(
|
||||
wafer=match.group("wafer"),
|
||||
date=date_text,
|
||||
filename=str(csv_path),
|
||||
)
|
||||
|
||||
# ===== Internal Helpers =====
|
||||
def _resolve_default_directory(self) -> Path:
|
||||
documents_dir = QStandardPaths.writableLocation(
|
||||
QStandardPaths.DocumentsLocation
|
||||
)
|
||||
base_dir = Path(documents_dir) if documents_dir else (Path.home() / "Documents")
|
||||
return base_dir / "isc_data"
|
||||
|
||||
def _set_current_directory(self, directory: Path) -> None:
|
||||
normalized = Path(directory)
|
||||
if normalized == self._current_directory:
|
||||
return
|
||||
self._current_directory = normalized
|
||||
self.currentDirectoryChanged.emit()
|
||||
|
||||
def _find_row(self, file_path: str) -> dict[str, object] | None:
|
||||
for row in self._files:
|
||||
if str(row.get("fileName", "")) == file_path:
|
||||
return row
|
||||
return None
|
||||
|
||||
def _sidecar_path(self, csv_path: Path) -> Path:
|
||||
return csv_path.with_suffix(f"{csv_path.suffix}.json")
|
||||
|
||||
def _show_error(self, message: str) -> None:
|
||||
QMessageBox.critical(None, "iSenseCloud", message)
|
||||
|
||||
@Slot(str)
|
||||
def showInfo(self, message: str) -> None:
|
||||
self._show_info(message)
|
||||
|
||||
@Slot(str, result="QVariantMap")
|
||||
def parseCsvMetadata(self, file_path: str) -> dict:
|
||||
from pathlib import Path as _Path
|
||||
|
||||
csv_path = _Path(file_path)
|
||||
if not csv_path.exists():
|
||||
return {"success": False, "error": "File not found"}
|
||||
|
||||
parser_data, _ = self._parser.parse(file_path)
|
||||
if parser_data is None:
|
||||
return {"success": False, "error": "Failed to parse CSV"}
|
||||
|
||||
wafer = parser_data.serial or ""
|
||||
date_text = ""
|
||||
if parser_data.date != datetime.min:
|
||||
date_text = CSVFileMetadata.format_date(parser_data.date)
|
||||
columns = len(parser_data.csv_headers) if parser_data.csv_headers else 0
|
||||
family_code = wafer[0].upper() if wafer else ""
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"wafer": wafer,
|
||||
"date": date_text,
|
||||
"columns": columns,
|
||||
"familyCode": family_code,
|
||||
}
|
||||
|
||||
def _show_info(self, message: str) -> None:
|
||||
QMessageBox.information(None, "iSenseCloud", message)
|
||||
|
||||
|
||||
# Backward-compatible alias while the QML dialog is still named SelectFileDialog.
|
||||
SelectFileDialogModel = FileBrowser
|
||||
@@ -0,0 +1,11 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Frame:
|
||||
"""One sample across all sensors ata a point in time"""
|
||||
|
||||
seq: int
|
||||
t: float
|
||||
values: list[float]
|
||||
@@ -0,0 +1,169 @@
|
||||
"""pyqtgraph PlotWidget wrapper for embedding in QML.
|
||||
|
||||
Exposes a QWidget with a pyqtgraph PlotWidget that can be displayed
|
||||
via QML's `import QtWidgets` or `QtWidgets.QWidget` integration.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
from PySide6.QtCore import QObject, Property, Signal, Slot
|
||||
from PySide6.QtWidgets import QWidget
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Import pyqtgraph after Qt is initialized
|
||||
import pyqtgraph as pg
|
||||
from pyqtgraph import PlotWidget
|
||||
|
||||
|
||||
class GraphView(QObject):
|
||||
"""QML-exposed controller for a pyqtgraph line chart.
|
||||
|
||||
Accepts sensor temperature data (list of lists) and renders
|
||||
each sensor as a separate line series.
|
||||
"""
|
||||
|
||||
# ---- signals ----
|
||||
dataReady = Signal(object) # {"success": bool, "sensory_names": list, "series": list}
|
||||
|
||||
def __init__(self, parent: Optional[QObject] = None) -> None:
|
||||
super().__init__(parent)
|
||||
self._plot_widget: Optional[PlotWidget] = None
|
||||
self._plot_window: Optional[QWidget] = None
|
||||
self._series: list[Any] = []
|
||||
self._sensor_names: list[str] = []
|
||||
|
||||
@Property(object, notify=dataReady)
|
||||
def plotWidget(self) -> Any:
|
||||
"""Return the QWidget hosting the pyqtgraph PlotWidget for QML embedding."""
|
||||
return self._plot_window
|
||||
|
||||
@Slot()
|
||||
def createPlotWidget(self, parent_widget: Optional[QWidget] = None) -> None:
|
||||
"""Create and return a QWidget containing a pyqtgraph PlotWidget.
|
||||
|
||||
Args:
|
||||
parent_widget: Optional parent widget.
|
||||
"""
|
||||
pg.setConfigOption("background", "default")
|
||||
pg.setConfigOption("foreground", "default")
|
||||
|
||||
self._plot_window = QWidget(parent=parent_widget)
|
||||
self._plot_widget = PlotWidget()
|
||||
self._plot_widget.setBackground("default")
|
||||
|
||||
from PySide6.QtWidgets import QVBoxLayout
|
||||
layout = QVBoxLayout(self._plot_window)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.setSpacing(0)
|
||||
layout.addWidget(self._plot_widget)
|
||||
|
||||
# Set axis labels
|
||||
self._plot_widget.setLabel("left", "Temperature", units="°C")
|
||||
self._plot_widget.setLabel("bottom", "Measurement Interval")
|
||||
self._plot_widget.setTitle("Sensor Temperature Over Time")
|
||||
|
||||
@Slot(str, str)
|
||||
def updateChart(self, sensor_names_str: str, series_data_str: str) -> None:
|
||||
"""Update the chart with sensor data.
|
||||
|
||||
Args:
|
||||
sensor_names_str: Comma-separated sensor names (e.g. "Sensor1,Sensor2").
|
||||
series_data_str: JSON-like string of nested lists for each sensor's values.
|
||||
"""
|
||||
import json
|
||||
|
||||
if not self._plot_widget:
|
||||
log.warning("PlotWidget not created yet")
|
||||
return
|
||||
|
||||
try:
|
||||
sensor_names = [s.strip() for s in sensor_names_str.split(",") if s.strip()]
|
||||
series_data = json.loads(series_data_str)
|
||||
except (json.JSONDecodeError, AttributeError) as exc:
|
||||
log.error("Failed to parse chart data: %s", exc)
|
||||
return
|
||||
|
||||
# Clear existing series
|
||||
self._plot_widget.clear()
|
||||
self._series = []
|
||||
|
||||
if not series_data:
|
||||
return
|
||||
|
||||
# Determine Y-axis range from all data
|
||||
all_values = []
|
||||
for sensor_values in series_data:
|
||||
for v in sensor_values:
|
||||
try:
|
||||
all_values.append(float(v))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
if all_values:
|
||||
y_min = min(all_values)
|
||||
y_max = max(all_values)
|
||||
y_range = y_max - y_min
|
||||
buffer = max(y_range * 0.1, 1.0) # At least 1 degree buffer
|
||||
self._plot_widget.setYRange(y_min - buffer, y_max + buffer)
|
||||
else:
|
||||
self._plot_widget.setYRange(-50, 150)
|
||||
|
||||
# X-axis: measurement intervals (0-based index)
|
||||
num_points = len(series_data[0]) if series_data else 0
|
||||
x_axis = list(range(num_points))
|
||||
|
||||
# Define a set of distinct colors for series
|
||||
colors = [
|
||||
(255, 87, 87), # Red
|
||||
(66, 165, 245), # Blue
|
||||
(102, 187, 106), # Green
|
||||
(255, 167, 38), # Orange
|
||||
(171, 71, 188), # Purple
|
||||
(0, 188, 212), # Cyan
|
||||
(255, 112, 67), # Deep Orange
|
||||
(121, 85, 72), # Brown
|
||||
(92, 107, 192), # Indigo
|
||||
(48, 125, 117), # Teal
|
||||
]
|
||||
|
||||
# Add each sensor as a line series
|
||||
for i, sensor_name in enumerate(sensor_names):
|
||||
if i >= len(series_data):
|
||||
break
|
||||
|
||||
sensor_values = series_data[i]
|
||||
y_values = []
|
||||
for v in sensor_values:
|
||||
try:
|
||||
y_values.append(float(v))
|
||||
except (ValueError, TypeError):
|
||||
y_values.append(0.0)
|
||||
|
||||
color = colors[i % len(colors)]
|
||||
pen = pg.mkPen(color=color, width=1)
|
||||
|
||||
curve = self._plot_widget.plot(x_axis, y_values, name=sensor_name, pen=pen)
|
||||
self._series.append(curve)
|
||||
|
||||
self._sensor_names = sensor_names
|
||||
|
||||
@Slot()
|
||||
def resetChart(self) -> None:
|
||||
"""Clear the chart."""
|
||||
if self._plot_widget:
|
||||
self._plot_widget.clear()
|
||||
self._series = []
|
||||
self._sensor_names = []
|
||||
|
||||
@Slot()
|
||||
def destroyPlotWidget(self) -> None:
|
||||
"""Destroy the plot widget."""
|
||||
if self._plot_window:
|
||||
self._plot_window.deleteLater()
|
||||
self._plot_window = None
|
||||
self._plot_widget = None
|
||||
self._series = []
|
||||
@@ -0,0 +1,104 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# ===== Settings Persistence Model =====
|
||||
class LocalSettings:
|
||||
# ===== Defaults =====
|
||||
def __init__(self):
|
||||
# Configuration settings
|
||||
self.chamber_id = ""
|
||||
self.reverse_z_wafer = False
|
||||
self.master = {} # Dict[str, str]
|
||||
self.wafer_data_size = {} # Dict[str, int]
|
||||
self.debug = False
|
||||
self.wafer_read_retries = 8
|
||||
# Timeout in msec for reading from the wafer (default 2 min)
|
||||
self.wafer_read_timeout = 120000
|
||||
self.wafer_detect_timeout = 5000
|
||||
self.split_threshold = 40.0
|
||||
|
||||
# Status persistence (operational state)
|
||||
self.connection_status = "Disconnected"
|
||||
self.selected_port = ""
|
||||
self.last_wafer_info = {} # Dict[str, Any]
|
||||
self.save_data_dir = ""
|
||||
self.activity_log = [] # List[str]
|
||||
self.data_row_count = 0
|
||||
self.data_col_count = 0
|
||||
self.last_csv_path = ""
|
||||
|
||||
# ===== File Path Helpers =====
|
||||
@classmethod
|
||||
def _settings_path(cls, directory: str) -> Path:
|
||||
return Path(directory) / "settings.json"
|
||||
|
||||
# ===== Load/Save =====
|
||||
@classmethod
|
||||
def read_settings(cls, directory: str) -> "LocalSettings":
|
||||
"""Read settings from settings.json in the provided directory."""
|
||||
settings = cls()
|
||||
path = cls._settings_path(directory)
|
||||
|
||||
if not path.exists():
|
||||
return settings
|
||||
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
for key, value in data.items():
|
||||
if hasattr(settings, key):
|
||||
setattr(settings, key, value)
|
||||
except Exception as e:
|
||||
print(f"Error reading setting file: {e}")
|
||||
|
||||
return settings
|
||||
|
||||
@classmethod
|
||||
def save_settings(cls, directory: str, settings: "LocalSettings") -> None:
|
||||
"""Save the current settings instance to settings.json."""
|
||||
path = cls._settings_path(directory)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
data = settings.__dict__.copy()
|
||||
|
||||
try:
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=4)
|
||||
except Exception as e:
|
||||
print(f"Error saving settings: {e}")
|
||||
|
||||
# ===== Master File Helpers =====
|
||||
@classmethod
|
||||
def get_master(cls, directory: str, wtype: str) -> str:
|
||||
"""Return the full path to a master file if it exists."""
|
||||
settings = cls.read_settings(directory)
|
||||
if wtype not in settings.master:
|
||||
return ""
|
||||
|
||||
file_path = Path(directory) / settings.master[wtype]
|
||||
if file_path.exists():
|
||||
return str(file_path)
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
def set_master(cls, directory: str, wtype: str, filename: str) -> None:
|
||||
"""Store a master filename for a wafer type and save settings."""
|
||||
settings = cls.read_settings(directory)
|
||||
settings.master[wtype] = os.path.basename(filename)
|
||||
cls.save_settings(directory, settings)
|
||||
|
||||
# ===== Wafer Sizing =====
|
||||
def get_wafer_data_size(self, wtype: str) -> int:
|
||||
"""Return the expected transfer size in bytes for a wafer type."""
|
||||
if not wtype:
|
||||
return 393216
|
||||
|
||||
if wtype in self.wafer_data_size:
|
||||
return self.wafer_data_size[wtype]
|
||||
|
||||
if wtype == "P":
|
||||
return 393216
|
||||
if wtype == "X":
|
||||
return 1310720
|
||||
return 393216
|
||||
@@ -0,0 +1,360 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from PySide6.QtCore import QObject, Property, QDateTime, QStandardPaths, Signal, Slot
|
||||
|
||||
from pygui.backend.local_settings import LocalSettings
|
||||
|
||||
|
||||
MASTER_FAMILIES = ("A", "B", "C", "D", "E", "F", "P", "X", "Z")
|
||||
|
||||
|
||||
class LocalSettingsModel(QObject):
|
||||
chamberIdChanged = Signal()
|
||||
reverseZWaferChanged = Signal()
|
||||
debugModeChanged = Signal()
|
||||
waferReadTimeoutChanged = Signal()
|
||||
waferDetectTimeoutChanged = Signal()
|
||||
waferRetriesChanged = Signal()
|
||||
mastersChanged = Signal()
|
||||
isDirtyChanged = Signal()
|
||||
isValidChanged = Signal()
|
||||
validationMessageChanged = Signal()
|
||||
saveStatusChanged = Signal()
|
||||
lastSavedAtChanged = Signal()
|
||||
|
||||
def __init__(self, parent: QObject | None = None) -> None:
|
||||
super().__init__(parent)
|
||||
self._data_dir = self._resolve_data_dir()
|
||||
self._defaults = self._new_defaults()
|
||||
|
||||
self._chamber_id = self._defaults["chamberId"]
|
||||
self._reverse_z_wafer = self._defaults["reverseZWafer"]
|
||||
self._debug_mode = self._defaults["debugMode"]
|
||||
self._wafer_read_timeout = self._defaults["waferReadTimeout"]
|
||||
self._wafer_detect_timeout = self._defaults["waferDetectTimeout"]
|
||||
self._wafer_retries = self._defaults["waferRetries"]
|
||||
self._masters = deepcopy(self._defaults["masters"])
|
||||
|
||||
self._is_dirty = False
|
||||
self._is_valid = True
|
||||
self._validation_message = ""
|
||||
self._save_status = "Ready"
|
||||
self._last_saved_at = ""
|
||||
self._saved_snapshot = self._snapshot()
|
||||
self._recompute_derived()
|
||||
|
||||
def _resolve_data_dir(self) -> Path:
|
||||
documents_dir = QStandardPaths.writableLocation(
|
||||
QStandardPaths.DocumentsLocation
|
||||
)
|
||||
base_dir = Path(documents_dir) if documents_dir else (Path.home() / "Documents")
|
||||
return base_dir / "isc_data"
|
||||
|
||||
def _new_defaults(self) -> dict[str, Any]:
|
||||
return {
|
||||
"chamberId": "2",
|
||||
"reverseZWafer": False,
|
||||
"debugMode": False,
|
||||
"waferReadTimeout": 120000,
|
||||
"waferDetectTimeout": 5000,
|
||||
"waferRetries": 8,
|
||||
"masters": {family: "" for family in MASTER_FAMILIES},
|
||||
}
|
||||
|
||||
def _snapshot(self) -> dict[str, Any]:
|
||||
return {
|
||||
"chamberId": self._chamber_id,
|
||||
"reverseZWafer": self._reverse_z_wafer,
|
||||
"debugMode": self._debug_mode,
|
||||
"waferReadTimeout": self._wafer_read_timeout,
|
||||
"waferDetectTimeout": self._wafer_detect_timeout,
|
||||
"waferRetries": self._wafer_retries,
|
||||
"masters": deepcopy(self._masters),
|
||||
}
|
||||
|
||||
def _set_is_dirty(self, value: bool) -> None:
|
||||
if self._is_dirty != value:
|
||||
self._is_dirty = value
|
||||
self.isDirtyChanged.emit()
|
||||
|
||||
def _set_is_valid(self, value: bool) -> None:
|
||||
if self._is_valid != value:
|
||||
self._is_valid = value
|
||||
self.isValidChanged.emit()
|
||||
|
||||
def _set_validation_message(self, value: str) -> None:
|
||||
if self._validation_message != value:
|
||||
self._validation_message = value
|
||||
self.validationMessageChanged.emit()
|
||||
|
||||
def _set_save_status(self, value: str) -> None:
|
||||
if self._save_status != value:
|
||||
self._save_status = value
|
||||
self.saveStatusChanged.emit()
|
||||
|
||||
def _set_last_saved_at(self, value: str) -> None:
|
||||
if self._last_saved_at != value:
|
||||
self._last_saved_at = value
|
||||
self.lastSavedAtChanged.emit()
|
||||
|
||||
def _validate(self) -> str:
|
||||
if not self._chamber_id.strip():
|
||||
return "Chamber ID is required."
|
||||
if self._wafer_retries < 0 or self._wafer_retries > 10:
|
||||
return "Retries must be between 0 and 10."
|
||||
if self._wafer_read_timeout < 100 or self._wafer_read_timeout > 300000:
|
||||
return "Read timeout must be between 100 and 300000 ms."
|
||||
if self._wafer_detect_timeout < 100 or self._wafer_detect_timeout > 300000:
|
||||
return "Detect timeout must be between 100 and 300000 ms."
|
||||
|
||||
for family, path_text in self._masters.items():
|
||||
cleaned = path_text.strip()
|
||||
if not cleaned:
|
||||
continue
|
||||
path = Path(cleaned)
|
||||
if path.suffix.lower() != ".csv":
|
||||
return f"Master {family} must point to a CSV file."
|
||||
if not path.exists():
|
||||
return f"Master {family} file was not found."
|
||||
return ""
|
||||
|
||||
def _recompute_derived(self) -> None:
|
||||
message = self._validate()
|
||||
self._set_is_valid(message == "")
|
||||
self._set_validation_message(message)
|
||||
self._set_is_dirty(self._snapshot() != self._saved_snapshot)
|
||||
|
||||
def _emit_all_changed(self) -> None:
|
||||
self.chamberIdChanged.emit()
|
||||
self.reverseZWaferChanged.emit()
|
||||
self.debugModeChanged.emit()
|
||||
self.waferReadTimeoutChanged.emit()
|
||||
self.waferDetectTimeoutChanged.emit()
|
||||
self.waferRetriesChanged.emit()
|
||||
self.mastersChanged.emit()
|
||||
|
||||
def _to_local_settings(self) -> LocalSettings:
|
||||
settings = LocalSettings()
|
||||
settings.chamber_id = self._chamber_id
|
||||
settings.reverse_z_wafer = self._reverse_z_wafer
|
||||
settings.debug = self._debug_mode
|
||||
settings.wafer_read_timeout = self._wafer_read_timeout
|
||||
settings.wafer_detect_timeout = self._wafer_detect_timeout
|
||||
settings.wafer_read_retries = self._wafer_retries
|
||||
settings.master = deepcopy(self._masters)
|
||||
return settings
|
||||
|
||||
@Property(str, notify=chamberIdChanged)
|
||||
def chamberId(self) -> str:
|
||||
return self._chamber_id
|
||||
|
||||
@chamberId.setter
|
||||
def chamberId(self, value: str) -> None:
|
||||
next_value = str(value).strip()
|
||||
if self._chamber_id == next_value:
|
||||
return
|
||||
self._chamber_id = next_value
|
||||
self.chamberIdChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Property(bool, notify=reverseZWaferChanged)
|
||||
def reverseZWafer(self) -> bool:
|
||||
return self._reverse_z_wafer
|
||||
|
||||
@reverseZWafer.setter
|
||||
def reverseZWafer(self, value: bool) -> None:
|
||||
if self._reverse_z_wafer == value:
|
||||
return
|
||||
self._reverse_z_wafer = bool(value)
|
||||
self.reverseZWaferChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Property(bool, notify=debugModeChanged)
|
||||
def debugMode(self) -> bool:
|
||||
return self._debug_mode
|
||||
|
||||
@debugMode.setter
|
||||
def debugMode(self, value: bool) -> None:
|
||||
if self._debug_mode == value:
|
||||
return
|
||||
self._debug_mode = bool(value)
|
||||
self.debugModeChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Property(int, notify=waferReadTimeoutChanged)
|
||||
def waferReadTimeout(self) -> int:
|
||||
return self._wafer_read_timeout
|
||||
|
||||
@waferReadTimeout.setter
|
||||
def waferReadTimeout(self, value: int) -> None:
|
||||
if self._wafer_read_timeout == value:
|
||||
return
|
||||
self._wafer_read_timeout = int(value)
|
||||
self.waferReadTimeoutChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Property(int, notify=waferDetectTimeoutChanged)
|
||||
def waferDetectTimeout(self) -> int:
|
||||
return self._wafer_detect_timeout
|
||||
|
||||
@waferDetectTimeout.setter
|
||||
def waferDetectTimeout(self, value: int) -> None:
|
||||
if self._wafer_detect_timeout == value:
|
||||
return
|
||||
self._wafer_detect_timeout = int(value)
|
||||
self.waferDetectTimeoutChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Property(int, notify=waferRetriesChanged)
|
||||
def waferRetries(self) -> int:
|
||||
return self._wafer_retries
|
||||
|
||||
@waferRetries.setter
|
||||
def waferRetries(self, value: int) -> None:
|
||||
if self._wafer_retries == value:
|
||||
return
|
||||
self._wafer_retries = int(value)
|
||||
self.waferRetriesChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Property("QVariantMap", notify=mastersChanged)
|
||||
def masters(self) -> dict[str, str]:
|
||||
return dict(self._masters)
|
||||
|
||||
@Property(bool, notify=isDirtyChanged)
|
||||
def isDirty(self) -> bool:
|
||||
return self._is_dirty
|
||||
|
||||
@Property(bool, notify=isValidChanged)
|
||||
def isValid(self) -> bool:
|
||||
return self._is_valid
|
||||
|
||||
@Property(str, notify=validationMessageChanged)
|
||||
def validationMessage(self) -> str:
|
||||
return self._validation_message
|
||||
|
||||
@Property(str, notify=saveStatusChanged)
|
||||
def saveStatus(self) -> str:
|
||||
return self._save_status
|
||||
|
||||
@Property(str, notify=lastSavedAtChanged)
|
||||
def lastSavedAt(self) -> str:
|
||||
return self._last_saved_at
|
||||
|
||||
@Property(str, notify=saveStatusChanged)
|
||||
def settingsFilePath(self) -> str:
|
||||
return str(self._data_dir / "settings.json")
|
||||
|
||||
@Slot()
|
||||
def loadSettings(self) -> None:
|
||||
loaded = LocalSettings.read_settings(str(self._data_dir))
|
||||
self._chamber_id = str(loaded.chamber_id).strip() or str(
|
||||
self._defaults["chamberId"]
|
||||
)
|
||||
self._reverse_z_wafer = bool(loaded.reverse_z_wafer)
|
||||
self._debug_mode = bool(loaded.debug)
|
||||
self._wafer_read_timeout = int(loaded.wafer_read_timeout)
|
||||
self._wafer_detect_timeout = int(loaded.wafer_detect_timeout)
|
||||
self._wafer_retries = int(loaded.wafer_read_retries)
|
||||
|
||||
masters = {family: "" for family in MASTER_FAMILIES}
|
||||
for family, value in loaded.master.items():
|
||||
normalized = str(family).strip().upper()
|
||||
if normalized in masters:
|
||||
masters[normalized] = str(value).strip()
|
||||
self._masters = masters
|
||||
|
||||
self._saved_snapshot = self._snapshot()
|
||||
self._emit_all_changed()
|
||||
self._recompute_derived()
|
||||
self._set_save_status("Loaded settings")
|
||||
|
||||
@Slot()
|
||||
def saveSettings(self) -> None:
|
||||
self._recompute_derived()
|
||||
if not self._is_valid:
|
||||
self._set_save_status("error: invalid settings")
|
||||
return
|
||||
|
||||
try:
|
||||
LocalSettings.save_settings(str(self._data_dir), self._to_local_settings())
|
||||
self._saved_snapshot = self._snapshot()
|
||||
self._recompute_derived()
|
||||
self._set_save_status("Saved")
|
||||
timestamp = QDateTime.currentDateTime().toString("yyyy-MM-dd HH:mm:ss")
|
||||
self._set_last_saved_at(timestamp)
|
||||
except Exception as exc: # pragma: no cover - defensive
|
||||
self._set_save_status(f"error: {exc}")
|
||||
|
||||
@Slot()
|
||||
def revertChanges(self) -> None:
|
||||
snap = self._saved_snapshot
|
||||
self._chamber_id = str(snap["chamberId"])
|
||||
self._reverse_z_wafer = bool(snap["reverseZWafer"])
|
||||
self._debug_mode = bool(snap["debugMode"])
|
||||
self._wafer_read_timeout = int(snap["waferReadTimeout"])
|
||||
self._wafer_detect_timeout = int(snap["waferDetectTimeout"])
|
||||
self._wafer_retries = int(snap["waferRetries"])
|
||||
self._masters = deepcopy(snap["masters"])
|
||||
|
||||
self._emit_all_changed()
|
||||
self._recompute_derived()
|
||||
self._set_save_status("Reverted unsaved changes")
|
||||
|
||||
@Slot()
|
||||
def resetDefaults(self) -> None:
|
||||
self._chamber_id = str(self._defaults["chamberId"])
|
||||
self._reverse_z_wafer = bool(self._defaults["reverseZWafer"])
|
||||
self._debug_mode = bool(self._defaults["debugMode"])
|
||||
self._wafer_read_timeout = int(self._defaults["waferReadTimeout"])
|
||||
self._wafer_detect_timeout = int(self._defaults["waferDetectTimeout"])
|
||||
self._wafer_retries = int(self._defaults["waferRetries"])
|
||||
self._masters = deepcopy(self._defaults["masters"])
|
||||
|
||||
self._emit_all_changed()
|
||||
self._recompute_derived()
|
||||
self._set_save_status("Defaults restored (not saved)")
|
||||
|
||||
@Slot(str, str)
|
||||
def setMaster(self, family: str, file_path: str) -> None:
|
||||
normalized = family.strip().upper()
|
||||
if normalized not in self._masters:
|
||||
return
|
||||
next_path = file_path.strip()
|
||||
if self._masters[normalized] == next_path:
|
||||
return
|
||||
self._masters[normalized] = next_path
|
||||
self.mastersChanged.emit()
|
||||
self._recompute_derived()
|
||||
|
||||
@Slot(str)
|
||||
def clearMaster(self, family: str) -> None:
|
||||
self.setMaster(family, "")
|
||||
|
||||
@Slot(str)
|
||||
def setChamberId(self, value: str) -> None:
|
||||
self.chamberId = value
|
||||
|
||||
@Slot(bool)
|
||||
def setReverseZWafer(self, value: bool) -> None:
|
||||
self.reverseZWafer = value
|
||||
|
||||
@Slot(bool)
|
||||
def setDebugMode(self, value: bool) -> None:
|
||||
self.debugMode = value
|
||||
|
||||
@Slot(int)
|
||||
def setWaferReadTimeout(self, value: int) -> None:
|
||||
self.waferReadTimeout = value
|
||||
|
||||
@Slot(int)
|
||||
def setWaferDetectTimeout(self, value: int) -> None:
|
||||
self.waferDetectTimeout = value
|
||||
|
||||
@Slot(int)
|
||||
def setWaferRetries(self, value: int) -> None:
|
||||
self.waferRetries = value
|
||||
@@ -0,0 +1,139 @@
|
||||
from typing import List, Tuple, Optional
|
||||
|
||||
import numpy as np
|
||||
|
||||
from pygui.backend.contour_models import ContourLine, ContourSegment
|
||||
|
||||
|
||||
# ===== Contour Generation =====
|
||||
class MarchingSquares:
|
||||
|
||||
# ===== Public API =====
|
||||
@staticmethod
|
||||
def generate_contours(grid: np.ndarray, levels: List[float]) -> List[ContourLine]:
|
||||
"""
|
||||
Generate contour lines for a 2D grid at specified levels.
|
||||
|
||||
Args:
|
||||
grid: 2D numpy array (shape: [width, height])
|
||||
levels: List of contour levels to compute
|
||||
|
||||
Returns:
|
||||
List of ContourLine objects
|
||||
"""
|
||||
if grid.size == 0:
|
||||
return []
|
||||
|
||||
width, height = grid.shape[0], grid.shape[1]
|
||||
contours = []
|
||||
|
||||
for level in levels:
|
||||
contour = ContourLine(level=level)
|
||||
|
||||
# Iterate over each cell (x, y) in the grid
|
||||
for y in range(height - 1):
|
||||
for x in range(width - 1):
|
||||
v0 = float(grid[x, y]) # top-left
|
||||
v1 = float(grid[x + 1, y]) # top-right
|
||||
v2 = float(grid[x + 1, y + 1]) # bottom-right
|
||||
v3 = float(grid[x, y + 1]) # bottom-left
|
||||
|
||||
if any(np.isnan([v0, v1, v2, v3])):
|
||||
continue # Skip cells with NaN values
|
||||
|
||||
state = (
|
||||
(1 if v0 > level else 0)
|
||||
| (2 if v1 > level else 0)
|
||||
| (4 if v2 > level else 0)
|
||||
| (8 if v3 > level else 0)
|
||||
)
|
||||
|
||||
seg = MarchingSquares._get_segment(
|
||||
x, y, v0, v1, v2, v3, level, state
|
||||
)
|
||||
if seg is not None:
|
||||
contour.segments.append(seg)
|
||||
|
||||
contours.append(contour)
|
||||
|
||||
return contours
|
||||
|
||||
# ===== Geometry Helpers =====
|
||||
@staticmethod
|
||||
def _lerp(
|
||||
x1: float,
|
||||
y1: float,
|
||||
x2: float,
|
||||
y2: float,
|
||||
val1: float,
|
||||
val2: float,
|
||||
level: float,
|
||||
) -> Tuple[float, float]:
|
||||
"""Linear interpolation between (x1,y1) and (x2,y2)."""
|
||||
if val2 == val1:
|
||||
return (x1 + x2) / 2, (y1 + y2) / 2
|
||||
t = (level - val1) / (val2 - val1)
|
||||
px = x1 + t * (x2 - x1)
|
||||
py = y1 + t * (y2 - y1)
|
||||
return px, py
|
||||
|
||||
# ===== Segment Lookup =====
|
||||
@staticmethod
|
||||
def _get_segment(
|
||||
x: int,
|
||||
y: int,
|
||||
v0: float,
|
||||
v1: float,
|
||||
v2: float,
|
||||
v3: float,
|
||||
level: float,
|
||||
state: int,
|
||||
) -> Optional[ContourSegment]:
|
||||
"""Return a ContourSegment for the given cell and state."""
|
||||
if state in (10,): # Ambiguous case — skip
|
||||
return None
|
||||
|
||||
# Map C# states to Python logic
|
||||
if state == 1 or state == 14:
|
||||
start = MarchingSquares._lerp(x, y, x + 1, y, v0, v1, level)
|
||||
end = MarchingSquares._lerp(x, y, x, y + 1, v0, v3, level)
|
||||
elif state == 2 or state == 13:
|
||||
start = MarchingSquares._lerp(x + 1, y, x + 1, y + 1, v1, v2, level)
|
||||
end = MarchingSquares._lerp(x, y, x + 1, y, v0, v1, level)
|
||||
elif state == 3 or state == 12:
|
||||
start = MarchingSquares._lerp(x, y, x, y + 1, v0, v3, level)
|
||||
end = MarchingSquares._lerp(x + 1, y, x + 1, y + 1, v1, v2, level)
|
||||
elif state == 4 or state == 11:
|
||||
start = MarchingSquares._lerp(x + 1, y, x + 1, y + 1, v1, v2, level)
|
||||
end = MarchingSquares._lerp(x + 1, y + 1, x, y + 1, v2, v3, level)
|
||||
elif state == 5:
|
||||
start = MarchingSquares._lerp(x, y, x, y + 1, v0, v3, level)
|
||||
end = MarchingSquares._lerp(x + 1, y, x + 1, y + 1, v1, v2, level)
|
||||
elif state == 6 or state == 9:
|
||||
start = MarchingSquares._lerp(x, y, x + 1, y, v0, v1, level)
|
||||
end = MarchingSquares._lerp(x + 1, y + 1, x, y + 1, v2, v3, level)
|
||||
elif state == 7 or state == 8:
|
||||
start = MarchingSquares._lerp(x, y, x, y + 1, v0, v3, level)
|
||||
end = MarchingSquares._lerp(x + 1, y + 1, x, y + 1, v2, v3, level)
|
||||
else:
|
||||
return None
|
||||
|
||||
return ContourSegment(
|
||||
start_x=start[0], start_y=start[1], end_x=end[0], end_y=end[1]
|
||||
)
|
||||
|
||||
# ===== Color Mapping =====
|
||||
@staticmethod
|
||||
def color_from_level(
|
||||
value: float, min_val: float, max_val: float
|
||||
) -> Tuple[int, int, int]:
|
||||
"""Return (R, G, B) tuple for a value between min and max."""
|
||||
range_val = max_val - min_val
|
||||
if range_val == 0:
|
||||
t = 0.5
|
||||
else:
|
||||
t = max(0.0, min(1.0, (value - min_val) / range_val))
|
||||
|
||||
r = int(255 * t)
|
||||
b = int(255 * (1 - t))
|
||||
return (r, 0, b)
|
||||
@@ -0,0 +1,28 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
# ===== Sensor Geometry =====
|
||||
@dataclass
|
||||
class Sensor:
|
||||
label: str
|
||||
x: float
|
||||
y: float
|
||||
|
||||
|
||||
# ===== Data Row =====
|
||||
@dataclass
|
||||
class DataRecord:
|
||||
time: float
|
||||
values: List[float] = field(default_factory=list)
|
||||
|
||||
|
||||
# ===== Parsed Z-Wafer Container =====
|
||||
@dataclass
|
||||
class ZWaferData:
|
||||
date: datetime = field(default_factory=lambda: datetime.min)
|
||||
serial: Optional[str] = None
|
||||
header: Dict[str, str] = field(default_factory=dict)
|
||||
sensors: List[Sensor] = field(default_factory=list)
|
||||
csv_headers: List[str] = field(default_factory=list)
|
||||
@@ -0,0 +1,131 @@
|
||||
from pathlib import Path
|
||||
from typing import Tuple, Optional
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from pygui.backend.zwafer_models import ZWaferData, Sensor
|
||||
|
||||
|
||||
# ===== Z-Wafer CSV Parser =====
|
||||
class ZWaferParser:
|
||||
"""Parses Z-wafer CSV files (header + sensor layout + data rows)."""
|
||||
|
||||
# ===== Public Parse API =====
|
||||
def parse(self, file_path: str) -> Tuple[Optional[ZWaferData], Optional[Path]]:
|
||||
"""
|
||||
Parse a Z-wafer file.
|
||||
|
||||
Returns:
|
||||
(ZWaferData, Path) on success
|
||||
(None, None) on error (e.g., file not found)
|
||||
"""
|
||||
try:
|
||||
path = Path(file_path)
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(f"File not found: {file_path}")
|
||||
|
||||
with path.open("r", encoding="utf-8") as f:
|
||||
return self._process_header(f), path
|
||||
except (ValueError, KeyError):
|
||||
raise
|
||||
except Exception:
|
||||
return None, None
|
||||
|
||||
# ===== Header Parsing =====
|
||||
def _process_header(self, file_obj) -> ZWaferData:
|
||||
"""Parse header and sensor layout from open file object."""
|
||||
wafer_data = ZWaferData()
|
||||
labels: Optional[list] = None
|
||||
x_coords: Optional[list] = None
|
||||
y_coords: Optional[list] = None
|
||||
|
||||
for line in file_obj:
|
||||
# Strip trailing comma + whitespace
|
||||
line = line.rstrip().rstrip(",").strip()
|
||||
if not line or line.replace(",", "").strip() == "":
|
||||
continue
|
||||
|
||||
parts = [p.strip() for p in line.split(",")]
|
||||
first_part = parts[0].lower()
|
||||
|
||||
# Detect end of header section
|
||||
if first_part == "data":
|
||||
wafer_data.csv_headers = labels or []
|
||||
self._build_sensor_layout(wafer_data, labels, x_coords, y_coords)
|
||||
return wafer_data
|
||||
|
||||
# Detect switch from metadata to sensor layout
|
||||
# Parse metadata (key=value pairs)
|
||||
if not self._parse_header_line(wafer_data, parts):
|
||||
# If not metadata, it's part of sensor layout
|
||||
label = parts[0].lower()
|
||||
values = parts[1:]
|
||||
|
||||
if label == "label":
|
||||
labels = values
|
||||
elif label == "x (mm)":
|
||||
x_coords = values
|
||||
elif label == "y (mm)":
|
||||
y_coords = values
|
||||
|
||||
return wafer_data # Incomplete file — return partial data
|
||||
|
||||
# ===== Metadata Parsing =====
|
||||
def _parse_header_line(self, wafer_data: ZWaferData, parts: list) -> bool:
|
||||
"""Parse key=value pairs from header line. Returns True if handled."""
|
||||
non_empty_parts = [p for p in parts if p]
|
||||
if not non_empty_parts:
|
||||
return False
|
||||
|
||||
found_kv = False
|
||||
for part in non_empty_parts:
|
||||
eq_idx = part.find("=")
|
||||
if eq_idx < 0:
|
||||
continue
|
||||
|
||||
key = part[:eq_idx].strip()
|
||||
value = part[eq_idx + 1 :].strip('=" ')
|
||||
found_kv = True
|
||||
|
||||
wafer_data.header[key] = value
|
||||
|
||||
# Extract special fields
|
||||
if key.lower() == "acquisition date":
|
||||
try:
|
||||
from datetime import datetime as dt
|
||||
|
||||
wafer_data.date = dt.strptime(value, "%m/%d/%Y")
|
||||
except ValueError:
|
||||
wafer_data.date = datetime.min # Fallback on parse error
|
||||
elif key.lower() == "wafer id":
|
||||
wafer_data.serial = value
|
||||
|
||||
if found_kv and wafer_data.date == datetime.min:
|
||||
wafer_data.date = datetime.min
|
||||
|
||||
return found_kv
|
||||
|
||||
# ===== Sensor Layout Parsing =====
|
||||
def _build_sensor_layout(
|
||||
self,
|
||||
wafer_data: ZWaferData,
|
||||
labels: Optional[list],
|
||||
x_coords: Optional[list],
|
||||
y_coords: Optional[list],
|
||||
) -> None:
|
||||
"""Build sensor list from layout arrays."""
|
||||
if not all([labels, x_coords, y_coords]):
|
||||
raise ValueError("Sensor layout section is incomplete or missing.")
|
||||
|
||||
if len(labels) != len(x_coords) or len(labels) != len(y_coords):
|
||||
raise ValueError(
|
||||
f"Mismatched sensor columns: labels={len(labels)}, "
|
||||
f"x={len(x_coords)}, y={len(y_coords)}"
|
||||
)
|
||||
|
||||
for i in range(len(labels)):
|
||||
try:
|
||||
wafer_data.sensors.append(
|
||||
Sensor(label=labels[i], x=float(x_coords[i]), y=float(y_coords[i]))
|
||||
)
|
||||
except ValueError as e:
|
||||
raise ValueError(f"Invalid coordinate at index {i}: {e}")
|
||||
@@ -0,0 +1,6 @@
|
||||
"""Serial port communication layer for the temperature-sensing wafer."""
|
||||
|
||||
from pygui.serialcomm.device_service import DeviceService
|
||||
from pygui.serialcomm.serial_port import SerialPort, WaferInfo
|
||||
|
||||
__all__ = ["DeviceService", "SerialPort", "WaferInfo"]
|
||||
@@ -0,0 +1,292 @@
|
||||
"""Binary data parser and temperature converter for wafer data.
|
||||
|
||||
Mirrors the C# Form1.cs binary parsing pipeline:
|
||||
1. Read raw bytes → strip per-block overhead → 1D hex array
|
||||
2. Chunk 1D array into rows × sensors → List[List[str]] (hex values)
|
||||
3. Convert hex values → float temperatures (family-dependent)
|
||||
4. Remove trailing zero rows
|
||||
5. Save to CSV with Sensor1, Sensor2, ... headers
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Max DUTs (sensors) per row before overhead bytes are stripped
|
||||
# P wafer: 244 valid readings per 256-block (12 overhead bytes)
|
||||
# X wafer: 80 valid readings per 256-block (14 overhead bytes)
|
||||
MAXDUT_P = 244
|
||||
MAXDUT_X = 80
|
||||
|
||||
|
||||
def csv_column_count(family_code: str) -> int:
|
||||
"""Return the number of columns to display for a family code."""
|
||||
mapping = {
|
||||
"A": 48,
|
||||
"E": 48,
|
||||
"P": 48,
|
||||
"B": 29,
|
||||
"C": 29,
|
||||
"D": 29,
|
||||
"F": 22,
|
||||
"X": 80,
|
||||
}
|
||||
return mapping.get(family_code, 0)
|
||||
|
||||
|
||||
def _hex_to_binary(hex_str: str) -> list[int]:
|
||||
"""Convert a 4-char hex string to a 16-bit binary list (MSB first)."""
|
||||
value = int(hex_str, 16)
|
||||
return [(value >> (15 - i)) & 1 for i in range(16)]
|
||||
|
||||
|
||||
def _twos_complement_excluding_msb(bits: list[int]) -> list[int]:
|
||||
"""Invert bits 1-15, add 1 (2's complement excluding MSB)."""
|
||||
bits = list(bits) # copy
|
||||
for i in range(1, len(bits)):
|
||||
bits[i] = 1 - bits[i]
|
||||
carry = 1
|
||||
for i in range(len(bits) - 1, 0, -1):
|
||||
s = bits[i] + carry
|
||||
bits[i] = s % 2
|
||||
carry = s // 2
|
||||
return bits
|
||||
|
||||
|
||||
def _binary_subsequence_to_int(bits: list[int], start: int, end: int) -> int:
|
||||
"""Convert bits[start:end+1] to integer."""
|
||||
result = 0
|
||||
for i in range(start, end + 1):
|
||||
result = (result << 1) | bits[i]
|
||||
return result
|
||||
|
||||
|
||||
def _binary_fraction_to_double(bits: list[int], start: int) -> float:
|
||||
"""Convert bits[start:] to a fractional value (0 < frac < 1)."""
|
||||
result = 0.0
|
||||
divisor = 2.0
|
||||
for i in range(start, len(bits)):
|
||||
result += bits[i] / divisor
|
||||
divisor *= 2.0
|
||||
return result
|
||||
|
||||
|
||||
def _convert_standard(binary_bits: list[int]) -> float:
|
||||
"""Convert 16-bit binary using standard formula (B/C/D/F families).
|
||||
|
||||
Bit layout:
|
||||
bit 0 : sign
|
||||
bits 1-11 : integer part (11 bits, 2^10 .. 2^0)
|
||||
bits 12-13 : fractional part (2 bits, 2^-1, 2^-2)
|
||||
bits 14-15 : unused
|
||||
"""
|
||||
value = 0.0
|
||||
for i in range(1, 14):
|
||||
if binary_bits[i]:
|
||||
value += 2.0 ** (11 - i)
|
||||
if binary_bits[0]:
|
||||
value = -value
|
||||
return value
|
||||
|
||||
|
||||
def _convert_aep(binary_bits: list[int]) -> float:
|
||||
"""Convert 16-bit binary using AEP formula.
|
||||
|
||||
Bit layout:
|
||||
bit 0 : sign
|
||||
bits 1-8 : integer part (8 bits)
|
||||
bits 9-15 : fractional part (7 bits)
|
||||
"""
|
||||
bits = binary_bits
|
||||
if bits[0] == 1:
|
||||
bits = _twos_complement_excluding_msb(bits)
|
||||
integer_part = _binary_subsequence_to_int(bits, 1, 8)
|
||||
fractional_part = _binary_fraction_to_double(bits, 9)
|
||||
result = integer_part + fractional_part
|
||||
if binary_bits[0] == 1:
|
||||
result = -result
|
||||
return result
|
||||
|
||||
|
||||
def _convert_hex_to_temp(hex_str: str, family_code: str) -> float:
|
||||
"""Convert a singi hale 4-char hex string to a float temperature."""
|
||||
bits = _hex_to_binary(hex_str)
|
||||
if family_code in ("A", "E", "P"):
|
||||
result = _convert_aep(bits)
|
||||
elif family_code in ("B", "C", "D"):
|
||||
result = _convert_standard(bits)
|
||||
if result < -2000:
|
||||
result = 0.0
|
||||
elif family_code == "X":
|
||||
# X wafer uses AEP-style conversion
|
||||
result = _convert_aep(bits)
|
||||
else:
|
||||
# Unknown family — try standard
|
||||
result = _convert_standard(bits)
|
||||
return round(result, 2)
|
||||
|
||||
|
||||
def parse_binary_data(data_bytes: bytes, family_code: str) -> Optional[list[list[str]]]:
|
||||
"""Parse raw wafer bytes into a 2D array of hex strings.
|
||||
|
||||
Strips per-block overhead bytes (12 for P, 14 for X) and chunks
|
||||
the remaining readings into rows.
|
||||
|
||||
Args:
|
||||
data_bytes: Raw binary data from the wafer.
|
||||
family_code: Wafer family code ("P", "X", "A", "B", "C", "D", "F").
|
||||
|
||||
Returns:
|
||||
List of rows, each row is a list of 4-char hex strings.
|
||||
Returns None on failure.
|
||||
"""
|
||||
try:
|
||||
if family_code == "X":
|
||||
return _parse_x_binary(data_bytes)
|
||||
else:
|
||||
return _parse_p_binary(data_bytes)
|
||||
except Exception as exc:
|
||||
log.error("Binary parse failed: %s", exc)
|
||||
return None
|
||||
|
||||
|
||||
def _parse_p_binary(data_bytes: bytes) -> list[list[str]]:
|
||||
"""Parse P-family (and A/B/C/D/E/F) binary data.
|
||||
|
||||
Each block of 256 readings has 244 valid + 12 overhead.
|
||||
Valid readings are chunked into rows of MAXDUT_P (244).
|
||||
"""
|
||||
readings: list[str] = []
|
||||
|
||||
# Read 2 bytes at a time (UInt16 little-endian)
|
||||
# Each 256-word block has MAXDUT_P valid readings (first N words), rest are overhead
|
||||
num_words = len(data_bytes) // 2
|
||||
for i in range(num_words):
|
||||
value = int.from_bytes(data_bytes[i * 2 : i * 2 + 2], byteorder="little")
|
||||
if i % 256 < MAXDUT_P:
|
||||
readings.append(f"{value:04X}")
|
||||
|
||||
# Chunk into rows of MAXDUT_P
|
||||
result: list[list[str]] = []
|
||||
idx = 0
|
||||
while idx + MAXDUT_P <= len(readings):
|
||||
result.append(readings[idx : idx + MAXDUT_P])
|
||||
idx += MAXDUT_P
|
||||
|
||||
log.info("Parsed P-family: %d rows × %d cols", len(result), MAXDUT_P)
|
||||
return result
|
||||
|
||||
|
||||
def _parse_x_binary(data_bytes: bytes) -> list[list[str]]:
|
||||
"""Parse X-family binary data.
|
||||
|
||||
Each block of 256 readings has 80 valid + 14 overhead.
|
||||
Valid readings are chunked into rows of MAXDUT_X (80).
|
||||
"""
|
||||
readings: list[str] = []
|
||||
|
||||
num_words = len(data_bytes) // 2
|
||||
for i in range(num_words):
|
||||
value = int.from_bytes(data_bytes[i * 2 : i * 2 + 2], byteorder="little")
|
||||
if i % 256 < MAXDUT_X:
|
||||
readings.append(f"{value:04X}")
|
||||
|
||||
# Chunk into rows of MAXDUT_X
|
||||
result: list[list[str]] = []
|
||||
idx = 0
|
||||
while idx + MAXDUT_X <= len(readings):
|
||||
result.append(readings[idx : idx + MAXDUT_X])
|
||||
idx += MAXDUT_X
|
||||
|
||||
log.info("Parsed X-family: %d rows × %d cols", len(result), MAXDUT_X)
|
||||
return result
|
||||
|
||||
|
||||
def convert_to_temperatures(
|
||||
hex_data: list[list[str]], family_code: str
|
||||
) -> list[list[str]]:
|
||||
"""Convert hex string values to temperature strings.
|
||||
|
||||
Args:
|
||||
hex_data: 2D array of 4-char hex strings from parse_binary_data.
|
||||
family_code: Wafer family code.
|
||||
|
||||
Returns:
|
||||
Same structure with temperature strings (e.g. "25.37").
|
||||
"""
|
||||
temp_value: list[list[str]] = []
|
||||
for row in hex_data:
|
||||
temp_row: list[str] = []
|
||||
for hex_val in row:
|
||||
temp = _convert_hex_to_temp(hex_val, family_code)
|
||||
temp_row.append(str(temp))
|
||||
temp_value.append(temp_row)
|
||||
return temp_value
|
||||
|
||||
|
||||
def remove_trailing_zeros(data: list[list[str]]) -> None:
|
||||
"""Remove rows of all-zero values from the end of data (in-place).
|
||||
|
||||
A value is considered zero if its float representation is < 0.01.
|
||||
"""
|
||||
while data:
|
||||
last_row = data[-1]
|
||||
is_all_zero = True
|
||||
for val in last_row:
|
||||
try:
|
||||
fval = float(val)
|
||||
except ValueError:
|
||||
fval = 99.9
|
||||
if fval >= 0.01:
|
||||
is_all_zero = False
|
||||
break
|
||||
if is_all_zero:
|
||||
data.pop()
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
def save_to_csv(
|
||||
data: list[list[str]],
|
||||
family_code: str,
|
||||
serial_number: str,
|
||||
output_dir: str,
|
||||
) -> Optional[str]:
|
||||
"""Save parsed temperature data to a CSV file.
|
||||
|
||||
Args:
|
||||
data: 2D array of temperature strings.
|
||||
family_code: Wafer family code.
|
||||
serial_number: Wafer serial number (e.g. "P00001").
|
||||
output_dir: Directory to save the CSV file.
|
||||
|
||||
Returns:
|
||||
Full file path on success, None on failure.
|
||||
"""
|
||||
try:
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
# Build filename: P00001-20260505_133045.csv
|
||||
from datetime import datetime
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"{serial_number}-{timestamp}.csv"
|
||||
filepath = os.path.join(output_dir, filename)
|
||||
|
||||
num_cols = csv_column_count(family_code)
|
||||
with open(filepath, "w", encoding="utf-8") as f:
|
||||
# Header row: Sensor1,Sensor2,...
|
||||
headers = [f"Sensor{j + 1}" for j in range(num_cols)]
|
||||
f.write(",".join(headers) + "\n")
|
||||
# Data rows
|
||||
for row in data:
|
||||
# Pad or trim to match header count
|
||||
padded = row[:num_cols] + ["0"] * max(0, num_cols - len(row))
|
||||
f.write(",".join(padded) + "\n")
|
||||
|
||||
log.info("Saved %d rows × %d cols to %s", len(data), num_cols, filepath)
|
||||
return filepath
|
||||
except Exception as exc:
|
||||
log.error("CSV save failed: %s", exc)
|
||||
return None
|
||||
@@ -0,0 +1,152 @@
|
||||
"""High-level wafer device communication service.
|
||||
|
||||
Coordinates port enumeration, detect, read, and erase operations
|
||||
using settings from LocalSettingsModel. Designed to be called from
|
||||
QML via @Slot methods on a QObject controller.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import serial.tools.list_ports
|
||||
|
||||
from pygui.backend.local_settings import LocalSettings
|
||||
from pygui.serialcomm.serial_port import SerialPort, WaferInfo
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DeviceService:
|
||||
"""High-level wafer device communication."""
|
||||
|
||||
# Expected hex string lengths for known wafer families
|
||||
# P wafer: 393216 hex chars (196608 bytes)
|
||||
# X wafer: 1310720 hex chars (655360 bytes)
|
||||
EXPECTED_HEX_LENGTHS = {
|
||||
"P": 393216,
|
||||
"X": 1310720,
|
||||
}
|
||||
|
||||
def __init__(self, settings: LocalSettings) -> None:
|
||||
self._settings = settings
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Public API
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def enumerate_ports(self) -> list[str]:
|
||||
"""Return list of available serial port names."""
|
||||
return [p.device for p in serial.tools.list_ports.comports()]
|
||||
|
||||
def detect_wafer(self, port: str) -> Optional[WaferInfo]:
|
||||
"""Try to detect a wafer on the given port.
|
||||
|
||||
Opens the port, sends s0 command, parses response.
|
||||
Returns WaferInfo if detected, None otherwise.
|
||||
"""
|
||||
try:
|
||||
sp = SerialPort(port)
|
||||
info = sp.detect(self._settings.wafer_detect_timeout)
|
||||
return info
|
||||
except Exception as exc:
|
||||
log.error("Detect failed on %s: %s", port, exc)
|
||||
return None
|
||||
|
||||
SCAN_PER_PORT_TIMEOUT_MS = 1500
|
||||
"""Per-port timeout while scanning. Wafer responds fast or not at all."""
|
||||
|
||||
def detect_all_ports(self) -> tuple[Optional[str], Optional[WaferInfo]]:
|
||||
"""Scan every available serial port until one responds to s0.
|
||||
|
||||
Mirrors C# chkConnection(): enumerates all ports, tries each one,
|
||||
returns the first that responds with a valid 1024-byte payload.
|
||||
|
||||
Uses a short per-port timeout (SCAN_PER_PORT_TIMEOUT_MS) so a
|
||||
machine with many serial devices (Bluetooth, debug consoles, etc.)
|
||||
doesn't take 30+ seconds to scan. The configured wafer_detect_timeout
|
||||
is reserved for confirmed-port operations.
|
||||
|
||||
Returns:
|
||||
(port_name, WaferInfo) on success, (None, None) if nothing found.
|
||||
"""
|
||||
ports = self.enumerate_ports()
|
||||
log.info("Scanning %d port(s) for wafer ...", len(ports))
|
||||
for port in ports:
|
||||
log.info(" trying %s ...", port)
|
||||
try:
|
||||
sp = SerialPort(port)
|
||||
info = sp.detect(self.SCAN_PER_PORT_TIMEOUT_MS)
|
||||
except Exception as exc:
|
||||
log.warning(" %s: %s", port, exc)
|
||||
info = None
|
||||
if info is not None:
|
||||
log.info(" found wafer on %s (family=%s)", port, info.family_code)
|
||||
return port, info
|
||||
log.info(" no wafer detected on any port")
|
||||
return None, None
|
||||
|
||||
def read_wafer_data(
|
||||
self,
|
||||
port: str,
|
||||
family_code: str = "",
|
||||
cmd: str = "D1",
|
||||
timeout_ms: int | None = None,
|
||||
retries: int | None = None,
|
||||
) -> Optional[bytes]:
|
||||
"""Full read workflow: open → send command → retry → close.
|
||||
|
||||
Args:
|
||||
port: Serial port name (e.g. "/dev/ttyUSB0").
|
||||
family_code: Wafer family ("P", "X", etc.) for size validation.
|
||||
cmd: Command string ("D1" for sensor data, "F1" for debug data).
|
||||
timeout_ms: Read timeout in ms (uses settings default if None).
|
||||
retries: Max retries on bad read (uses settings default if None).
|
||||
|
||||
Returns:
|
||||
Raw bytes from the wafer, or None on failure.
|
||||
"""
|
||||
if timeout_ms is None:
|
||||
timeout_ms = self._settings.wafer_read_timeout
|
||||
if retries is None:
|
||||
retries = self._settings.wafer_read_retries
|
||||
|
||||
# Determine expected hex string length.
|
||||
# get_wafer_data_size returns BYTES; hex string is 2× that.
|
||||
if family_code:
|
||||
expected_hex_len = self._settings.get_wafer_data_size(family_code) * 2
|
||||
else:
|
||||
expected_hex_len = 0 # no validation
|
||||
|
||||
try:
|
||||
sp = SerialPort(port)
|
||||
data = sp.read_memory(
|
||||
cmd=cmd,
|
||||
expected_hex_len=expected_hex_len,
|
||||
timeout_ms=timeout_ms,
|
||||
retries=retries,
|
||||
)
|
||||
if data is not None:
|
||||
log.info("Read %d bytes from wafer on %s", len(data), port)
|
||||
else:
|
||||
log.warning("Read returned no data from %s", port)
|
||||
return data
|
||||
except Exception as exc:
|
||||
log.error("Read failed on %s: %s", port, exc)
|
||||
return None
|
||||
|
||||
def erase_wafer(self, port: str) -> bool:
|
||||
"""Send p1 erase command. Wafer takes ~15s to erase.
|
||||
|
||||
Returns True if command was sent successfully.
|
||||
"""
|
||||
try:
|
||||
sp = SerialPort(port)
|
||||
return sp.erase_memory()
|
||||
except Exception as exc:
|
||||
log.error("Erase failed on %s: %s", port, exc)
|
||||
return False
|
||||
|
||||
def get_expected_hex_length(self, family_code: str) -> int:
|
||||
"""Return expected hex string length for a wafer family."""
|
||||
return self._settings.get_wafer_data_size(family_code)
|
||||
@@ -0,0 +1,203 @@
|
||||
"""Low-level serial port communication with the wafer device.
|
||||
|
||||
Mirrors the C# Form1.cs wafer protocol:
|
||||
- Detect : s0 + 510×'F' → 1024 hex chars → WaferInfo
|
||||
- ReadMemory: D1 + 510×'F' → N hex chars (retry on bad length)
|
||||
- Erase : p1 + 510×'F' → no response (~15s blocking)
|
||||
|
||||
All commands are 512-char strings: 2-char command + 510×'F' padding.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterator, Optional
|
||||
|
||||
import serial as pyserial
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class WaferInfo:
|
||||
"""Parsed wafer identification from the s0 detect response."""
|
||||
|
||||
family_code: str # "P", "X", "A", "B", "C", "D", "E", "F"
|
||||
serial_number: str # e.g. "P00001"
|
||||
sensor_count: int
|
||||
mfg_date_hex: str # raw 8 hex chars
|
||||
sensor_assigned_hex: str # raw 2 hex chars
|
||||
locked_hex: str # raw 2 hex chars
|
||||
runtime: int # seconds
|
||||
cycle_count: int
|
||||
|
||||
|
||||
class SerialPort:
|
||||
"""Low-level serial transport for the temperature-sensing wafer.
|
||||
|
||||
The serial port is opened per-operation (matching C# behaviour)
|
||||
rather than kept open persistently.
|
||||
"""
|
||||
|
||||
BAUDRATE = 888888
|
||||
COMMAND_PAD = "F" * 510 # pad to 512 chars total
|
||||
|
||||
def __init__(self, port_name: str) -> None:
|
||||
self._port_name = port_name
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Context manager for scoped port access
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
@contextmanager
|
||||
def _open(self, timeout: float | None = None) -> Iterator[pyserial.Serial]:
|
||||
"""Open the serial port, yield it, then close on exit."""
|
||||
port = pyserial.Serial(
|
||||
self._port_name,
|
||||
self.BAUDRATE,
|
||||
parity=pyserial.PARITY_NONE,
|
||||
bytesize=8,
|
||||
stopbits=pyserial.STOPBITS_ONE,
|
||||
xonxoff=False,
|
||||
rtscts=False,
|
||||
dsrdtr=False,
|
||||
)
|
||||
if timeout is not None:
|
||||
port.timeout = timeout
|
||||
try:
|
||||
yield port
|
||||
finally:
|
||||
port.close()
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Public API
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def detect(self, timeout_ms: int = 5000) -> Optional[WaferInfo]:
|
||||
"""Try s0 command. Returns WaferInfo if response is 1024 hex chars."""
|
||||
with self._open(timeout_ms / 1000) as port:
|
||||
port.write(("s0" + self.COMMAND_PAD).encode())
|
||||
response = port.readline().decode().strip()
|
||||
if len(response) == 1024:
|
||||
return self._parse_wafer_info(response)
|
||||
return None
|
||||
|
||||
def read_memory(
|
||||
self,
|
||||
cmd: str = "D1",
|
||||
expected_hex_len: int = 393216,
|
||||
timeout_ms: int = 120000,
|
||||
retries: int = 8,
|
||||
) -> Optional[bytes]:
|
||||
"""Send read command, retry on bad length or odd-length string.
|
||||
|
||||
The wafer sends hex-encoded bytes. A dropped byte corrupts the
|
||||
alignment, so the entire buffer must be retried.
|
||||
|
||||
Returns raw bytes on success, None on failure.
|
||||
"""
|
||||
with self._open(timeout_ms / 1000) as port:
|
||||
port.write((cmd + self.COMMAND_PAD).encode())
|
||||
|
||||
for attempt in range(retries, 0, -1):
|
||||
data_string = port.readline().decode(errors="ignore").strip()
|
||||
|
||||
# Trim trailing odd nibble — wafer sometimes drops 1 char
|
||||
# at the tail; one stray nibble is harmless, the rest is good.
|
||||
if len(data_string) % 2 != 0:
|
||||
data_string = data_string[:-1]
|
||||
|
||||
# Defensively cap at expected length if the wafer over-sends.
|
||||
if expected_hex_len > 0 and len(data_string) > expected_hex_len:
|
||||
data_string = data_string[:expected_hex_len]
|
||||
|
||||
# Accept if we got at least 99% of expected (matches C# tolerance).
|
||||
threshold = int(expected_hex_len * 0.99) if expected_hex_len > 0 else 1
|
||||
if len(data_string) >= threshold:
|
||||
break
|
||||
|
||||
log.warning(
|
||||
"Short hex string (len=%d, expected~%d), retries left=%d",
|
||||
len(data_string),
|
||||
expected_hex_len,
|
||||
attempt - 1,
|
||||
)
|
||||
|
||||
if attempt > 1:
|
||||
port.reset_input_buffer()
|
||||
# Re-issue the command — reset_input_buffer alone leaves
|
||||
# the wafer waiting for nothing.
|
||||
port.write((cmd + self.COMMAND_PAD).encode())
|
||||
continue
|
||||
|
||||
log.error("Retries exhausted after bad read")
|
||||
return None
|
||||
|
||||
try:
|
||||
return bytes.fromhex(data_string)
|
||||
except ValueError as exc:
|
||||
log.error("Failed to decode hex: %s", exc)
|
||||
return None
|
||||
|
||||
def erase_memory(self, timeout_ms: int = 5000) -> bool:
|
||||
"""Send p1 erase command. Wafer takes ~15s to erase.
|
||||
|
||||
Returns True if command was sent successfully.
|
||||
"""
|
||||
with self._open(timeout_ms / 1000) as port:
|
||||
try:
|
||||
port.write(("p1" + self.COMMAND_PAD).encode())
|
||||
return True
|
||||
except Exception as exc:
|
||||
log.error("Erase command failed: %s", exc)
|
||||
return False
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _hex_to_ascii(hex_str: str) -> str:
|
||||
"""Convert hex string (e.g. '50') to ASCII char ('P')."""
|
||||
hex_str = hex_str.replace(" ", "")
|
||||
result = []
|
||||
for i in range(0, len(hex_str), 2):
|
||||
hex_char = hex_str[i : i + 2]
|
||||
if len(hex_char) == 2:
|
||||
result.append(chr(int(hex_char, 16)))
|
||||
return "".join(result)
|
||||
|
||||
def _parse_wafer_info(self, hex_response: str) -> WaferInfo:
|
||||
"""Parse the 1024-char hex response into WaferInfo.
|
||||
|
||||
Layout (from C# checkPort):
|
||||
bytes 0-1 : FamilyCode (2 hex chars → ASCII)
|
||||
bytes 2-5 : Serial number (3 bytes hex → decimal, 5 digits)
|
||||
bytes 6-7 : Sensor count (1 byte hex)
|
||||
bytes 8-15 : Mfg date (4 bytes hex)
|
||||
bytes 16-17 : Sensor assigned (1 byte hex)
|
||||
bytes 18-19 : Locked (1 byte hex)
|
||||
bytes 1016-1019 : Runtime (2 bytes hex → decimal seconds)
|
||||
bytes 1020-1023 : Cycle time (2 bytes hex → decimal cycles)
|
||||
"""
|
||||
raw = bytes.fromhex(hex_response)
|
||||
|
||||
family_code = self._hex_to_ascii(hex_response[0:2])
|
||||
serial_num = f"{family_code}{int(hex_response[2:6], 16):05d}"
|
||||
sensor_count = int(hex_response[6:8], 16)
|
||||
mfg_date_hex = hex_response[8:16]
|
||||
sensor_assigned_hex = hex_response[16:18]
|
||||
locked_hex = hex_response[18:20]
|
||||
runtime = int(hex_response[1016:1020], 16)
|
||||
cycle_count = int(hex_response[1020:1024], 16)
|
||||
|
||||
return WaferInfo(
|
||||
family_code=family_code,
|
||||
serial_number=serial_num,
|
||||
sensor_count=sensor_count,
|
||||
mfg_date_hex=mfg_date_hex,
|
||||
sensor_assigned_hex=sensor_assigned_hex,
|
||||
locked_hex=locked_hex,
|
||||
runtime=runtime,
|
||||
cycle_count=cycle_count,
|
||||
)
|
||||
Reference in New Issue
Block a user