diff --git a/README.md b/README.md index ea00894..511082e 100644 --- a/README.md +++ b/README.md @@ -191,11 +191,12 @@ in a ring of **twenty-four** LEDs, written whole in one output report, per LED. Lighting LED 0 white and 1 to 3 green on the device's own numbering showed both rings counting **clockwise**, from the top on the left knob and from the **bottom** on the right: the right ring is mounted a half turn round, -which is the offset of twelve node-elgato-stream-deck gives it. This library -keeps the numbering the Galleon's rings settled — segment 0 at the top, -advancing anticlockwise — and `setRingColors` turns each ring into it; the same -marker sent through it put white at the top of both knobs with the green -running anticlockwise. Unlike the Galleon's, the Studio's rings **keep** their +which is the offset of twelve node-elgato-stream-deck gives it. Neither starts +quite where it looks to, though. With the marker sent through this library's +own numbering, white landed one LED clockwise of twelve o'clock on both knobs: +there is an LED dead at the top, and the device's LED 0 is the one after it. +This library numbers every ring from that top LED, clockwise, which is the +device's 23 on the left ring and its 11 on the right. Unlike the Galleon's, the Studio's rings **keep** their colour: drawn once, they stayed lit for as long as the connection was held. The **Mini** settled its own image transform, which two references had agreed @@ -326,6 +327,14 @@ And **both rings show that same arrangement**, which is the thing the two rotations exist to do and the reason to believe them: a wrong rotation would have left one ring turned against the other. +That was the numbering the two references produce, and it is not the one this +library now offers. Segments are numbered **clockwise** from the top on every +model with a ring, which is the way a level is read round a dial, so a Galleon's +segment 1 is at three o'clock and 3 at nine; `Encoders` reverses the ring before +applying the rotations above. That reversal is arithmetic on the layout the +photograph settled, and has not itself been looked at on a Galleon; the Studio's +clockwise numbering has. + One more thing the experiment turned up, which neither implementation mentions and which matters to anyone driving these. **A ring does not keep its colour.** The keyboard's own lighting engine owns those LEDs and paints back over diff --git a/src/Encoders.zig b/src/Encoders.zig index 0b52c97..41249fc 100644 --- a/src/Encoders.zig +++ b/src/Encoders.zig @@ -106,15 +106,22 @@ pub fn ledRingSteps(self: *const Encoders) u8 { /// All of it is confirmed against a Galleon. Walking encoder 0 lights the /// left knob and encoder 1 the right, which is what indices running from /// the right-hand encoder predicts. Lighting all four segments at once -/// in four colours then showed **segment 0 at the top of the ring, with -/// the segments advancing anticlockwise** -- 1 at nine o'clock, 2 at -/// six, 3 at three -- and showed both rings presenting that same -/// arrangement, which is the thing the two rotations exist to do. A -/// wrong rotation would have left one ring turned against the other. +/// in four colours then showed the rotations putting **the ring's zero +/// at the top, with the ring advancing anticlockwise** -- 1 at nine +/// o'clock, 2 at six, 3 at three -- and showed both rings presenting +/// that same arrangement, which is the thing the two rotations exist to +/// do. A wrong rotation would have left one ring turned against the +/// other. +/// +/// This library numbers segments **clockwise** from the top, on every +/// model, because that is the way a level is read round a dial. So a +/// segment is reversed into that anticlockwise order before it is +/// rotated: segment 1 is at three o'clock and 3 at nine. fn ledIndex(self: *const Encoders, encoder: u8, segment: u8) u8 { const steps = self.ledRingSteps(); const rotation: u8 = if (encoder == 0) 3 else 1; - const hardware = (segment + steps - rotation) % steps; + const anticlockwise = (steps - segment) % steps; + const hardware = (anticlockwise + steps - rotation) % steps; return (self.count() - 1 - encoder) * steps + hardware; } @@ -194,9 +201,10 @@ pub fn setRingColor( /// round the ring from its segment zero. /// /// Segment 0 is at the **top** of the ring and they advance -/// **anticlockwise**, so `colors` reads round the ring the way a clock -/// does not. That is what a Galleon does of its own accord, and a -/// Studio is turned into it; both confirmed on hardware. +/// **clockwise**, so `colors` reads round the ring the way a clock does +/// and a level can be drawn as a run from the start of the slice. Each +/// model's own numbering is turned into that; both confirmed on +/// hardware. /// /// `colors` has to hold exactly `ledRingSteps` entries, each a red, /// green and blue byte; anything else is `error.WrongNumberOfColors`. @@ -237,24 +245,26 @@ const studio_ring_steps = 24; /// of its twenty-four LEDs, in one output report where a Galleon takes a /// feature report per LED. node-elgato-stream-deck sends the same. /// -/// The device numbers each ring **clockwise**, and not from the same -/// place: encoder 0's LED 0 is at the top of the left knob, and encoder -/// 1's at the **bottom** of the right one, the ring mounted a half turn -/// round -- which is the offset of twelve node applies to it. Both were -/// read off a Studio lit with LED 0 white and 1 to 3 green. `colors` -/// arrives in this library's own order, segment 0 at the top running -/// anticlockwise as on the Galleon, so each entry is put where the -/// device will show it there. +/// The device numbers each ring **clockwise**, and not from the top: on +/// encoder 0 its LED 0 is one step clockwise of twelve o'clock, so the +/// LED at the top is its 23, and encoder 1's ring is mounted a half +/// turn round, so its top is 11 -- node's offset of twelve between +/// them. Both were read off a Studio lit with LED 0 white and 1 to 3 +/// green, and the step at the top from the same marker sent through +/// this function, which first put white one LED clockwise of the top. +/// `colors` arrives in this library's own order, segment 0 at the top +/// running clockwise, the device's own direction, so each ring needs +/// only turning. fn studioRingReport(packet: *[StreamDeck.MAX_PACKET_SIZE]u8, encoder: u8, colors: []const [3]u8) []const u8 { std.debug.assert(colors.len == studio_ring_steps); @memset(packet, 0); packet[0] = 0x02; packet[1] = 0x0f; packet[2] = encoder; - const start: usize = if (encoder == 0) 0 else studio_ring_steps / 2; + // The device's LED at twelve o'clock. + const start: usize = if (encoder == 0) studio_ring_steps - 1 else studio_ring_steps / 2 - 1; for (colors, 0..) |colour, segment| { - // Anticlockwise from the top is clockwise backwards from it. - const led = (start + studio_ring_steps - segment) % studio_ring_steps; + const led = (start + segment) % studio_ring_steps; @memcpy(packet[3 + led * 3 ..][0..3], &colour); } return packet; @@ -432,20 +442,21 @@ test "a Studio's rings are turned into this library's numbering" { var packet: [StreamDeck.MAX_PACKET_SIZE]u8 = undefined; - // The left ring: the device's LED 0 is at the top, as segment 0 is, - // and it counts clockwise where segments count anticlockwise, so - // segment 1 is the device's LED 23. + // The left ring: the LED at the top is the device's 23, and it + // counts clockwise as segments do, so segment 1 is its 0. const left = studioRingReport(&packet, 0, &colors); try std.testing.expectEqualSlices(u8, &.{ 0x02, 0x0f, 0x00 }, left[0..3]); - try std.testing.expectEqualSlices(u8, &.{ 255, 255, 255 }, left[3..6]); - try std.testing.expectEqualSlices(u8, &.{ 0, 255, 0 }, left[3 + 23 * 3 ..][0..3]); - try std.testing.expectEqualSlices(u8, &.{ 1, 1, 1 }, left[3 + 1 * 3 ..][0..3]); + try std.testing.expectEqualSlices(u8, &.{ 255, 255, 255 }, left[3 + 23 * 3 ..][0..3]); + try std.testing.expectEqualSlices(u8, &.{ 0, 255, 0 }, left[3..6]); + try std.testing.expectEqualSlices(u8, &.{ 1, 1, 1 }, left[3 + 22 * 3 ..][0..3]); - // The right ring: its LED 0 is at the bottom, so the top is its 12. + // The right ring, a half turn round: the top is its 11 and + // segment 1 its 12. const right = studioRingReport(&packet, 1, &colors); try std.testing.expectEqual(0x01, right[2]); - try std.testing.expectEqualSlices(u8, &.{ 255, 255, 255 }, right[3 + 12 * 3 ..][0..3]); - try std.testing.expectEqualSlices(u8, &.{ 0, 255, 0 }, right[3 + 11 * 3 ..][0..3]); + try std.testing.expectEqualSlices(u8, &.{ 255, 255, 255 }, right[3 + 11 * 3 ..][0..3]); + try std.testing.expectEqualSlices(u8, &.{ 0, 255, 0 }, right[3 + 12 * 3 ..][0..3]); + try std.testing.expectEqualSlices(u8, &.{ 1, 1, 1 }, right[3 + 10 * 3 ..][0..3]); // Nothing past the twenty-four colours. for (right[3 + 24 * 3 ..]) |b| try std.testing.expectEqual(0, b); @@ -458,22 +469,25 @@ test "an encoder ring's segments are numbered from the right-hand encoder" { // ring's segment zero sits at a different angle, so the segments // within a ring are rotated as well. // - // These are the values rust-elgato-streamdeck asserts, read the - // other way round: it maps hardware segment to logical, and this - // maps logical to the flat index. node-elgato-stream-deck arrives - // at the same numbering through a `ledRingOffset` of three and one. + // The rotations are the ones rust-elgato-streamdeck asserts, read + // the other way round: it maps hardware segment to logical, and + // this maps logical to the flat index. node-elgato-stream-deck + // arrives at the same through a `ledRingOffset` of three and one. + // Both number the result anticlockwise, and this library reverses + // it to run clockwise, so segments 1 and 3 have traded places with + // what those two assert. var galleon = testDeck(.CorsairGalleonK100); const e = &galleon.encoders; try std.testing.expectEqual(5, e.ledIndex(0, 0)); - try std.testing.expectEqual(6, e.ledIndex(0, 1)); + try std.testing.expectEqual(4, e.ledIndex(0, 1)); try std.testing.expectEqual(7, e.ledIndex(0, 2)); - try std.testing.expectEqual(4, e.ledIndex(0, 3)); + try std.testing.expectEqual(6, e.ledIndex(0, 3)); try std.testing.expectEqual(3, e.ledIndex(1, 0)); - try std.testing.expectEqual(0, e.ledIndex(1, 1)); + try std.testing.expectEqual(2, e.ledIndex(1, 1)); try std.testing.expectEqual(1, e.ledIndex(1, 2)); - try std.testing.expectEqual(2, e.ledIndex(1, 3)); + try std.testing.expectEqual(0, e.ledIndex(1, 3)); // Between them the two rings address all eight LEDs exactly once, // which is what says the rotations are a permutation rather than a diff --git a/src/devices/CorsairGalleonK100.zig b/src/devices/CorsairGalleonK100.zig index 3147d16..e992b89 100644 --- a/src/devices/CorsairGalleonK100.zig +++ b/src/devices/CorsairGalleonK100.zig @@ -67,11 +67,11 @@ //! `touch = false` for this model. See `Events.zig` for the reports //! themselves, which are tests now. //! -//! It is also the only model here whose encoders carry LEDs: a ring of -//! four around each, addressable through `Encoders.setRingColor` and +//! Its encoders carry LEDs, as only the Studio's otherwise do: a ring +//! of four around each, addressable through `Encoders.setRingColor` and //! `setRingColors`, and all of it seen on hardware: encoder 0 is the -//! left knob, segment 0 is at the top of a ring, and the segments -//! advance anticlockwise. A ring will not **keep** a colour, though -- +//! left knob, and the device's own ring runs anticlockwise from the +//! top, which `Encoders` reverses so that segments run clockwise. A ring will not **keep** a colour, though -- //! the keyboard's lighting engine paints back over it within about a //! second, so holding one means re-sending it, and `keepAlive` does not //! help. `probe --rings` is the experiment. diff --git a/tools/probe.zig b/tools/probe.zig index 6be42e3..168bbf6 100644 --- a/tools/probe.zig +++ b/tools/probe.zig @@ -1080,7 +1080,7 @@ fn showRings( // So it gets the marker `--studio-rings` put on the device's own // numbering, segment 0 white and 1 to 3 green, drawn once, with // `--hold` to keep it up. If this library's numbering is right both - // knobs show white at the top and green running anticlockwise. + // knobs show white at the top and green running clockwise. if (sd.device.tcp() != null) { for (0..steps) |i| colours[i] = switch (i) { 0 => .{ 255, 255, 255 }, @@ -1106,7 +1106,7 @@ fn showRings( try out.print(" encoder {d}: {t}\n", .{ encoder, err }); return; }; - try out.print(" encoder {d}: segment 0 red, 1 green, 2 blue, 3 white\n", .{encoder}); + try out.print(" encoder {d}: segment 0 red, 1 green, 2 blue, 3 white, running clockwise from the top\n", .{encoder}); } }