limit_switch.c File Reference

Software limit switches. More...

Go to the source code of this file.

Defines

#define LS_NUM_SWITCHES   (10)
 The maximum number of limit switches.

Functions

LS_STATE ls_get_state (int id)
 Returns the state of the switch with the given id.
int ls_init_switch (unsigned int new_limit, INT_VOID_F funct)
 Initializes a limit switch with the given limit and signal function.
void ls_update (void)
 Updates the state for every limit switch.

Variables

int ls_count = 0
 The number of limit switches currently being used.
LIMIT_SWITCH ls_switches [LS_NUM_SWITCHES]
 The array of limit switches.

Detailed Description

Software limit switches.

A limit switch will wait for a given limit of consecutive high signals before turning high, and the same for turning low again. If a low signal interrupts a stream of high signals (or vice versa), the counter is reset.

Example pin setup for Limit Switch (used by ankle controllers):

  // *******************************************************************************
  // Limit Switch Setup                                            
  // *******************************************************************************  
  PINSEL0 &= ~(3<<0); //set P0.0 to GPIO
  PINSEL0 &= ~(3<<2); //set P0.1 to GPIO
  FIO0DIR &= ~(1<<0); //set P0.0 to input
  FIO0DIR &= ~(1<<1); //set P0.1 to input
Author:
Nicolas Williamson
Date:
July 2009

Definition in file limit_switch.c.


Define Documentation

#define LS_NUM_SWITCHES   (10)

The maximum number of limit switches.

Definition at line 28 of file limit_switch.c.

Referenced by ls_init_switch().


Function Documentation

LS_STATE ls_get_state ( int  id  ) 

Returns the state of the switch with the given id.

The id of the switch was set by the ls_init_switch function. The id is also the index of the switch within the array of switches, so it begins at 0 for the first switch, and increments with each switch that is initialized.

Parameters:
id The id of the switch.
Returns:
The output state of the switch.

Definition at line 97 of file limit_switch.c.

References error_occurred(), ls_switches, and LIMIT_SWITCH::state.

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 }

int ls_init_switch ( unsigned int  new_limit,
INT_VOID_F  funct 
)

Initializes a limit switch with the given limit and signal function.

Call this function from software_setup for each limit switch.

Parameters:
new_limit The limit of consecutive signals needed before output changes.
funct A signal functino which returns 0 for low and non-zero for high.
Returns:
The id of the limit switch, or -1 if the switch failed to initialize.

Definition at line 40 of file limit_switch.c.

References LIMIT_SWITCH::count, error_occurred(), LIMIT_SWITCH::function, LIMIT_SWITCH::limit, ls_count, LS_NUM_SWITCHES, ls_switches, LIMIT_SWITCH::prev, and LIMIT_SWITCH::state.

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 }


Variable Documentation

int ls_count = 0

The number of limit switches currently being used.

Definition at line 31 of file limit_switch.c.

Referenced by ls_init_switch(), and ls_update().

LIMIT_SWITCH ls_switches[LS_NUM_SWITCHES]

The array of limit switches.

Definition at line 30 of file limit_switch.c.

Referenced by ls_get_state(), ls_init_switch(), and ls_update().

Generated on Tue Jun 29 16:36:15 2010 by  doxygen 1.6.3