Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Control System (http://www.chiefdelphi.com/forums/forumdisplay.php?f=177)
-   -   Code to Read the Binary Inputs on Analog Buttons (http://www.chiefdelphi.com/forums/showthread.php?t=55442)

Kingofl337 08-03-2007 16:17

Code to Read the Binary Inputs on Analog Buttons
 
Does anyone have example code on how to read the binary digits from the OIs analog inputs? According to Mike only the first 4 binary digits are used to determine buttons being pressed.

For Example:

1000 0000 = Button 1
0100 0000 = Button 2
0010 0000 = BUtton 3
0001 0000 = Button 4

The trouble is the last four digits are random and fluctuate greatly
depending on what mood the chicklet is in so sometimes

1000 0000 = Button 1
or
1000 0001 = Button 1
or
1000 0010 = Button 1

The problem is we really don't want to make 4 cases for every button.
So we just want to ignore them.

kaszeta 08-03-2007 16:23

Re: Code to Read the Binary Inputs on Analog Buttons
 
Quote:

Originally Posted by Kingofl337 (Post 593324)
Does anyone have example code on how to read the binary digits from the OIs analog inputs? According to Mike only the first 4 binary digits are used to determine buttons being pressed.

The important thing is to use the bitwise AND function in C (&). That's what it's designed for.

Make a set of global variables, and a function that you call every time through the loop that sets these variables from the OI state.

Something like this should work (and I've gotta love any chance I have to legitimately use C' ?: trinary operator) :

Code:

int p1_sw_5,p1_sw_6,p1_sw_7,p1_sw_8; // Virtual buttons

void Buttons(void)
{
  p1_sw_5=(p1_aux&1<<7)?0:1;
  p1_sw_6=(p1_aux&1<<6)?0:1;
  p1_sw_7=(p1_aux&1<<5)?0:1;
  p1_sw_8=(p1_aux&1<<4)?0:1;
}


Kingofl337 08-03-2007 16:40

Re: Code to Read the Binary Inputs on Analog Buttons
 
Nice, never used that before :)

aegeon 13-03-2007 21:14

Re: Code to Read the Binary Inputs on Analog Buttons
 
now correct me if im wrong but isnt your use of the ? operator the same as the ! operator

im pretty sure that

p1_sw_5 = !(p1_aux&1<<7);
will do the same as
p1_sw_5=(p1_aux&1<<7)?0:1;

tdlrali 13-03-2007 21:35

Re: Code to Read the Binary Inputs on Analog Buttons
 
the "(statement)?true:false" syntax is like an if statement in one line
if statement, then true, else false

in this case, however, it would do the same

kaszeta 14-03-2007 11:01

Re: Code to Read the Binary Inputs on Analog Buttons
 
Quote:

Originally Posted by aegeon (Post 596951)
now correct me if im wrong but isnt youre use of the ? operator the same as the ! operator

You are correct.


All times are GMT -5. The time now is 20:26.

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