Added Sketch for WS2812 Network Utilization Stips

This commit is contained in:
Nis Wechselberg 2017-10-31 15:27:40 +01:00
parent e52be1a0d8
commit 60fd193b35

View file

@ -0,0 +1,64 @@
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#define PIN_RX 10
#define PIN_TX 11
#define PIXELS 24
// Prepare two independent strips for rx data and tx data
Adafruit_NeoPixel strip_rx = Adafruit_NeoPixel(PIXELS, PIN_RX, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip_tx = Adafruit_NeoPixel(PIXELS, PIN_TX, NEO_GRB + NEO_KHZ800);
void setup() {
// Initialize both strips to off
strip_rx.begin(); strip_rx.show();
strip_tx.begin(); strip_tx.show();
// Initialize serial port to 115200 baud
Serial.begin(115200);
}
void loop() {
if (Serial.available() >= 3) {
byte b = Serial.read();
if (b == 0xFF) {
// Read next two bytes as rx and tx rate
byte rxrate = Serial.read();
byte txrate = Serial.read();
if (rxrate <= 100 && txrate <= 100) {
showPercentage_rx(strip_rx.Color(0, 0, 255), rxrate);
showPercentage_tx(strip_tx.Color(255, 0, 0), txrate);
}
}
}
delay(10);
}
void showPercentage_tx(uint32_t c, uint8_t perc) {
char lastPixel = (PIXELS * perc) / 100;
char i = 0;
while(i < lastPixel) {
strip_tx.setPixelColor(i, c);
++i;
}
while (i < PIXELS) {
strip_tx.setPixelColor(i, 0);
++i;
}
strip_tx.show();
}
void showPercentage_rx(uint32_t c, uint8_t perc) {
char lastPixel = (PIXELS * perc) / 100;
char i = 0;
while(i < lastPixel) {
strip_rx.setPixelColor(i, c);
++i;
}
while (i < PIXELS) {
strip_rx.setPixelColor(i, 0);
++i;
}
strip_rx.show();
}