46. GPIO State Enum

#include <iostream>
#include <cstdint>

enum class PinState : uint8_t {
    Low,
    High
};

const char* toString(PinState s) {
    switch (s) {
        case PinState::Low:  return "LOW";
        case PinState::High: return "HIGH";
    }
    return "LOW"; // defensive fallback
}

int main() {
    int x;
    std::cin >> x;

    PinState s = (x == 0) ? PinState::Low : PinState::High;
    std::cout << toString(s);
    return 0;
}

Explanation & Logic Summary:

  • enum class enforces strong typing and scoped names (PinState::Low).
  • uint8_t matches typical GPIO register widths in embedded systems.
  • toString centralizes conversion for logging or debug output.

Firmware Relevance & Real Embedded Meaning:

  • Eliminates magic numbers in GPIO state handling.
  • Encourages type-safe APIs for hardware abstraction layers.
  • Demonstrates efficient enum sizing aligned with hardware registers.

 

 

 


 

Loading...

Input

0

Expected Output

LOW