osd-contiki/examples/osd/arduino-ledstrip/RGBdriver.cpp

117 lines
2.6 KiB
C++
Raw Permalink Normal View History

2016-01-22 15:41:55 +01:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// Modified Record:
/***************************************************************************/
#include "RGBdriver.h"
2016-01-24 16:04:37 +01:00
2016-01-22 15:41:55 +01:00
RGBdriver::RGBdriver(uint8_t Clk, uint8_t Data)
{
Clkpin = Clk;
Datapin = Data;
pinMode(Datapin, OUTPUT);
pinMode(Clkpin, OUTPUT);
}
void RGBdriver::begin(void)
{
Send32Zero();
}
void RGBdriver::end(void)
{
Send32Zero();
}
void RGBdriver::ClkRise(void)
{
2016-01-24 16:04:37 +01:00
2016-01-22 15:41:55 +01:00
digitalWrite(Clkpin, LOW);
delayMicroseconds(20);
digitalWrite(Clkpin, HIGH);
2016-01-24 16:04:37 +01:00
delayMicroseconds(20);
2016-01-22 15:41:55 +01:00
}
void RGBdriver::Send32Zero(void)
{
unsigned char i;
2016-01-24 16:04:37 +01:00
uint8_t volatile sreg;
sreg = SREG; /* Save status register before disabling interrupts. */
cli(); /* Disable interrupts. */
2016-01-22 15:41:55 +01:00
for (i=0; i<32; i++)
{
digitalWrite(Datapin, LOW);
ClkRise();
}
2016-01-24 16:04:37 +01:00
SREG = sreg; /* Enable interrupts. */
2016-01-22 15:41:55 +01:00
}
uint8_t RGBdriver::TakeAntiCode(uint8_t dat)
{
uint8_t tmp = 0;
if ((dat & 0x80) == 0)
{
tmp |= 0x02;
}
if ((dat & 0x40) == 0)
{
tmp |= 0x01;
}
return tmp;
}
// gray data
void RGBdriver::DatSend(uint32_t dx)
{
uint8_t i;
2016-01-24 16:04:37 +01:00
uint8_t volatile sreg;
sreg = SREG; /* Save status register before disabling interrupts. */
cli(); /* Disable interrupts. */
2016-01-22 15:41:55 +01:00
for (i=0; i<32; i++)
{
if ((dx & 0x80000000) != 0)
{
digitalWrite(Datapin, HIGH);
}
else
{
digitalWrite(Datapin, LOW);
}
dx <<= 1;
2016-01-24 16:04:37 +01:00
ClkRise();
2016-01-22 15:41:55 +01:00
}
2016-01-24 16:04:37 +01:00
SREG = sreg; /* Enable interrupts. */
2016-01-22 15:41:55 +01:00
}
// Set color
void RGBdriver::SetColor(uint8_t Red,uint8_t Green,uint8_t Blue)
{
uint32_t dx = 0;
dx |= (uint32_t)0x03 << 30; // highest two bits 1flag bits
dx |= (uint32_t)TakeAntiCode(Blue) << 28;
dx |= (uint32_t)TakeAntiCode(Green) << 26;
dx |= (uint32_t)TakeAntiCode(Red) << 24;
dx |= (uint32_t)Blue << 16;
dx |= (uint32_t)Green << 8;
dx |= Red;
DatSend(dx);
}