-
Notifications
You must be signed in to change notification settings - Fork 36
Add 2bit grayscale support #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
8000
|
@@ -16,6 +16,7 @@ | |
| **Hardware:** | ||
|
|
||
| * `Adafruit SSD1306 OLED displays <https://www.adafruit.com/?q=ssd1306>`_ | ||
| * `Adafruit HT16K33 Matrix displays <https://www.adafruit.com/?q=ht16k33>`_ | ||
|
|
||
| **Software and Dependencies:** | ||
|
|
||
|
|
@@ -36,6 +37,60 @@ | |
| GS4_HMSB = 2 # Unimplemented! | ||
| MHMSB = 3 # Single bit displays like the Sharp Memory | ||
| RGB888 = 4 # Neopixels and Dotstars | ||
| GS2_HMSB = 5 # 2-bit color displays like the HT16K33 8x8 Matrix | ||
|
|
||
|
|
||
| class GS2HMSBFormat: | ||
| """GS2HMSBFormat""" | ||
|
|
||
| @staticmethod | ||
| def set_pixel(framebuf, x, y, color): | ||
| """Set a given pixel to a color.""" | ||
| index = (y * framebuf.stride + x) >> 2 | ||
| pixel = framebuf.buf[index] | ||
|
|
||
| shift = (x & 0b11) << 1 | ||
| mask = 0b11 << shift | ||
| color = (color & 0b11) << shift | ||
|
|
||
| framebuf.buf[index] = color | (pixel & (~mask)) | ||
|
|
||
| @staticmethod | ||
| def get_pixel(framebuf, x, y): | ||
| """Get the color 8000 of a given pixel""" | ||
| index = (y * framebuf.stride + x) >> 2 | ||
| pixel = framebuf.buf[index] | ||
|
|
||
| shift = (x & 0b11) << 1 | ||
| return (pixel >> shift) & 0b11 | ||
|
|
||
| @staticmethod | ||
| def fill(framebuf, color): | ||
| """completely fill/clear the buffer with a color""" | ||
| if color: | ||
| bits = color & 0b11 | ||
| fill = (bits << 6) | (bits << 4) | (bits << 2) | (bits << 0) | ||
| else: | ||
| fill = 0x00 | ||
|
|
||
| framebuf.buf = [fill for i in range(len(framebuf.buf))] | ||
|
|
||
| @staticmethod | ||
| def rect(framebuf, x, y, width, height, color): | ||
| """Draw the outline of a rectangle at the given location, size and color.""" | ||
| # pylint: disable=too-many-arguments | ||
| for _x in range(x, x + width): | ||
| for _y in range(y, y + height): | ||
| if _x in [x, x + width] or _y in [y, y + height]: | ||
| GS2HMSBFormat.set_pixel(framebuf, _x, _y, color) | ||
|
|
||
| @staticmethod | ||
| def fill_rect(framebuf, x, y, width, height, color): | ||
| """Draw the outline and interior of a rectangle at the given location, size and color.""" | ||
| # pylint: disable=too-many-arguments | ||
| for _x in range(x, x + width): | ||
| for _y in range(y, y + height): | ||
| GS2HMSBFormat.set_pixel(framebuf, _x, _y, color) | ||
|
|
||
|
|
||
| class MHMSBFormat: | ||
|
|
@@ -256,6 +311,8 @@ def __init__(self, buf, width, height, buf_format=MVLSB, stride=None): | |
| self.format = RGB888Format() | ||
| elif buf_format == RGB565: | ||
| self.format = RGB565Format() | ||
| elif buf_format == GS2_HMSB: | ||
| self.format = GS2HMSBFormat() | ||
| else: | ||
| raise ValueError("invalid format") | ||
| self._rotation = 0 | ||
|
|
@@ -480,9 +537,7 @@ def image(self, img): | |
| imwidth, imheight = img.size | ||
| if imwidth != width or imheight != height: | ||
| raise ValueError( | ||
| "Image must be same dimensions as display ({0}x{1}).".format( | ||
| width, height | ||
| ) | ||
| f"Image must be same dimensions as display ({width}x{height})." | ||
| ) | ||
| # Grab all the pixels from the image, faster than getpixel. | ||
| pixels = img.load() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't actually understand why we right shift 2, then left shift 1 and then left shift by
shift. I wonder if there is a conceptually simpler way to write this.