MHVLib  20111227
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_WS2801.cpp
Go to the documentation of this file.
00001 /* Copyright (c) 2011, Make, Hack, Void Inc
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *  * Redistributions of source code must retain the above copyright
00007  *    notice, this list of conditions and the following disclaimer.
00008  *  * Redistributions in binary form must reproduce the above copyright
00009  *    notice, this list of conditions and the following disclaimer in the
00010  *    documentation and/or other materials provided with the distribution.
00011  *  * Neither the name of the Make, Hack, Void nor the
00012  *    names of its contributors may be used to endorse or promote products
00013  *    derived from this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00016  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00020  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00021  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00022  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 
00027 
00028 #include <MHV_WS2801.h>
00029 
00038 MHV_WS2801::MHV_WS2801(MHV_RGB buf[], uint16_t length, MHV_Shifter &shifter) :
00039                 _length(length),
00040                 _data(buf),
00041                 _shifter(shifter) {}
00042 
00050 void MHV_WS2801::setPixel(uint16_t pixel, uint8_t red, uint8_t green, uint8_t blue) {
00051         MHV_RGB *chip = _data + pixel;
00052 
00053         chip->red = red;
00054         chip->green = green;
00055         chip->blue = blue;
00056 }
00057 
00063 void MHV_WS2801::setPixel(uint16_t pixel, MHV_RGB *value) {
00064         mhv_memcpy(_data + pixel, value, MHV_BYTESIZEOF(*value));
00065 }
00066 
00073 void MHV_WS2801::setAll(uint8_t red, uint8_t green, uint8_t blue) {
00074         MHV_RGB *chip;
00075 
00076         for (uint16_t i = 0; i < _length; i++) {
00077                 chip = _data + i;
00078 
00079                 chip->red = red;
00080                 chip->green = green;
00081                 chip->blue = blue;
00082         }
00083 }
00084 
00089 void MHV_WS2801::setAll(MHV_RGB *value) {
00090         for (uint16_t i = 0; i < _length; i++) {
00091                 mhv_memcpy(_data + i, value, MHV_BYTESIZEOF(*value));
00092         }
00093 }
00094 
00102 void MHV_WS2801::setPixelGamma(uint16_t pixel, uint8_t red, uint8_t green, uint8_t blue) {
00103         MHV_RGB *chip = _data + pixel;
00104 
00105         chip->red = mhv_precalculatedGammaCorrect(red);
00106         chip->green = mhv_precalculatedGammaCorrect(green);
00107         chip->blue = mhv_precalculatedGammaCorrect(blue);
00108 }
00109 
00115 void MHV_WS2801::setPixelGamma(uint16_t pixel, MHV_RGB *value) {
00116         MHV_RGB *chip = _data + pixel;
00117 
00118         chip->red = mhv_precalculatedGammaCorrect(value->red);
00119         chip->green = mhv_precalculatedGammaCorrect(value->green);
00120         chip->blue = mhv_precalculatedGammaCorrect(value->blue);
00121 }
00122 
00129 void MHV_WS2801::setAllGamma(uint8_t red, uint8_t green, uint8_t blue) {
00130         MHV_RGB *chip;
00131 
00132         for (uint16_t i = 0; i < _length; i++) {
00133                 chip = _data + i;
00134 
00135                 chip->red = mhv_precalculatedGammaCorrect(red);
00136                 chip->green = mhv_precalculatedGammaCorrect(green);
00137                 chip->blue = mhv_precalculatedGammaCorrect(blue);
00138         }
00139 }
00140 
00145 void MHV_WS2801::setAllGamma(MHV_RGB *value) {
00146         MHV_RGB newValue = {mhv_precalculatedGammaCorrect(value->red),
00147                         mhv_precalculatedGammaCorrect(value->green),
00148                         mhv_precalculatedGammaCorrect(value->blue)};
00149 
00150         for (uint16_t i = 0; i < _length; i++) {
00151                 mhv_memcpy(_data + i, &newValue, MHV_BYTESIZEOF(newValue));
00152         }
00153 }
00154 
00155 
00159 void MHV_WS2801::flush() {
00160         _shifter.shiftOut((uint8_t *)_data, MHV_BYTESIZEOF(*_data) * _length);
00161 }
00162 
00167 void MHV_WS2801::rotate(bool forwards) {
00168         MHV_RGB temp;
00169 
00170         if (forwards) {
00171                 mhv_memcpy(&temp, _data + _length - 1, MHV_BYTESIZEOF(temp));
00172                 mhv_memcpyTailFirst(_data + 1, _data, MHV_BYTESIZEOF(*_data), _length - 1);
00173                 mhv_memcpy(_data, &temp, MHV_BYTESIZEOF(temp));
00174         } else {
00175                 mhv_memcpy(&temp, _data, MHV_BYTESIZEOF(temp));
00176                 mhv_memcpy(_data, _data + 1,  MHV_BYTESIZEOF(*_data), _length - 1);
00177                 mhv_memcpy(_data, &temp, MHV_BYTESIZEOF(temp));
00178         }
00179 }