From 8352e4aae0e094faf1a11325b2054cdab667a7d0 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 23 Nov 2019 09:26:28 +0100 Subject: [PATCH] drivers/ws281x: add VT100 backend for native To quickly iterate on animations it is handy to being able to simulate the output on native. This adds a VT100 terminal backend to the ws281x driver that outputs the colors straight to the terminal. --- drivers/Makefile.dep | 9 ++++++++ drivers/ws281x/vt100.c | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 drivers/ws281x/vt100.c diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 2814ef26c364..c6d63a087ae0 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -678,10 +678,14 @@ endif ifneq (,$(filter ws281x,$(USEMODULE))) FEATURES_OPTIONAL += arch_avr8 + FEATURES_OPTIONAL += arch_native ifeq (,$(filter ws281x_%,$(USEMODULE))) ifneq (,$(filter arch_avr8,$(FEATURES_USED))) USEMODULE += ws281x_atmega endif + ifneq (,$(filter arch_native,$(FEATURES_USED))) + USEMODULE += ws281x_vt100 + endif endif ifneq (,$(filter ws281x_atmega,$(USEMODULE))) FEATURES_REQUIRED += arch_avr8 @@ -689,6 +693,11 @@ ifneq (,$(filter ws281x,$(USEMODULE))) USEMODULE += xtimer endif +ifneq (,$(filter ws281x_vt100,$(USEMODULE))) + CFLAGS += -DWS281X_HAVE_PREPARE_TRANSMISSION + CFLAGS += -DWS281X_HAVE_END_TRANSMISSION +endif + ifneq (,$(filter xbee,$(USEMODULE))) FEATURES_REQUIRED += periph_uart FEATURES_REQUIRED += periph_gpio diff --git a/drivers/ws281x/vt100.c b/drivers/ws281x/vt100.c new file mode 100644 index 000000000000..82a659c6a96f --- /dev/null +++ b/drivers/ws281x/vt100.c @@ -0,0 +1,51 @@ +/* + * Copyright 2019 Benjamin Valentin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_ws281x + * + * @{ + * + * @file + * @brief Implementation of `ws281x_write()` for VT100 terminals + * + * @author Benjamin Valentin + * + * @} + */ +#include +#include "ws281x.h" + +void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size) +{ + (void) dev; + const uint8_t *src = buf; + + for (unsigned i = 0; i < size; ++i) { + int r = src[WS281X_BYTES_PER_DEVICE * i + WS281X_OFFSET_R]; + int g = src[WS281X_BYTES_PER_DEVICE * i + WS281X_OFFSET_G]; + int b = src[WS281X_BYTES_PER_DEVICE * i + WS281X_OFFSET_B]; + printf("\033[48;2;%d;%d;%dm ", r, g, b); + } +} + +void ws281x_prepare_transmission(ws281x_t *dev) +{ + (void) dev; + + /* clear the line and reset cursor position */ + printf("\033[2K\r"); +} + +void ws281x_end_transmission(ws281x_t *dev) +{ + (void) dev; + + /* set color back to normal */ + printf("\033[0m"); +}