From 5d74bf64c1a33fd7c7adbba7fcbf2f64c984da31 Mon Sep 17 00:00:00 2001
From: Christian Koernig <ckoernig@mail.desy.de>
Date: Fri, 26 Mar 2021 11:37:09 +0100
Subject: [PATCH] Improved example script

---
 README.md                                  |  1 +
 main.cpp => examples/simple_connection.cpp |  0
 examples/simple_connection.py              | 73 ++++++++++++++++++----
 3 files changed, 62 insertions(+), 12 deletions(-)
 rename main.cpp => examples/simple_connection.cpp (100%)

diff --git a/README.md b/README.md
index 5c43552..f685f68 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ The low level communication is implemented in C++ and supports both Ethernet via
 The low level implementation is based on [previous work](https://github.com/ALBA-Synchrotron/AmptekPX5DS) at the ALBA synchrotron. However, the communication layer has been completely redesigned for more stability and added support for USB connections, circumventing the bottleneck of the low speed base-10 ethernet PHY of the hardware.
 This code is meant to be a mostly drop-in replacement of the original Tango server, therefore the same server name and many commands & attributes are used. So far, the SCA channels and the MCS mode is not implemented, if those are needed, the original code should be used.
 
+Additionally, a basic simulator interface can be used during DAQ logic development if no hardware is available.
 
 Prerequisites
 -------------
diff --git a/main.cpp b/examples/simple_connection.cpp
similarity index 100%
rename from main.cpp
rename to examples/simple_connection.cpp
diff --git a/examples/simple_connection.py b/examples/simple_connection.py
index 651eac7..03f03df 100644
--- a/examples/simple_connection.py
+++ b/examples/simple_connection.py
@@ -1,14 +1,38 @@
-import AmptekHardwareInterface as ahi
+"""Example script for the basic usage of the detector interface
+"""
+
+__author__ = "Christian Koernig"
+
+
+import sys
+import time
+
+import matplotlib.pyplot as plt 
+
+import amptek_hardware_interface as ahi
+
+
+# create the interface
 amptek = ahi.AmptekHardwareInterface()
-#amptek.connectUDP("192.168.1.10", 10001, 1)
+
+# connect the intrerface via USB to the first DP5 device.
+# To connect to a specific device, change the -1 to the serial number
+# For basic tests, the simulator interface can be used, of no hardware is available.
+# Use amptek.connectSimulator() instead
 amptek.connectUSB(-1)
-print("ping")
-print( amptek.Ping() )
-# print("status")
-# dat= amptek.readStatus(-1)
-print( "fnished" )
-amptek.GetSpectrum()
 
+
+
+
+# Send a ping to test the connection
+if amptek.Ping() :
+    print("Ping succeeded")
+else:
+    print("Ping failed! Exited")
+    sys.exit()
+
+
+# Print some common configuration values
 config_names = ["RESC", "CLCK", "TPEA", "GAIF", "GAIN", "RESL", "TFLA", "TPFA", 
                "PURE", "RTDE", "MCAS", "MCAC", "SOFF", "AINP", "INOF", "GAIA",
                "CUSP", "PDMD", "THSL", "TLLD", "THFA", "DACO", "DACF", "DACF",
@@ -16,8 +40,33 @@ config_names = ["RESC", "CLCK", "TPEA", "GAIF", "GAIN", "RESL", "TFLA", "TPFA",
                "PREC", "PRCL", "PRCH", "HVSE", "TECS", "PAPZ", "PAPS", "SCOE",
                "SCOT", "SCOG", "MCSL", "MCSH", "MCST", "AUO2", "TPMO", "GPED",
                "GPGA", "GPMC", "MCAE", "VOLU", "CON1", "CON2"]
-print( "amptek.GetTextConfiguration( config_names )")
 configs = amptek.GetTextConfiguration( config_names )
-print(configs)
-print(amptek.SerialNb())
-print("done")
\ No newline at end of file
+for config in configs:
+    print(config)
+
+# prepare a 10 second acquisition
+amptek.ClearSpectrum()
+amptek.SetPresetAccumulationTime(4)
+
+# start acquisition
+amptek.Enable()
+print("Acquisition started")
+
+
+# check the current status and some status attributes every second until finished
+while True:
+    time.sleep(1)    
+    status = amptek.updateStatus(-1)
+    # test if finished
+    if not status.IsEnabled():
+        break 
+    
+    print("\rAccumulation Time: {:.2f}s, Fast Counts: {:d}, Slow Counts: {:d}".format( status.AccTime(), status.FastCount(), status.SlowCount() ), end="", flush=True)
+
+print("Acquisition finished")
+
+# plot finished spectrum
+fig, ax = plt.subplots()
+ax.plot( amptek.GetSpectrum() )
+
+plt.show()
\ No newline at end of file
-- 
GitLab