]> mj.ucw.cz Git - home-hw.git/commitdiff
Indicators: Manual testing
authorMartin Mares <mj@ucw.cz>
Sun, 20 Feb 2022 15:10:06 +0000 (16:10 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 20 Feb 2022 15:10:06 +0000 (16:10 +0100)
indicators/firmware/interface.h
indicators/firmware/main.c
indicators/test/Makefile [new file with mode: 0644]
indicators/test/test.c [new file with mode: 0644]

index 3936e0ea60beb0a5084317220c73a6e10598aed9..b76d2b0d33f7f8c964845243ead0a22e0424dbed 100644 (file)
@@ -8,6 +8,8 @@
 #define NPIX_USB_PRODUCT 0x0009
 #define NPIX_USB_VERSION 0x0100
 
+#define NPIX_NUM_LEDS 12
+
 /*
  *     Endpoints:
  *
index 23f250aa3f376ac3004a0eae1cc250042f3001b5..d133e9595e75d2c2e8034fde252de235d777aced 100644 (file)
@@ -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 (file)
index 0000000..a31dce1
--- /dev/null
@@ -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 (file)
index 0000000..55c37fb
--- /dev/null
@@ -0,0 +1,75 @@
+#include <ucw/lib.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <time.h>
+#include <libusb-1.0/libusb.h>
+
+#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<devn; i++)
+    {
+      struct libusb_device_descriptor desc;
+      libusb_device *dev = devlist[i];
+      if (!libusb_get_device_descriptor(dev, &desc))
+       {
+         if (desc.idVendor == NPIX_USB_VENDOR && desc.idProduct == NPIX_USB_PRODUCT)
+           {
+             printf("Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+             // FIXME: Free device list
+             return dev;
+           }
+       }
+    }
+
+  libusb_free_device_list(devlist, 1);
+  fprintf(stderr, "Device not found\n");
+  exit(1);
+}
+
+int main(int argc, char **argv)
+{
+  byte packet[NPIX_NUM_LEDS*3] = { };
+  int len = 0;
+
+  if (argc > 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;
+}