]> mj.ucw.cz Git - home-hw.git/commitdiff
Display test: Trivial clock
authorMartin Mares <mj@ucw.cz>
Sat, 9 May 2020 22:19:30 +0000 (00:19 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 9 May 2020 22:19:30 +0000 (00:19 +0200)
test-display/host/Makefile [new file with mode: 0644]
test-display/host/test.c [new file with mode: 0644]
test-display/main.c

diff --git a/test-display/host/Makefile b/test-display/host/Makefile
new file mode 100644 (file)
index 0000000..5e6e00c
--- /dev/null
@@ -0,0 +1,12 @@
+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
+
+clean:
+       rm -f test
diff --git a/test-display/host/test.c b/test-display/host/test.c
new file mode 100644 (file)
index 0000000..cad8474
--- /dev/null
@@ -0,0 +1,104 @@
+#include <ucw/lib.h>
+#include <ucw/unaligned.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <time.h>
+#include <libusb-1.0/libusb.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 == 0x4242 && desc.idProduct == 0x0007)
+           {
+             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(void)
+{
+  int err;
+  if (err = libusb_init(&usb_ctxt))
+    {
+      fprintf(stderr, "Cannot initialize libusb: error %d\n", err);
+      exit(1);
+    }
+  // libusb_set_debug(usb_ctxt, 3);
+
+  libusb_device *dev = find_device();
+
+  if (err = libusb_open(dev, &devh))
+    {
+      fprintf(stderr, "Cannot open device: error %d\n", err);
+      exit(1);
+    }
+  libusb_reset_device(devh);
+  if (err = libusb_claim_interface(devh, 0))
+    {
+      fprintf(stderr, "Cannot claim interface: error %d\n", err);
+      exit(1);
+    }
+
+  for (;;)
+    {
+      time_t t = time(NULL);
+      struct tm *tm = localtime(&t);
+
+      unsigned char req[8] = {
+       tm->tm_hour / 10,
+       tm->tm_hour % 10,
+       tm->tm_min / 10,
+       tm->tm_min % 10,
+       (tm->tm_sec % 2 ? 0xff : 0),
+      };
+      int transferred;
+      if (err = libusb_bulk_transfer(devh, 0x01, req, 5, &transferred, 1000))
+       die("Transfer failed: error %d", err);
+      // printf("Transferred %d bytes\n", transferred);
+
+#if 0
+      unsigned char resp[64];
+      int received;
+      if (err = libusb_bulk_transfer(devh, 0x82, resp, 64, &received, 2000))
+       die("Receive failed: error %d", err);
+      // printf("Received %d bytes\n", received);
+      if (received >= 12)
+       {
+         int t = get_u32_be(resp);
+         int p = get_u32_be(resp + 4);
+         uint cnt = get_u32_be(resp + 8);
+         msg(L_INFO, "Temperature %d ddegC, pressure %d Pa, cnt %u", t, p, cnt);
+       }
+#endif
+
+      sleep(1);
+    }
+
+  return 0;
+}
index 47c553f573ce53dd6d8fb90a32221389920b1bf9..0573a5e7c42f9c884c1d8a98613ea1bba195a4d1 100644 (file)
@@ -170,6 +170,25 @@ static void display_test(void)
        mode = !mode;
 }
 
+static const byte lcd_font[] = {
+       [0] = 0x7e,
+       [1] = 0x12,
+       [2] = 0xbc,
+       [3] = 0xb6,
+       [4] = 0xd2,
+       [5] = 0xe6,
+       [6] = 0xee,
+       [7] = 0x32,
+       [8] = 0xfe,
+       [9] = 0xf6,
+       [10] = 0xfa,
+       [11] = 0xce,
+       [12] = 0x6c,
+       [13] = 0x9e,
+       [14] = 0xec,
+       [15] = 0xe8,
+};
+
 /*** USB ***/
 
 static usbd_device *usbd_dev;
@@ -296,14 +315,24 @@ static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
        return USBD_REQ_HANDLED;
 }
 
-# if 0
 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
 {
        // We received a frame from the USB host
-       uint len = usbd_ep_read_packet(dev, 0x01, dmx_next_packet, DMX_MAX_PACKET_SIZE);
+       byte buf[8];
+       uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
        debug_printf("USB: Host sent %u bytes\n", len);
+       if (len >= 5) {
+               for (uint i=0; i<4; i++) {
+                       if (buf[i] < 16)
+                               disp[i] = lcd_font[buf[i]];
+                       else
+                               disp[i] = 0;
+               }
+               if (buf[4])
+                       disp[1] |= 1;
+               display_update();
+       }
 }
-#endif
 
 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
 {
@@ -312,7 +341,7 @@ static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
                USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
                USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
                dfu_control_cb);
-       // usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
+       usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
        usb_configured = 1;
 }