Skip to content

Commit

Permalink
Initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Dec 21, 2020
0 parents commit 01a12ca
Show file tree
Hide file tree
Showing 4 changed files with 490 additions and 0 deletions.
245 changes: 245 additions & 0 deletions HyperSerialEsp8266.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
#include <NeoPixelBus.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// CONFIG SECTION STARTS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

#define THIS_IS_RGBW // RGBW SK6812, otherwise comment it
bool skipFirstLed = true; // if set the first led in the strip will be set to black (for level shifters)
int serialSpeed = 2000000; // serial port speed

////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// CONFIG SECTION ENDS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

int pixelCount = 16; // This is dynamic, don't change it

#ifdef THIS_IS_RGBW
#define LED_TYPE NeoGrbwFeature
#else
#define LED_TYPE NeoGrbFeature
#endif


NeoPixelBus<LED_TYPE, NeoEsp8266Uart1800KbpsMethod>* strip = NULL;

void Init(int count)
{
if (strip != NULL)
delete strip;

pixelCount = count;
strip = new NeoPixelBus<LED_TYPE, NeoEsp8266Uart1800KbpsMethod>(pixelCount);
}

enum class AwaProtocol {
HEADER_A,
HEADER_w,
HEADER_a,
HEADER_HI,
HEADER_LO,
HEADER_CRC,
RED,
GREEN,
BLUE,
FLETCHER1,
FLETCHER2
};

// static data buffer for the loop
uint8_t buffer[4096];
AwaProtocol state = AwaProtocol::HEADER_A;
uint8_t CRC = 0;
uint16_t count = 0;
uint16_t currentPixel = 0;
uint16_t fletcher1 = 0;
uint16_t fletcher2 = 0;

#ifdef THIS_IS_RGBW
RgbwColor inputColor;
uint8_t rChannel[256];
uint8_t gChannel[256];
uint8_t bChannel[256];

#else
RgbColor inputColor;
#endif

void readSerialData()
{
uint16_t bufferPointer = 0;
uint16_t internalIndex = min(Serial.available(), 4096);

if (internalIndex > 0)
internalIndex = Serial.readBytes(buffer, internalIndex);

while (bufferPointer < internalIndex)
{
byte input = buffer[bufferPointer++];
switch (state)
{
case AwaProtocol::HEADER_A:
if (input == 'A') state = AwaProtocol::HEADER_w;
break;

case AwaProtocol::HEADER_w:
if (input == 'w') state = AwaProtocol::HEADER_a;
else state = AwaProtocol::HEADER_A;
break;

case AwaProtocol::HEADER_a:
if (input == 'a') state = AwaProtocol::HEADER_HI;
else state = AwaProtocol::HEADER_A;
break;

case AwaProtocol::HEADER_HI:
currentPixel = 0;
count = input * 0x100;
CRC = input;
fletcher1 = 0;
fletcher2 = 0;
state = AwaProtocol::HEADER_LO;
break;

case AwaProtocol::HEADER_LO:
count += input;
CRC = CRC ^ input ^ 0x55;
state = AwaProtocol::HEADER_CRC;
break;

case AwaProtocol::HEADER_CRC:
if (CRC == input)
{
if (count+1 != pixelCount) Init(count+1);
state = AwaProtocol::RED;
}
else
state = AwaProtocol::HEADER_A;
break;

case AwaProtocol::RED:
inputColor.R = input;
fletcher1 = (fletcher1 + (uint16_t)input) % 255;
fletcher2 = (fletcher2 + fletcher1) % 255;

state = AwaProtocol::GREEN;
break;

case AwaProtocol::GREEN:
inputColor.G = input;
fletcher1 = (fletcher1 + (uint16_t)input) % 255;
fletcher2 = (fletcher2 + fletcher1) % 255;

state = AwaProtocol::BLUE;
break;

case AwaProtocol::BLUE:
inputColor.B = input;

#ifdef THIS_IS_RGBW
inputColor.W = min(rChannel[inputColor.R],
min(gChannel[inputColor.G],
bChannel[inputColor.B]));
inputColor.R -= rChannel[inputColor.W];
inputColor.G -= gChannel[inputColor.W];
inputColor.B -= bChannel[inputColor.W];
#endif

fletcher1 = (fletcher1 + (uint16_t)input) % 255;
fletcher2 = (fletcher2 + fletcher1) % 255;

if (currentPixel == 0 && skipFirstLed)
{
#ifdef THIS_IS_RGBW
strip->SetPixelColor(currentPixel++, RgbwColor(0, 0, 0, 0));
#else
strip->SetPixelColor(currentPixel++, RgbColor(0, 0, 0));
#endif
}
else
setStripPixel(currentPixel++, inputColor);

if (count-- > 0) state = AwaProtocol::RED;
else state = AwaProtocol::FLETCHER1;
break;

case AwaProtocol::FLETCHER1:
if (input != fletcher1) state = AwaProtocol::HEADER_A;
else state = AwaProtocol::FLETCHER2;
break;

case AwaProtocol::FLETCHER2:
if (input == fletcher2) strip->Show();
state = AwaProtocol::HEADER_A;
break;
}
}
}

#ifdef THIS_IS_RGBW
void setStripPixel(uint16_t pix, RgbwColor& inputColor)
{
if (pix < pixelCount)
{
strip->SetPixelColor(pix, inputColor);
}
}
#else
void setStripPixel(uint16_t pix, RgbColor& inputColor)
{
if (pix < pixelCount)
{
strip->SetPixelColor(pix, inputColor);
}
}
#endif

void setup()
{
// Init serial port
Serial.begin(serialSpeed);

// Init NeoPixelBus
Init(pixelCount);
strip->Begin();

#ifdef THIS_IS_RGBW
// prepare
for (int i = 0; i < 256; i++)
{
uint32_t rCorrection = 0xB0 * (uint32_t)i; // adjust red -> white in 0-0xFF range
uint32_t gCorrection = 0xB0 * (uint32_t)i; // adjust green -> white in 0-0xFF range
uint32_t bCorrection = 0x80 * (uint32_t)i; // adjust blue -> white in 0-0xFF range

rCorrection /= 0xFF;
gCorrection /= 0xFF;
bCorrection /= 0xFF;

rChannel[i] = (uint8_t)rCorrection;
gChannel[i] = (uint8_t)gCorrection;
bChannel[i] = (uint8_t)bCorrection;
}
#endif

// Say ambilight like "Hello" to the world
for (int i = 0; i < 9; i++)
{
if (i < 3)
strip->SetPixelColor(0, RgbColor(((i + 1) * 80) % 255, 0, 0));
else if (i < 6)
strip->SetPixelColor(0, RgbColor(0, ((6 - i) * 80) % 255, 0));
else
strip->SetPixelColor(0, RgbColor(0, 0, ((i - 5) * 80) % 255));

strip->Show();
delay(200);
}

// Clear it
strip->SetPixelColor(0, RgbColor(0, 0, 0));
strip->Show();
}

void loop()
{
readSerialData();
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 awawa-dev

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.
Loading

0 comments on commit 01a12ca

Please sign in to comment.