59. GPIO Pin Class

#include <iostream>
using namespace std;

class GpioPin {
public:
    int pinNumber;
    int state;

    // Default constructor
    GpioPin() {
        state = 0; // LOW
    }

    void write(int value) {
        state = value;
    }

    int read() {
        return state;
    }
};

int main() {
    int pin, initialValue, finalValue;
    cin >> pin >> initialValue >> finalValue;

    GpioPin gpio;
    gpio.pinNumber = pin;

    gpio.write(initialValue);
    gpio.write(finalValue);

    cout << "GPIO Pin " << gpio.pinNumber
         << " State " << gpio.read();

    return 0;
}

Explanation & Logic Summary:

  • A class groups related variables and functions
  • Object is created using a default constructor
  • Pin number is assigned manually
  • Functions update and return the pin state
  • Output formatting demonstrates combining data and text

Firmware Relevance & Real-World Context:

  • Mimics how GPIO information is logged or printed in firmware debugging
  • Reinforces structured data usage before introducing OOP concepts
  • Ideal as the first class-based exercise in embedded C++ learning

 

 

 

 

Loading...

Input

13 0 1

Expected Output

GPIO Pin 13 State 1