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)
def test_set_rotation(GPIO, spidev): from unicornhatmini import UnicornHATMini unicornhatmini = UnicornHATMini() shapes = {0: (17, 7), 90: (7, 17), 180: (17, 7), 270: (7, 17)} for rotation in (0, 90, 180, 270): unicornhatmini.set_rotation(rotation) assert unicornhatmini.get_shape() == shapes[rotation] unicornhatmini.set_pixel(0, 0, 255, 255, 255)
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()
def test_set_pixel(GPIO, spidev): from unicornhatmini import UnicornHATMini unicornhatmini = UnicornHATMini() unicornhatmini.set_pixel(0, 0, 255, 255, 255)
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)
unicornhatmini = UnicornHATMini() unicornhatmini.set_brightness(0.1) unicornhatmini.set_rotation(0) width, height = unicornhatmini.get_shape() frames = 0 t_start = time.time() t_report = time.time() report_freq = 5.0 print("Please wait...") while True: for y in range(height): for x in range(width): hue = (time.time() / 10.0) + (x / float(width * 2)) r, g, b = [int(c * 255) for c in hsv_to_rgb(hue, 1.0, 1.0)] unicornhatmini.set_pixel(x, y, r, g, b) unicornhatmini.show() frames += 1 if time.time() - t_report > report_freq: t_report = time.time() t = time.time() - t_start fps = frames / t print("FPS: {:05.3f} ({} frames in {:.1f} seconds)".format( fps, frames, t))
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)
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()
#draw = ImageDraw.Draw(image) display_width, display_height = image.size for y in range(display_height): for x in range(display_width): hue = (time.time() / 10.0) + (x / float(display_width * 2)) r, g, b = [int(c * 255) for c in hsv_to_rgb(hue, 1.0, 1.0)] try: xc = image.getpixel((x + offset_x, y)) xr = int(xc[0]) xg = int(xc[1]) xb = int(xc[2]) #if xr < 255 and xb < 255 and xg < 255: if xr == 0 and xg == 0 and xb == 0: unicornhatmini.set_pixel(x, y, 0, 0, 0) elif xb > 0 and xr < 255: # hour hand unicornhatmini.set_pixel(x, y, 0, 255, 155) elif xr == 255 and xg == 255 and xb == 255: # outside the clock unicornhatmini.set_pixel(x, y, r, g, b) else: # second hand and all remaining non black or white color unicornhatmini.set_pixel(x, y, xr, xg, xb) except IndexError: #unicornhatmini.set_pixel(x, y, 0, 0, 0)