/ examples / epd_bonnet.py
epd_bonnet.py
  1  import time
  2  import busio
  3  import board
  4  from digitalio import DigitalInOut, Direction
  5  
  6  from PIL import Image
  7  from PIL import ImageDraw
  8  from PIL import ImageFont
  9  from adafruit_epd.epd import Adafruit_EPD
 10  from adafruit_epd.ssd1675b import Adafruit_SSD1675B  # pylint: disable=unused-import
 11  
 12  # create two buttons
 13  switch1 = DigitalInOut(board.D6)
 14  switch2 = DigitalInOut(board.D5)
 15  switch1.direction = Direction.INPUT
 16  switch2.direction = Direction.INPUT
 17  
 18  # create the spi device and pins we will need
 19  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 20  ecs = DigitalInOut(board.D8)
 21  dc = DigitalInOut(board.D22)
 22  rst = DigitalInOut(board.D27)
 23  busy = DigitalInOut(board.D17)
 24  
 25  # give them all to our driver
 26  display = Adafruit_SSD1675B(
 27      122,
 28      250,
 29      spi,  # 2.13" HD mono display (rev B)
 30      cs_pin=ecs,
 31      dc_pin=dc,
 32      sramcs_pin=None,
 33      rst_pin=rst,
 34      busy_pin=busy,
 35  )
 36  display.rotation = 1
 37  
 38  # Create blank image for drawing.
 39  # Make sure to create image with mode '1' for 1-bit color.
 40  width = display.width
 41  height = display.height
 42  image = Image.new("RGB", (width, height))
 43  
 44  WHITE = (0xFF, 0xFF, 0xFF)
 45  BLACK = (0x00, 0x00, 0x00)
 46  
 47  # clear the buffer
 48  display.fill(Adafruit_EPD.WHITE)
 49  # clear it out
 50  display.display()
 51  
 52  # Get drawing object to draw on image.
 53  draw = ImageDraw.Draw(image)
 54  # empty it
 55  draw.rectangle((0, 0, width, height), fill=WHITE)
 56  
 57  # Draw an outline box
 58  draw.rectangle((1, 1, width - 2, height - 2), outline=BLACK, fill=WHITE)
 59  # Draw some shapes.
 60  # First define some constants to allow easy resizing of shapes.
 61  padding = 5
 62  shape_width = 30
 63  top = padding
 64  bottom = height - padding
 65  # Move left to right keeping track of the current x position for drawing shapes.
 66  x = padding
 67  # Draw an ellipse.
 68  draw.ellipse((x, top, x + shape_width, bottom), outline=BLACK, fill=WHITE)
 69  x += shape_width + padding
 70  # Draw a rectangle.
 71  draw.rectangle((x, top, x + shape_width, bottom), outline=WHITE, fill=BLACK)
 72  x += shape_width + padding
 73  # Draw a triangle.
 74  draw.polygon(
 75      [(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)],
 76      outline=BLACK,
 77      fill=WHITE,
 78  )
 79  x += shape_width + padding
 80  # Draw an X.
 81  draw.line((x, bottom, x + shape_width, top), fill=BLACK)
 82  draw.line((x, top, x + shape_width, bottom), fill=BLACK)
 83  x += shape_width + padding
 84  
 85  # Load default font.
 86  font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
 87  
 88  # Alternatively load a TTF font.  Make sure the .ttf font
 89  # file is in the same directory as the python script!
 90  # Some other nice fonts to try: http://www.dafont.com/bitmap.php
 91  # font = ImageFont.truetype('Minecraftia.ttf', 8)
 92  
 93  # Write two lines of text.
 94  draw.text((x, top), "Hello", font=font, fill=BLACK)
 95  draw.text((x, top + 20), "World!", font=font, fill=BLACK)
 96  
 97  while True:
 98      if not switch1.value:
 99          print("Switch 1")
100          display.image(image)
101          display.display()
102          while not switch1.value:
103              time.sleep(0.01)
104      if not switch2.value:
105          print("Switch 2")
106          display.fill(Adafruit_EPD.WHITE)
107          display.display()
108          while not switch2.value:
109              time.sleep(0.01)
110      time.sleep(0.01)