From 8cdeb8700566f054a2b0be2bdd0c6d37d289c40b Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 20 Feb 2022 16:10:06 +0100 Subject: [PATCH] Indicators: Manual testing --- indicators/firmware/interface.h | 2 + indicators/firmware/main.c | 9 ++-- indicators/test/Makefile | 9 ++++ indicators/test/test.c | 75 +++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 indicators/test/Makefile create mode 100644 indicators/test/test.c diff --git a/indicators/firmware/interface.h b/indicators/firmware/interface.h index 3936e0e..b76d2b0 100644 --- a/indicators/firmware/interface.h +++ b/indicators/firmware/interface.h @@ -8,6 +8,8 @@ #define NPIX_USB_PRODUCT 0x0009 #define NPIX_USB_VERSION 0x0100 +#define NPIX_NUM_LEDS 12 + /* * Endpoints: * diff --git a/indicators/firmware/main.c b/indicators/firmware/main.c index 23f250a..d133e95 100644 --- a/indicators/firmware/main.c +++ b/indicators/firmware/main.c @@ -99,10 +99,9 @@ static void delay_ms(uint ms) #define NPIX_B0 30 #define NPIX_B1 60 -#define NUM_LEDS 12 -static byte led_rgb[NUM_LEDS][3]; +static byte led_rgb[NPIX_NUM_LEDS][3]; -static byte neopixel_buf[NPIX_RESET + NUM_LEDS*24 + 1]; +static byte neopixel_buf[NPIX_RESET + NPIX_NUM_LEDS*24 + 1]; static bool neopixel_dma_running; static bool neopixel_want_send; @@ -135,7 +134,7 @@ static void neopixel_recalc(void) byte *buf = neopixel_buf; for (uint i = 0; i < NPIX_RESET; i++) *buf++ = 0; - for (uint i = 0; i < NUM_LEDS; i++) { + for (uint i = 0; i < NPIX_NUM_LEDS; i++) { // The order is GRB, MSB first for (uint m = 0x80; m; m >>= 1) *buf++ = ((led_rgb[i][1] & m) ? NPIX_B1 : NPIX_B0); @@ -396,7 +395,7 @@ int main(void) if (ms_ticks - last_blink >= 100) { debug_led_toggle(); last_blink = ms_ticks; - led_rgb[NUM_LEDS - 1][1] ^= 0x33; + led_rgb[NPIX_NUM_LEDS - 1][1] ^= 0x33; neopixel_want_send = 1; } diff --git a/indicators/test/Makefile b/indicators/test/Makefile new file mode 100644 index 0000000..a31dce1 --- /dev/null +++ b/indicators/test/Makefile @@ -0,0 +1,9 @@ +UCWCF:=$(shell PKG_CONFIG_PATH=$(LIBUCW)/lib/pkgconfig pkg-config --cflags libucw) +UCWLF:=$(shell PKG_CONFIG_PATH=$(LIBUCW)/lib/pkgconfig pkg-config --libs libucw) + +CFLAGS=-std=gnu99 -O2 -Wall -Wextra -Wno-parentheses $(UCWCF) +LDLIBS=-lusb-1.0 $(UCWLF) + +all: test + +test: test.c diff --git a/indicators/test/test.c b/indicators/test/test.c new file mode 100644 index 0000000..55c37fb --- /dev/null +++ b/indicators/test/test.c @@ -0,0 +1,75 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include "../firmware/interface.h" + +struct libusb_context *usb_ctxt; +struct libusb_device_handle *devh; + +static libusb_device *find_device(void) +{ + libusb_device **devlist; + ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist); + if (devn < 0) + { + fprintf(stderr, "Cannot enumerate USB devices: error %d\n", (int) devn); + exit(1); + } + + for (ssize_t i=0; i NPIX_NUM_LEDS*3 + 1) + die("Too many arguments!"); + + for (int i=1; i < argc; i++) + packet[len++] = atoi(argv[i]); + + int err; + if (err = libusb_init(&usb_ctxt)) + die("Cannot initialize libusb: error %d", err); + + libusb_device *dev = find_device(); + + if (err = libusb_open(dev, &devh)) + die("Cannot open device: error %d", err); + // libusb_reset_device(devh); + if (err = libusb_claim_interface(devh, 0)) + die("Cannot claim interface: error %d", err); + + int transferred; + if (err = libusb_bulk_transfer(devh, 0x01, packet, len, &transferred, 1000)) + die("Transfer failed: error %d\n", err); + if (transferred != len) + die("Short transfer: %d out of %d bytes", transferred, len); + + return 0; +} -- 2.39.2