FSST - Hardwarenahe Programmierung

Blinky Codes 1

Methoden

Standard Blinky

aus mbed-Simulator

und Intro

Copy-Paste die folgenden Codes

#include "mbed.h"

DigitalOut led(LED1);

int main() {
    while (1) {
        led = !led;
        printf("Blink! LED is now %d\n", led.read());

        wait_ms(500);
    }
}

Mit Funktionen

#include "mbed.h"

DigitalOut led(LED1);  // globale Variable led
 
void LedOn() {
    led = 1;
}
void LedOff() {
    led = 0;
}
int read() {
    return led.read();
}

int main() {
    printf("Blink LED is now %d\n", read());
    while (1) {
        LedOn();
        wait_ms(500);
        LedOff();
        wait_ms(500);
    }
}

Funktionsprototyping

#include "mbed.h"
// Funktions-Prototyping bzw. -Deklaration
void LedOn();
void LedOff();
int read(); 

DigitalOut led(LED1);   // globale Variable led

int main() {
    printf("Blink LED is now %d\n", read());
    while (1) {
        LedOn();        // Funktionsaufruf
        wait_ms(500);
        LedOff();
        wait_ms(500);
    }
}
// Funktions-Definition
void LedOn() {
    led = 1;
}
void LedOff() {
    led = 0;
}
int read() {
    return led.read();
}

Mit Parameter

#include "mbed.h"
// Funktions-Prototyping bzw. -Deklaration
void LedOn(DigitalOut );    // nur Datentype für Compiler von Interesse
void LedOff(DigitalOut ld);
int read(DigitalOut led); 

DigitalOut led(LED1);       // globale Variable led

int main() {
    printf("Blink LED is now %d\n", read(led));
    while (1) {
        LedOn(led);         // Funktionsaufruf
        wait_ms(500);
        LedOff(led);        // aktueller Parameter led aud Stack
        wait_ms(500);
    }
}
// Funktions-Definition
void LedOn(DigitalOut ld) { // formaler Parameter ld initialisiert mit Wert von Stack
    ld = 1;
}
void LedOff(DigitalOut led) {
    led = 0;
}
int read(DigitalOut digOut) { 
    return digOut.read();
}

Mit Klasse HasA

#include "mbed.h"

class Blinky {
public:
    Blinky(PinName ld) : _myled(ld) {}
    void LedOn() {
        _myled = 1;
    }
    void LedOff() {
        _myled = 0;
    }
    int read() {
        return _myled.read();
    }
private:
    DigitalOut _myled;
};

Blinky led(LED1);

//DigitalOut led(LED1);

int main() {
    printf("Blink LED is now %d\n", led.read());
    while (1) {
        led.LedOn();
        wait_ms(500);
        led.LedOff();
        wait_ms(500);
    }
}

Überladene Methode

#include "mbed.h"
 
class Blinky {
public:
    Blinky(PinName ld) : _myled(ld) {}
    void LedOn() {
        _myled = 1;
    }
    // überladene Methode LedOn
    void LedOn(int wert) {
        _myled = wert;
    }
    void LedOff() {
        _myled = 0;
    }
    int read() {
        return _myled.read();
    }
private:
    DigitalOut _myled;
};
 
Blinky led(LED1);
 
int main() {
    printf("Blink LED is now %d\n", led.read());
    while (1) {
        led.LedOn(1);
        wait_ms(500);
        led.LedOff();
        wait_ms(500);
    }
}

Überladener Konstruktor

#include "mbed.h"
 
class Blinky {
public:
    Blinky() : _myled(LED1) {}          // Standardkonstruktor
    Blinky(PinName ld) : _myled(ld) {}  // überladener Konstruktor
    void LedOn() {
        _myled = 1;
    }
    // überladene Methode LedOn
    void LedOn(int wert) {
        _myled = wert;
    }
    void LedOff() {
        _myled = 0;
    }
    int read() {
        return _myled.read();
    }
private:
    DigitalOut _myled;
};
 
//Blinky led(LED1);  // Konstruktor mit LED-Pin als Argument
Blinky led;          // Aufruf Standardkonstruktor

int main() {
    printf("Blink LED is now %d\n", led.read());
    while (1) {
        led.LedOn(1);
        wait_ms(500);
        led.LedOff();
        wait_ms(500);
    }
}

Vererbung IsA

#include "mbed.h"

class DigOut : public DigitalOut
{
public:
    DigOut() : DigitalOut(LED1, 0) {};
    DigOut(PinName _pin) : DigitalOut(_pin, 0) {  }
    
    void LedOn() {
        write(1);
    }
    void LedOff() {
        write(0);
    }
    DigitalOut &operator= (int value)
    {
        write(value);
        return *this;
    }
};

DigOut led1;
DigOut led(LED2);

int main() {
    while (1) {
        led = !led;
        printf("Blink! LED is now %d\n", led.read());
//        led.LedOn();
//        wait_ms(500);
//        printf("Blink! LED is now %d\n", led.read());
//        led.LedOff();
        wait_ms(500);    
    }
}

HexLeds

#include "mbed.h"
class HexLeds
{
public:
    HexLeds(PinName pin1, PinName pin2, PinName pin3, PinName pin4) : _pin1(pin1), _pin2(pin2), _pin3(pin3), _pin4(pin4), _Leds(pin1, pin2, pin3, pin4) 
    {
        _pin1 = 0;
        _pin2 = 0;
        _pin3 = 0;
        _pin4 = 0;
        _Leds = 0;
    }
    void input(void);
    void output(void);
    void hexOut(void);
    void printStatus(void);
 
private:
    BusOut _Leds;
    DigitalOut _pin1;
    DigitalOut _pin2;
    DigitalOut _pin3;
    DigitalOut _pin4;
    int _Hexwert;
};
 
void HexLeds::input(void)
{
    int temp = 0;
    scanf ("%d", &temp);
    if(temp < 16)
        _Hexwert = temp;
    else
        _Hexwert = 0;
}
 
void HexLeds::output(void)
{
    printf("Wert = %d\n", _Hexwert);
}
 
void HexLeds::hexOut(void)
{
    _Leds = _Hexwert;
}
 
void HexLeds::printStatus(void)
{
    printf("LED Status:\n LED1: %d\n LED2: %d\n LED3: %d\n LED4: %d\n",
           _pin1.read(), _pin2.read(), _pin3.read(), _pin4.read());
}

HexLeds hexleds(LED1, LED2, LED3, LED4);
 
int main() 
{
   hexleds.input();     
   hexleds.output();     
   hexleds.hexOut();      
   hexleds.printStatus(); 
}

Deklaration

Diskussion Deklaration versus Definition


All wikipages