uXe
October 30, 2017, 12:55am
1
A little frustrated at the simple task of just reading buttons without a clear example…
Looking at https://github.com/pokitto/PokittoLib/blob/master/Pokitto/POKITTO_CORE/PokittoButtons.cpp didn’t help, because it is a jumble of some things that are apparently only for the Simulator and some that are not…
I get that mygame.begin() and mygame.update() initialise the buttons, and then update them.
But if I want for example to read the button states into a variable:
uint8_t buttons = …what?
and then which bits in that byte correspond to which buttons?
Thanks!
Pharap
October 30, 2017, 1:06am
2
It’s a little bit more complicated than that.
The Pokitto doesn’t store pressed buttons as a mask, it stores them in an array. The button states are saved into an array that logs how long they’ve been held for.
// GB compatibility functions
static void begin();
static void update();
static bool pressed(uint8_t button);
static bool released(uint8_t button);
static bool held(uint8_t button, uint8_t time);
static bool repeat(uint8_t button, uint8_t period);
static uint8_t timeHeld(uint8_t button);
static uint8_t pins[NUM_BTN];
static uint8_t states[NUM_BTN];
static uint8_t heldStates[NUM_BTN]; // additional, not GB based
private:
static uint16_t cHWLongPress; //forced hardware detection of C long press
};
}
#endif // POKITTOBUTTONS_H
You can access that array freely though, so if you really need to pack them into a single byte then you can do.
jonne
October 30, 2017, 4:51am
3
Sorry. I will try to address this asap
3 Likes
spinal
October 30, 2017, 7:17am
4
I read buttons slightly different to the pokitto lib, that is, I do it manually.
/**************************************************************************/
#define HELD 0
#define NEW 1
#define RELEASE 2
byte CompletePad, ExPad, TempPad, myPad;
bool _A[3], _B[3], _C[3], _Up[3], _Down[3], _Left[3], _Right[3];
DigitalIn _aPin(P1_9);
DigitalIn _bPin(P1_4);
DigitalIn _cPin(P1_10);
DigitalIn _upPin(P1_13);
DigitalIn _downPin(P1_3);
DigitalIn _leftPin(P1_25);
DigitalIn _rightPin(P1_7);
void UPDATEPAD(int pad, int var) {
_C[pad] = (var >> 1)&1;
_B[pad] = (var >> 2)&1;
_A[pad] = (var >> 3)&1;
_Down[pad] = (var >> 4)&1;
_Left[pad] = (var >> 5)&1;
_Right[pad] = (var >> 6)&1;
_Up[pad] = (var >> 7)&1;
}
void UpdatePad(int joy_code){
ExPad = CompletePad;
CompletePad = joy_code;
UPDATEPAD(HELD, CompletePad); // held
UPDATEPAD(RELEASE, (ExPad & (~CompletePad))); // released
UPDATEPAD(NEW, (CompletePad & (~ExPad))); // newpress
}
byte updateButtons(byte var){
var = 0;
if (_cPin) var |= (1<<1);
if (_bPin) var |= (1<<2);
if (_aPin) var |= (1<<3); // P1_9 = A
if (_downPin) var |= (1<<4);
if (_leftPin) var |= (1<<5);
if (_rightPin) var |= (1<<6);
if (_upPin) var |= (1<<7);
return var;
}
/**************************************************************************/
// inside main loop
myPad = updateButtons(myPad);
UpdatePad(myPad);
// to read buttons, use something like...
if(_A[NEW]){
// newpress A
}
if(_Up[HELD]){
// holding UP
}
if(_Up[RELEASE]){
// released UP
}
Although my way does not count how many frames a button has been held for, it does update the button states ONLY when YOU want them updated.
New to pokitto here! I’m trying to read the button held state as well and I noticed there’s a series of methods of the form “aHeld” right besides “aBtn” but they don’t seem to be implemented. I used the alternative suggested above (using DigitalIn) and that seems to work fine but it appears it’s not implemented on the Simulator (which helps a lot for iteration).
Are there any workarounds to this? I don’t mind using either of the solutions, as long as it’s consistent between the device and the simulator.
jonne
November 19, 2017, 6:51am
6
I will make an example here today
Basically the simplest (correct way) is:
Pokitto::Core game;
And then
if (game.buttons.aBtn()) ... //instantaneous check
2 Likes
What It seems to miss from the api is the xxxPressed() method.
I use
Is there a reason Released is present but Released not?
I’ve tried that (aBtn) and it works for simple cases, but I’m interested in requiring the user to press the button again (no automated simulated button presses when holding). In my experience this is critical to make responsive game play. The function of the form “aHeld” is ideal in my case but it doesn’t seem to be implemented.
Thanks for looking into it, jonne!
jonne
November 19, 2017, 8:07am
9
I’m going out for a couple of hrs. When I come back I will put up a complete wiki on buttons
3 Likes