limit_switch.c

Go to the documentation of this file.
00001 
00026 #include <includes.h>
00027 
00028 #define LS_NUM_SWITCHES (10) 
00030 LIMIT_SWITCH ls_switches[LS_NUM_SWITCHES]; 
00031 int ls_count = 0; 
00040 int ls_init_switch(unsigned int new_limit, INT_VOID_F funct)
00041 {
00042   if (ls_count < LS_NUM_SWITCHES){
00043     int id = ls_count;
00044     LIMIT_SWITCH* ls = &(ls_switches[ls_count++]);
00045     ls->limit = new_limit;
00046     ls->function = funct;
00047     ls->count = 0;
00048     ls->prev = LS_OFF;
00049     ls->state = LS_OFF;
00050     return id;
00051   } else {
00052     error_occurred(ERROR_LS_NUM_SWITCH);
00053     return -1;
00054   }
00055 }
00056 
00060 void ls_update(void)
00061 {
00062   int i;
00063   for (i = 0; i < ls_count; i++){
00064     LS_STATE new;
00065     LIMIT_SWITCH* ls = &(ls_switches[i]);
00066     INT_VOID_F get_state = ls->function;
00067     // ***** CHECK SWITCH ***** //
00068     if (get_state()){ //true, so switch ON
00069       new = LS_ON;
00070     } else {
00071       new = LS_OFF;
00072     }
00073     // ***** INCREMENT COUNT ***** //
00074     if (new == ls->prev){ //consistent values, increment count
00075       ls->count = ls->count + 1;
00076     } else { //different values, reset count
00077       ls->count = 0;
00078     }
00079     ls->prev = new;
00080     // ***** CHECK LIMIT ***** //
00081     if (ls->count >= ls->limit){ //we have reached the limit, set state
00082       ls->state = new;
00083       ls->count = 0;
00084     }
00085   }
00086 }
00087 
00097 LS_STATE ls_get_state(int id)
00098 {
00099   if (id < ls_count && id >= 0){
00100     LIMIT_SWITCH* ls = &(ls_switches[id]);
00101     return ls->state;
00102   } else {
00103     error_occurred(ERROR_LS_INVALID_ID);
00104     return LS_OFF;
00105   }
00106 }
00107 
00108 
Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3