Go to Post You don't need to prototype everything, just the stuff you want to work. - JVN [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 11-02-2005, 18:07
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Code: Toggle on Tap

Recently there have been a lot of questions about how to make something toggle when a button is tapped (i.e. the button is pressed and it previously wasn't). The something being toggled is most likely going to be the state of a relay for pneumatics, but it can also be a variable or PWM output.

Below is a function that we use to abstract the functionality of toggling on the tap of a button.

The intended audience is
* Programmers that are new to the concept of state toggling
* Programmers that have many instances of state toggling

Feel free to use/modify this code in any way desired.

Code:
/***************************
* FUNCTION NAME: toggle_on_tap
* PURPOSE:       Toggle a parameter when a button is tapped
* ARGUMENTS:   unsigned char cur_button - The current button state
*              unsigned char prev_button - The button state for the previous loop
*              unsigned char prev_state - The parameter state for the previous loop
*              unsigned char state1 - A state that the parameter can have
*              unsigned char state2 - The other state that the parameter can have
* RETURNS:     unsigned char new_state - The new state of the parameter
******************************/
unsigned char toggle_on_tap( unsigned char cur_button, 
                             unsigned char prev_button, 
                             unsigned char prev_state,
                             unsigned char state1,
                             unsigned char state2)
{ 
    unsigned char new_state = prev_state;
    if ((cur_button == 1) && (prev_button == 0))
    {
       if (prev_state == state1)
       {
          new_state = state2;
       }
       else
       {
          new_state = state1;
       }
    }
//  printf("Cur %d PrevB %d  PrevS %d  NewS %d\r",cur_button,prev_button,prev_state,new_state);
    return new_state;
}
Here's an example of how it could be used.
* P1 trigger cycles relay 1 between STATE_A and STATE_B
* P1 top cycles relay 2 between STATE_C and STATE_D

Code:
#define STATE_A 1
#define STATE_B 0
#define STATE_C 0
#define STATE_D 1

...

static unsigned char p1_sw_trig_prev = 0;
static unsigned char p1_sw_top_prev = 0;

static unsigned char relay1_state_prev = STATE_A;
static unsigned char relay2_state_prev = STATE_C;

unsigned char new_state;

// Get the new state based on the current button state
// and the previous button and relay states
new_state = toggle_on_tap(p1_sw_trig, 
                          p1_sw_trig_prev,
                          relay1_state_prev,
                          STATE_A,
                          STATE_B);
// Set the output to the new state
relay1_fwd = new_state;

// This may or may not be needed based on the application
// relay1_rev = relay1_state_prev;

//Store the current trigger state as the previous triggger state
p1_sw_trig_prev = p1_sw_trig;

//Store the new relay state as the previous relay state
relay1_state_prev = new_state;

// Get the new state based on the current button state
// and the previous button and relay states
new_state = toggle_on_tap(p1_sw_top, 
                          p1_sw_top_prev,
                          relay2_state_prev,
                          STATE_C,
                          STATE_D);

// Set the output to the new state
relay2_fwd = new_state;

// This may or may not be needed based on the application
// relay2_rev = relay1_state_prev;

//Store the current trigger state as the previous triggger state
p1_sw_top_prev = p1_sw_top;

//Store the new relay state as the previous relay state
relay2_state_prev = new_state;
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Example gyro code released. Kevin Watson Programming 60 17-03-2005 18:32
Updated: Serial Port Driver Code Kevin Watson Programming 4 05-02-2005 18:39
Team THRUST - Kevin's Code and Camera Code Combine Chris_Elston Programming 3 31-01-2005 22:28
Sourceforge for Code Repository and other stuff SilverStar Programming 9 15-01-2005 21:16
heres the code. y this not working omega Programming 16 31-03-2004 15:18


All times are GMT -5. The time now is 00:18.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi