#include #include #include #include #include #include #include #include #include #define NARR(arr) (sizeof(arr)/sizeof(arr[0])) #define ATT1 0 #define ATT2 1 #define CALA 2 static char *dbnms[] = { "ATT1", "ATT2", "CALA" }; typedef struct SubAttPvt_ { DBADDR dbas[NARR(dbnms)]; int latt[NARR(dbnms)]; } SubAttPvtRec, *SubAttPvt; static long subAttIni(subRecord *psub, long (*process)(subRecord*)) { SubAttPvt pvt; char nm[100]; char *col; int i; if ( !(psub->dpvt = (pvt = malloc(sizeof(*pvt)))) ) { epicsPrintf("subAttIni(): no memory\n"); goto bail; } strcpy(nm, psub->name); if ( !(col = strrchr(nm, ':')) ) { epicsPrintf("subAttIni(): FATAL ERROR -- no ':' found in '%s'\n", nm); goto bail; } col++; for ( i=0; idbas[i]) ) { epicsPrintf("subAttIni(): FATAL ERROR -- record '%s' not found\n", nm); goto bail; } pvt->latt[i] = -1; } return 0; bail: free(psub->dpvt); psub->dpvt = 0; return -1; } #define A1MAX 15 #define A2MAX 15 #define ACMAX 31 #define A1 (v[ATT1]) #define A2 (v[ATT2]) #define CA (v[CALA]) static long subAtt(subRecord *psub) { SubAttPvt pvt = psub->dpvt; int v[NARR(dbnms)]; unsigned tot, a; int i; long rval = 0; if ( !pvt ) { epicsPrintf("subAtt(): CONFIG ERROR -- No dbaddrs\n"); return -1; } /* If nothing changed return. * Don't bother trying to save time * for other special cases since * 'A' is changed most frequently * and that potentially requires updating all * attenuators anyways... */ if ( psub->a == psub->la && psub->b == psub->lb && psub->c == psub->lc && psub->d == psub->ld ) return 0; a = (unsigned)psub->a; if ( (tot = a + (unsigned)psub->b) > A1MAX + A2MAX ) tot = A1MAX + A2MAX; if ( (A1 = ((int)( tot - (unsigned)psub->d ))/2) < 0 ) { A1 = 0; A2 = tot; } else { A2 = tot - A1; } /* At this point 0 <= a1 <= tot/2 * and 0 <= tot <= AMAX */ if ( A2 > A2MAX ) { A2 = A2MAX; A1 = tot - A2MAX; } CA = (unsigned)(psub->c + A1MAX + A2MAX) - a; if ( CA < 0 ) { CA = 0; } else if ( CA > ACMAX ) { CA = ACMAX; } for ( i=0; i < NARR(dbnms); i++ ) { if ( pvt->latt[i] != v[i] ) { if ( dbPutField( &pvt->dbas[i], DBR_LONG, &v[i], 1) ) { epicsPrintf("dbPutField to %s failed\n", dbnms[i]); rval = -1; } else { pvt->latt[i] = v[i]; } } } return rval; } epicsRegisterFunction(subAttIni); epicsRegisterFunction(subAtt);