Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Led

 

Led is a class for an LED light.

 

Led is used in the tool TestLed.

 

 

 

 

 

led.h

 

//---------------------------------------------------------------------------
/*
Led, LED class
Copyright (C) 2011 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>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/noncopyable.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
///Led manages an LED lamp
struct Led : public boost::noncopyable
{
  Led(
    const double intensity    = 0.0,
    const unsigned char red   = 255,
    const unsigned char green =   0,
    const unsigned char blue  =   0);

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

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

  ///Get the Led its blueness
  unsigned char GetBlue()  const { return m_blue;  }

  ///Get the Led its greenness
  unsigned char GetGreen() const { return m_green; }

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

  ///Get the Led its redness
  unsigned char GetRed()   const { return m_red;   }

  ///Set the Led its blueness
  void SetBlue(const unsigned char blue);

  ///Set the Led its greenness
  void SetGreen(const unsigned char green);

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

  ///Set the Led its redness
  void SetRed(const unsigned char red);

  private:
  ///Led can only be deleted by Boost smart pointers
  virtual ~Led() {}
  ///Led can only be deleted by Boost smart pointers
  //Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(Led*);

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

  ///The Led its redness
  unsigned char m_red;

  ///The Led its greenness
  unsigned char m_green;

  ///The Led its blueness
  unsigned char m_blue;

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

  public:
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const Led& led);
//---------------------------------------------------------------------------
#endif // LED_H

 

 

 

 

 

led.cpp

 

//---------------------------------------------------------------------------
/*
Led, LED class
Copyright (C) 2011 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
//---------------------------------------------------------------------------
#include <cassert>
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
//---------------------------------------------------------------------------
#include "led.h"
//---------------------------------------------------------------------------
Led::Led(
  const double intensity,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_intensity(intensity),
    m_red(red),
    m_green(green),
    m_blue(blue)
{
  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%)");
}
//---------------------------------------------------------------------------
const std::string Led::GetVersion()
{
  return "1.2";
}
//---------------------------------------------------------------------------
const std::vector<std::string> Led::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-04-10: Version 1.0: initial version");
  v.push_back("2011-08-17: Version 1.1: emit a signal when the color is changed");
  v.push_back("2011-08-20: Version 1.2: added operator<<");
  return v;
}
//---------------------------------------------------------------------------
void Led::SetColor(
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
{
  SetRed(red);
  SetGreen(green);
  SetBlue(blue);
}
//---------------------------------------------------------------------------
void Led::SetBlue(const unsigned char blue)
{
  if (m_blue != blue)
  {
    m_blue = blue;
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
void Led::SetGreen(const unsigned char green)
{
  if (m_green != green)
  {
    m_green = green;
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
void Led::SetIntensity(const double intensity)
{
  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();
  }
}
//---------------------------------------------------------------------------
void Led::SetRed(const unsigned char red)
{
  if (m_red != red)
  {
    m_red = red;
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const Led& led)
{
  os
    << "<Led>"
    << "<blue>"
      << static_cast<int>(led.m_blue)
    << "</blue>"
    << "<green>"
      << static_cast<int>(led.m_green)
    << "</green>"
    << "<intensity>"
      << led.m_intensity
    << "</intensity>"
    << "<red>"
      << static_cast<int>(led.m_red)
    << "</red>"
    << "</Led>";
  return os;
}
//---------------------------------------------------------------------------

 

 

 

 

 

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