Пример #1
0
def displayInky(output):
    from PIL import Image, ImageFont, ImageDraw
    from inky import InkyPHAT

    inky_display = InkyPHAT("red")
    inky_display.set_border(inky_display.RED)

    img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
    draw = ImageDraw.Draw(img)

    headerSize = 12
    headerFont = ImageFont.truetype(
        "/usr/share/fonts/truetype/liberation2/LiberationSans-Bold.ttf",
        headerSize)
    messageFont = ImageFont.truetype(
        "/usr/share/fonts/truetype/liberation2/LiberationMono-Regular.ttf",
        int(75 / (output['bodyLines'])))

    x = 0
    y = 0
    draw.text((x, y), output['header'] + ' - ' + output['subHeader'],
              inky_display.RED, headerFont)
    draw.text((x, y + headerSize), output['body'], inky_display.BLACK,
              messageFont)
    inky_display.set_image(img)
    inky_display.show()
Пример #2
0
def updateeink(issdetails, mapdot, trail):
    """update location text and map to the e-ink screen"""
    issx = int(mapdot[0])
    issy = int(mapdot[1])
    inky_display = InkyPHAT("yellow")

    palette = [255, 255, 255, 0, 0, 0, 255, 0, 0]

    font10 = ImageFont.truetype('/home/pi/isstracker/FreeSans.ttf', 10)
    font14 = ImageFont.truetype('/home/pi/isstracker/FreeSans.ttf', 14)
    font16 = ImageFont.truetype('/home/pi/isstracker/FreeSans.ttf', 16)
    font18 = ImageFont.truetype('/home/pi/isstracker/FreeSans.ttf', 18)

    im = Image.open('/home/pi/isstracker/small-world-map.png')
    d = ImageDraw.ImageDraw(im)

    taille_pt = 3
    isspos = (int(mapdot[0]) - taille_pt, int(mapdot[1]) - taille_pt,
              int(mapdot[0]) + taille_pt, int(mapdot[1]) + taille_pt)
    d.ellipse(isspos, fill=2)
    for item in point2ellipse(trail, 1):
        d.ellipse(item, fill=2)

    d.text((3, 80),
           'à ' + str(round(issdetails[2])) + ' Km de ' + issdetails[1] +
           ' (' + issdetails[0] + ')',
           font=font18,
           fill=2)

    from uptime import uptime
    uptime = round(uptime() / 60, 1)
    d.text((0, 70), "ut : " + str(uptime) + " min", font=font10, fill=1)

    inky_display.set_image(im)
    inky_display.show()
Пример #3
0
class InkyDriver:
    def __init__(self):
        self.inky = InkyPHAT('yellow')

    def create_new_image(self, countries_cases):
        current_height = 0
        # inky_display.set_rotation(180)
        self.inky.set_border(self.inky.RED)

        # Create a new canvas to draw on
        img = Image.new("P", (self.inky.WIDTH, self.inky.HEIGHT))
        draw = ImageDraw.Draw(img)

        # Load the fonts
        font = ImageFont.truetype(HankenGroteskBold, 20)

        # Calculate the positioning and draw the text
        for countryCases in countries_cases:
            text = str(countryCases[0]) + ": " + str(countryCases[1])
            width, height = font.getsize(text)
            center = int((self.inky.WIDTH - width) / 2)
            draw.text((center, current_height),
                      text,
                      self.inky.BLACK,
                      font=font)
            current_height += height

        # Display the completed picture
        self.inky.set_image(img)
        self.inky.show()
Пример #4
0
def hype(word):
	print(word)

	# Set up the correct display and scaling factors
	inky_display = InkyPHAT('black')
	inky_display.set_border(inky_display.BLACK)
	# inky_display.set_rotation(180)

	w = inky_display.WIDTH
	h = inky_display.HEIGHT

	# Create a new canvas to draw on
	img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
	draw = ImageDraw.Draw(img)

	# Load the fonts
	font_size = 88
	from fonts.ttf import AmaticSC, FredokaOne

	#font = ImageFont.truetype(SourceSansProSemibold, font_size)
	font = ImageFont.truetype(AmaticSC, font_size)
	#font = ImageFont.truetype(FredokaOne, font_size)

	padding = 20 
	max_width = w - padding
	max_height = h - padding

	below_max_length = False
	while not below_max_length:
			p_w, p_h = font.getsize(word)  # Width and height of quote
			#p_h = p_h * (word.count("\n") + 1)   # Multiply through by number of lines

			if p_h < max_height and p_w < max_width:
					below_max_length = True              # The quote fits! Break out of the loop.

			else:
					font_size = font_size - 2
					#font = ImageFont.truetype(SourceSansProSemibold, font_size)
					font = ImageFont.truetype(AmaticSC, font_size)
					#font = ImageFont.truetype(FredokaOne, font_size)

					continue

	# x- and y-coordinates for the top left of the quote
	#word_x = (w - max_width) / 2
	#word_x = (max_width - p_w) / 2
	#word_y = (max_height - p_h) / 2
	word_x = (w - max_width) / 2
	#word_y = ((h - max_height) + (max_height - p_h - font.getsize("ABCD ")[1])) / 2
	word_y = (h - p_h) / 2

	draw.multiline_text((word_x, word_y), word, fill=inky_display.BLACK, font=font, align="left")
	draw.line((169, 58, 169, 58), 2)

	# Display the completed canvas on Inky wHAT
	inky_display.set_image(img)
	inky_display.show()
Пример #5
0
    def draw_to_display():
        # Set up properties of eInk display
        inky_display = InkyPHAT("red")
        inky_display.set_border(inky_display.BLACK)

        # Load previously generated image
        img = draw_semester_display()

        # Display generated semester progress image
        inky_display.set_image(img)
        inky_display.show()
Пример #6
0
def run():

    inky_display = InkyPHAT(COLOR)

    # inky_display.set_rotation(180)
    inky_display.set_border(InkyPHAT.RED)

    img = Image.new("P", (InkyPHAT.WIDTH, InkyPHAT.HEIGHT))
    palletize(img)
    image_draw = ImageDraw.Draw(img)

    if DEBUG:
        # Draw vertical line
        for y in xrange(InkyPHAT.HEIGHT):
            img.putpixel((InkyPHAT.WIDTH / 2, y), inky_display.RED)
            img.putpixel((InkyPHAT.WIDTH / 4, y), inky_display.RED)

        # Draw horizontal line
        for x in xrange(InkyPHAT.WIDTH):
            img.putpixel((x, InkyPHAT.HEIGHT / 2), inky_display.RED)

    weather = Weather(location_coords)

    if weather.is_same_as_temp_data():
        if DEBUG:
            print("Not updating the display since the forecast is the same as last time")
        weather.save_temp_forecast(only_if_no_such_file=True)
        return
    else:
        if DEBUG:
            print("Updating display since the forecast has changed since the last render")
        weather.save_temp_forecast()

    draw_text(image_draw, 2, "UV",                        "l")
    draw_text(image_draw, 2, weather.uv_index,            "r",
              color=InkyPHAT.RED if weather.is_uv_warning() else InkyPHAT.BLACK)
    draw_text(image_draw, 1, get_high_temp_copy(weather), "c")
    draw_text(image_draw, 4, get_low_temp_copy(weather),  "c")

    if weather.precipitation_is_likely():
        precip_chance_str = str(int(round(weather.current_precip_probability * 100))) + "%"
        draw_text(image_draw, 3, get_sky_icon(weather), "l", is_icon=True)
        draw_text(image_draw, 3, precip_chance_str,     "r", is_icon=False)
    else:
        draw_text(image_draw, 3, get_sky_icon(weather), "c", is_icon=True)

    inky_display.set_image(img)
    inky_display.show()

    if DEBUG:
        print(weather.eink_data_string())
Пример #7
0
def draw_display_message(text: str):
    # Set up properties of eInk display
    inky_display = InkyPHAT("red")
    inky_display.set_border(inky_display.BLACK)

    hanked_medium = ImageFont.truetype(HankenGroteskMedium, 20)

    img = Image.new("P", size=(InkyPHAT.WIDTH, InkyPHAT.HEIGHT))
    draw = ImageDraw.Draw(img)

    text_w, text_h = hanked_medium.getsize(text)
    text_x = (InkyPHAT.WIDTH - text_w) // 2
    text_y = (InkyPHAT.HEIGHT - text_h) // 2
    draw.text((text_x, text_y), text, InkyPHAT.BLACK, font=hanked_medium)

    inky_display.set_image(img)
    inky_display.show()
Пример #8
0
def refreshDisplay(intervall, priority, scheduler):

    logging.info("Refresh Display - start")

    lastValue = readLastValue()
    avgMonth = readAvgFromDays(31)
    avgYear = readAvgFromDays(365)

    inkyDisplay = InkyPHAT(colour)
    inkyDisplay.set_border(inkyDisplay.BLACK)

    # Load our background image
    img = Image.open("/var/local/cellarsense/cellarsense-background.png")
    draw = ImageDraw.Draw(img)

    mainHeaderFont = ImageFont.truetype("FreeMonoBold.ttf", 20)
    mainFont = ImageFont.truetype("FreeMono.ttf", 20)
    medianFont = ImageFont.truetype("FreeMono.ttf", 15)

    lastValuePrint = "{:4.1f}°C {:4.1f}%".format(lastValue["temperature"],
                                                 lastValue["humidity"])
    avgMonthPrint = "øm {:4.1f}°C {:4.1f}%".format(avgMonth["temperature"],
                                                   avgMonth["humidity"])
    avgYearPrint = "øa {:4.1f}°C {:4.1f}%".format(avgYear["temperature"],
                                                  avgYear["humidity"])

    logging.debug("Refresh Display - %s", lastValuePrint)
    logging.debug("Refresh Display - %s", avgMonthPrint)
    logging.debug("Refresh Display - %s", avgYearPrint)

    draw.text((5, 5),
              "CellarSense",
              fill=inkyDisplay.BLACK,
              font=mainHeaderFont)
    draw.text((20, 35), lastValuePrint, fill=inkyDisplay.BLACK, font=mainFont)
    draw.text((10, 70), avgMonthPrint, fill=inkyDisplay.BLACK, font=medianFont)
    draw.text((10, 85), avgYearPrint, fill=inkyDisplay.BLACK, font=medianFont)

    inkyDisplay.set_image(img)
    inkyDisplay.show()

    logging.info("Refresh Display - done")

    # reschedule
    scheduler.enter(intervall, priority, refreshDisplay,
                    (intervall, priority, scheduler))
Пример #9
0
class RenderQueue():
    def __init__(self):
        self.img_stack = []
        self.inky_display = InkyPHAT("black")
        pub.subscribe(self.add_image, 'add_image')

    def add_image(self, img):
        self.img_stack.append(img)

    def run(self):
        while True:
            if len(self.img_stack) > 0:
                most_recent_frame = self.img_stack.pop()
                self.img_stack = []
                self.inky_display.set_image(most_recent_frame)
                self.inky_display.show()
            time.sleep(0.1)
Пример #10
0
def updateScreen(shelf):
    w, h = (212, 104)
    dpi = 144

    fig, ax = plt.subplots(figsize=(212 / dpi, 104 / dpi), dpi=dpi)
    fig.subplots_adjust(top=1, bottom=0, left=0.15, right=1)

    ticks_font = font_manager.FontProperties(fname='04B_03__.TTF', size=4)
    plt.rcParams['text.antialiased'] = False

    ax.yaxis.set_ticks(np.arange(0, 3, 1))
    for label in ax.get_yticklabels():
        label.set_fontproperties(ticks_font)
    ax.yaxis.set_tick_params(pad=1, width=1)

    ax.xaxis.set_ticks([])
    ax.set_frame_on(False)

    plt.autoscale(enable=False)
    ax.plot(timestamps, values)
    ax.set_ylim(-.2, 2.2)
    ax.set_xlim(0, 15)
    ax.autoscale_view()

    with io.BytesIO() as f:
        inky_display = InkyPHAT("yellow")

        fig.savefig(f,
                    dpi=dpi,
                    cmap="bwr",
                    interpolation="none",
                    origin="lower",
                    pad_inches=0)
        f.seek(0)
        i = Image.open(f)

        # ensure the image is using the correct pallet
        pal_img = Image.new('P', (1, 1))
        pal_img.putpalette((255, 255, 255, 0, 0, 0, 255, 0, 0) +
                           (0, 0, 0) * 252)
        i = i.convert('RGB', palette=pal_img).quantize(palette=pal_img,
                                                       dither=Image.NONE)

        inky_display.set_image(i)
        inky_display.show()
Пример #11
0
def my_draw(message):
  inky_display = InkyPHAT("black")
  inky_display.set_border(inky_display.WHITE)

  img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
  draw = ImageDraw.Draw(img)

  font = ImageFont.truetype(FredokaOne, 30)

  w, h = font.getsize(message)
  #x = (inky_display.WIDTH / 2) - (w / 2)
  #y = (inky_display.HEIGHT / 2) - (h / 2)
  x = 1
  y = 1
      
  draw.text((x, y), message, inky_display.BLACK, font)
  inky_display.set_image(img)
  inky_display.show()
Пример #12
0
def print_text(text):
    """Displays text in Inky pHAT display.

    Args:
        text: String that is displayed on the Inky pHAT
    """
    inky_display = InkyPHAT("black")
    inky_display.set_border(inky_display.WHITE)
    img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(FredokaOne, 30)
    message = text
    width, height = font.getsize(message)
    x_axis = (inky_display.WIDTH / 2) - (width / 2)
    y_axis = (inky_display.HEIGHT / 2) - (height / 2)
    draw.text((x_axis, y_axis), message, inky_display.BLACK, font)
    inky_display.set_image(img)
    inky_display.show()
Пример #13
0
class MagicBoxScreen():
    def __init__(self):
        self._inky = InkyPHAT("black")
        self._font = ImageFont.truetype(FredokaOne, 16)

    def displayMessage(self, message: str="No Message"):
        w, h = self._font.getsize(message)
        x = (self._inky.WIDTH / 2) - (w / 2)
        y = (self._inky.HEIGHT / 2) - (h / 2)
        img = Image.new("P", (self._inky.WIDTH, self._inky.HEIGHT))
        draw = ImageDraw.Draw(img)
        draw.text((x, y), message, self._inky.BLACK, self._font)
        self._inky.set_image(img)
        self._inky.show()

    def displayQuestion(self, \
        questionText: str="What is the speed of light?", \
        answerText: List[str] = \
        ['0 m/s', '100 m/s', '1 km/s', '3,000 km/s', '300,000 km/s']):
Пример #14
0
def presentOnInkyScreen(image):
    logging.info("Loading View onto Inky Phat.")
    from inky import InkyPHAT

    #Test invert image.
    image = image.convert('L')
    image = ImageOps.invert(image)
    image = image.convert('1')

    # Set up the display
    inky_display = InkyPHAT("black")
    #inky_display.set_border(inky_display.BLACK)
    inky_display.v_flip = True
    inky_display.h_flip = True

    # Display
    inky_display.set_border("black")
    inky_display.set_image(image)
    inky_display.show()
    logging.info("View Sent to Device Successfully")
Пример #15
0
class Clock():
    def __init__(self, rotation=180):
        self.inky_display = InkyPHAT("red")
        self.inky_display.set_border(self.inky_display.BLACK)
        self.WIDTH = self.inky_display.WIDTH
        self.HEIGHT = self.inky_display.HEIGHT
        self.WHITE = self.inky_display.WHITE
        self.RED = self.inky_display.RED
        self.BLACK = self.inky_display.BLACK
        self.rotation = 180

    def create_image(self, currenttime, currentdate, temp, tempmax, tempmin):
        img = Image.open("./images/backdrop.png")
        draw = ImageDraw.Draw(img)

        #Adding the time and date
        font = ImageFont.truetype(FredokaOne, 28)
        draw.text((6, 15), currentdate, self.RED, font=font)

        font = ImageFont.truetype(FredokaOne, 40)
        draw.text((int(self.WIDTH / 2), 5), currenttime, self.BLACK, font=font)

        #adding max temp
        font = ImageFont.truetype(FredokaOne, 20)
        draw.text((22, 70), temp, self.RED, font=font)
        draw.text((92, 70), tempmin, self.RED, font=font)
        draw.text((160, 70), tempmax, self.RED, font=font)

        self.display_img(img)

    def display_img(self, img):
        img = img.rotate(self.rotation)
        self.inky_display.set_image(img)
        self.inky_display.show()

    def test_image(self):
        img = Image.open("./images/TagTest-212x104.png")
        self.display_img(img)
Пример #16
0
class ScreenController():
    def __init__(self):
        self.inky_display = InkyPHAT('red')
        self.screen_width = self.inky_display.WIDTH
        self.screen_height = self.inky_display.HEIGHT

    def display_message(self, message, theme='light'):
        screen = MessageScreen(message['theme'] or theme, self.inky_display,
                               message)
        self.inky_display.set_image(screen.image)
        self.inky_display.show()

    def display_alert(self, message, theme='light'):
        screen = AlertScreen(theme, self.inky_display, message)
        self.inky_display.set_image(screen.image)
        self.inky_display.show()

    def display_prompt(self, message, instruction, theme='light'):
        screen = PromptScreen(theme, self.inky_display, message, instruction)
        self.inky_display.set_image(screen.image)
        self.inky_display.show()
Пример #17
0
mediumFont = ImageFont.truetype(FredokaOne, 16)
largeFont = ImageFont.truetype(FredokaOne, 32)

am2320 = AM2320(args.i2c)
(sensor_dt, sensor_t, sensor_h) = sample_hygrometer(am2320)

dt = format_datetime(sensor_dt)
t = format_temperature(sensor_t)
h = format_humidity(sensor_h)

draw.text((5, 5), dt, phat.BLACK, mediumFont)

_tw, th = largeFont.getsize(t)
hw, hh = largeFont.getsize(h)
sh = max(th, hh)
sample_offset_x = phat.WIDTH - 5 - hw
sample_offset_y = phat.HEIGHT - 5 - sh

t_label = "TEMPERATURE"
lw, lh = smallFont.getsize(t_label)
label_offset_y = sample_offset_y - lh
draw.text((5, label_offset_y), t_label, phat.RED, smallFont)
draw.text((5, sample_offset_y), t, phat.RED, largeFont)

h_label = "HUMIDITY"
draw.text((sample_offset_x, label_offset_y), h_label, phat.RED, smallFont)
draw.text((sample_offset_x, sample_offset_y), h, phat.RED, largeFont)

phat.set_image(img)
phat.show()
Пример #18
0
img = Image.open("./logo.png")
draw = ImageDraw.Draw(img)

# get api data

try:
    f = urllib2.urlopen('http://pi.hole/admin/api.php')
    json_string = f.read()
    parsed_json = json.loads(json_string)
    adsblocked = parsed_json['ads_blocked_today']
    ratioblocked = parsed_json['ads_percentage_today']
    f.close()
except:
    queries = '?'
    adsblocked = '?'
    ratio = '?'

font = ImageFont.truetype(FredokaOne, 32)

inky_display = InkyPHAT("red")
inky_display.set_border(inky_display.WHITE)

draw.text((20, 20), str(adsblocked), inky_display.BLACK, font)
draw.text((20, 50),
          str("%.1f" % round(ratioblocked, 2)) + "%", inky_display.BLACK, font)

flipped = img.rotate(180)
inky_display.set_image(flipped)

inky_display.show()
def main():

    # yyyy-mm-dd hh:mm:ss
    currenttime = strftime("%Y-%m-%d %H:%M:%S", gmtime())

    # Write text with weather values to the canvas
    inkydatetime = strftime("%d/%m %H:%M")

    # IoT Host Name
    host = os.uname()[1]

    # - start timing
    starttime = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')
    start = time.time()

    # Ip address
    ipaddress = minifiutil.IP_address()

    parser = argparse.ArgumentParser()
    parser.add_argument('--price',
                        help='price to be recognized.',
                        required=True)
    args = parser.parse_args()

    # end of processing
    end = time.time()

    # Output JSON
    row = {}
    uuid2 = '{0}_{1}'.format(strftime("%Y%m%d%H%M%S", gmtime()), uuid.uuid4())
    cpuTemp = int(float(minifiutil.getCPUtemperature()))
    usage = psutil.disk_usage("/")

    # Format Fields
    row['host'] = os.uname()[1]
    row['cputemp'] = str(round(cpuTemp, 2))
    row['ipaddress'] = str(ipaddress)
    row['endtime'] = '{0:.2f}'.format(end)
    row['runtime'] = '{0:.2f}'.format(end - start)
    row['systemtime'] = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')
    row['starttime'] = str(starttime)
    row['diskfree'] = "{:.1f}".format(float(usage.free) / 1024 / 1024)
    row['memory'] = str(psutil.virtual_memory().percent)
    row['uuid'] = str(uuid2)
    row['price'] = str(args.price)

    # Output JSON
    json_string = json.dumps(row)
    print(json_string)

    # Set up the display
    inky_display = InkyPHAT("red")
    inky_display.set_border(inky_display.BLACK)

    # Create a new canvas to draw on
    # 212x104
    img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
    draw = ImageDraw.Draw(img)

    # Load the FredokaOne font
    font = ImageFont.truetype(FredokaOne, 22)

    # draw data
    draw.text((0, 0),
              "{}".format('Cloudera Mug'),
              inky_display.BLACK,
              font=font)
    draw.text((0, 32),
              "Price: $ {}".format(args.price),
              inky_display.RED,
              font=font)

    # Display the data on Inky pHAT
    inky_display.set_image(img)
    inky_display.show()
Пример #20
0
#! /usr/bin/env python3

from PIL import Image, ImageFont, ImageDraw
from font_fredoka_one import FredokaOne
from inky import InkyPHAT
from time import sleep

if __name__ == "__main__":

    inkyphat = InkyPHAT("black")
    font = ImageFont.truetype(FredokaOne, 22)
    count = 0
    try:
        while True:
            count += 1
            message = f"Hi! Count = {count}"
            print(message)
            w, h = font.getsize(message)
            x = (inkyphat.WIDTH / 2) - (w / 2)
            y = (inkyphat.HEIGHT / 2) - (h / 2)
            img = Image.new("P", (inkyphat.WIDTH, inkyphat.HEIGHT))
            draw = ImageDraw.Draw(img)
            draw.text((x, y), message, inkyphat.BLACK, font)
            inkyphat.set_image(img)
            inkyphat.show()

    except KeyboardInterrupt:
        pass
Пример #21
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--driver', default=None, choices=['inky', 'inkyphat', 'waveshare_epd'])
    parser.add_argument("--pair", default='XETHZUSD', help="currency pair")
    parser.add_argument('--flip', dest='flip', action='store_true', help="rotate the display")
    parser.set_defaults(flip=False)
    parser.add_argument("--output", help="save plot as png")
    args = parser.parse_args()

    yesterday = datetime.now() - timedelta(hours=12)

    quotes = quotes_historical_kraken_ohlc(args.pair, yesterday, interval=15)
    if not quotes:
        raise ValueError('Empty OHLC data')

    candlestick_ohlc(ax, quotes, width=1)

    ax.xaxis_date()
    ax.autoscale_view()

    ymin, ymax = ax.get_ylim()

    last_low = quotes[-1][3]
    last_high = quotes[-1][2]
    last_close = quotes[-1][4]

    ttf = pkg_resources.open_binary(fonts, '04B_03__.TTF')
    font = ImageFont.truetype(ttf, 8)

    RED = (255,0,0)
    BLACK = (0,0,0)

    display = None
    driver = args.driver

    if driver == 'inky':
        from inky import InkyPHAT
        RED = InkyPHAT.RED
        BLACK = InkyPHAT.BLACK

        display = InkyPHAT('black')
    elif driver == 'inkyphat':
        from inkyphat import RED as inky_RED, BLACK as inky_BLACK, set_image as _set_image, show as _show
        RED = inky_RED
        BLACK = inky_BLACK

        class InkyPHAT():
            def __init__(*args):
                pass
            set_image = staticmethod(_set_image)
            show = staticmethod(_show)

        display = InkyPHAT()
    elif driver == 'waveshare_epd':
        from waveshare_epd import epd2in13_V2

        class EPD():
            def __init__(self, *args):
                self.epd = epd2in13_V2.EPD()
                self.epd.init(self.epd.FULL_UPDATE)
                self.buf = []
            def set_image(self, image):
                self.buf = self.epd.getbuffer(image)
            def show(self):
                self.epd.display(self.buf)

        display = EPD()

    with io.BytesIO() as f:
        fig.savefig(f, dpi=dpi, cmap="bwr", interpolation="none", origin="lower", pad_inches=0)
        f.seek(0)
        i = Image.open(f)
        d = ImageDraw.Draw(i)
        ypos = 0 if ymax - last_high > last_low - ymin else h - 6

        d.text((148, ypos), '{:.2f}'.format(last_close), BLACK, font)
        d.text((176, ypos), args.pair, RED, font)

        if args.flip:
            i = i.transpose(Image.ROTATE_180)

        if display:
            display.set_image(i.convert("P"))
            display.show()

        if args.output:
            i.save(args.output)
Пример #22
0
class Display(object):
    """
    classdocs
    """

    COLOUR = "black"

    CLEAR_TIME = 1.0  # seconds
    DRAW_TIME = 6.0  # seconds
    DEFAULT_CLEAN_CYCLES = 1

    __LOCK_TIMEOUT = 20.0  # seconds

    __SPI_BUS = 0
    __SPI_DEVICE = 0

    __SPI_CLOCK = 300000  # Hz    was 488000
    __SPI_MODE = 1

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, font):
        """
        Constructor
        """
        self.__font = font

        self.__device = InkyPHAT(self.COLOUR)

        self.__image = Image.new("P",
                                 (self.__device.WIDTH, self.__device.HEIGHT))
        self.__drawing = ImageDraw.Draw(self.__image)

        m_width, m_height = self.__font.getsize("M")

        self.__text_width = self.__device.WIDTH // m_width
        self.__text_height = self.__device.HEIGHT // m_height + 1

        self.__stop = None
        self.__render_timeout = Timeout(self.DRAW_TIME)

        # self.__spi = SPI(self.__SPI_BUS, self.__SPI_DEVICE, None, None)
        self.__spi = SPI(self.__SPI_BUS, self.__SPI_DEVICE, self.__SPI_MODE,
                         self.__SPI_CLOCK)

    # ----------------------------------------------------------------------------------------------------------------

    def clean(self, cycles=None):
        try:
            self.obtain_lock()

            for _ in range(
                    self.DEFAULT_CLEAN_CYCLES if cycles is None else cycles):
                # clear...
                self.__image = Image.new(
                    "P", (self.__device.WIDTH, self.__device.HEIGHT))
                self.__drawing = ImageDraw.Draw(self.__image)

                self.__show()

        finally:
            self.release_lock()

    def clear(self):
        try:
            self.obtain_lock()

            self.__image = Image.new(
                "P", (self.__device.WIDTH, self.__device.HEIGHT))
            self.__drawing = ImageDraw.Draw(self.__image)

            time.sleep(self.CLEAR_TIME)

        finally:
            self.release_lock()

    def draw_text(self, buffer):
        try:
            self.obtain_lock()

            for row in range(len(buffer)):
                y_offset = row * self.__text_height
                self.__drawing.text((0, y_offset), buffer[row],
                                    self.__device.BLACK, self.__font)

        finally:
            self.release_lock()

    def render(self):
        try:
            self.obtain_lock()

            print("Display: starting render", file=sys.stderr)
            sys.stderr.flush()

            self.__show()

        except TimeoutError:
            print("Display: render timeout", file=sys.stderr)
            sys.stderr.flush()

        finally:
            self.release_lock()

            print("Display: ending render", file=sys.stderr)
            sys.stderr.flush()

    # ----------------------------------------------------------------------------------------------------------------

    def __show(self):
        # update...
        self.__device.set_image(self.__image)

        # show...
        with self.__render_timeout:
            try:
                self.__spi.acquire_lock()

                self.__device.show(
                )  # Do not let the display enter deep sleep - OPC affected!?

            finally:
                self.__spi.release_lock()

        # wait...
        time.sleep(self.DRAW_TIME)

    # ----------------------------------------------------------------------------------------------------------------

    def obtain_lock(self):
        Lock.acquire(self.__lock_name, self.__LOCK_TIMEOUT)

    def release_lock(self):
        Lock.release(self.__lock_name)

    @property
    def __lock_name(self):
        return self.__class__.__name__

    # ----------------------------------------------------------------------------------------------------------------

    @property
    def text_width(self):
        return self.__text_width

    @property
    def text_height(self):
        return self.__text_height

    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "Display:{text_width:%s, text_height:%s, colour:%s, render_timeout:%s, spi:%s}" % \
               (self.text_width, self.text_height, self.COLOUR, self.__render_timeout, self.__spi)
Пример #23
0
    
    if len(word_list) <= max_lines:
        message_does_not_fit = False

    if font_size < 9:
        message_does_not_fit = False

logging.info("Font size: %s", font_size)
offset_x, offset_y = font.getoffset(message)

# Rejoin the wrapped lines with newline chars
separator = '\n'
output_text = separator.join(word_list)

w, h = draw.multiline_textsize(output_text, font=font, spacing=0)

x = (WIDTH - w)/2
y = (HEIGHT - h - offset_y)/2
draw.multiline_text((x, y), output_text, BLACK, font, align="center", spacing=0)

# Rotate and display the image
if "ROTATE" in os.environ:
    img = img.rotate(180)

if "WAVESHARE" in os.environ:
    # epd does not have a set_image method.
    display.display(display.getbuffer(img))
else:
    display.set_image(img)
    display.show()
Пример #24
0

# update the display during normal waking hours (roughly)
now = datetime.now(tz=timezone.utc).astimezone(pytz.timezone("Europe/London"))
if now.hour >= 7 and (now.hour < 22 or (now.hour == 22 and now.minute == 0)):
    data = last_data()
    sensor_dt = data["datetime"]
    sensor_t = data["temperature"]
    sensor_h = data["humidity"]

    phat = InkyPHAT("red")
    phat.set_border(phat.WHITE)
    #phat.h_flip = True
    #phat.v_flip = True

    img_dt = render_datetime(sensor_dt)
    img_t = render_sensor_data("temperature", sensor_t)
    img_h = render_sensor_data("humidity", sensor_h)

    canvas = Image.new("P", (phat.WIDTH, phat.HEIGHT))
    canvas.paste(img_dt, box=(3, phat.HEIGHT - img_dt.height - 3))
    canvas.paste(img_t,
                 box=(phat.WIDTH - img_t.width - 12 - img_h.width,
                      phat.HEIGHT - img_t.height - 3))
    canvas.paste(img_h,
                 box=(phat.WIDTH - img_h.width - 3,
                      phat.HEIGHT - img_h.height - 3))

    phat.set_image(canvas)
    phat.show()
Пример #25
0
class InkyDevice():

    def __init__(self):
        self.display = InkyPHAT("black")
        self.display.set_border(self.display.WHITE)
        self.font = ImageFont.truetype('resources/Eden_Mills_Bold.ttf', 12)
        self.lock = threading.Lock()

        self.startup()

    def startup(self):
        print('writing startup image...')
        with self.lock:
            img = Image.open("resources/k8s-bw.png")
            draw = ImageDraw.Draw(img)
            self.display.set_image(img)
            self.display.show()

    def shutdown(self):
        print('writing shutdown image...')
        with self.lock:
            img = Image.open("resources/8-bit-dino.png")
            draw = ImageDraw.Draw(img)

            font = ImageFont.truetype('resources/Eden_Mills_Bold.ttf', 28)
            
            message = "offline    "
            message_width, message_height = self.get_text_size(message, font)
            x, y = Location.CenterRight.place(message, font, message_width, message_height, self.display.WIDTH, self.display.HEIGHT)
            draw.text((x, y), message, self.display.BLACK, font)

            self.display.set_image(img)
            self.display.show()

    def get_text_size(self, message, font):
        # render the text in another text buffer to get the dimensions
        message_width, message_height = 0,0
        for line in message.split("\n"):
            partial_width, partial_height = font.getsize(line)
            message_width = max(message_width, partial_width)
            approx_line_spacing = 1.2
            message_height += int(partial_height*approx_line_spacing)

        return message_width, message_height

    def write(self, render_result):
        with self.lock:
            img = Image.new("P", (self.display.WIDTH, self.display.HEIGHT))
            draw = ImageDraw.Draw(img)

            for location, message in render_result.items():

                # render the text in another text buffer to get the dimensions
                message_width, message_height = self.get_text_size(message, self.font)

                # find the placement of the text buffer and overlay onto the screen buffer
                x, y = location.place(message, self.font, message_width, message_height, self.display.WIDTH, self.display.HEIGHT)
                draw.text((x, y), message, self.display.BLACK, self.font)

            # flush the screen buffer to the device
            self.display.set_image(img)
            self.display.show()
Пример #26
0
if len(sys.argv) > 1:
    INKY_COLOUR = sys.argv[1]

if INKY_COLOUR not in ['red', 'yellow', 'black']:
    print("Usage: {} <red, yellow, black>".format(sys.argv[0]))
    sys.exit(1)

phat = InkyPHAT(INKY_COLOUR)

white = Image.new('P', (212, 104), phat.WHITE)
black = Image.new('P', (212, 104), phat.BLACK)

while True:
    print("White")
    phat.set_border(phat.WHITE)
    phat.set_image(black)
    phat.show()
    time.sleep(1)

    if INKY_COLOUR == 'red':
        print("Red")
        phat.set_border(phat.RED)
        phat.set_image(white)
        phat.show()
        time.sleep(1)

    if INKY_COLOUR == 'yellow':
        print("Yellow")
        phat.set_border(phat.YELLOW)
        phat.set_image(white)
        phat.show()
Пример #27
0
class Displayer:
    lasthash = None

    def __init__(self, config):
        cfg = configparser.ConfigParser()
        cfg.read(config)
        self.broker = cfg["mqtt"]["broker"]
        self.ph = InkyPHAT(cfg["display"]["color"])

    def client_id(self) -> str:
        return open("/etc/machine-id").read().strip()

    def client_topic(self) -> str:
        cid = self.client_id()
        return f"phat/client/{cid}"

    def update_state(self, status: str):
        client.publish(self.client_topic(), payload=status, qos=1, retain=True)

    def on_connect(self, client, userdata, flags, rc):
        logging.info("connected")
        self.update_state("ALIVE")
        client.subscribe("phat/image", 1)
        client.subscribe(f"phat/image/{self.client_id()}", 1)
        logging.info("setup done")

    def epaper_display_image(self, img):
        self.ph.set_image(img)
        self.ph.show()

    def epaper_display_error(self, msg):
        im = Image.new("P", (212, 104))
        err_image = ImageDraw.Draw(im)
        font = ImageFont.truetype(FredokaOne, 22)
        err_image.text((20, 20), msg, self.ph.RED, font)
        self.epaper_display_image(im)

    def on_message(self, client, userdata, message):
        logging.debug(f"{message.topic}--{str(message.payload.strip())}")
        try:
            data = json.loads(message.payload.strip())
        except:
            logging.error(sys.exc_info())
            self.epaper_display_error(f"BADJSON")
            return
        if self.lasthash == data["hash"]:
            logging.info("skipping same image update")
            return
        self.lasthash = data["hash"]
        rqh = {
            "Accept": "image/*",
            "If-None-Match": self.lasthash,
        }
        resp = requests.get(data["url"], headers=rqh, stream=True)
        if resp.status_code == 200:
            rspct = resp.headers["Content-Type"]
            if not rspct.startswith("image/"):
                self.epaper_display_error(f"resp wasn't image, ignoring: {rspct}")
            # logging.info(resp.headers)
            img = Image.open(BytesIO(resp.content))
            if img.size != (212, 104):
                self.epaper_display_error("image size is incorrect!")
            self.epaper_display_image(img)
        elif resp.status_code == 304:
            logging.warning("not updating image due to http 304")
        else:
            logging.error(f"unhandled response status: {resp.status_code}")

    def on_disconnect(self, client, userdata, rc):
        self.epaper_display_error(f"disconnected rc:{rc}")

    def debug_mqtt(self, client, userdata, level, buf):
        logging.debug(f"{level}: {buf}", file=sys.stderr)
Пример #28
0
def set_inky_display(image):
    inky_display = InkyPHAT(display_colour)
    inky_display.set_image(image)
    inky_border = inky_display.WHITE if border_colour == 'white' else inky_display.BLACK
    inky_display.set_border(inky_border)
    inky_display.show()
Пример #29
0
# code taken from tutorial at https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-inky-phat
# John Harney, 1.17.2020

from inky import InkyPHAT
from PIL import Image, ImageFont, ImageDraw
from font_fredoka_one import FredokaOne

inky_display = InkyPHAT("yellow")
inky_display.set_border(inky_display.WHITE)

img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
draw = ImageDraw.Draw(img)

font = ImageFont.truetype(FredokaOne, 22)

message = "Centre College"
w, h = font.getsize(message)
x = (inky_display.WIDTH / 2) - (w / 2)
y = (inky_display.HEIGHT / 2) - (h / 2)

draw.text((x, y), message, inky_display.YELLOW, font)
inky_display.set_image(img)
inky_display.show()
Пример #30
0
class Inky(DisplayImpl):
    def __init__(self, config):
        super(Inky, self).__init__(config, 'inky')
        self._display = None

    def layout(self):
        fonts.setup(10, 8, 10, 28, 25, 9)
        self._layout['width'] = 212
        self._layout['height'] = 104
        self._layout['face'] = (0, 37)
        self._layout['name'] = (5, 18)
        self._layout['channel'] = (0, 0)
        self._layout['aps'] = (30, 0)
        self._layout['uptime'] = (147, 0)
        self._layout['line1'] = [0, 12, 212, 12]
        self._layout['line2'] = [0, 92, 212, 92]
        self._layout['friend_face'] = (0, 76)
        self._layout['friend_name'] = (40, 78)
        self._layout['shakes'] = (0, 93)
        self._layout['mode'] = (187, 93)
        self._layout['status'] = {
            'pos': (102, 18),
            'font': fonts.status_font(fonts.Small),
            'max': 20
        }
        return self._layout

    def initialize(self):
        logging.info("initializing inky display")

        if self.config['color'] == 'fastAndFurious':
            logging.info("Initializing Inky in 2-color FAST MODE")
            logging.info(
                "THIS MAY BE POTENTIALLY DANGEROUS. NO WARRANTY IS PROVIDED")
            logging.info("USE THIS DISPLAY IN THIS MODE AT YOUR OWN RISK")

            from pwnagotchi.ui.hw.libs.inkyphat.inkyphatfast import InkyPHATFast
            self._display = InkyPHATFast('black')
            self._display.set_border(InkyPHATFast.BLACK)
        elif self.config['color'] == 'auto':
            from inky.auto import auto
            self._display = auto()
            self._display.set_border(self._display.BLACK)
            self._layout['width'] = self._display.WIDTH
            self._layout['height'] = self._display.HEIGHT
        else:
            from inky import InkyPHAT
            self._display = InkyPHAT(self.config['color'])
            self._display.set_border(InkyPHAT.BLACK)

    def render(self, canvas):
        if self.config['color'] == 'black' or self.config[
                'color'] == 'fastAndFurious':
            display_colors = 2
        else:
            display_colors = 3

        img_buffer = canvas.convert('RGB').convert('P',
                                                   palette=1,
                                                   colors=display_colors)
        if self.config['color'] == 'red':
            img_buffer.putpalette([
                255,
                255,
                255,  # index 0 is white
                0,
                0,
                0,  # index 1 is black
                255,
                0,
                0  # index 2 is red
            ])
        elif self.config['color'] == 'yellow':
            img_buffer.putpalette([
                255,
                255,
                255,  # index 0 is white
                0,
                0,
                0,  # index 1 is black
                255,
                255,
                0  # index 2 is yellow
            ])
        else:
            img_buffer.putpalette([
                255,
                255,
                255,  # index 0 is white
                0,
                0,
                0  # index 1 is black
            ])

        self._display.set_image(img_buffer)
        try:
            self._display.show()
        except:
            logging.exception("error while rendering on inky")

    def clear(self):
        self._display.Clear()