def draw(): uni = UnicornHATMini() uni.clear() uni.set_rotation(0) uni.set_brightness(0.1) display_width, display_height = uni.get_shape() interval = 60 / 17 interval = 5 while True: data = getData() for x in range(display_width): if x >= len(data): r = g = 0 else: r = 255 if data[x] == 0 else 0 g = 255 if data[x] == 1 else 0 for y in range(display_height): uni.set_pixel(x, y, r, g, 0) uni.show() time.sleep(interval)
class UnicornHatMiniNotifier(_UnicornHatNotifier): """ A notifier for the Unicorn HAT Mini. """ def __init__(self, brightness=0.5): """ @see _UnicornHatNotifier.__init__() """ super(UnicornHatMiniNotifier, self).__init__(brightness=brightness) from unicornhatmini import UnicornHATMini self._hat = UnicornHATMini() def _brightness(self, brightness): """ @see _UnicornHatNotifier._brightness """ self._hat.set_brightness(brightness) def _rotation(self, rotation): """ @see _UnicornHatNotifier._rotation """ self._hat.set_rotation(rotation) def _get_shape(self): """ @see _UnicornHatNotifier._get_shape """ return self._hat.get_shape() def _set_pixel(self, x, y, r, g, b): """ @see _UnicornHatNotifier._set_pixel """ self._hat.set_pixel(x, y, r, g, b) def _show(self): """ @see _UnicornHatNotifier._show """ self._hat.show() def _off(self): """ @see _UnicornHatNotifier._off """ self._hat.clear() self._hat.show()
def draw(width): uni = UnicornHATMini() uni.clear() uni.set_rotation(0) uni.set_brightness(0.1) display_width, display_height = uni.get_shape() r, g, b = getColour(width) for x in range(display_width): for y in range(display_height): if width > x: uni.set_pixel(x, y, r, g, b) else: uni.set_pixel(x, y, 0, 0, 0) uni.show() time.sleep(3) uni.clear() uni.show()
def draw(data): uni = UnicornHATMini() uni.clear() uni.set_rotation(0) uni.set_brightness(0.1) display_width, display_height = uni.get_shape() for x in range(display_width): r = g = 0 if len(data) > x: if data[x] == 1: g = 255 elif data[x] == 2: r = 255 elif data[x] == 3: r = 255 g = 150 for y in range(display_height): uni.set_pixel(x, y, r, g, 0) uni.show() time.sleep(2) uni.clear() uni.show()
#!/usr/bin/env python3 import time from colorsys import hsv_to_rgb from unicornhatmini import UnicornHATMini print("""Unicorn HAT Mini: colour-cycle.py Cycles through colour hues across all of Unicorn HAT Mini's pixels. Press Ctrl+C to exit! """) unicornhatmini = UnicornHATMini() unicornhatmini.set_brightness(0.1) while True: hue = (time.time() / 10.0) r, g, b = [int(c * 255) for c in hsv_to_rgb(hue, 1.0, 1.0)] unicornhatmini.set_all(r, g, b) unicornhatmini.show() time.sleep(1.0 / 60)
def test_show(GPIO, spidev): from unicornhatmini import UnicornHATMini unicornhatmini = UnicornHATMini() unicornhatmini.show()
class UnicornWrapper: def __init__(self, hat=None): if hat is None: try: spidev.SpiDev(0, 0) hat = 'mini' except FileNotFoundError: hat = 'phat' if hat == 'mini': self.hat = UnicornHATMini() self.type = hat self.hat.set_brightness(0.5) self.hat.set_rotation(90) elif hat == 'dummy': self.hat = None self.type = 'none' else: self.hat = unicornhat self.type = hat self.hat.set_layout(unicornhat.PHAT) self.hat.brightness(0.5) self.hat.rotation(90) self.brightness = 0.5 self.rotation = 0 self.width, self.height = self.hat.get_shape() def getType(self): return self.type def getHat(self): return self.hat def clear(self): return self.hat.clear() def getShape(self): return self.hat.get_shape() def setAll(self, r, g, b): self.hat.set_all(r, g, b) def getBrightness(self): if self.type == 'phat': return self.hat.get_brightness() return self.brightness def setBrightness(self, brightness): self.brightness = brightness if self.type == 'phat': f = io.StringIO() with contextlib.redirect_stdout(f): self.hat.brightness(brightness) elif self.type == 'mini': self.hat.set_brightness(brightness) def setPixel(self, x, y, r, g, b): self.hat.set_pixel(x, y, r, g, b) def setColour(self, r=None, g=None, b=None, RGB=None): if RGB is not None: r = RGB[0] g = RGB[1] b = RGB[2] self.hat.clear() for x in range(self.width): for y in range(self.height): self.setPixel(x, y, r, g, b) self.hat.show() def setRotation(self, r=0): if self.type == 'phat': self.hat.rotation(r) elif self.type == 'mini': self.hat.set_rotation(r) self.rotation = r def getRotation(self): return self.rotation def show(self): self.hat.show() def off(self): self.hat.clear() self.hat.show() # Colour converstion operations as we only understand RGB def hsvIntToRGB(self, h, s, v): h = h / 360 s = s / 100 v = v / 100 return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h, s, v)) def htmlToRGB(self, html): if len(html) is 6: r = int(f"{html[0]}{html[1]}", 16) g = int(f"{html[2]}{html[3]}", 16) b = int(f"{html[4]}{html[5]}", 16) elif len(html) > 6: r = int(f"{html[1]}{html[2]}", 16) g = int(f"{html[3]}{html[4]}", 16) b = int(f"{html[5]}{html[6]}", 16) else: raise Exception( "The Hex value is not in the correct format it should RRGGBB or #RRGGBB the same as HTML" ) return tuple(r, g, b)
class Writer(): def __init__(self): self.uni = UnicornHATMini() self.uni.clear() self.uni.set_rotation(0) self.uni.set_brightness(0.1) display_width, display_height = self.uni.get_shape() self.display_width = display_width self.display_height = display_height self.font = ImageFont.truetype( os.path.dirname(os.path.realpath(__file__)) + "/5x7.ttf", 8) def drawText(self, text): text_width, text_height = self.font.getsize(text) image = Image.new('P', (text_width + self.display_width + self.display_width, self.display_height), 0) draw = ImageDraw.Draw(image) draw.text((self.display_width, -1), text, font=self.font, fill=255) return image def getColour(self, s): if s == "Rainbow": return s mapValue = { "Red": "255,10,0", "Green": "0,255,10", "Blue": "0,10,255", "White": "255,255,255", "Black": "0,0,0" }[s] try: r, g, b = mapValue.split(",") except NameError: r, g, b = s.split(",") return [int(r), int(g), int(b)] def scroll(self, text, colour="Rainbow", show_total=1): image = self.drawText(text) show_count = 0 offset_x = 0 if colour != "Rainbow": r, g, b = self.getColour(colour) while show_count < show_total: for y in range(self.display_height): for x in range(self.display_width): hue = (time.time() / 10.0) + (x / float(self.display_width * 2)) if colour == "Rainbow": r, g, b = [ int(c * 255) for c in hsv_to_rgb(hue, 1.0, 1.0) ] if image.getpixel((x + offset_x, y)) == 255: self.uni.set_pixel(x, y, r, g, b) else: self.uni.set_pixel(x, y, 0, 0, 0) offset_x += 1 if offset_x + self.display_width > image.size[0]: offset_x = 0 show_count = show_count + 1 self.uni.show() time.sleep(0.05) def clear(self): self.uni.clear() self.uni.show() time.sleep(0.05)
class DisplayController: '''used to controll switching between the different dispaly modes''' def __init__(self): self.modes_data = get_mode_data() self.mode = None self.unicornhatmini = UnicornHATMini() self.config = get_config() mode_id = self.config.get('GENERAL', 'INITIAL_MODE_ID', fallback='clock_mode') if mode_id not in get_valid_mode_ids(): print( f'initial mode id of {mode_id} is not a valid, clock_mode is being used instead' ) mode_id = 'clock_mode' self.mode_id = mode_id self.mode_update_needed = False self.running = False self.mode_custom_options = None def set_mode(self, new_mode_id, custom_options=None): '''sets the current mode''' if new_mode_id not in get_valid_mode_ids(): print( f'cannot change to mode with id: {new_mode_id} as it is invalid or disabled' ) return self.mode_id = new_mode_id self.mode_custom_options = custom_options self.mode_update_needed = True def update_mode(self): '''updates the current mode by instancating new mode instance''' # only passes custom options if it's not None if self.mode_custom_options is None: self.mode = self.modes_data[self.mode_id]['class_constructor']( self.unicornhatmini, self.config) else: self.mode = self.modes_data[self.mode_id]['class_constructor']( self.unicornhatmini, self.config, self.mode_custom_options) self.mode_update_needed = False def run(self): '''used to start running the led display''' self.unicornhatmini.set_brightness( self.config.getfloat('GENERAL', 'BRIGHTNESS', fallback=0.1)) self.unicornhatmini.set_rotation( self.config.getint('GENERAL', 'ROTATION', fallback=0)) self.update_mode() self.running = True frame_interval = 1.0 / 30 while self.running: start_time = time.time() frame_interval = 1.0 / self.mode.display_frame() end_time = time.time() if end_time - start_time < frame_interval: time.sleep(frame_interval - (end_time - start_time)) if self.mode_update_needed is True: self.update_mode() if not self.running: print('display controller shutting down') self.unicornhatmini.clear() self.unicornhatmini.show() def stop(self): '''used to stop execution of the display controller''' self.running = False
import time from unicornhatmini import UnicornHATMini uh = UnicornHATMini() uh.set_brightness(0.5) uh.set_rotation(0) uh.set_pixel(0, 0, 255, 255, 0) #set one pixel uh.show() time.sleep(3) for x in range(17): #loop to set all pixels for y in range(7): uh.set_pixel(x, y, 0, 255, 255) uh.show() time.sleep(3) uh.clear() #clear all pixels uh.show()