Files
MXSC/src/main/java/ch/psi/mxsc/Puck.java
2025-08-20 15:34:13 +02:00

482 lines
14 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.device.DeviceConfig;
import ch.psi.pshell.imaging.DimensionDouble;
import ch.psi.pshell.imaging.PointDouble;
import ch.psi.pshell.swing.DevicePanel;
import java.io.IOException;
import java.util.ArrayList;
/**
*
*/
public class Puck extends DeviceBase {
final static int signalX = -1;
final static PointDouble[] SAMPLES_POSITION_NORMAL = new PointDouble[]{
new PointDouble(0 * signalX, 24.24),
new PointDouble(23.05360995 * signalX, 7.490571944),
new PointDouble(14.24791452* signalX, -19.61057194),
new PointDouble(-14.24791452 * signalX, -19.61057194),
new PointDouble(-23.05360995 * signalX, 7.490571944),
new PointDouble(0 * signalX, 52.52),
new PointDouble(28.39445573 * signalX, 44.18263554),
new PointDouble(47.7738724 * signalX, 21.81759648),
new PointDouble(51.98542213 * signalX, -7.474375306),
new PointDouble(39.69196765 * signalX, -34.39328575),
new PointDouble(14.79659389 * signalX, -50.39257097),
new PointDouble(-14.79659389 * signalX, -50.39257097),
new PointDouble(-39.69196765 * signalX, -34.39328575),
new PointDouble(-51.98542213 * signalX, -7.474375306),
new PointDouble(-47.7738724 * signalX, 21.81759648),
new PointDouble(-28.39445573 * signalX, 44.18263554)
};
final static PointDouble[] SAMPLES_POSITION_RT = new PointDouble[]{
new PointDouble(-30.0,-40.0),
new PointDouble(-2.0, -30.0),
new PointDouble(25.0, -40.0),
new PointDouble(45.0, -15.0),
new PointDouble(-30.0,0.0),
new PointDouble(-30.0, 40.0),
new PointDouble(-2.0, 30.0),
new PointDouble(25.0, 40.0),
new PointDouble(45.0, 15.0)
};
final static PointDouble[] samplesPosition = (MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.rt) ?
SAMPLES_POSITION_RT : SAMPLES_POSITION_NORMAL;
static final Character[] SEGMENTS_NORMAL =new Character[]{
'A', 'A','A', 'A', 'A',
'B', 'B', 'B', 'B', 'B',
'C', 'C', 'C', 'C', 'C',
'D', 'D', 'D', 'D', 'D',
'E', 'E', 'E', 'E', 'E',
'F', 'F', 'F', 'F', 'F',
};
static final Character[] SEGMENTS_SF = new Character[]{
'A', 'A',
'F', 'F', 'F', 'F', 'F',
'E', 'E', 'E', 'E', 'E',
'D', 'D', 'D', 'D', 'D',
'C', 'C', 'C', 'C', 'C',
'B', 'B', 'B', 'B', 'B',
'A', 'A', 'A'} ;
static final Character[] SEGMENTS_6D =new Character[]{
'A', 'A','A', 'A', 'A',
'B', 'B', 'B', 'B', 'B',
'C', 'C', 'C', 'C', 'C',
'D', 'D', 'D', 'D', 'D',
'E', 'E', 'E', 'E', 'E',
'F', 'F', 'F', 'F', 'F',
};
static final Character[] SEGMENTS_RT =new Character[]{
'A', 'B','C', 'D', 'E', 'F'
};
static final int[] NUMBERS_NORMAL = new int[]{
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
};
static final int[] NUMBERS_SF = new int[]
{1, 2,
5, 4, 3, 1, 2,
5, 4, 3, 1, 2,
5, 4, 3, 1, 2,
5, 4, 3, 1, 2,
5, 4, 3, 1, 2,
5, 4, 3};
static final int[] NUMBERS_6D = new int[]{
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
};
static final int[] NUMBERS_RT = new int[]{
1, 1, 1, 1, 1, 1
};
static final int[] ADDRESSES_NORMAL = new int[]{
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25,
26, 27, 28, 29, 30
};
static final int[] ADDRESSES_SF = new int[]
{6, 7,
5, 4, 3, 1, 2,
20, 19, 18, 16, 17,
15, 14, 13, 11, 12,
30, 29, 28, 26, 27,
25, 24, 23, 21, 22,
10, 9, 8};
static final int[] ADDRESSES_6D = new int[]{
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
21, 22, 23, 24, 25,
26, 27, 28, 29, 30,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
};
static final double[] ANGLES_NORMAL = new double[]{
0.00, 0.00, 330.00, 340.89, 319.11,
300.00, 300.00, 270.00, 280.89, 259.11,
240.00, 240.00, 210.00, 220.89, 199.11,
180.00, 180.00, 150.00, 160.89, 139.11,
120.00, 120.00, 90.00, 100.89, 79.11,
60.00, 60.00, 30.00, 40.89, 19.11
};
static final double[] ANGLES_SF = new double[]{
0.00, 0.00, 19.11, 40.89, 30.00,
60.00, 60.00, 79.11, 100.89, 90.00,
120.00, 120.00, 139.11, 160.89, 150.00,
180.00, 180.00, 199.11, 220.89, 210.00,
240.00, 240.00, 259.11, 280.89, 270.00,
300.00, 300.00, 319.11, 340.89, 330.00
};
static final double[] ANGLES_6D = new double[]{
180.00, 180.00, 150.00, 160.89, 139.11,
120.00, 120.00, 90.00, 100.89, 79.11,
60.00, 60.00, 30.00, 40.89, 19.11,
0.00, 0.00, 330.00, 340.89, 319.11,
300.00, 300.00, 270.00, 280.89, 259.11,
240.00, 240.00, 210.00, 220.89, 199.11,
};
static final double[] ANGLES_RT = new double[]{
0.00, 0.00, 0.00, 0.00, 0.00, 0.00
};
static final Character[] SEGMENTS = (MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.normal) ? SEGMENTS_NORMAL : (
(MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.sf) ? SEGMENTS_SF : (
(MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.rt) ? SEGMENTS_RT : SEGMENTS_NORMAL
)
);
static final int[] NUMBERS = (MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.normal) ? NUMBERS_NORMAL : (
(MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.sf) ? NUMBERS_SF :
((MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.rt) ? NUMBERS_RT : NUMBERS_NORMAL)
);
static final int[] ADDRESSES =(MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.normal) ? ADDRESSES_NORMAL : (
(MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.sf) ? ADDRESSES_SF : ADDRESSES_6D
);
static final double[] ANGLES = (MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.normal) ? ANGLES_NORMAL : (
(MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.sf) ? ANGLES_SF :
((MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.rt) ? ANGLES_RT : ANGLES_NORMAL)
);
final static PointDouble referencePosition = new PointDouble(0.0, -66.9 - 0.2);
final static Double referenceSize = 6.2 + 0.2;
final static PointDouble ledUnipuckPosition = new PointDouble(0.0, 0.0);
final static PointDouble ledMinispinePosition = new PointDouble(0.0, -36.0);
final static Double unipuckLedSize = 10.0;
final static Double minispineLedSize = 8.0;
PuckType puckType = PuckType.Unknown;
public PuckType getPuckType(){
if (MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.rt){
return PuckType.RT;
}
return puckType;
}
public void setPuckType(PuckType value){
puckType = value;
}
public enum PuckType{
Empty,
Minispine,
Unipuck,
RT,
Error,
Unknown;
public boolean isDefined(){
return (this==Minispine) || (this==Unipuck)|| (this==RT);
}
}
public String getStatus(){
Detection detection = getDetection();
PuckType puckType = getPuckType();
if ((detection==Detection.Present) && puckType.isDefined()) {
return puckType.toString();
}
return detection.toString();
}
public static boolean displayDetectionError;
public static void setDisplayDetectionError(boolean value){
displayDetectionError = value;
}
public static boolean getDisplayDetectionError(){
return displayDetectionError;
}
public double getAngle() {
return ANGLES[index];
}
final int numberOfSamples = samplesPosition.length;
final int index;
Character segment;
final int address;
final int number;
final double angle;
public static enum PuckDetectionMode{
Inductive,
Mechanical,
Both
}
public static class PuckConfig extends DeviceConfig{
public boolean disabled;
public PuckDetectionMode detection = PuckDetectionMode.Both;
}
Puck(BasePlate basePlate, int index) {
//super(String.valueOf(index+1));
super(SEGMENTS[index] + "" + NUMBERS[index], new PuckConfig());
this.setParent(basePlate);
this.index = index;
this.segment = SEGMENTS[index];
this.number = NUMBERS[index];
this.address = ADDRESSES[index];
this.angle = getAngle();
for (int i = 0; i < numberOfSamples; i++) {
new Sample(this, i);
}
}
Puck(BasePlate basePlate, int index, Character segment, int number, int address, int angle) {
//super(String.valueOf(index+1));
super(segment + "" + number, new PuckConfig());
this.setParent(basePlate);
this.index = index;
this.segment = segment;
this.number = number;
this.address = address;
this.angle = angle;
for (int i = 0; i < numberOfSamples; i++) {
new Sample(this, i);
}
}
@Override
public PuckConfig getConfig(){
return (PuckConfig) super.getConfig();
}
public boolean isDisabled(){
return getConfig().disabled;
}
public PuckDetectionMode getDetectionMode(){
return getConfig().detection == null ? PuckDetectionMode.Both: getConfig().detection;
}
public BasePlate getBasePlate() {
return (BasePlate) getParent();
}
DimensionDouble getSize() {
return new DimensionDouble(67.0, 67.0);
}
public Sample[] getSamples() {
ArrayList<Sample> ret = new ArrayList<>();
for (Device d : getChildren()) {
ret.add((Sample) d);
}
return ret.toArray(new Sample[0]);
}
public int getIndex() {
return index;
}
String id;
public String getId() {
//return "XXX000" + index;
if (getDetection()!=Detection.Present){
return null;
}
return id;
}
public void setId(String value) {
if (value!=null){
for (Device d : getParent().getChildren()) {
if (d instanceof Puck) {
if (((Puck)d).getId() != null){
if (isSameId (((Puck)d).getId(), value)){
((Puck)d).setId(null);
}
}
}
}
}
id = value;
}
public static boolean isSameId(String id1, String id2){
if ((id1==null) || (id2==null)){
return false;
}
return id1.equals(id2) || BarcodeMatcher.matchBarcodes(id1,id2);
}
public static boolean hasSameId(Puck p1, Puck p2){
return isSameId(p1.getId(), p2.getId());
}
public boolean hasSameId(Puck p){
return isSameId(getId(), p.getId());
}
public char getSegment() {
return segment;
}
public int getNumber() {
return number;
}
public int getAddress() {
return address;
}
public int getUserIndex(){
return (getSegment() - new Character('A')) * BasePlate.numberOfPucksPerSegments + getNumber() - 1;
}
/*
boolean enabled;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean value) {
enabled = value;
}
*/
public enum Detection {
Empty,
Present,
Offline,
Error
}
Detection detection = Detection.Error;
public Detection getDetection() {
if (MainPanel.BASE_PLATE_LAYOUT == MainPanel.BasePlateLayout.rt){
return Detection.Present;
}
return detection;
}
boolean detectionError;
public boolean isDetectionError() {
return detectionError;
}
private boolean selected;
public boolean isSelected() {
return selected;
}
public void toggleSelected(boolean value) {
setSelected(!selected);
}
public void setSelected(boolean value) {
if (value != selected) {
if (value == true) {
for (Device d : getParent().getChildren()) {
if (d instanceof Puck) {
((Puck) d).selected = false;
for (Sample sample : ((Puck) d).getSamples()) {
sample.selected = false;
}
}
}
}
for (Sample sample : getSamples()) {
sample.selected = false;
}
selected = value;
getBasePlate().onSelectionChanged();
}
}
private boolean pointed;
public boolean isPointed() {
return pointed;
}
public void setPointed(boolean value) {
if (value != pointed) {
if (value == true) {
for (Device d : getParent().getChildren()) {
if (d instanceof Puck puck) {
puck.pointed = false;
}
}
}
pointed = value;
}
}
public int getNumberOfSamples() {
return numberOfSamples;
}
}