todbot / blink1-tool

Command-line tools and C library for blink(1) USB RGB LED
https://blink1.thingm.com/
Other
84 stars 15 forks source link

blink1_readRGB returns wrong values #59

Closed profburke closed 1 year ago

profburke commented 1 year ago

Ok, this is probably PEBKAC, but ...

I'm working on updating my Lua wrapper of the Blink library and am having an issue with blink1_readRGB. Below is a minimal C program that demonstrates the problem:

#include <stdio.h>
#include "blink1-lib.h"

int main(int argc, char **argv) {
  blink1_device *d = blink1_open();

  uint8_t r, g, b;
  uint16_t millis;

  for (int i = 255; i >= 0; i-=10) {
    blink1_setRGB(d, r, g, b);
    r = i; g = i; b = i;
    printf("Setting: %d %d %d -- ", r, g, b);
    blink1_setRGB(d, r, g, b);
    blink1_sleep(250);
    blink1_readRGB(d, &millis, &r, &g, &b, 1);
    printf("Reading: %d %d %d\n", r, g, b);
    blink1_sleep(250);
  }

  return 0;
}

My expectation is that the values returned by the read call should match the preceding set. But that's not the case:

Setting: 255 255 255 -- Reading: 255 255 255
Setting: 245 245 245 -- Reading: 233 233 233
Setting: 235 235 235 -- Reading: 212 212 212
Setting: 225 225 225 -- Reading: 193 193 193
Setting: 215 215 215 -- Reading: 174 174 174
Setting: 205 205 205 -- Reading: 157 157 157
Setting: 195 195 195 -- Reading: 140 140 140
Setting: 185 185 185 -- Reading: 124 124 124
Setting: 175 175 175 -- Reading: 110 110 110
Setting: 165 165 165 -- Reading: 96 96 96
Setting: 155 155 155 -- Reading: 84 84 84
Setting: 145 145 145 -- Reading: 72 72 72
Setting: 135 135 135 -- Reading: 62 62 62
Setting: 125 125 125 -- Reading: 52 52 52
Setting: 115 115 115 -- Reading: 43 43 43
Setting: 105 105 105 -- Reading: 35 35 35
Setting: 95 95 95 -- Reading: 28 28 28
Setting: 85 85 85 -- Reading: 22 22 22
Setting: 75 75 75 -- Reading: 16 16 16
Setting: 65 65 65 -- Reading: 12 12 12
Setting: 55 55 55 -- Reading: 8 8 8
Setting: 45 45 45 -- Reading: 5 5 5
Setting: 35 35 35 -- Reading: 3 3 3
Setting: 25 25 25 -- Reading: 1 1 1
Setting: 15 15 15 -- Reading: 0 0 0
Setting: 5 5 5 -- Reading: 0 0 0

Bug? Or am I making a mistake?

Thanks!

todbot commented 1 year ago

By default, the blink1-tool and blink1 C library applies a gamma-correction curve to the colors to match how our eyes perceive brightness.

To disable this, use the (somewhat confusingly named) blink1_enableDegamma().

Apologies this isn't described well.

todbot commented 1 year ago

And thank you for making a Lua library for blink(1)!

profburke commented 1 year ago

Sure, happy to have done it. I think I may be the only person who uses it 🤣

Ok, that's the danger of just diving in and skimming for one thing in particular, rather than getting familiar with all the code. I hadn't noticed the gamma functions. Even worse, I didn't see blink1_adjustBrightness -- which should make unnecessary my attempt to code up my own half-baked version!