#include #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(psub) ((int)(psub)->e) #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)]; int 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 = (int)psub->a; if ( (tot = a + (int)psub->b) > A1MAX + A2MAX(psub) ) tot = A1MAX + A2MAX(psub); if ( tot < 0 ) tot = 0; /* breakpoint psub->d must be >= 0 */ if ( (A1 = ( tot - (int)psub->d )/2) < 0 ) { A1 = 0; A2 = tot; } else { if ( A1 > A1MAX ) A1 = A1MAX; A2 = tot - A1; } /* At this point 0 <= a1 <= A1MAX * and 0 <= tot <= AMAX */ if ( A2 > A2MAX(psub) ) { A2 = A2MAX(psub); A1 = tot - A2MAX(psub); } /* Here 0 <= A1 <= A1MAX, 0 <= A2 <= A2MAX holds */ CA = (int)(psub->c + A1MAX + A2MAX(psub)) - 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);