Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) RaspberryPi

 

RPiQt CreatorLubuntu

 

RaspberryPi is a collection of class for working with a Raspberry Pi.

Technical facts

 

 

 

 

 

 

./CppRaspberryPi/CppRaspberryPi.pri

 

INCLUDEPATH += \
    ../../Classes/CppDial

SOURCES += \
    ../../Classes/CppDial/dial.cpp

HEADERS  += \
    ../../Classes/CppDial/dial.h

OTHER_FILES += \
    ../../Classes/CppDial/Licence.txt

 

 

 

 

 

./CppRaspberryPi/raspberrypifwd.h

 

#ifndef RASPBERRYPIFWD_H
#define RASPBERRYPIFWD_H

namespace rpi {
  namespace gpio {
    struct Pin;
  } //~namespace gpio
} //namespace rpi

#endif // RASPBERRYPIFWD_H

 

 

 

 

 

./CppRaspberryPi/raspberrypigpiopin.h

 

#ifndef RASPBERRYPIGPIOPIN_H
#define RASPBERRYPIGPIOPIN_H

#include <set>

namespace rpi {

namespace gpio {

constexpr int GetMaxPinNumber() { return 26; }

struct Pin
{
  ///Create a pin and set its output to high
  Pin(const int pin_number);

  ///Create all valid Pin indices
  static const std::set<int> CreatePinNumbers();

  ///Get the pin number
  int GetPinNumber() const { return m_pin_number; }

  ///Set the output to high
  void SetOutputHigh();

  ///Set the output to low
  void SetOutputLow();

  //Toggle the output value
  void Toggle();

  private:
  ///Is the output high? Or is it low?
  bool m_is_high;

  ///The pin number
  const int m_pin_number;
};

bool operator<(const Pin& lhs, const Pin& rhs);

} //namespace gpio

} //namespace rpi

#endif // RASPBERRYPIGPIOPIN_H

 

 

 

 

 

./CppRaspberryPi/raspberrypigpiopin.cpp

 

#include "raspberrypigpiopin.h"

#include <cassert>
#include <iostream>
#include <string>

#include <boost/lexical_cast.hpp>

namespace rpi {

namespace gpio {


Pin::Pin(const int pin)
  : m_is_high(true),
    m_pin_number(pin)
{
  #ifndef NDEBUG
  const std::set<int> pins = CreatePinNumbers();
  assert(pins.count(pin));
  #endif
  this->SetOutputHigh();
}

const std::set<int> Pin::CreatePinNumbers()
{
  //Note that I commented out pin number 27
  //I do not know why I have so often included it, but I must have read it somewhere
  const std::set<int> pins { 3,5,7,8,10,11,12,13,15,16,18,19,21,22,23,24,26 };
  return pins;
}

void Pin::SetOutputHigh()
{
  {
    const std::string cmd = "echo \"" + boost::lexical_cast<std::string>(GetPinNumber()) + "\" > /sys/class/gpio/export";
    std::clog << cmd << '\n';
    const int error = std::system(cmd.c_str());
    if (error) std::cerr << "Error: " << error << '\n';
  }
  {
    const std::string cmd = "echo \"out\" > /sys/class/gpio/gpio" + boost::lexical_cast<std::string>(GetPinNumber())+ "/direction";
    std::clog << cmd << '\n';
    const int error = std::system(cmd.c_str());
    if (error) std::cerr << "Error: " << error << '\n';
  }
  {
    const std::string cmd = "echo \"1\" > /sys/class/gpio/gpio" + boost::lexical_cast<std::string>(GetPinNumber())+ "/value";
    std::clog << cmd << '\n';
    const int error = std::system(cmd.c_str());
    if (error) std::cerr << "Error: " << error << '\n';
  }
  m_is_high = true;
}

void Pin::SetOutputLow()
{
  {
    const std::string cmd = "echo \"0\" > /sys/class/gpio/gpio" + boost::lexical_cast<std::string>(GetPinNumber())+ "/value";
    std::clog << cmd << '\n';
    const int error = std::system(cmd.c_str());
    if (error) std::cerr << "Error: " << error << '\n';
  }
  {
    const std::string cmd = "echo \"" + boost::lexical_cast<std::string>(GetPinNumber()) + "\" > /sys/class/gpio/unexport";
    std::clog << cmd << '\n';
    const int error = std::system(cmd.c_str());
    if (error) std::cerr << "Error: " << error << '\n';
  }
  m_is_high = false;
}

void Pin::Toggle()
{
  m_is_high ? SetOutputLow() : SetOutputHigh();
}

bool operator<(const Pin& lhs, const Pin& rhs)
{
  assert(lhs.GetPinNumber() != rhs.GetPinNumber());
  return lhs.GetPinNumber() < rhs.GetPinNumber();
}

} //namespace gpio


} //namespace rpi

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml