-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadafruit_pixel_framebuf.py
executable file
·130 lines (107 loc) · 4.5 KB
/
adafruit_pixel_framebuf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_pixel_framebuf`
================================================================================
Neopixel and Dotstar Framebuffer Helper
* Author(s): Melissa LeBlanc-Williams
Implementation Notes
--------------------
**Hardware:**
* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
* `Adafruit DotStars <https://www.adafruit.com/category/885>`_
* `Flexible 8x32 NeoPixel RGB LED Matrix <https://www.adafruit.com/product/2294>`_
* `Flexible 16x16 NeoPixel RGB LED Matrix <https://www.adafruit.com/product/2547>`_
* `Flexible 8x8 NeoPixel RGB LED Matrix <https://www.adafruit.com/product/2612>`_
* `Adafruit NeoPixel 8x8 NeoMatrices <https://www.adafruit.com/product/3052>`_
* `Adafruit DotStar High Density 8x8 Grid <https://www.adafruit.com/product/3444>`_
* `Adafruit NeoPixel FeatherWing <https://www.adafruit.com/product/2945>`_
* `Adafruit DotStar FeatherWing <https://www.adafruit.com/product/3449>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
* Adafruit's LED Animation library:
https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation
* Adafruit's framebuf library: https://github.com/adafruit/Adafruit_CircuitPython_framebuf
"""
# imports
try:
from circuitpython_typing.led import FillBasedColorUnion
except ImportError:
pass
import adafruit_framebuf
from adafruit_led_animation.grid import PixelGrid
from micropython import const
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Pixel_Framebuf.git"
HORIZONTAL: int = const(1)
VERTICAL: int = const(2)
# pylint: disable=too-many-function-args
class PixelFramebuffer(adafruit_framebuf.FrameBuffer):
"""
NeoPixel and Dotstar FrameBuffer for easy drawing and text on a
grid of either kind of pixel
:param strip: An object that implements the Neopixel or Dotstar protocol.
:param width: Framebuffer width.
:param height: Framebuffer height.
:param orientation: Orientation of the strip pixels - HORIZONTAL (default) or VERTICAL.
HORIZONTAL and VERTICAL are primitive integers created by micropython.const(x).
:param alternating: Whether the strip alternates direction from row to row (default True).
:param reverse_x: Whether the strip X origin is on the right side (default False).
:param reverse_y: Whether the strip Y origin is on the bottom (default False).
:param tuple top: (x, y) coordinates of grid top left corner (Optional)
:param tuple bottom: (x, y) coordinates of grid bottom right corner (Optional)
:param int rotation: A value of 0-3 representing the rotation of the framebuffer (default 0)
"""
# pylint: disable=too-many-arguments
def __init__(
self,
pixels: FillBasedColorUnion,
width: int,
height: int,
orientation: int = HORIZONTAL,
alternating: bool = True,
reverse_x: bool = False,
reverse_y: bool = False,
top: int = 0,
bottom: int = 0,
rotation: int = 0,
) -> None:
self._width = width
self._height = height
self._grid = PixelGrid(
pixels,
width,
height,
orientation,
alternating,
reverse_x,
reverse_y,
top,
bottom,
)
self._buffer = bytearray(width * height * 3)
self._double_buffer = bytearray(width * height * 3)
super().__init__(
self._buffer, width, height, buf_format=adafruit_framebuf.RGB888
)
self.rotation = rotation
def blit(self) -> None:
"""blit is not yet implemented"""
raise NotImplementedError()
def display(self) -> None:
"""Copy the raw buffer changes to the grid and show"""
for _y in range(self._height):
for _x in range(self._width):
index = (_y * self.stride + _x) * 3
if (
self._buffer[index : index + 3]
!= self._double_buffer[index : index + 3]
):
self._grid[(_x, _y)] = tuple(self._buffer[index : index + 3])
self._double_buffer[index : index + 3] = self._buffer[
index : index + 3
]
self._grid.show()