Testing new line breaks

Testing new line breaks
This commit is contained in:
Noe Ruiz 2019-04-03 10:30:11 -04:00
parent 81dde482e1
commit 89317201c6

View file

@ -1,104 +1,104 @@
import digitalio import digitalio
import busio import busio
import board import board
from adafruit_epd.epd import Adafruit_EPD from adafruit_epd.epd import Adafruit_EPD
from adafruit_epd.il0373 import Adafruit_IL0373 from adafruit_epd.il0373 import Adafruit_IL0373
# create the spi device and pins we will need # create the spi device and pins we will need
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
ecs = digitalio.DigitalInOut(board.D9) ecs = digitalio.DigitalInOut(board.D9)
dc = digitalio.DigitalInOut(board.D10) dc = digitalio.DigitalInOut(board.D10)
srcs = None srcs = None
rst = None rst = None
busy = None busy = None
# give them all to our driver # give them all to our driver
print("Creating display") print("Creating display")
display = Adafruit_IL0373(104, 212, spi, # 2.13" Tri-color display display = Adafruit_IL0373(104, 212, spi, # 2.13" Tri-color display
cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs, cs_pin=ecs, dc_pin=dc, sramcs_pin=srcs,
rst_pin=rst, busy_pin=busy) rst_pin=rst, busy_pin=busy)
display.rotation = 3 display.rotation = 3
FILENAME = "blinka.bmp" FILENAME = "blinka.bmp"
def read_le(s): def read_le(s):
# as of this writting, int.from_bytes does not have LE support, DIY! # as of this writting, int.from_bytes does not have LE support, DIY!
result = 0 result = 0
shift = 0 shift = 0
for byte in bytearray(s): for byte in bytearray(s):
result += byte << shift result += byte << shift
shift += 8 shift += 8
return result return result
class BMPError(Exception): class BMPError(Exception):
pass pass
def display_bitmap(epd, filename): def display_bitmap(epd, filename):
# pylint: disable=too-many-locals, too-many-branches # pylint: disable=too-many-locals, too-many-branches
try: try:
f = open("/" + filename, "rb") f = open("/" + filename, "rb")
except OSError: except OSError:
print("Couldn't open file") print("Couldn't open file")
return return
print("File opened") print("File opened")
try: try:
if f.read(2) != b'BM': # check signature if f.read(2) != b'BM': # check signature
raise BMPError("Not BitMap file") raise BMPError("Not BitMap file")
bmpFileSize = read_le(f.read(4)) bmpFileSize = read_le(f.read(4))
f.read(4) # Read & ignore creator bytes f.read(4) # Read & ignore creator bytes
bmpImageoffset = read_le(f.read(4)) # Start of image data bmpImageoffset = read_le(f.read(4)) # Start of image data
headerSize = read_le(f.read(4)) headerSize = read_le(f.read(4))
bmpWidth = read_le(f.read(4)) bmpWidth = read_le(f.read(4))
bmpHeight = read_le(f.read(4)) bmpHeight = read_le(f.read(4))
flip = True flip = True
print("Size: %d\nImage offset: %d\nHeader size: %d" % print("Size: %d\nImage offset: %d\nHeader size: %d" %
(bmpFileSize, bmpImageoffset, headerSize)) (bmpFileSize, bmpImageoffset, headerSize))
print("Width: %d\nHeight: %d" % (bmpWidth, bmpHeight)) print("Width: %d\nHeight: %d" % (bmpWidth, bmpHeight))
if read_le(f.read(2)) != 1: if read_le(f.read(2)) != 1:
raise BMPError("Not singleplane") raise BMPError("Not singleplane")
bmpDepth = read_le(f.read(2)) # bits per pixel bmpDepth = read_le(f.read(2)) # bits per pixel
print("Bit depth: %d" % (bmpDepth)) print("Bit depth: %d" % (bmpDepth))
if bmpDepth != 24: if bmpDepth != 24:
raise BMPError("Not 24-bit") raise BMPError("Not 24-bit")
if read_le(f.read(2)) != 0: if read_le(f.read(2)) != 0:
raise BMPError("Compressed file") raise BMPError("Compressed file")
print("Image OK! Drawing...") print("Image OK! Drawing...")
rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary rowSize = (bmpWidth * 3 + 3) & ~3 # 32-bit line boundary
for row in range(bmpHeight): # For each scanline... for row in range(bmpHeight): # For each scanline...
if flip: # Bitmap is stored bottom-to-top order (normal BMP) if flip: # Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize
else: # Bitmap is stored top-to-bottom else: # Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize pos = bmpImageoffset + row * rowSize
# print ("seek to %d" % pos) # print ("seek to %d" % pos)
f.seek(pos) f.seek(pos)
rowdata = f.read(3*bmpWidth) rowdata = f.read(3*bmpWidth)
for col in range(bmpWidth): for col in range(bmpWidth):
b, g, r = rowdata[3*col:3*col+3] # BMP files store RGB in BGR b, g, r = rowdata[3*col:3*col+3] # BMP files store RGB in BGR
if r < 0x80 and g < 0x80 and b < 0x80: if r < 0x80 and g < 0x80 and b < 0x80:
epd.pixel(col, row, Adafruit_EPD.BLACK) epd.pixel(col, row, Adafruit_EPD.BLACK)
elif r >= 0x80 and g >= 0x80 and b >= 0x80: elif r >= 0x80 and g >= 0x80 and b >= 0x80:
pass # epd.pixel(row, col, Adafruit_EPD.WHITE) pass # epd.pixel(row, col, Adafruit_EPD.WHITE)
elif r >= 0x80: elif r >= 0x80:
epd.pixel(col, row, Adafruit_EPD.RED) epd.pixel(col, row, Adafruit_EPD.RED)
except OSError: except OSError:
print("Couldn't read file") print("Couldn't read file")
except BMPError as e: except BMPError as e:
print("Failed to parse BMP: " + e.args[0]) print("Failed to parse BMP: " + e.args[0])
finally: finally:
f.close() f.close()
print("Finished drawing") print("Finished drawing")
# clear the buffer # clear the buffer
display.fill(Adafruit_EPD.WHITE) display.fill(Adafruit_EPD.WHITE)
display_bitmap(display, FILENAME) display_bitmap(display, FILENAME)
display.display() display.display()