Fill a part of the buffer with RGB data. Order of colors in buffer is changed from RGB to GRB because WS2812 LED has GRB order of colors. Each color is represented by 4 bytes in buffer (1 byte for each 2 bits). Returns the index of the first unfilled LED
(self, data, start=0)
| 92 | gc.collect() |
| 93 | |
| 94 | def update_buf(self, data, start=0): |
| 95 | """ |
| 96 | Fill a part of the buffer with RGB data. |
| 97 | |
| 98 | Order of colors in buffer is changed from RGB to GRB because WS2812 LED |
| 99 | has GRB order of colors. Each color is represented by 4 bytes in buffer |
| 100 | (1 byte for each 2 bits). |
| 101 | |
| 102 | Returns the index of the first unfilled LED |
| 103 | |
| 104 | Note: If you find this function ugly, it's because speed optimisations |
| 105 | beated purity of code. |
| 106 | """ |
| 107 | |
| 108 | buf = self.buf |
| 109 | buf_bytes = self.buf_bytes |
| 110 | intensity = self.intensity |
| 111 | |
| 112 | mask = 0x03 |
| 113 | index = start * 12 |
| 114 | for red, green, blue in data: |
| 115 | red = int(red * intensity) |
| 116 | green = int(green * intensity) |
| 117 | blue = int(blue * intensity) |
| 118 | |
| 119 | buf[index] = buf_bytes[green >> 6 & mask] |
| 120 | buf[index+1] = buf_bytes[green >> 4 & mask] |
| 121 | buf[index+2] = buf_bytes[green >> 2 & mask] |
| 122 | buf[index+3] = buf_bytes[green & mask] |
| 123 | |
| 124 | buf[index+4] = buf_bytes[red >> 6 & mask] |
| 125 | buf[index+5] = buf_bytes[red >> 4 & mask] |
| 126 | buf[index+6] = buf_bytes[red >> 2 & mask] |
| 127 | buf[index+7] = buf_bytes[red & mask] |
| 128 | |
| 129 | buf[index+8] = buf_bytes[blue >> 6 & mask] |
| 130 | buf[index+9] = buf_bytes[blue >> 4 & mask] |
| 131 | buf[index+10] = buf_bytes[blue >> 2 & mask] |
| 132 | buf[index+11] = buf_bytes[blue & mask] |
| 133 | |
| 134 | index += 12 |
| 135 | |
| 136 | return index // 12 |
| 137 | |
| 138 | def fill_buf(self, data): |
| 139 | """ |