#avr Logs

Mar 08 2022

#avr Calendar

03:59 AM specing_ is now known as specing
03:25 PM * LeoNerd ponders the tricky task of LED gamma curves, on an 8bit AVR
03:25 PM LeoNerd: I don't *really* want to do floating point maths to do exp(log(val) * 1.8)
03:25 PM LeoNerd: I imagine some sort of lookup table
03:26 PM qu1j0t3: haha yeah floating point on an avr
03:28 PM qu1j0t3: LeoNerd: you could also use a lookup and bilinear interp in fixed point if you need more precision
03:28 PM LeoNerd: I don't need much. It's just to make LED dimming look a bit visually smoother
03:28 PM qu1j0t3: idk what val looks like
03:28 PM qu1j0t3: probably a straight table is enough yeah
03:28 PM LeoNerd: An 8bit table, 256 bytes. I have plenty of spare flash
03:29 PM LeoNerd: Flash lookups are cheap on tiny412 thanks to unified memory :)
03:29 PM * qu1j0t3 nods
03:29 PM qu1j0t3: perfick
03:29 PM * qu1j0t3 goes back to despised front-end
03:35 PM cehteh: LeoNerd: just exponential is already pretty good
03:36 PM cehteh: i mean as in val²
03:37 PM LeoNerd: I suppose a gamma of 2.0 isn't -far- off ideal LED gamma of 1.8
03:38 PM cehteh: yes, and 1.8 isnt ideal either, depends on the color too
03:40 PM cehteh: i never tried, but my plan would be to make a 16 entry table and interpolate inbetween
03:41 PM cehteh: doesnt even need a table since the values are power of 2 - 1
03:41 PM LeoNerd: A 256 entry table would take 256 bytes of flash. A 16 entry table with interpolation would eat 16 bytes + whatever the interpolation code takes. It wouldn't surprsie me if that was bigger
03:41 PM cehteh: 0 1 3 7 15 31 63 127 255
03:42 PM qu1j0t3: yeah interpolation will take mucho cycles even if fixed point
03:42 PM cehteh: when you calculate in 16bit you need no division, the code would be pretty small
03:42 PM cehteh: nah
03:43 PM qu1j0t3: i mean relative to a lookup
03:43 PM cehteh: and modern avrs have mul
03:43 PM cehteh: yeah even that its not that costly, ok 16bit and little math, but its really cheap
03:43 PM twnqx: just add an external FPU
03:43 PM cehteh: lol
03:43 PM qu1j0t3: hahahahhahashhajsahsajhs
03:44 PM qu1j0t3: don't joke
03:44 PM * twnqx has some amd for 8080
03:44 PM qu1j0t3: internet of things: calls an https remote api to do each brightness calc
03:44 PM twnqx: and some undocumented NEC chip for 8088 :P
03:45 PM cehteh: enlighten the cloud!
03:45 PM LeoNerd: A little lookup table generated by `perl -E 'printf "%d, ", 0.5+255*(($_/255) ** 1.8) for 0..255'` turns out to be juuuuust fine
03:45 PM qu1j0t3: API responds with an IFRAME and embedded ad
03:45 PM qu1j0t3: LeoNerd: SHIP IT
03:45 PM LeoNerd: And I popped that code in a comment so I can easily regenerate it with other values if I need to :)
03:45 PM qu1j0t3: kind of like you've done this before LeoNerd !
03:46 PM LeoNerd: It's already shipped, burned into my chip, and controlling my bedside lamp as we speak :)
03:46 PM qu1j0t3: now ZZZZZ
03:46 PM cehteh: hey i bet you can take a zillion tiny25 order them in a quadratic matrix, make some connections and let them behave like a neuronal net! or fpga when you want it simple
03:46 PM LeoNerd: Oooh wait a moment, I was going to do it 10bit output using the fancier PWM chip
03:46 PM qu1j0t3: rip
03:47 PM cehteh: neuronal net even in analaog with rc filter on pwm and adc inputs!
03:47 PM qu1j0t3: suddenly interpolation looks good
03:47 PM cehteh: :)
03:47 PM qu1j0t3: cheap to get an extra 2 bits!
03:47 PM LeoNerd: Nah, still 8bit input, 10bit output
03:47 PM qu1j0t3: oh
03:47 PM qu1j0t3: nm
03:47 PM cehteh: lol
03:47 PM LeoNerd: The control value is 8bit, I just haev more bits of PWM to play with, so the lower end is smoother
03:47 PM cehteh: go fancy and make it PCM nt PWM .. way smoother
03:48 PM cehteh: esp on the low end you'll get much dimmer brightness with pcm when done right
03:49 PM cehteh: and less noticeable steps
03:50 PM cehteh: ΣΔ led :)
03:55 PM twnqx: use some 8bit float for more range :P
03:57 PM qu1j0t3: yes, you only need to store the exponent, after all.
03:59 PM specing_ is now known as specing
04:02 PM LeoNerd: OK I went a bit fancier.. 12bit PWM, because it's smoother at the low end. ;)
04:03 PM LeoNerd: The hardware is definitely fast enough to cope
04:04 PM qu1j0t3: <scotty voice> She canna take much more of this
04:23 PM LeoNerd: https://twitter.com/cpan_pevans/status/1501318122283884544 -- here it is BTW
05:39 PM LeoNerd: I've just thought of a nice way to do an 8->10bit gamma lookup without needing to waste 512 bytes on a 256-entry 10bit lookup table
05:40 PM LeoNerd: Rely on the fact that it's monotonic. Store the lower 8bits in a lookup table as normal, then construct the top 2 bits of output by realising that they are going to be 00 for input values up to some limit 'x', then 01 up to some limit 'y', then 10 to some limit 'z', then 11 after that. So a simple 3 way "if" to put those bits on
05:41 PM LeoNerd: A 8->12 would be similar but needs 15 limit values; maybe a second 15-entry lookup table and a linear scan of that
06:03 PM Phantom: too costly
06:06 PM Phantom: uint16_t color; uint8_t r,g,b,x; color = r & (x & 0b00000011)<<8; color = g & ((x >> 2) & 0b00000011)<<8 <=== even that is unoptimised...