00001 /**************************************************************** 00002 ** This file contains the objects for creating an event trigger 00003 ** 00004 ** It creates an event based on a trigger variable, and the past 00005 ** history of that variable. 00006 ** 00007 ** The event is signaled by calling a callback function 00008 ** 00009 ** Created May 7, 2008 00010 ** 00011 ****************************************************************/ 00012 00013 #include <stdio.h> 00014 #include <stdlib.h> 00015 #include "EventTrigger.h" 00016 00017 EVTB **EventsList; 00018 00019 00020 EVTB *NewEventTrigger(unsigned char mode, void (*cb)(char,void*),void *arg) 00021 { 00022 //------------------------------------------------------------------------ 00023 // Create an Event Trigger Block 00024 // 00025 // parameters: 00026 // mode.....trigger mode (positive (low->high), negative (high->low), both 00027 // cb.......pointer to callback function to execute on event true 00028 // arg......pointer to arguments to pass to callback function 00029 // 00030 // return value: 00031 // returns pointer to EVTB on success, NULL on fail 00032 //------------------------------------------------------------------------ 00033 EVTB *rv; 00034 00035 rv = (EVTB *) malloc(sizeof(EVTB)); 00036 if(rv) 00037 { 00038 rv->Previous = 0; 00039 rv->TrigMode = mode; 00040 rv->callback = cb; 00041 rv->arg = arg; 00042 } 00043 return rv; 00044 } 00045 00046 void FreeEventTrigger(EVTB *pE) 00047 { 00048 //------------------------------------------------------------------------- 00049 // Free an Event Trigger Block 00050 // 00051 // parameter: 00052 // pE......pointer to EVTB to free 00053 //------------------------------------------------------------------------- 00054 free((void *)pE); 00055 } 00056 00057 int EventTrigger(EVTB *pE,unsigned char v) 00058 { 00059 //---------------------------------------------------------------------------- 00060 // Process an event.... 00061 // v is the value from the event. If v is greater than the preveous value 00062 // then generate a postive trigger event if trigger mode is set to postive. 00063 // if v is less than the previous value and the trigger mode is set to negative 00064 // then generate a negative trigger event. 00065 // We can generate an event on either positive or negative transistions, or 00066 // just positive or just negative. 00067 // 00068 // parameters: 00069 // pE..........pointer to the event control block 00070 // v...........value that controls the event 00071 //---------------------------------------------------------------------------- 00072 int rv = 0; 00073 if(v != pE->Previous) 00074 { 00075 if(( ( (v - pE->Previous) > 0) ) && (pE->TrigMode & EVENTTRIGGER_MODE_POS) ) 00076 { 00077 if(pE->callback) (*pE->callback)(EVENTTRIGGER_MODE_POS,pE->arg); 00078 rv = 1; 00079 } 00080 else if (( ( (v - pE->Previous) < 0) ) && pE->TrigMode & EVENTTRIGGER_MODE_NEG) 00081 { 00082 if(pE->callback) (*pE->callback)(EVENTTRIGGER_MODE_NEG,pE->arg); 00083 rv = 1; 00084 } 00085 pE->Previous = v; 00086 } 00087 return rv; 00088 } 00089 00090 void InitEventTriggers(int n) 00091 { 00092 int i; 00093 00094 EventsList = malloc(sizeof(EVTB *) * n); 00095 for(i=0;i<n;++i) 00096 EventsList[i] = NewEventTrigger(EVENTTRIGGER_MODE_POS, (void (*)(char,void*))NULL,NULL); 00097 } 00098 00099 int CheckEventTrigger(int e,int v) 00100 { 00101 return EventTrigger(EventsList[e],v); 00102 } 00103