Library for multi-pin RGB leds that encapsulates three PwmOut objects.
MultipinRGB.cpp
- Committer:
- j3
- Date:
- 2017-03-28
- Revision:
- 2:ecd3840bb6a7
- Parent:
- 0:434f7d7a11d8
- Child:
- 3:327cc9243faf
File content as of revision 2:ecd3840bb6a7:
/****************************************************************************** * 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_redDc = 1.0F; m_grnDc = 1.0F; m_bluDc = 1.0F; } else { m_redDc = 0.0F; m_grnDc = 0.0F; m_bluDc = 0.0F; } m_red.write(m_redDc); m_green.write(m_grnDc); m_blue.write(m_bluDc); } /*****************************************************************************/ MultipinRGB::~MultipinRGB() { //empty block } /*****************************************************************************/ void MultipinRGB::writeLed(const Led_e led, const float dc) { switch(led) { case(Red): m_redDc = dc; (m_ledActiveState == MultipinRGB::ActiveLow) ? m_red.write(1.0F - m_redDc) : m_red.write(m_redDc); break; case(Green): m_grnDc = dc; (m_ledActiveState == MultipinRGB::ActiveLow) ? m_green.write(1.0F - m_grnDc) : m_green.write(m_grnDc); break; case(Blue): m_bluDc = dc; (m_ledActiveState == MultipinRGB::ActiveLow) ? m_blue.write(1.0F - m_bluDc) : m_blue.write(m_bluDc); break; default: mbed_die(); break; }; } /*****************************************************************************/ void MultipinRGB::writeLeds(const float r, const float g, const float b) { if(m_ledActiveState == MultipinRGB::ActiveLow) { m_redDc = (1.0F - r); m_grnDc = (1.0F - g); m_bluDc = (1.0F - b); } else { m_redDc = r; m_grnDc = g; m_bluDc = b; } m_red.write(m_redDc); m_green.write(m_grnDc); m_blue.write(m_bluDc); } /*****************************************************************************/ float MultipinRGB::readLed(const Led_e led) { float rtnVal; switch(led) { case(Red): (m_ledActiveState == MultipinRGB::ActiveLow) ? (rtnVal = (1.0F - m_red.read())) : (rtnVal = m_red.read()); break; case(Green): (m_ledActiveState == MultipinRGB::ActiveLow) ? (rtnVal = (1.0F - m_green.read())) : (rtnVal = m_green.read()); break; case(Blue): (m_ledActiveState == MultipinRGB::ActiveLow) ? (rtnVal = (1.0F - m_blue.read())) : (rtnVal = m_blue.read()); break; default: mbed_die(); break; }; return rtnVal; } /*****************************************************************************/ void MultipinRGB::readLeds(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(); } } /*****************************************************************************/ void MultipinRGB::setPeriod(const float p) { m_red.period(p); m_green.period(p); m_blue.period(p); }