Library for multi-pin RGB leds that encapsulates three PwmOut objects.
Diff: MultipinRGB.cpp
- Revision:
- 0:434f7d7a11d8
- Child:
- 2:ecd3840bb6a7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MultipinRGB.cpp Tue Mar 28 20:22:56 2017 +0000 @@ -0,0 +1,182 @@ +/****************************************************************************** +* MIT License +* +* Copyright (c) 2017 Justin J. Jordan +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: + +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +******************************************************************************/ + + +#include "MultipinRGB.h" + + +/*****************************************************************************/ +MultipinRGB::MultipinRGB(PinName red, PinName green, PinName blue, LedLogic_e activeState): +m_red(red), m_green(green), m_blue(blue), m_ledActiveState(activeState) +{ + //turn off all leds, use default period + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + m_red.write(1.0F); + m_green.write(1.0F); + m_blue.write(1.0F); + } + else + { + m_red.write(0.0F); + m_green.write(0.0F); + m_blue.write(0.0F); + } +} + + +/*****************************************************************************/ +MultipinRGB::~MultipinRGB() +{ + //empty block +} + + +/*****************************************************************************/ +void MultipinRGB::writeRed(const float r) +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + m_red.write(1.0F - r); + } + else + { + m_red.write(r); + } +} + + +/*****************************************************************************/ +void MultipinRGB::writeGreen(const float g) +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + m_green.write(1.0F - g); + } + else + { + m_green.write(g); + } +} + + +/*****************************************************************************/ +void MultipinRGB::writeBlue(const float b) +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + m_blue.write(1.0F - b); + } + else + { + m_blue.write(b); + } +} + + +/*****************************************************************************/ +void MultipinRGB::writeRGB(const float r, const float g, const float b) +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + m_red.write(1.0F - r); + m_green.write(1.0F - g); + m_blue.write(1.0F - b); + } + else + { + m_red.write(r); + m_green.write(g); + m_blue.write(b); + } +} + + +/*****************************************************************************/ +void MultipinRGB::setPeriod(const float p) +{ + m_red.period(p); + m_green.period(p); + m_blue.period(p); +} + + +/*****************************************************************************/ +float MultipinRGB::readRed() +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + return (1.0F - m_red.read()); + } + else + { + return m_red.read(); + } +} + + +/*****************************************************************************/ +float MultipinRGB::readGreen() +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + return (1.0F - m_green.read()); + } + else + { + return m_green.read(); + } +} + + +/*****************************************************************************/ +float MultipinRGB::readBlue() +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + return (1.0F - m_blue.read()); + } + else + { + return m_blue.read(); + } +} + + +/*****************************************************************************/ +void MultipinRGB::readRGB(float& r, float& g, float& b) +{ + if(m_ledActiveState == MultipinRGB::ActiveLow) + { + r = (1.0F - m_red.read()); + g = (1.0F - m_green.read()); + b = (1.0F - m_blue.read()); + } + else + { + r = m_red.read(); + g = m_green.read(); + b = m_blue.read(); + } +}