Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Led

 

STLQt CreatorLubuntu

 

Led is a class for an LED light.

 

Led is used in the tool TestLed.

Technical facts

 

 

 

 

 

 

./CppLed/CppLed.pri

 

INCLUDEPATH += \
    ../../Classes/CppLed

SOURCES += \
    ../../Classes/CppLed/led.cpp

HEADERS  += \
    ../../Classes/CppLed/led.h

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

 

 

 

 

 

./CppLed/led.h

 

//---------------------------------------------------------------------------
/*
Led, LED class
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLed.htm
//---------------------------------------------------------------------------
#ifndef LED_H
#define LED_H

#include <string>
#include <vector>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <boost/checked_delete.hpp>
#include <boost/make_shared.hpp>
#include <boost/signals2.hpp>
#pragma GCC diagnostic pop

namespace ribi {

///Led manages an LED lamp
struct Led
{
  explicit Led(
    const double intensity    = 0.0,
    const int red   = 255,
    const int green =   0,
    const int blue  =   0);
  Led(const Led&) = delete;
  Led& operator=(const Led&) = delete;

  mutable boost::signals2::signal<void (Led*)> m_signal_color_changed;
  mutable boost::signals2::signal<void (Led*)> m_signal_intensity_changed;

  ///Set the Led its color
  void SetColor(
    const int red,
    const int green,
    const int blue
  ) noexcept;

  ///Get the Led its blueness
  int GetBlue()  const noexcept { return m_blue;  }

  ///Get the Led its greenness
  int GetGreen() const noexcept { return m_green; }

  ///Get the Led its intensity, from 0.0 to 1.0
  double GetIntensity() const noexcept { return m_intensity; }

  ///Get the Led its redness
  int GetRed() const noexcept { return m_red;   }

  ///Obtain this class its version
  static std::string GetVersion() noexcept;

  ///Obtain this class its version history
  static std::vector<std::string> GetVersionHistory() noexcept;

  ///Set the Led its blueness, range [0.0,255]
  void SetBlue(const int blue) noexcept;

  ///Set the Led its greenness, range [0.0,255]
  void SetGreen(const int green) noexcept;

  ///Set the Led its intensity, from 0.0 to 1.0
  void SetIntensity(const double intensity) noexcept;

  ///Set the Led its redness, range [0.0,255]
  void SetRed(const int red) noexcept;

  std::string ToStr() const noexcept;
  std::string ToXml() const noexcept;

  private:
  virtual ~Led() noexcept {}
  friend void boost::checked_delete<>(Led*);
  friend void boost::checked_delete<>(const Led*);
  friend struct std::default_delete<      Led>;
  friend struct std::default_delete<const Led>;
  friend class boost::detail::sp_ms_deleter<      Led>;
  friend class boost::detail::sp_ms_deleter<const Led>;

  ///The Led its blueness, range [0.0,255]
  int m_blue;

  ///The Led its greenness, range [0.0,255]
  int m_green;

  ///m_intensity has range [0.0,1.0]
  double m_intensity;

  ///The Led its redness, range [0.0,255]
  int m_red;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif

  friend std::ostream& operator<<(std::ostream& os, const Led& led) noexcept;

};

std::ostream& operator<<(std::ostream& os, const Led& led) noexcept;
bool operator==(const Led& lhs, const Led& rhs) noexcept;

} //~namespace ribi

#endif // LED_H

 

 

 

 

 

./CppLed/led.cpp

 

//---------------------------------------------------------------------------
/*
Led, LED class
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLed.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "led.h"

#include <cassert>
#include <iostream>

#include <boost/lexical_cast.hpp>

#include "testtimer.h"
#include "trace.h"
#include "testtimer.h"

#pragma GCC diagnostic pop

ribi::Led::Led(
  const double intensity,
  const int red,
  const int green,
  const int blue)
  : m_signal_color_changed{},
    m_signal_intensity_changed{},
    m_blue(blue),
    m_green(green),
    m_intensity(intensity),
    m_red(red)
{
  #ifndef NDEBUG
  Test();
  #endif

  assert(red >= 0);
  assert(red < 256);
  assert(green >= 0);
  assert(green < 256);
  assert(blue >= 0);
  assert(blue < 256);

  assert(intensity >= 0.0
    && "An LED intensity must be a positive value");
  assert(intensity <= 1.0
    && "An LED intensity must be equal or lower than 1.0 (that is, 100%)");
}

std::string ribi::Led::GetVersion() noexcept
{
  return "1.3";
}

std::vector<std::string> ribi::Led::GetVersionHistory() noexcept
{
  return {
    "2011-04-10: Version 1.0: initial version",
    "2011-08-17: Version 1.1: emit a signal when the color is changed",
    "2011-08-20: Version 1.2: added operator<<",
    "2014-06-20: Version 1.3: changed color data types from unsigned char to int"
  };
}

void ribi::Led::SetColor(
  const int red,
  const int green,
  const int blue
) noexcept
{
  SetRed(red);
  SetGreen(green);
  SetBlue(blue);
}

void ribi::Led::SetBlue(const int blue) noexcept
{
  assert(blue >= 0);
  assert(blue < 256);
  if (m_blue != blue)
  {
    m_blue = blue;
    m_signal_color_changed(this);
  }
}

void ribi::Led::SetGreen(const int green) noexcept
{
  assert(green >= 0);
  assert(green < 256);
  if (m_green != green)
  {
    m_green = green;
    m_signal_color_changed(this);
  }
}

void ribi::Led::SetIntensity(const double intensity) noexcept
{
  assert(intensity >= 0.0
    && "An LED intensity must be a positive value");
  assert(intensity <= 1.0
    && "An LED intensity must be equal or lower than 1.0 (that is, 100%)");
  if (intensity != m_intensity)
  {
    m_intensity = intensity;
    //Emit signal
    m_signal_intensity_changed(this);
  }
}

void ribi::Led::SetRed(const int red) noexcept
{
  assert(red >= 0);
  assert(red < 256);
  if (m_red != red)
  {
    m_red = red;
    m_signal_color_changed(this);
  }
}

#ifndef NDEBUG
void ribi::Led::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

std::string ribi::Led::ToStr() const noexcept
{
  std::stringstream s;
  s << (*this);
  return s.str();
}

std::string ribi::Led::ToXml() const noexcept
{
  std::stringstream s;
  s
    << "<Led>"
    << "<blue>"
      << m_blue
    << "</blue>"
    << "<green>"
      << m_green
    << "</green>"
    << "<intensity>"
      << m_intensity
    << "</intensity>"
    << "<red>"
      << m_red
    << "</red>"
    << "</Led>";
  return s.str();

}

std::ostream& ribi::operator<<(std::ostream& os, const Led& led) noexcept
{
  os
    << led.m_intensity << " ("
    << led.m_red << ", "
    << led.m_green << ", "
    << led.m_blue << ")"
  ;
  return os;
}

bool ribi::operator==(const Led& lhs, const Led& rhs) noexcept
{
  return
       lhs.GetBlue()      == rhs.GetBlue()
    && lhs.GetGreen()     == rhs.GetGreen()
    && lhs.GetIntensity() == rhs.GetIntensity()
    && lhs.GetRed()       == rhs.GetRed()
  ;
}

 

 

 

 

 

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