bla

include the mbed library with this snippet

// Calculator: Addirion and Subtraction
// Joystick Up: +  ; Joystick Down -
// Joystick Left: count value up (+1); Joystick Right: count value down (-1)
// Joystick Calculate
// Joystick Left or Joystick Right: start at input of value 1 again

#include "mbed.h"
#include "C12832_lcd.h"

InterruptIn iiUp(p15);
DigitalIn diUp(p15);
InterruptIn iiDown(p12);
DigitalIn diDown(p12);
InterruptIn iiCenter(p14);
DigitalIn diCenter(p14);
InterruptIn iiLeft(p13);
DigitalIn diLeft(p13);
InterruptIn iiRight(p16);
DigitalIn diRight(p16);
uint8_t step = 0;
char calcOp = '+';
bool updateLcd = true;
int value1, value2, result;

// prototypes
void count_up(){
    switch (step) {
        case 0 : value1++;
                 updateLcd = true;
                 break;    
        case 1 : step = 2;
                 break;
        case 2 : value2++;
                 updateLcd = true;
                 break;
        case 3 : value1 = 0;
                 value2 = 0;
                 step = 0;
                 break;
    }   
}

void count_down(){
    switch (step) {
        case 0 : value1--;
                 updateLcd = true;
                 break;    
        case 1 : step = 2;
                 break;
        case 2 : value2--;
                 updateLcd = true;
                 break;
        case 3 : value1 = 0;
                 value2 = 0;
                 step = 0;
                 break;
    }
    
}

void calculate(){
    step = 3;
    result = 0;
    if (calcOp == '+') result = value1 + value2;
    if (calcOp == '-') result = value1 - value2;
    updateLcd = true;
}

void opChange(){
    step = 1;
    if (calcOp == '+') calcOp = '-';
        else calcOp = '+';
        updateLcd = true;
}

// functions
uint8_t debounce(DigitalIn myIn)  
{
    #define LEVEL_CHECKS 8
    #define MAX_LOOPS 30                // stoppt das Überprüfen des Prellen nach max. MAX_LOOPS Durchläufen
    unsigned char port_buffer;
    unsigned char debounceCounter = 0;
    uint8_t loopCounter = 0;
    
    do
    {
        port_buffer = myIn;
        wait_us(100);
        loopCounter++;
        if(myIn == port_buffer)
          debounceCounter++;    // mindestens 'LEVEL_CHECKS' Abtastungen in Folge: gleicher Pegel
        else
          debounceCounter = 0;      
    }
    while ((debounceCounter <= LEVEL_CHECKS) && (loopCounter <= MAX_LOOPS));
    return loopCounter;
}

// ISR



// main program
int main() {
    C12832_LCD lcd;
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("5ABELI: Calulator");

    iiLeft.rise(&count_up);
    iiRight.rise(&count_down);
    iiUp.rise(&opChange);   
    iiDown.rise(&opChange); 
    iiCenter.rise(&calculate);
    
    while(1) { 
        if (updateLcd) {
            updateLcd = false;
            switch (step) {
                case 0: lcd.locate(0,10);
                        lcd.printf("%d         ", value1);  
                        break;
                
                case 1: lcd.locate(0,10);
                        lcd.printf("%d %c",value1, calcOp);
                        break;
                
                case 2: lcd.locate(0,10);
                        lcd.printf("%d %c %d", value1, calcOp, value2);
                        break;
                
                case 3: lcd.locate(0,10);
                        lcd.printf("%d %c %d = %d", value1, calcOp, value2, result);
                        break;

                default: break;                                                                                                                                                                                                                                                                                                                                                                                                                                       
            }
        }
    }
}

Changes

RevisionDateWhoCommit message