diff --git a/.gitignore b/.gitignore index 4f7baf9..fecbed3 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ python* # extras *~ *.dbl +iocBoot/iocalarmdemo/dbDumpRecord.log diff --git a/iocBoot/iocalarmdemo/Makefile b/iocBoot/iocalarmdemo/Makefile new file mode 100644 index 0000000..f464941 --- /dev/null +++ b/iocBoot/iocalarmdemo/Makefile @@ -0,0 +1,9 @@ +# SPDX-FileCopyrightText: 1998 Argonne National Laboratory +# +# SPDX-License-Identifier: EPICS + +TOP = ../.. +include $(TOP)/configure/CONFIG +ARCH = $(EPICS_HOST_ARCH) +TARGETS = envPaths dllPath.bat +include $(TOP)/configure/RULES.ioc diff --git a/iocBoot/iocalarmdemo/Test/camonitor_amsg.bat b/iocBoot/iocalarmdemo/Test/camonitor_amsg.bat new file mode 100644 index 0000000..b084208 --- /dev/null +++ b/iocBoot/iocalarmdemo/Test/camonitor_amsg.bat @@ -0,0 +1 @@ +"C:\Program Files\EPICS Windows Tools\camonitor" CALCOUT.AMSG AAO.AMSG AO.AMSG BO.AMSG INT64OUT.AMSG LONGOUT.AMSG LSO.AMSG MBBODIRECT.AMSG MBBO.AMSG STRINGOUT.AMSG AAI.AMSG AI.AMSG BI.AMSG INT64IN.AMSG LONGIN.AMSG LSI.AMSG MBBIDIRECT.AMSG MBBI.AMSG STRINGIN.AMSG WAVEFORM.AMSG diff --git a/iocBoot/iocalarmdemo/Test/camonitor_amsg.sh b/iocBoot/iocalarmdemo/Test/camonitor_amsg.sh new file mode 100755 index 0000000..9b35570 --- /dev/null +++ b/iocBoot/iocalarmdemo/Test/camonitor_amsg.sh @@ -0,0 +1 @@ +camonitor CALCOUT.AMSG AAO.AMSG AO.AMSG BO.AMSG INT64OUT.AMSG LONGOUT.AMSG LSO.AMSG MBBODIRECT.AMSG MBBO.AMSG STRINGOUT.AMSG AAI.AMSG AI.AMSG BI.AMSG INT64IN.AMSG LONGIN.AMSG LSI.AMSG MBBIDIRECT.AMSG MBBI.AMSG STRINGIN.AMSG WAVEFORM.AMSG diff --git a/iocBoot/iocalarmdemo/Test/camonitor_inp.bat b/iocBoot/iocalarmdemo/Test/camonitor_inp.bat new file mode 100644 index 0000000..231b4fc --- /dev/null +++ b/iocBoot/iocalarmdemo/Test/camonitor_inp.bat @@ -0,0 +1 @@ +"C:\Program Files\EPICS Windows Tools\camonitor" AAI AI BI INT64IN LONGIN LSI MBBIDIRECT MBBI STRINGIN WAVEFORM \ No newline at end of file diff --git a/iocBoot/iocalarmdemo/Test/camonitor_inp.sh b/iocBoot/iocalarmdemo/Test/camonitor_inp.sh new file mode 100755 index 0000000..9ebf01a --- /dev/null +++ b/iocBoot/iocalarmdemo/Test/camonitor_inp.sh @@ -0,0 +1 @@ +camonitor AAI AI BI INT64IN LONGIN LSI MBBIDIRECT MBBI STRINGIN WAVEFORM diff --git a/iocBoot/iocalarmdemo/Test/camonitor_out.bat b/iocBoot/iocalarmdemo/Test/camonitor_out.bat new file mode 100644 index 0000000..2df7e80 --- /dev/null +++ b/iocBoot/iocalarmdemo/Test/camonitor_out.bat @@ -0,0 +1 @@ +camonitor CALCOUT AAO AO BO INT64OUT LONGOUT LSO MBBODIRECT MBBO STRINGOUT diff --git a/iocBoot/iocalarmdemo/Test/camonitor_out.sh b/iocBoot/iocalarmdemo/Test/camonitor_out.sh new file mode 100755 index 0000000..b625c93 --- /dev/null +++ b/iocBoot/iocalarmdemo/Test/camonitor_out.sh @@ -0,0 +1 @@ +camonitor CALCOUT AAO AO BO INT64OUT LONGOUT LSO MBBODIRECT MBBO STRINGOUT diff --git a/iocBoot/iocalarmdemo/alarm.py b/iocBoot/iocalarmdemo/alarm.py new file mode 100644 index 0000000..f051b6b --- /dev/null +++ b/iocBoot/iocalarmdemo/alarm.py @@ -0,0 +1,203 @@ +import devsup.ptable as PT +from devsup.ptable import _ONPROC +from devsup.hooks import addHook +from devsup import NO_ALARM, MINOR_ALARM, MAJOR_ALARM, INVALID_ALARM, READ_ALARM, WRITE_ALARM +from devsup.db import getRecord +import signal +import sys +import numpy as np +from threading import Thread, Event, Lock + + +class AlarmTest(PT.TableBase): + # Output paramaters + calcout = PT.Parameter() + aao = PT.Parameter() + ao = PT.Parameter() + bo = PT.Parameter() + int64out = PT.Parameter() + longout = PT.Parameter() + lso = PT.Parameter() + mbbo = PT.Parameter() + mbboDirect = PT.Parameter() + stringout = PT.Parameter() + + # Input parameters + aai = PT.Parameter(iointr=True) + ai = PT.Parameter(iointr=True) + bi = PT.Parameter(iointr=True) + int64in = PT.Parameter(iointr=True) + longin = PT.Parameter(iointr=True) + lsi = PT.Parameter(iointr=True) + mbbiDirect = PT.Parameter(iointr=True) + mbbi = PT.Parameter(iointr=True) + stringin = PT.Parameter(iointr=True) + waveform = PT.Parameter(iointr=True) + + def __init__(self, name): + super().__init__(name=name) + self.stop = False + self.odd_alarm = False + self.count = None + self.onproc_event = Event() + self.calcout.addAction(self.proc_out_calcout, _ONPROC) + self.aao.addAction(self.proc_out_aao, _ONPROC) + self.ao.addAction(self.proc_out_ao, _ONPROC) + self.bo.addAction(self.proc_out_bo, _ONPROC) + self.int64out.addAction(self.proc_out_int64out, _ONPROC) + self.longout.addAction(self.proc_out_longout, _ONPROC) + self.lso.addAction(self.proc_out_lso, _ONPROC) + self.mbboDirect.addAction(self.proc_out_mbboDirect, _ONPROC) + self.mbbo.addAction(self.proc_out_mbbo, _ONPROC) + self.stringout.addAction(self.proc_out_stringout, _ONPROC) + self.inp_list = [self.aai, self.ai, self.bi, self.int64in, self.longin, self.lsi, self.mbbiDirect, self.mbbi, self.stringin, self.waveform] + self.run_thread = Thread(target=self._run) + + def start(self): + self.run_thread.start() + + def stop_threads(self): + print("stopping threads") + self.stop = True + self.onproc_event.set() + self.run_thread.join() + + def set_alarm(self, PV): + """ + This method sets the alarm status on two PVs in alternation. + These two PVs would be either the inputs and driven from the _run thread, + or output PVs and driven from the onproc method(s). + """ + if self.count is None: + return + odd_number = bool(self.count % 2) + if PV is not None: + if odd_number == self.odd_alarm: + PV.alarm = MAJOR_ALARM + if self.odd_alarm: + PV.amsg = "odd number" + else: + PV.amsg = "even number" + if self.calcout.value is not None and self.calcout.value > 10: + # Just to show that other alarm status values than COMM are possible. + PV.stat = READ_ALARM + else: + PV.alarm = NO_ALARM + PV.amsg = None + self.odd_alarm = not self.odd_alarm + + def _run(self): + while True: + if self.onproc_event.wait(): + self.onproc_event.clear() + if self.stop: + break + if self.calcout.value is None: + continue + + self.aai.value = [self.count] + # Note that the RVAL field for the AI type does not accept float values + # as it is aimed at use by A to D converters. + self.ai.value = self.count + self.bi.value = self.count % 2 + self.int64in.value = self.count + self.longin.value = self.count + self.lsi.value = str(self.count) + self.mbbiDirect.value = self.count + self.mbbi.value = self.count % 16 # 0..15 values available + self.stringin.value = str(self.count) + self.waveform.value = [self.count] + self.odd_alarm = False + for PV in self.inp_list: + self.set_alarm(PV) + #print(PV.name, PV.value, type(PV.value), PV.alarm) + PV.notify() + + def proc_out_calcout(self): + """ + This method is called in response to timer scanning within the database scan. + This drives the application timing. + """ + if self.calcout.value is not None: + self.count = round(self.calcout.value) + if self.count is not None: + self.odd_alarm = False + self.set_alarm(self.calcout) + #self.onproc_event.set() + aao = getRecord("AAO") + if aao.scan is not None: + aao.VAL = [self.count] + # The record needs to be scanned to update the scan status. + aao.scan(sync=False, force=0) + + def proc_out_aao(self): + self.set_alarm(self.aao) + ao = getRecord("AO") + if ao.scan is not None and self.count is not None: + # Note that the RVAL field for the AO type does not accept float values + # as it is aimed at use by A to D converters + ao.VAL = float(self.count) + ao.scan(sync=False, force=0) + + def proc_out_ao(self): + self.set_alarm(self.ao) + bo = getRecord("BO") + if bo.VAL is not None and self.count is not None: + bo.VAL = self.count % 2 + bo.scan(sync=False, force=0) + + def proc_out_bo(self): + self.set_alarm(self.bo) + int64out = getRecord("INT64OUT") + if int64out.VAL is not None and self.count is not None: + int64out.VAL = self.count + int64out.scan(sync=False, force=0) + + def proc_out_int64out(self): + self.set_alarm(self.int64out) + longout = getRecord("LONGOUT") + if longout.VAL is not None and self.count is not None: + longout.VAL = self.count + longout.scan(sync=False, force=0) + + def proc_out_longout(self): + self.set_alarm(self.longout) + lso = getRecord("LSO") + if lso.VAL is not None and self.count is not None: + lso.VAL = str(self.count) + lso.scan(sync=False, force=0) + + def proc_out_lso(self): + self.set_alarm(self.lso) + mbboDirect = getRecord("MBBODIRECT") + if mbboDirect.VAL is not None and self.count is not None: + mbboDirect.VAL = self.count + mbboDirect.scan(sync=False, force=0) + + def proc_out_mbboDirect(self): + self.set_alarm(self.mbboDirect) + mbbo = getRecord("MBBO") + if mbbo.VAL is not None and self.count is not None: + mbbo.VAL = self.count % 16 # 0..15 values available + mbbo.scan(sync=False, force=0) + + def proc_out_mbbo(self): + self.set_alarm(self.mbbo) + stringout = getRecord("STRINGOUT") + if stringout.VAL is not None and self.count is not None: + stringout.VAL = str(self.count) + stringout.scan(sync=False, force=0) + + def proc_out_stringout(self): + self.set_alarm(self.stringout) + self.onproc_event.set() + +def build(): + sup = AlarmTest(name="alarm") + + signal.signal(signal.SIGINT, signal.SIG_DFL) + + addHook('AfterIocRunning', sup.start) + addHook('AtIocExit', sup.stop_threads) + + return sup diff --git a/iocBoot/iocalarmdemo/db/alarm.db b/iocBoot/iocalarmdemo/db/alarm.db new file mode 100644 index 0000000..11cf56e --- /dev/null +++ b/iocBoot/iocalarmdemo/db/alarm.db @@ -0,0 +1,240 @@ +# Output parameters +record(calcout, "CALCOUT") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set calcout") + # No RVAL field + ${HASH="#"}field(VAL, "1") + field(INPA, "CALCOUT") + field(CALC, "A+1") + field(SCAN, "5 second") +} + +record(aao, "AAO") { + field(PINI, ${PINI="NO"}) + # NB, an array needs to have at least two elements. Or it is not an array. + field(NELM, "2") + field(DTYP, "Python Device") + field(FTVL, "LONG") + # No RVAL field + field(OUT, "@devsup.ptable alarm set aao") + # Cannot set VAL + #field(VAL, "2") +} + +record(ao, "AO") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set ao") + # Has RVAL field but this is unsuitable for output use + ${HASH="#"}field(VAL, "3") +} + +record(bo, "BO") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(ZNAM, "OFF") + field(ONAM, "ON") + field(OUT, "@devsup.ptable alarm set bo") + # Has RVAL field but this is unsuitable for output use + ${HASH="#"}field(${RVAL="VAL"}, "4") +} + +record(int64out, "INT64OUT") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set int64out") + # No RVAL field + ${HASH="#"}field(VAL, "5") +} + +record(longout, "LONGOUT") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set longout") + # No RVAL field + ${HASH="#"}field("VAL", "6") +} + +record(lso, "LSO") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set lso") + # No RVAL field + # Cannot set VAL + #field("VAL", "7") +} + +record(mbbo, "MBBO") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set mbbo") + field(ZRST, "Zero") + field(ONST, "One") + field(TWST, "Two") + field(THST, "Three") + field(FRST, "Four") + field(FVST, "Five") + field(SXST, "Six") + field(SVST, "Seven") + field(EIST, "Eight") + field(NIST, "Nine") + field(TEST, "Ten") + field(ELST, "Eleven") + field(TVST, "Twelve") + field(TTST, "Thirteen") + field(FTST, "Fourteen") + field(FFST, "Fifteen") + # Has RVAL field but this is unsuitable for output use + ${HASH="#"}field(VAL, "8") +} + +record(mbboDirect, "MBBODIRECT") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set mbboDirect") + # Has RVAL field but this is unsuitable for output use + ${HASH="#"}field(VAL, "9") +} + +record(stringout, "STRINGOUT") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(OUT, "@devsup.ptable alarm set stringout") + # No RVAL field + ${HASH="#"}field(VAL, "10") +} + +# Input parameters +record(aai, "AAI") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + # NB, an array needs to have at least two elements. Or it is not an array. + field(NELM, "2") + field(FTVL, "LONG") + field(INP, "@devsup.ptable alarm get aai") + # No RVAL field + # Cannot set VAL + #field(VAL, "{11}") + field(SCAN, "I/O Intr") +} + +record(ai, "AI") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get ai") + info("pyfield", ${RVAL="VAL"}) + ${HASH="#"}field(VAL, "12") + field(SCAN, "I/O Intr") +} + +record(bi, "BI") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get bi") + info("pyfield", ${RVAL="VAL"}) + ${HASH="#"}field(VAL, "13") + field(ZNAM, "OFF") + field(ONAM, "ON") + field(SCAN, "I/O Intr") +} + +record(int64in, "INT64IN") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get int64in") + # No RVAL field + ${HASH="#"}field(VAL, "14") + field(SCAN, "I/O Intr") +} + +record(longin, "LONGIN") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get longin") + # No RVAL field + ${HASH="#"}field(VAL, "15") + field(SCAN, "I/O Intr") +} + +record(lsi, "LSI") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get lsi") + # No RVAL field + # Cannot set VAL + #field(VAL, "16") + field(SCAN, "I/O Intr") +} + +record(mbbiDirect, "MBBIDIRECT") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get mbbiDirect") + info("pyfield", ${RVAL="VAL"}) + ${HASH="#"}field(VAL, "17") + field(SCAN, "I/O Intr") +} + +record(mbbi, "MBBI") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get mbbi") + field(ZRVL, 0) + field(ONVL, 1) + field(TWVL, 2) + field(THVL, 3) + field(FRVL, 4) + field(FVVL, 5) + field(SXVL, 6) + field(SVVL, 7) + field(EIVL, 8) + field(NIVL, 9) + field(TEVL, 10) + field(ELVL, 11) + field(TVVL, 12) + field(TTVL, 13) + field(FTVL, 14) + field(FFVL, 15) + field(ZRST, "Zero") + field(ONST, "One") + field(TWST, "Two") + field(THST, "Three") + field(FRST, "Four") + field(FVST, "Five") + field(SXST, "Six") + field(SVST, "Seven") + field(EIST, "Eight") + field(NIST, "Nine") + field(TEST, "Ten") + field(ELST, "Eleven") + field(TVST, "Twelve") + field(TTST, "Thirteen") + field(FTST, "Fourteen") + field(FFST, "Fifteen") + info("pyfield", ${RVAL="VAL"}) + ${HASH="#"}field(VAL, "18") + field(SCAN, "I/O Intr") +} + +record(stringin, "STRINGIN") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(INP, "@devsup.ptable alarm get stringin") + # No RVAL field + ${HASH="#"}field(VAL, "19") + field(SCAN, "I/O Intr") +} + +record(waveform, "WAVEFORM") { + field(PINI, ${PINI="NO"}) + field(DTYP, "Python Device") + field(FTVL, "LONG") + # NB, an array needs to have at least two elements. Or it is not an array. + field(NELM, "2") + field(INP, "@devsup.ptable alarm get waveform") + # No RVAL field + # Cannot set VAL + #field(VAL, "20") + field(SCAN, "I/O Intr") +} diff --git a/iocBoot/iocalarmdemo/st.bat b/iocBoot/iocalarmdemo/st.bat new file mode 100644 index 0000000..5bf2461 --- /dev/null +++ b/iocBoot/iocalarmdemo/st.bat @@ -0,0 +1,24 @@ +@echo off + +REM So previous PATH can be restored. +set OLD_PATH=%PATH% + +set PINI="NO" +if not "%1"=="" set PINI=%1 + +set RVAL="VAL" +if not "%2"=="" set RVAL=%2 + +REM HASH defaults to # if PINI is NO. +REM It defaults to nothing if PINI is YES. +REM The empty string cannot be used as it creates an empty macro. +if "%PINI%"=="NO" (set HASH="#") else (set HASH=" ") +if not "%3"=="" set HASH=%3 + +call ..\env_vars.bat +call dllPath.bat +@echo on +%TOP%\bin\windows-x64\softIocPy%PY_VER%.exe st.cmd + +@echo off +set PATH=%OLD_PATH% diff --git a/iocBoot/iocalarmdemo/st.cmd b/iocBoot/iocalarmdemo/st.cmd new file mode 100755 index 0000000..23e4870 --- /dev/null +++ b/iocBoot/iocalarmdemo/st.cmd @@ -0,0 +1,23 @@ +#!../../bin/linux-x86_64/softIocPy3.6 + +#- SPDX-FileCopyrightText: 2003 Argonne National Laboratory +#- +#- SPDX-License-Identifier: EPICS + +#- You may have to change alarmDemo to something else +#- everywhere it appears in this file + +< envPaths + +epicsEnvSet("PINI", "${PINI='NO'}") +epicsEnvSet("RVAL", "${RVAL='VAL'}") +epicsEnvSet("HASH", "${HASH='#'}") + +py "import alarm" +py "alarm.build()" + +## Load record instances +dbLoadRecords("db/alarm.db","PINI=${PINI}, RVAL=${RVAL}, HASH=${HASH}") + +iocInit +dbDumpRecord > dbDumpRecord.log diff --git a/iocBoot/iocalarmdemo/st.sh b/iocBoot/iocalarmdemo/st.sh new file mode 100755 index 0000000..16fa26f --- /dev/null +++ b/iocBoot/iocalarmdemo/st.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +export PINI="NO" +if [ "$#" -gt 0 ]; then + export PINI=$1 +fi + +export RVAL="VAL" +if [ "$#" -gt 1 ]; then + export RVAL=$2 +fi + +# HASH defaults to # if PINI is NO. +export HASH="#" +if [ ${PINI} = "YES" ]; then + # It defaults to nothing if PINI is YES. + # The empty string cannot be used as it creates an empty macro. + export HASH=" " +fi + +if [ "$#" -gt 2 ]; then + export HASH=$3 +fi + +# Launch the IOC +./st.cmd