Beispiel #1
0
class App(object):
    def __init__(self, config):
        self.epd = EPD()
        self.epd.init()
        self.font = config['font']
        self.route = config['route']
        self.stop = config['stop']
        self.curlbus = config['curlbus']

    def draw(self, eta, last_updated):
        self.epd.init()
        image = Image.new('1', (self.epd.height, self.epd.width), 255)
        draw = ImageDraw.Draw(image)

        # Draw the line number on the top conrner
        font = ImageFont.truetype(self.font, 30)
        draw.text((5, 2), self.route, font=font, fill=0)
        draw.rectangle([(0, 0), (60, 40)])

        # Draw the ETA
        font = ImageFont.truetype(self.font, 60)
        draw.text((self.epd.height / 2 - 50, self.epd.width / 2 - 50),
                  eta,
                  font=font,
                  fill=0)

        # Draw the last_updated timestamp
        font = ImageFont.truetype(self.font, 16)
        draw.text((0, self.epd.width - 20), last_updated, font=font, fill=0)
        self.epd.smart_update(image.rotate(90, expand=True))
        self.epd.sleep()

    def update(self):
        response = requests.get("{0}/{1}".format(self.curlbus, self.stop),
                                headers=HEADERS)
        response_data = response.json()
        last_updated = response_data['timestamp']
        next_eta = "Nothing :("
        etas = []
        for visit in response_data['visits'][self.stop]:
            if visit['line_name'] == self.route:
                etas.append(parse(visit['eta']))
        if len(etas) > 0:
            closest_eta = sorted(etas)[0]
            next_eta = round(
                (closest_eta -
                 datetime.now(dateutil.tz.tzlocal())).total_seconds() / 60)
            if next_eta <= 0:
                next_eta = 'Now'
            else:
                next_eta = '{0}m'.format(next_eta)
        self.draw(next_eta, last_updated)

    def run(self):
        while True:
            self.update()
            time.sleep(60)
Beispiel #2
0
def main():
    print("initializing", end="")
    sys.stdout.flush()
    epd = EPD()
    epd.init()
    print(".", end="")
    sys.stdout.flush()

    image = Image.new('1', (epd.width, epd.height), 255)
    draw = ImageDraw.Draw(image)

    font = ImageFont.truetype(
        '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 20)
    draw.text((0, 5), 'Partial refresh', font=font, fill=0)

    font = ImageFont.truetype(
        '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 18)
    draw.line([0, 28, epd.width, 28], fill=0, width=3)
    epd.display_frame(image)
    print(".")
    loc = 30

    draw.text((0, loc), "Look!", font=font, fill=0)
    epd.smart_update(image)
    loc += 20
    print(".", end="")
    sys.stdout.flush()

    draw.text((0, loc), "No need to refresh", font=font, fill=0)
    draw.text((0, loc + 20), "the entire screen.", font=font, fill=0)
    epd.smart_update(image)
    loc += 45
    print(".", end="")
    sys.stdout.flush()

    draw.text((0, loc), "It's fast", font=font, fill=0)
    draw.text((0, loc + 20), "and convenient", font=font, fill=0)
    epd.smart_update(image)
    loc += 20
    print(".", end="")
    sys.stdout.flush()

    epd.sleep()
    print("!")