Permalink
Browse files

Migrated joypad class to C, using Python-like syntax

  • Loading branch information...
1 parent bcf82d1 commit 2468365f3fe6dcaa6d139c449b496a549512442f @Chingzilla committed Nov 14, 2011
Showing with 159 additions and 176 deletions.
  1. +123 −0 lib/joypad/joypad.c
  2. +0 −144 lib/joypad/joypad.cpp
  3. +36 −32 lib/joypad/joypad.h
View
@@ -0,0 +1,123 @@
+/**
+ * joypad.cpp
+ * Implements joypad.h
+ **/
+
+#include "joypad.h"
+
+// Joypad constructors
+void joypad_init(Joypad * self){
+ joypad_init(&self, JOYPADDEFAULT);
+}
+
+void joypad_init(Joypad * self, int joy_num){
+
+ self.joy_num = joy_num;
+
+ //set deadzones
+ self.joy1_deadzones = DEADZONE;
+ self.joy2_deadzones = DEADZONE;
+
+ //set joystick scales
+ setJoystickScale(&self, 1, JOYPADSCALE);
+ setJoystickScale(&self, 2, JOYPADSCALE);
+
+ //get 1st values from joypad
+ update(&self);
+}
+
+// Joypad functions
+void update(Joypad * self){
+ //update joystick values
+ getJoystickSettings(self.joy_num);
+
+ //update joystick vaules
+ switch(self.joy_num){
+ case 1:
+ setJoyValue(&self, 1, joystick.joy1_x1, joystick.joy1_y1);
+ setJoyValue(&self, 2, joystick.joy1_x2, joystick.joy1_y2);
+ break;
+ case 2:
+ setJoyValue(&self, 1, joystick.joy2_x1, joystick.joy2_y1);
+ setJoyValue(&self, 2, joystick.joy2_x2, joystick.joy2_y2);
+ break;
+ case *:
+ //Currently, teleop only suports 2 joysticks
+ self.joy1_x = 0;
+ self.joy1_y = 0;
+ self.joy2_x = 0;
+ self.joy2_y = 0;
+ break;
+ }
+}
+
+bool isButtonPressed(Joypad * self, int button){
+ switch(self.joy_num){
+ case 1:
+ return (joystick.joy1_Buttons & (1 << (button - 1)) != 0)
+ break;
+ case 2:
+ return (joystick.joy2_Buttons & (1 << (button - 1)) != 0)
+ break;
+ case *:
+ return false;
+ }
+}
+
+void setDeadzone(Joypad * self, int joystick, int deadzone){
+ switch(joystick){
+ case 1:
+ self.joy1_deadzone = deadzone;
+ break;
+ case 2:
+ self.joy2_deadzone = deadzone;
+ break;
+ case *:
+ break;
+ }
+}
+
+void setJoystickScale(Joypad * self, int joystick, double scale){
+ setJoystickScale(&self, joystick, scale, scale);
+}
+
+void setJoystickScale(Joypad * self, int joystick, double scale_x, double scale_y){
+ //Adjustment to fix range(-128 -> 127) to range(-100 -> 100)
+ const double range_scale = 100/127;
+
+ switch(joystick){
+ case 1:
+ self.joy1_scale_x = scale_x * range_scale;
+ self.joy1_scale_y = scale_y * range_scale;
+ break;
+ case 2:
+ self.joy2_scale_x = scale_x * range_scale;
+ self.joy2_scale_y = scale_y * range_scale;
+ break;
+ case *:
+ break;
+ }
+}
+
+void setJoyValue(Joypad * self, int joystick, int raw_x, int raw_y){
+ switch(joystick){
+ case 1:
+ self.joy_x = scaleJoystick(raw_x, self.joy1_scale_x, self.joy1_deadzone);
+ self.joy_y = scaleJoystick(raw_y, self.joy1_scale_y, self.joy1_deadzone);
+ break;
+ case 2:
+ self.joy_x = scaleJoystick(raw_x, self.joy2_scale_x, self.joy2_deadzone);
+ self.joy_y = scaleJoystick(raw_y, self.joy2_scale_y, self.joy2_deadzone);
+ break;
+ }
+}
+
+int scaleJoystick(int joy_raw, double scale, int deadzone){
+ if( abs(joy_raw) > deadzone ){
+ return (int)((double)joy_raw * scale);
+ }
+ else{
+ //Return zero if joystick is within the deadzone
+ return 0;
+ }
+}
View
@@ -1,144 +0,0 @@
-/**
- * joypad.cpp
- * Implements joypad.h
- **/
-
-#include "joypad.h"
-
-// Joypad constructors
-Joypad::Joypad(){
- Joypad(JOYPADDEFAULT);
-}
-
-Joypad::Joypad(int joy_num){
- this->joy_num = joy_num;
-
- //set deadzones
- joy1_deadzones = DEADZONE;
- joy2_deadzones = DEADZONE;
-
- //set joystick scales
- setJoystickScale(1, JOYPADSCALE);
- setJoystickScale(2, JOYPADSCALE);
-
- //get 1st values from joypad
- update();
-}
-
-// Joypad functions
-
-Joypad::update(){
- //update joystick values
- getJoystickSettings(joy_num);
-
- //update joystick vaules
- switch(joy_num){
- case 1:
- setJoyValue(1, joystick.joy1_x1, joystick.joy1_y1);
- setJoyValue(2, joystick.joy1_x2, joystick.joy1_y2);
- break;
- case 2:
- setJoyValue(1, joystick.joy2_x1, joystick.joy2_y1);
- setJoyValue(2, joystick.joy2_x2, joystick.joy2_y2);
- break;
- case *:
- joy1_x = 0;
- joy1_y = 0;
- joy2_x = 0;
- joy2_y = 0;
- break;
- }
-}
-
-Joypad::isButtonPressed(int button){
- int btn;
-
- switch(joy_num){
- case 1:
- return (joystick.joy1_Buttons & (1 << (button - 1)) != 0)
- break;
- case 2:
- return (joystick.joy2_Buttons & (1 << (button - 1)) != 0)
- break;
- case *:
- return false;
- }
-}
-
-Joypad::setDeadzone(int joystick, int deadzone){
- switch(joystick){
- case 1:
- joy1_deadzone = deadzone;
- break;
- case 2:
- joy2_deadzone = deadzone;
- break;
- case *:
- break;
- }
-}
-
-Joypad::setJoystickScale(int joystick, double scale){
- setJoystickScale(joystick, scale, scale);
-}
-
-Joypad::setJoystickScale(int joystick, double scale_x, double scale_y){
- //Adjustment to fix range(-128 -> 127) to range(-100 -> 100)
- const double range_scale = 100/127;
-
- switch(joystick){
- case 1:
- joy1_scale_x = scale_x * range_scale;
- joy1_scale_y = scale_y * range_scale;
- break;
- case 2:
- joy2_scale_x = scale_x * range_scale;
- joy2_scale_y = scale_y * range_scale;
- break;
- case *:
- break;
- }
-}
-
-Joypad::setJoyValue(int joystick, int raw_x, int raw_y){
-
- //use pointers to limit memory and cpu overhead
- double * scale_x;
- double * scale_y;
-
- int * joy_x;
- int * joy_y;
-
- int * deadzone;
-
- switch(joystick){
- case 1:
- scale_x = &joy1_scale_x;
- scale_y = &joy1_scale_y;
- joy_x = &joy1_x;
- joy_y = &joy1_x;
- deadzone = &joy1_deadzone;
- break;
- case 2:
- scale_x = &joy2_scale_x;
- scale_y = &joy2_scale_y;
- joy_x = &joy2_x;
- joy_y = &joy2_x;
- deadzone = &joy2_deadzone;
- break;
- }
-
- if( abs(*joy_x) > *deadzone ){
- *joy_x = (int)( (double)raw_x * *scale_x );
- }
- else{
- *joy_x = 0;
- }
-
- if( abs(*joy_y) > *deadzone ){
- *joy_y = (int)( (double)raw_y * *scale_y );
- }
- else{
- *joy_y = 0;
- }
-}
View
@@ -1,18 +1,16 @@
/**
* joypad.h
- * library of fuctions and classes that offers an advanced interface to the
+ * library of fuctions and 'classes' that offers an advanced interface to the
* Logitec joystick
**/
#ifndef JOYPAD_H
#define JOYPAD_H
-#include "JoystickDriver.c"
-
// Default deadzone size for joysticks
-const int DEADZONE = 3;
-const int JOYPADDEFALT = 1;
-const double JOYPADSCALE = 1.0;
+const int DEADZONE = 3;
+const int JOYPADDEFALT = 1;
+const double JOYPADSCALE = 1.0;
//TODO: get real button values
const int BUTTON_A = 1;
@@ -29,58 +27,64 @@ const int BUTTON_LS1 = 9;
const int BUTTON_LS2 = 9;
//Joystick class used for collecting i/o from logitec joypad when using tele-op
-class Joypad {
-
+typedef struct Joypad {
+ //public:
/************** Variables **************/
- public int joy1_x;
- public int joy1_y;
- public int joy2_x;
- public int joy2_y;
+ int joy1_x;
+ int joy1_y;
+ int joy2_x;
+ int joy2_y;
+ //private:
//joystick offset
- private int joy_num;
+ int joy_num;
- private double joy1_scale_x;
- private double joy1_scale_y;
- private double joy2_scale_x;
- private double joy2_scale_y;
+ double joy1_scale_x;
+ double joy1_scale_y;
+ double joy2_scale_x;
+ double joy2_scale_y;
//dead zone varables
- private int joy1_deadzone;
- private int joy2_deadzone;
-
+ int joy1_deadzone;
+ int joy2_deadzone;
+} Joypad
+
/*************** Constructors **************/
+
// Create joystick with default vaule
- public:
- Joypad();
+ void joypad_init(Joypad * self);
// Pass in joystick's number
- Joypad(int joy_num);
+ void joypad_init(Joypad * self, int joy_num);
+
/*************** Functions **************/
- public:
+
/** Fuction: update
* Discription: updates values for joystick.
* IMPORTANT: call functions before an values are to be used
**/
- void update();
+ void update(*Joypad self);
//TODO
- bool isButtonPressed(int button);
+ bool isButtonPressed(*Joypad self, int button);
//TODO
- void setDeadzone(int joystick, int deadzone);
+ void setDeadzone(*Joypad self, int joystick, int deadzone);
//TODO
- void setJoystickScale(int joystick, double scale);
+ void setJoystickScale(*Joypad self, int joystick, double scale);
//TODO
- void setJoystickScale(int joystick, double scale_x, double scale_y);
+ void setJoystickScale(*Joypad self, int joystick, double scale_x, double scale_y);
+
+ //TODO
+ int setJoyValue(*Joypad self, int joystick, int raw_value);
+
- private:
+ /***************** Non-class fuctions******************/
//TODO
- int setJoyValue(int joystick, int raw_value);
-}
+ int scaleJoystick(int joy_raw, double scale, int deadzone);
#endif //JOYPAD_H

0 comments on commit 2468365

Please sign in to comment.