bdfagfdasgfd

#include "mbed.h"


Serial pc(USBTX, USBRX); // tx, rx
 
unsigned int newEvent = 0; 
unsigned int event = 0, state = 0;

InterruptIn iiJsUp(p15);   
DigitalIn diJsUp(p15);

char OP;
int erg;

int noFunction();
int getOperator();
int printOutput();
uint8_t debounce(DigitalIn myIn);

uint8_t nextstate_tab[2][2]=
// current event/  present state 0  1  
//----------------------
/* Event 0 (Joystick UP)    */{{ 1, 1 },  
/* Event 1 (Eingabe      )  */ { 0, 0 }}; 
        
int (*action[2][2])()= //      0        1  
/* event 0 */       {{ getOperator, noFunction },
/* event 1 */        { noFunction, printOutput }};

int noFunction() {
    return 0;
}
 
int getOperator() {
      
    int RandomNumber1;
    int RandomNumber2;       
    OP = pc.getc();
    
    RandomNumber1 = rand() % 100 + 1;
    RandomNumber2 = rand() % 101 + (-50);
    

    switch(OP) {
        case '+' : erg = RandomNumber1 + RandomNumber2;
        break;
        case '-' : erg = RandomNumber1 - RandomNumber2;
        break;    
    }

    return 0;
}

int printOutput(){
    pc.printf(erg);
    event = 0;
    return 0;
}

int upPressed(){
    event++;
    newEvent = 1;
    return 0;
}

void init() {   
    iiJsLeft.rise(&upPressed);
    state = 0; 
    event = -1;
}

unsigned char debounce(PinName name, unsigned char samples) {
    DigitalIn joystick(name);   
    unsigned char i = 0; 
    
    for(unsigned char j=0; j < samples; j++) {
        if(joystick == 1)
            j++;    
        else {
            j = 0;
            i++;
        }
            
        if(j >= samples)
            break;         
        if(i >= samples)
            break;
            
        wait(0.001);
    }
    return joystick;
}

int main() {
    
    unsigned char released = 0;
    init();
    
    pc.printf("Random Calculator mit der Taste JoyStick UP starten: Mark Ilgerl");
    
    while(1) {

        if (newEvent==1) {
            newEvent = 0;
            (*action[event][state])(); 
            state=nextstate_tab[event][state]; 
        }
        
    }
    
}

History