From 3f57c599e57dec0b543b5dc3a13194a71a78653e Mon Sep 17 00:00:00 2001 From: David Todd Date: Sun, 26 Jan 2020 17:17:36 -0600 Subject: [PATCH] Add beginnings of library --- pixels.py | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 pixels.py diff --git a/pixels.py b/pixels.py new file mode 100644 index 0000000..ad4fb29 --- /dev/null +++ b/pixels.py @@ -0,0 +1,119 @@ +import time +import random +from board import TOTAL_DOTS, BOARD + +class Pixo: + """ + Class that provides ease of life methods for + displaying content on the board. + + During boot, `BOARD.auto_write` is set to False, + which prevents newly written data from immediately + updating the content. Call `BOARD.show()` whenever + you want content to be updated. + """ + + def _random_color(self): + """ + Generates a random value from 0-224 + """ + return random.randrange(0, 7) * 32 + + def clear(self): + """ + Fills the board with a 0 value and updates + the content, turning off all LEDs + """ + self.fill((0, 0, 0)) + BOARD.show() + + return self + + def fill(self, color): + """ + Fills the board with an RGB value, provided + via `color` as either a list or tuple + """ + BOARD.fill(color) + BOARD.show() + + return self + + def fill_image(self, image, show=True, slow=False): + """ + Fills the board with the provided `image`, + which is a list of tuples containing RGB values + + When `show` is False, the board will not be updated + automatically. This is useful if chaining commands + together (such as setting the image, and then forcing + a specific color for active pixels in the image) + + When `slow` is True, the content is updated pixel + by pixel, rather than all at once + """ + for dot in range(TOTAL_DOTS): + BOARD[dot] = image[dot] + if slow and show: + BOARD.show() + if not slow and show: + BOARD.show() + + return self + + def fill_random(self, slow=False): + """ + Fills the board with random RGB values + + When `Slow` is True, the content is updated pixel + by pixel, rather than all at once + """ + for dot in range(TOTAL_DOTS): + BOARD[dot] = ( + self._random_color(), + self._random_color(), + self._random_color() + ) + if slow: + BOARD.show() + if not slow: + BOARD.show() + + return self + + def blink_image(self, image, show=True, delay=0.3): + """ + Fills the board with the provided `image`, + which is a list of tuples containing RGB values + + When `show` is False, the board will not be updated + automatically. This is useful if chaining commands + together (such as setting the image, and then forcing + a specific color for active pixels in the image) + + `delay` is a float of how many seconds to wait + before the board gets blanked out. + + To blink multiple times, call this in a loop + """ + self.fill_image(image, show) + time.sleep(delay) + self.fill((0, 0, 0)) + + return self + + def force_color(self, color): + for dot in range(TOTAL_DOTS): + if BOARD[dot] != (0, 0, 0) and BOARD[dot] != color: + BOARD[dot] = color + BOARD.show() + + return self + + # This doesn't give the desired cycle that a gaming keyboard would + # def color_cycle(self): + # for r in range(100, 200): + # for g in range(100, 200): + # for b in range(100, 200): + # print((r, g, b)) + # self.fill((r, g, b)) \ No newline at end of file