aboutsummaryrefslogtreecommitdiff
path: root/rotate.py
blob: dabedf4e5af75f2d4efc7322e0858a054cd2b7d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# -*- coding:utf-8 -*-

import argparse
import contextlib
import itertools
import logging
import os
import random
import sys
import time

from PIL import Image,ImageDraw,ImageFont

parser = argparse.ArgumentParser()
parser.add_argument("imgdir", help="location of dithered bmps to rotate")
parser.add_argument("--debug", action="store_true")
parser.add_argument("--delay", default=600, type=int)
parser.add_argument("--simulate", action="store_true")
parser.add_argument("--clear", action="store_true", help="clear panel first run")
args = parser.parse_args()

if args.simulate:
    import mock_epd as epd7in3f
else:
    from waveshare_epd import epd7in3f

log_format = "%(asctime)s %(levelname)s %(name)s: %(message)s"
if args.debug is True:
    logging.basicConfig(level=logging.DEBUG, format=log_format)
else:
    logging.basicConfig(level=logging.INFO, format=log_format)
logger = logging.getLogger(__name__)

paths = [os.path.join(args.imgdir, f) for f in os.listdir(args.imgdir)
         if f.endswith(".bmp")
         and os.path.isfile(os.path.join(args.imgdir, f))]
random.shuffle(paths)
if args.simulate is True:
    delay = args.delay
else:
    delay = max(args.delay, 180)

logger.info("hello!")
logger.info(f"rotating images in {args.imgdir} every {delay}s")

@contextlib.contextmanager
def EPD():
    epd = epd7in3f.EPD()
    try:
        epd.init()
        yield epd
    finally:
        epd.sleep()

try:
    iteration = 1
    for filename in itertools.cycle(paths):
        with EPD() as epd:
            if iteration == 1 and args.clear:
                epd.Clear()
            logger.info(f"displaying {filename}")
            Himage = Image.open(filename)
            epd.display(epd.getbuffer(Himage))
        logger.info(f"sleeping for {delay}s...")
        time.sleep(delay)
        iteration += 1

except IOError as e:
    logger.info(e)

except KeyboardInterrupt:
    logger.info("ctrl + c")
    logger.info("clearing the screen")
    with EPD() as epd:
        epd.Clear()

finally:
    logger.info("cleaning up")
    epd7in3f.epdconfig.module_exit(cleanup=True)
    logger.info("bye!")