--- /dev/null
+#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;
+}
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;
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)
{
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;
}