# include <Adafruit_NeoPixel.h>
/*
Serial Buffer Ring Demo
Green = bytes waiting in the serial receive buffer
Yellow = byte position just consumed
Try Serial Monitor at 300 baud to watch the buffer fill visibly.
*/
const byte LED_PIN = 6;
const byte LED_COUNT = 32;
const unsigned long CHARACTER_INTERVAL_MS = 333;
const unsigned long YELLOW_MS = 100;
Adafruit_NeoPixel ring(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long lastCharacterTime = 0;
unsigned long yellowUntil = 0;
byte readPixel = 0;
byte yellowPixel = 0;
void showBufferLevel(int available)
{
unsigned long now = millis();
ring.clear();
int shown = min(available, LED_COUNT);
for (int i = 0; i < shown; i++)
{
byte pixel = (readPixel + i) % LED_COUNT;
ring.setPixelColor(pixel, 0x00ff00); // green
}
if (now < yellowUntil)
{
ring.setPixelColor(yellowPixel, 0xffff00); // yellow
}
ring.show();
}
void setup()
{
Serial.begin(300);
while (!Serial)
{
;
}
ring.begin();
ring.clear();
ring.show();
Serial.println();
Serial.println("serial demo\n");
if (0) {
Serial.println("Paste text. Green shows waiting bytes.");
Serial.println("Yellow marks the byte just consumed.");
Serial.println("Bytes are s l o w l y read");
Serial.println();
}
}
void loop()
{
unsigned long now = millis();
if (Serial.available() > 0 &&
now - lastCharacterTime >= CHARACTER_INTERVAL_MS)
{
int ch = Serial.read();
yellowPixel = readPixel;
yellowUntil = now + YELLOW_MS;
readPixel = (readPixel + 1) % LED_COUNT;
lastCharacterTime = now;
Serial.print("# = ");
Serial.print(Serial.available());
Serial.print("'");
if (ch >= 32 && ch <= 126)
Serial.write((char)ch);
else
Serial.print('?');
Serial.println("'");
}
showBufferLevel(Serial.available());
}