flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > color encoding for ws2812, MCU and why not, x86

Author
Thread Post new topic Reply to topic
edfed



Joined: 20 Feb 2006
Posts: 4270
Location: Now
edfed
I am playing with ws2812, ATmega328 with arduino IDE (shame on me)
I use adafruit neopixel lib(shame on me)
and now, i meet a real limit... a need more ram. but i don't need more ram.

in this lib, they use 32 or 24 bits per pixel, but i don't need that much bits.

then, I want to modify this lib and then have a 8 bits per pixel encoding.

my idea is to split the byte in 2 nibbles, the first will be the color, and the second will be the light.

and the resulting palette is this:
Image

i don't find anything about. then, what do you think about it?


Description:
Filesize: 1023 Bytes
Viewed: 1203 Time(s)

new 8bits palette.png


Post 22 Jan 2022, 00:29
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18748
Location: In your JS exploiting you and your system
revolution
Would a 3:3:2 distribution give more colour choices?

You could use either RGB or YUV encoding for the bit fields.
Post 22 Jan 2022, 02:44
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
LED stick can be fun! I use WS2318 with ATmega328P, to create an audio visualizer:

Image
Post 22 Jan 2022, 09:17
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4270
Location: Now
edfed
yeppee, i had the time to code.

the lib can use a palette or a direct value.
if it uses a palette, the pixels are stored in one byte.
with direct value, it is like original with 3bytes.
the palette can be any size (no check at runtime) between 0 and 256 colors, stored as 4 bytes.

https://gitlab.com/foolavr/ws2812

here is the atomic part responsible of the NRZ transmission with palette.
the timings seems to be ok even if i use dirty C adressing to lookup the palette.
Code:
if (isPalette) {
    i = numBytes; 
    volatile uint8_t c;
    volatile uint16_t p;    
    while (i != 0) {
      p = *ptr;
      for (c = 0; c < 3; c++) {
        bit = 8;
        b = ((uint8_t*)(palette))[(p<<2)+c];
        asm volatile(
         "head201:"                   "\n\t" // Clk  Pseudocode    (T =  0)
          "st   %a[port],  %[hi]"    "\n\t" // 2    PORT = hi     (T =  2)
          "sbrc %[byte],  7"         "\n\t" // 1-2  if(b & 128)
           "mov  %[next], %[hi]"     "\n\t" // 0-1   next = hi    (T =  4)
          "dec  %[bit]"              "\n\t" // 1    bit--         (T =  5)
          "st   %a[port],  %[next]"  "\n\t" // 2    PORT = next   (T =  7)
          "mov  %[next] ,  %[lo]"    "\n\t" // 1    next = lo     (T =  8)
          "breq nextbyte201"          "\n\t" // 1-2  if(bit == 0) (from dec above)
          "rol  %[byte]"             "\n\t" // 1    b <<= 1       (T = 10)
          "rjmp .+0"                 "\n\t" // 2    nop nop       (T = 12)
          "nop"                      "\n\t" // 1    nop           (T = 13)
          "st   %a[port],  %[lo]"    "\n\t" // 2    PORT = lo     (T = 15)
          "nop"                      "\n\t" // 1    nop           (T = 16)
          "rjmp .+0"                 "\n\t" // 2    nop nop       (T = 18)
          "rjmp head201"              "\n\t" // 2    -> head20 (next bit out)
         "nextbyte201:"               "\n\t" //                    (T = 10)
          "st   %a[port], %[lo]"     "\n\t" // 2    PORT = lo     (T = 15)
    
          : [port]  "+e" (port),
            [byte]  "+r" (b),
            [bit]   "+r" (bit),
            [next]  "+r" (next)
          : [hi]     "r" (hi),
            [lo]     "r" (lo)
        );
      }
      ptr++;
      i--;
    }
  }

    


this is a trim of adafruit lib to fit my specific requirements, WS2812 with a ATMEGA328 at 16MHz, using arduino IDE.

it looks to work very well with my 16*16 matrix and i can now return to the project that needed this ram use reduction.
Post 18 Feb 2022, 00:31
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.