3.Reading Pokitto buttons

Reading Pokitto Buttons

In this example we read the state of Pokitto buttons (pressed or not pressed) in the most simple way possible.

##Project on Pokitto mbed Community Team page:
https://os.mbed.com/teams/Pokitto-Community-Team/code/Buttons/

##Project settings (My_settings.h)

#define PROJ_HIRES 0
#define PROJ_ENABLE_SOUND 0

##Program file (TestButtons.cpp)

#include "Pokitto.h"

Pokitto::Core mygame;

int main () {
    mygame.begin();
    while (mygame.isRunning()) {
        if (mygame.update()) {         
            mygame.display.setColor(1); // set text color - let's make all texts in different color!
            mygame.display.println("Button test"); // println prints a row of text and goes to the next row (=enter)
            mygame.display.setColor(2); 
            if (mygame.buttons.aBtn()) mygame.display.println("A");
            mygame.display.setColor(3); 
            if (mygame.buttons.bBtn()) mygame.display.println("B");
            mygame.display.setColor(4); 
            if (mygame.buttons.cBtn()) mygame.display.println("C");
            mygame.display.setColor(5); 
            if (mygame.buttons.upBtn()) mygame.display.println("Up");
            mygame.display.setColor(6); 
            if (mygame.buttons.downBtn()) mygame.display.println("Down");
            mygame.display.setColor(7); 
            if (mygame.buttons.leftBtn()) mygame.display.println("Left");
            mygame.display.setColor(8); 
            if (mygame.buttons.rightBtn()) mygame.display.println("Right");      
        }
    }
    return 0; // report success - doing this at the end of main is a good habit
}
6 Likes

Maybe this can help.

#include "Pokitto.h"

Pokitto::Core mygame;

int main () {
    mygame.begin();
    while (mygame.isRunning()) {
        if (mygame.update()) {            
            mygame.display.print("Idemo do kraja");
            } 
        if (mygame.buttons.aBtn())
        {
            mygame.display.print("A");
        } 
        if (mygame.buttons.bBtn())
        {
            mygame.display.print("B");
        }     
        if (mygame.buttons.cBtn())
        {
            mygame.display.print("C");
        }    
        if (mygame.buttons.upBtn())
        {
            mygame.display.print("Up");
        }     
        if (mygame.buttons.downBtn())
        {
            mygame.display.print("Down");
        } 
        if (mygame.buttons.leftBtn())
        {
            mygame.display.print("Left");
        } 
        if (mygame.buttons.rightBtn())
        {
            mygame.display.print("Right");
        }  
        
    }
    return 0; // this is "good programming manners"
}
1 Like

ummm… should this be in it as well ?

return 0; // this is “good programming manners”. Program informs it ended without errors

:blush:

2 Likes

Would be best to enable some warnings flags (e.g. -Wall or the dreaded -pedantic) to the compiler tells you that’s wrong, including function supposed to be returning a value but not only :blush:

1 Like

Some people think it’s more ‘idiomatic’ to not have a return 0; if no error codes are being returned at any point.
I’m in the camp that believes adding a return 0; to main is good practice, and I certainly think it’ll make things easier for beginners because they won’t be asking "how come main doesn’t need to return 0;".

(To be honest, this is one of those times where I disagree with the standard - I think void main(void) ought to be allowed, precisely because of situations like this.)

1 Like

There is actually a very good reason for adding the return statement.

Pokitto programs can also be compiled on Code::Blocks into Windows/Linux/Mac targets. Even though on an embedded hardware the return statement has no effect, performing a clean exit is certainly a better idea on a multitasking OS.

1 Like

I was actually a little joking towards Jonne, he put this in the first example but left it out in the second one. :smile:

I concur.

I’ve edited it in.
If anyone wants to change the comment they can - this is a wiki after all.

1 Like