Friday, February 6, 2015

1Sheeld's GamePad and a Gameduino and you are Good-To-Go

After converting the Arduino Libraries to C# for Netduino I needed a little play time.  So, I decided to dig out my Gameduino and see what I could come up with.  It didn't take long to think "hmmm, 1Sheeld's GamePad may just work for the Frogger demo that the Gameduino folks have provided" ... and a small change to the Frogger Sketch (adding 1Sheeld Gamepad support) and voila, Frogger on an Arduino with 1Sheeld GamePad support...very, very cool!


Here's the entire Controller class using the OneSheeld library GamePad support, #include the OneSheeld.h at the top of the Frogger sketch and you will be crossing the street with a frog in tow in no time.

 class Controller {  
  public:  
   void begin() {  
    OneSheeld.begin();  
    prev = 0;  
   }  
   byte read() {  
    byte r = 0;  
    if (GamePad.isDownPressed())  
     r |= CONTROL_DOWN;  
    if (GamePad.isUpPressed())  
     r |= CONTROL_UP;  
    if (GamePad.isLeftPressed())  
     r |= CONTROL_LEFT;  
    if (GamePad.isRightPressed())  
     r |= CONTROL_RIGHT;  
    byte edge = r & ~prev;  
    prev = r;  
    return edge;  
   }  
  private:  
   byte prev;  
 };  

Put the 1Sheeld on an Arduino UNO r3 (or similar), put the Gameduino on the 1Sheeld.

For Manic Miner (updated):

Well, what I thought would work doesn't (the code I placed here earlier just doesn't seem to want to work).   What I have found is that it seems like it's best to just define pins on the 1Sheeld GamePad (rather than using the GamePad class) and then tweak the Manic Miner code like below:

 /* Setup 1Sheeld Gamepad Buttons to point to some pins... I used analog pins, but it probably doesn't matter. */  
 #define PIN_L A2  
 #define PIN_R A3  
 #define PIN_J A5  
   
 /* This was already in the code, but I just copied and pasted everything that needs some changes ... nothing here though */  
 #define CONTROL_LEFT 1  
 #define CONTROL_RIGHT 2  
 #define CONTROL_JUMP 4  
   
 /* Don't do any of the digitalWrite's in the setup_control() function ... I just commented them out... all we have to do is setup the pin modes for input. */  
 static byte setup_control()  
 {  
  pinMode(PIN_L, INPUT);  
  digitalWrite(PIN_L, HIGH);   
  pinMode(PIN_R, INPUT);  
  digitalWrite(PIN_R, HIGH);  
  pinMode(PIN_J, INPUT);  
  digitalWrite(PIN_J, HIGH);  
 }  
   
 /* Finally get rid of the ! in front of the digital read calls, we want the character to jump or move when we press the button(s) ... note, this was fine for the hardware joysticks, but for the 1Sheeld Game Pad, we need to do things a bit different! */  
 static byte control()  
 {  
  byte r = 0;  
  if (!digitalRead(PIN_J))  
   r |= CONTROL_JUMP;  
  if (!digitalRead(PIN_L))  
   r |= CONTROL_LEFT;  
  if (!digitalRead(PIN_R))  
   r |= CONTROL_RIGHT;  
  return r;  
 }  

One of the benefits of doing it this way is you don't have to load the OneSheeld libraries at all, so the code stays small enough to run on an UNO too!!!

Frogger could have been done similarly.

Updates

02-15-2015 - Updated changes for Manic Miner to work.

No comments:

Post a Comment