Function!! Servo, PS3, BT and beyond :S

Hey, I'm guessing some of you guys could help me out with some codeing and stuff! :P

I'm currently making some simple code for controlling a modded servo, my plan is to control about 4-8 servos/motors with a PS3 controller. Anyway, figured it would be easier if I defined some functions instead of repeating the code 4-8 times!

/*Control a modded servo via PS3 BT controller
  takes a PS3BT controller, two PS3 buttons and the servo to control
*/
void servoStyring(PS3BT PS3, Button dir1, Button dir2, Servo servo){
  int state=3;
  if(PS3.getButtonPress(dir1))state=0;
  if(PS3.getButtonPress(dir2))state=1;
  switch(state) {
           case 0:  servo.writeMicroseconds(2000);
           break;
           case 1:  servo.writeMicroseconds(1000);
           break;
           default: servo.writeMicroseconds(1500);
  }
}

/*Control a modded servo via PS3 BT controller
  takes a PS3BT controller, two PS3 analog-buttons and the servo to control
*/
void servoAnalogStyring(PS3BT PS3, AnalogButton dir1, AnalogButton dir2, Servo servo){
  int state=3;
  if(PS3.getAnalogButton(dir1)>0)state=0;
  if(PS3.getAnalogButton(dir2)>0)state=1;
  switch(state) {
           case 0:  
             servo.writeMicroseconds(map(PS3.getAnalogButton(dir1), 0, 254, 1500, 1700));
           break;
           case 1:  
             servo.writeMicroseconds(map(PS3.getAnalogButton(dir2), 0, 254, 1500, 1300 ));
           break;
           default: servo.writeMicroseconds(1500);
  }
}

I was windering if some of you guys know if it is possible to merge the two functions into one that takes either a Button or AnalogButton as arguments for the dir1 and dir2??

any other comments and critique on the code is appreciated :P :D

-Eivind

I was windering if some of you guys know if it is possible to merge the two functions into one that takes either a Button or AnalogButton as arguments for the dir1 and dir2??

That would depend on what Button and AnalogButton derived from,, to a large extent..

What do Button and AnalogButton derive from??

Given that the code to deal with a Button and an AnalogButton are so different,, I''m not sure why you would want to try to merge the two functions into one anyway..

The double spacing and punctuation looks a little silly, doesn't it?

eivinp: void servoAnalogStyring(PS3BT PS3, AnalogButton dir1, AnalogButton dir2, Servo servo){   int state=3;   if(PS3.getAnalogButton(dir1)>0)state=0;   if(PS3.getAnalogButton(dir2)>0)state=1;   switch(state) {            case 0:               servo.writeMicroseconds(map(PS3.getAnalogButton(dir1), 0, 254, 1500, 1700));            break;            case 1:               servo.writeMicroseconds(map(PS3.getAnalogButton(dir2), 0, 254, 1500, 1300 ));            break;            default: servo.writeMicroseconds(1500);   } }

This is a very tortuous way to do something very simple. Why not just use a couple of if statements?

As far as merging those two functions, the part of them which is similar should be reduced to a couple of lines of code, and what's left (i.e. working out what value to output based on the input) is not particularly similar. You could abstract the idea of two inputs with some precedence between them, but unless you have some particular need to deal with sets of these in some common way it seems like overkill.