61. UART Config Class

#include <iostream>
using namespace std;

class UartConfig {
private:
    int baudrate;
    int parity;
    int stopBits;

public:
    // Constructor initializes all fields
    UartConfig(int b, int p, int s)
        : baudrate(b), parity(p), stopBits(s) {}

    // Update all configuration fields
    void update(int b, int p, int s) {
        baudrate = b;
        parity = p;
        stopBits = s;
    }

    // Print configuration in required format
    void print() {
        cout << "baud=" << baudrate
             << " parity=" << parity
             << " stop=" << stopBits;
    }
};

int main() {
    int b1, p1, s1;
    int b2, p2, s2;

    cin >> b1 >> p1 >> s1;
    cin >> b2 >> p2 >> s2;

    UartConfig cfg(b1, p1, s1);
    cfg.update(b2, p2, s2);

    cfg.print();
    return 0;
}

Explanation & Logic Summary:

  • The UartConfig class encapsulates UART parameters using private data members.
  • The constructor ensures the object is initialized in a valid state.
  • The update() method demonstrates controlled modification of configuration data.
  • The print() method outputs the configuration in a deterministic, testable format.

Firmware Relevance & Real-World Context:

  • UART configuration objects or structs are foundational in embedded frameworks such as STM32 HAL, ESP-IDF, and Arduino.
  • Encapsulation prevents accidental misuse of hardware configuration values.
  • Runtime updates to UART settings are common during bootloaders, diagnostics, and protocol negotiation.

 

 

 

 

Loading...

Input

9600 0 1 115200 1 1

Expected Output

baud=115200 parity=1 stop=1