]> mj.ucw.cz Git - ursary.git/blobdiff - nocturn.c
Added udev rules
[ursary.git] / nocturn.c
index 12ea9c185ca83c5f343bda92001320344b28b137..a313b4949c8f6bd757a4d28b99d3c40283e407ea 100644 (file)
--- a/nocturn.c
+++ b/nocturn.c
@@ -88,6 +88,8 @@ static const char noct_magic[4][9] = {
 
 static struct libusb_transfer *noct_read_xfer;
 static bool noct_read_pending;
+char noct_rotary_touched[10];
+char noct_button_pressed[16];
 
 static void noct_read_done(struct libusb_transfer *xfer)
 {
@@ -140,6 +142,7 @@ static void noct_read_done(struct libusb_transfer *xfer)
          if (arg < 0x80)
            {
              DBG("Noct: Slider value = %d", arg);
+             notify_rotary(9, arg);
              continue;
            }
          break;
@@ -160,6 +163,8 @@ static void noct_read_done(struct libusb_transfer *xfer)
            {
              int state = !!arg;
              DBG("Noct: Center touch = %d", state);
+             noct_rotary_touched[8] = state;
+             notify_touch(8, state);
              continue;
            }
          break;
@@ -168,6 +173,8 @@ static void noct_read_done(struct libusb_transfer *xfer)
            {
              int state = !!arg;
              DBG("Noct: Slider touch = %d", state);
+             noct_rotary_touched[9] = state;
+             notify_touch(9, state);
              continue;
            }
          break;
@@ -177,6 +184,8 @@ static void noct_read_done(struct libusb_transfer *xfer)
              int r = cmd - 0x60;
              int state = !!arg;
              DBG("Noct: Rotary %d touch = %d", r, state);
+             noct_rotary_touched[r] = state;
+             notify_touch(r, state);
              continue;
            }
          break;
@@ -186,6 +195,7 @@ static void noct_read_done(struct libusb_transfer *xfer)
              int b = cmd - 0x70;
              int state = !!arg;
              DBG("Noct: Button %d = %d", b, state);
+             noct_button_pressed[b] = state;
              notify_button(b, state);
              continue;
            }
@@ -215,13 +225,13 @@ static void noct_read_init(void)
     noct_read_pending = 1;
 }
 
-static byte noct_button_state[16];
+static byte noct_button_light[16];
 static byte noct_ring_mode[8];         // RING_MODE_xxx
 static byte noct_ring_val[9];
 
-static uns noct_dirty_button;
-static uns noct_dirty_ring_mode;
-static uns noct_dirty_ring_val;
+static uint noct_dirty_button;
+static uint noct_dirty_ring_mode;
+static uint noct_dirty_ring_val;
 
 static struct libusb_transfer *noct_write_xfer;
 static bool noct_write_pending;
@@ -241,7 +251,7 @@ static void noct_write_done(struct libusb_transfer *xfer)
   noct_sched_write();
 }
 
-static void noct_do_write(uns cmd, uns arg)
+static void noct_do_write(uint cmd, uint arg)
 {
   DBG("USB: Submitting write %02x %02x", cmd, arg);
   ASSERT(!noct_write_pending);
@@ -260,14 +270,14 @@ static void noct_do_write(uns cmd, uns arg)
 
 static void noct_sched_write(void)
 {
-  if (noct_write_pending)
+  if (!usb_dev || noct_write_pending)
     return;
 
   if (noct_dirty_button)
     {
       int i = bit_ffs(noct_dirty_button);
       noct_dirty_button ^= 1U << i;
-      noct_do_write(0x70 + i, noct_button_state[i]);
+      noct_do_write(0x70 + i, noct_button_light[i]);
     }
   else if (noct_dirty_ring_mode)
     {
@@ -280,7 +290,7 @@ static void noct_sched_write(void)
       int i = bit_ffs(noct_dirty_ring_val);
       noct_dirty_ring_val ^= 1U << i;
       if (i == 8)
-       noct_do_write(0x50 + i, noct_ring_val[i]);
+       noct_do_write(0x50, noct_ring_val[i]);
       else
        noct_do_write(0x40 + i, noct_ring_val[i]);
     }
@@ -310,14 +320,22 @@ void noct_set_button(int button, int val)
 {
   ASSERT(button >= 0 && button < 16);
   ASSERT(val == 0 || val == 1);
-  if (noct_button_state[button] != val)
+  if (noct_button_light[button] != val)
     {
-      noct_button_state[button] = val;
+      noct_button_light[button] = val;
       noct_dirty_button |= 1U << button;
       noct_sched_write();
     }
 }
 
+void noct_clear(void)
+{
+  for (int i=0; i<=8; i++)
+    noct_set_ring(i, RING_MODE_LEFT, 0);
+  for (int i=0; i<16; i++)
+    noct_set_button(i, 0);
+}
+
 static void noct_write_init(void)
 {
   DBG("Noct: Write init");
@@ -325,12 +343,8 @@ static void noct_write_init(void)
   noct_write_xfer = libusb_alloc_transfer(0);
   libusb_fill_interrupt_transfer(noct_write_xfer, usb_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
 
-#if 0 // FIXME
-  noct_button_state[2] = 1;
-  noct_ring_mode[0] = 4;
-  noct_ring_val[0] = 0x40;
-#endif
-
+  bzero(noct_button_pressed, sizeof(noct_button_pressed));
+  bzero(noct_rotary_touched, sizeof(noct_rotary_touched));
   noct_dirty_button = 0xffff;
   noct_dirty_ring_mode = 0xff;
   noct_dirty_ring_val = 0x1ff;
@@ -357,7 +371,7 @@ static void noct_connect(struct main_timer *t)
          desc.idVendor == 0x1235 &&
          desc.idProduct == 0x000a)
        {
-         msg(L_DEBUG, "Found device: bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+         msg(L_INFO, "Nocturn found at bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
          if (found_dev)
            {
              msg(L_ERROR, "Multiple Nocturn devices found. Using the first one.");
@@ -375,13 +389,13 @@ static void noct_connect(struct main_timer *t)
       return;
     }
 
-  msg(L_DEBUG, "Initializing Nocturn");
+  DBG("Initializing Nocturn");
 
   if ((err = libusb_open(found_dev, &usb_dev)) < 0)
     return noct_error(err, "libusb_open failed");
 
   // There exist configurations 1 (high brightness) and 2 (power-save)
-  if ((err = libusb_set_configuration(usb_dev, 1)) < 0)
+  if ((err = libusb_set_configuration(usb_dev, 2)) < 0)
     return noct_error(err, "libusb_set_configuration failed");
 
   if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
@@ -397,12 +411,6 @@ static void noct_connect(struct main_timer *t)
        return noct_error(err, stk_printf("Partial send of init packet (%d < %d)", done, noct_magic[i][0]));
     }
 
-#if 0
-  byte xxx[] = { 0x7f, 0x01 };
-  int done;
-  libusb_interrupt_transfer(usb_dev, 0x02, xxx, 2, &done, 5000);
-#endif
-
   noct_read_init();
   noct_write_init();
   schedule_update();
@@ -424,6 +432,7 @@ static int noct_error_handler(struct main_hook *h)
              noct_read_pending = 0;
            }
          DBG("Noct: Tearing down read xfer");
+         xfree(noct_read_xfer->buffer);
          libusb_free_transfer(noct_read_xfer);
          noct_read_xfer = NULL;
        }
@@ -436,6 +445,7 @@ static int noct_error_handler(struct main_hook *h)
              noct_write_pending = 0;
            }
          DBG("Noct: Tearing down write xfer");
+         xfree(noct_write_xfer->buffer);
          libusb_free_transfer(noct_write_xfer);
          noct_write_xfer = NULL;
        }
@@ -470,7 +480,7 @@ static void noct_error(int usb_err, char *text)
 
 bool noct_is_ready(void)
 {
-  return !!usb_dev;    // FIXME
+  return !!usb_dev;
 }
 
 void noct_init(void)
@@ -501,5 +511,5 @@ void noct_init(void)
 
   // Schedule search for the Nocturn
   noct_connect_timer.handler = noct_connect;
-  timer_add_rel(&noct_connect_timer, 0);
+  timer_add_rel(&noct_connect_timer, 100);
 }