refactoring pep8 & pylint

This commit is contained in:
Bartosz Kościów 2017-04-25 20:46:00 +02:00
parent af1ae95a53
commit 736bb0547b
10 changed files with 105 additions and 85 deletions

View File

@ -1,7 +1,9 @@
"""Chip interface"""
import abc import abc
class Chip(metaclass=abc.ABCMeta): class Chip(metaclass=abc.ABCMeta):
"""Chip class"""
def __init__(self, width, height, driver, auto_flush): def __init__(self, width, height, driver, auto_flush):
self.options = {} self.options = {}
self._width = width self._width = width
@ -31,20 +33,24 @@ class Chip(metaclass=abc.ABCMeta):
@property @property
def color(self): def color(self):
"""get RGB colour"""
return self.options['color'] return self.options['color']
@color.setter @color.setter
def color(self, rgb): def color(self, rgb):
"""set (R, G, B) colour """
self.options['color'] = { self.options['color'] = {
'R': rgb[0], 'G': rgb[1], 'B': rgb[2] 'R': rgb[0], 'G': rgb[1], 'B': rgb[2]
} }
@property @property
def background_color(self): def background_color(self):
"""get background colour"""
return self.options['background_color'] return self.options['background_color']
@background_color.setter @background_color.setter
def background_color(self, rgb): def background_color(self, rgb):
"""set (R, G, B) background colour """
self.options['background_color'] = { self.options['background_color'] = {
'R': rgb[0], 'G': rgb[1], 'B': rgb[2] 'R': rgb[0], 'G': rgb[1], 'B': rgb[2]
} }
@ -65,31 +71,31 @@ class Chip(metaclass=abc.ABCMeta):
pass pass
@abc.abstractmethod @abc.abstractmethod
def draw_pixel(self, x, y): def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y""" """draw a pixel at x,y"""
pass pass
@abc.abstractmethod @abc.abstractmethod
def draw_line(self, x1, y1, x2, y2): def draw_line(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw a line from point x1,y1 to x2,y2""" """draw a line from point x1,y1 to x2,y2"""
pass pass
@abc.abstractmethod @abc.abstractmethod
def draw_rect(self, x1, y1, x2, y2): def draw_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw a rectangle""" """draw a rectangle"""
pass pass
@abc.abstractmethod @abc.abstractmethod
def draw_circle(self, x, y, r): def draw_circle(self, pos_x, pos_y, radius):
"""draw a circle""" """draw a circle"""
pass pass
@abc.abstractmethod @abc.abstractmethod
def draw_arc(self, x, y, radius, start, end): def draw_arc(self, pos_x, pos_y, radius, start, end):
"""draw an arc""" """draw an arc"""
pass pass
@abc.abstractmethod @abc.abstractmethod
def fill_rect(self, x1, y1, x2, y2): def fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw a filled rectangle""" """draw a filled rectangle"""
pass pass

View File

@ -3,6 +3,7 @@ import abc
class Driver(metaclass=abc.ABCMeta): class Driver(metaclass=abc.ABCMeta):
"""Driver interface"""
@abc.abstractmethod @abc.abstractmethod
def init(self): def init(self):
"""initialize a device""" """initialize a device"""

View File

@ -7,7 +7,7 @@ class Area(Pixel):
"""Area drawing algorithm""" """Area drawing algorithm"""
def __init__(self, driver): def __init__(self, driver):
self.driver = driver self.driver = driver
Pixel.__init__(self) Pixel.__init__(self, driver)
def init(self): def init(self):
"""additional initialization""" """additional initialization"""
@ -18,20 +18,20 @@ class Area(Pixel):
self._set_area(pos_x, pos_y, pos_x, pos_y) self._set_area(pos_x, pos_y, pos_x, pos_y)
self.driver.data(self._converted_color(), None) self.driver.data(self._converted_color(), None)
def _set_area(self, x1, y1, x2, y2): def _set_area(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""select area to work with""" """select area to work with"""
self.driver.cmd(0x0020, None) self.driver.cmd(0x0020, None)
self.driver.data(x1, None) self.driver.data(pos_x1, None)
self.driver.cmd(0x0021, None) self.driver.cmd(0x0021, None)
self.driver.data(y1, None) self.driver.data(pos_y1, None)
self.driver.cmd(0x0050, None) self.driver.cmd(0x0050, None)
self.driver.data(x1, None) self.driver.data(pos_x1, None)
self.driver.cmd(0x0052, None) self.driver.cmd(0x0052, None)
self.driver.data(y1, None) self.driver.data(pos_y1, None)
self.driver.cmd(0x0051, None) self.driver.cmd(0x0051, None)
self.driver.data(x2, None) self.driver.data(pos_x2, None)
self.driver.cmd(0x0053, None) self.driver.cmd(0x0053, None)
self.driver.data(y2, None) self.driver.data(pos_y2, None)
self.driver.cmd(0x0022, None) self.driver.cmd(0x0022, None)
def _draw_vertical_line(self, pos_x, pos_y, length): def _draw_vertical_line(self, pos_x, pos_y, length):
@ -57,35 +57,35 @@ class Area(Pixel):
return steps return steps
def draw_line(self, x1, y1, x2, y2): def draw_line(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw diagonal line""" """draw diagonal line"""
width = abs(x2 - x1) width = abs(pos_x2 - pos_x1)
height = abs(y2 - y1) height = abs(pos_y2 - pos_y1)
if x1 == x2: if pos_x1 == pos_x2:
steps = [height] steps = [height]
horizontal = False horizontal = False
offset_x = offset_y = 0 offset_x = offset_y = 0
elif y1 == y2: elif pos_y1 == pos_y2:
steps = [width] steps = [width]
horizontal = True horizontal = True
offset_x = offset_y = 0 offset_x = offset_y = 0
elif width > height: elif width > height:
if x2 < x1: if pos_x2 < pos_x1:
x1, x2 = x2, x1 pos_x1, pos_x2 = pos_x2, pos_x1
y1, y2 = y2, y1 pos_y1, pos_y2 = pos_y2, pos_y1
offset_y = 1 if y2 > y1 else -1 offset_y = 1 if pos_y2 > pos_y1 else -1
offset_x = 1 if x2 > x1 else -1 offset_x = 1 if pos_x2 > pos_x1 else -1
horizontal = True horizontal = True
step = height step = height
length = width / step length = width / step
steps = self._calculate_steps(length, step, width) steps = self._calculate_steps(length, step, width)
else: else:
if y2 < y1: if pos_y2 < pos_y1:
x1, x2 = x2, x1 pos_x1, pos_x2 = pos_x2, pos_x1
y1, y2 = y2, y1 pos_y1, pos_y2 = pos_y2, pos_y1
offset_y = 1 if y2 > y1 else -1 offset_y = 1 if pos_y2 > pos_y1 else -1
offset_x = 1 if x2 > x1 else -1 offset_x = 1 if pos_x2 > pos_x1 else -1
horizontal = False horizontal = False
step = width step = width
length = height / step length = height / step
@ -96,27 +96,27 @@ class Area(Pixel):
for idx, step in enumerate(steps): for idx, step in enumerate(steps):
if horizontal: if horizontal:
self._draw_horizontal_line( self._draw_horizontal_line(
int(x1 + delta_x), int(pos_x1 + delta_x),
int(y1 + (idx * offset_y)), int(pos_y1 + (idx * offset_y)),
int(step) int(step)
) )
delta_x += step * offset_x delta_x += step * offset_x
else: else:
self._draw_vertical_line( self._draw_vertical_line(
int(x1 + (idx * offset_x)), int(pos_x1 + (idx * offset_x)),
int(y1 + delta_y), int(pos_y1 + delta_y),
int(step) int(step)
) )
delta_y += step * offset_y delta_y += step * offset_y
def fill_rect(self, x1, y1, x2, y2): def fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""fill an area""" """fill an area"""
size = (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1) size = (abs(pos_x2 - pos_x1) + 1) * (abs(pos_y2 - pos_y1) + 1)
self._set_area( self._set_area(
min(x1, x2), min(pos_x1, pos_x2),
min(y1, y2), min(pos_y1, pos_y2),
max(x1, x2), max(pos_x1, pos_x2),
max(y1, y2) max(pos_y1, pos_y2)
) )
color = self._converted_background_color() color = self._converted_background_color()
for _ in range(0, size): for _ in range(0, size):

View File

@ -4,17 +4,18 @@ from gfxlcd.drawing.pixel import Pixel
class Page(Pixel, metaclass=abc.ABCMeta): class Page(Pixel, metaclass=abc.ABCMeta):
"""Page drawing algorithm""" """Page drawing algorithm"""
def __init__(self): def __init__(self, driver):
Pixel.__init__(self) self.driver = driver
Pixel.__init__(self, driver)
self.buffer = [] self.buffer = []
def init(self): def init(self):
"""init page""" """init page"""
self.buffer = [[0] * (self.height // 8) for x in range(self.width)] self.buffer = [[0] * (self.height // 8) for x in range(self.width)]
def draw_pixel(self, x, y): def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y""" """draw a pixel at x,y"""
self.buffer[x][y//8] |= 1 << (y % 8) self.buffer[pos_x][pos_y//8] |= 1 << (pos_y % 8)
self.flush() self.flush()
def _calculate_steps(self, length, step, required_length): def _calculate_steps(self, length, step, required_length):
@ -26,67 +27,67 @@ class Page(Pixel, metaclass=abc.ABCMeta):
return steps return steps
def draw_line(self, x1, y1, x2, y2): def draw_line(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw diagonal line""" """draw diagonal line"""
width = abs(x2 - x1) width = abs(pos_x2 - pos_x1)
height = abs(y2 - y1) height = abs(pos_y2 - pos_y1)
if x1 == x2: if pos_x1 == pos_x2:
steps = [height] steps = [height]
horizontal = False horizontal = False
offset_x = offset_y = 0 offset_x = offset_y = 0
elif y1 == y2: elif pos_y1 == pos_y2:
steps = [width] steps = [width]
horizontal = True horizontal = True
offset_x = offset_y = 0 offset_x = offset_y = 0
elif width > height: elif width > height:
if x2 < x1: if pos_x2 < pos_x1:
x1, x2 = x2, x1 pos_x1, pos_x2 = pos_x2, pos_x1
y1, y2 = y2, y1 pos_y1, pos_y2 = pos_y2, pos_y1
offset_y = 1 if y2 > y1 else -1 offset_y = 1 if pos_y2 > pos_y1 else -1
offset_x = 1 if x2 > x1 else -1 offset_x = 1 if pos_x2 > pos_x1 else -1
horizontal = True horizontal = True
step = height step = height
length = width / step length = width / step
steps = self._calculate_steps(length, step, width) steps = self._calculate_steps(length, step, width)
else: else:
if y2 < y1: if pos_y2 < pos_y1:
x1, x2 = x2, x1 pos_x1, pos_x2 = pos_x2, pos_x1
y1, y2 = y2, y1 pos_y1, pos_y2 = pos_y2, pos_y1
offset_y = 1 if y2 > y1 else -1 offset_y = 1 if pos_y2 > pos_y1 else -1
offset_x = 1 if x2 > x1 else -1 offset_x = 1 if pos_x2 > pos_x1 else -1
horizontal = False horizontal = False
step = width step = width
length = height / step length = height / step
steps = self._calculate_steps(length, step, height) steps = self._calculate_steps(length, step, height)
dy = 0 delta_y = 0
dx = 0 delta_x = 0
for idx, step in enumerate(steps): for idx, step in enumerate(steps):
if horizontal: if horizontal:
for appendix in range(int(step)+1): for appendix in range(int(step)+1):
self.draw_pixel( self.draw_pixel(
int(x1 + dx + appendix), int(pos_x1 + delta_x + appendix),
int(y1 + (idx * offset_y)) int(pos_y1 + (idx * offset_y))
) )
dx += step * offset_x delta_x += step * offset_x
else: else:
for appendix in range(int(step)+1): for appendix in range(int(step)+1):
self.draw_pixel( self.draw_pixel(
int(x1 + (idx * offset_x)), int(pos_x1 + (idx * offset_x)),
int(y1 + dy + appendix) int(pos_y1 + delta_y + appendix)
) )
dy += step * offset_y delta_y += step * offset_y
def fill_rect(self, x1, y1, x2, y2): def fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw a filled rectangle""" """draw a filled rectangle"""
if y2 < y1: if pos_y2 < pos_y1:
y1, y2 = y2, y1 pos_y1, pos_y2 = pos_y2, pos_y1
if x2 < x1: if pos_x2 < pos_x1:
x1, x2 = x2, x1 pos_x1, pos_x2 = pos_x2, pos_x1
start_page = y1 // 8 start_page = pos_y1 // 8
start_bit = y1 % 8 start_bit = pos_y1 % 8
end_page = y2 // 8 end_page = pos_y2 // 8
end_bit = y2 % 8 end_bit = pos_y2 % 8
rows = [] rows = []
first_page = int(('0' * start_bit).rjust(8, '1'), 2) first_page = int(('0' * start_bit).rjust(8, '1'), 2)
last_page = int('1' * (end_bit+1), 2) last_page = int('1' * (end_bit+1), 2)
@ -99,9 +100,9 @@ class Page(Pixel, metaclass=abc.ABCMeta):
rows.append(first_page & last_page) rows.append(first_page & last_page)
page = start_page page = start_page
for v in rows: for value in rows:
for x in range(x2-x1+1): for x in range(pos_x2-pos_x1+1):
self.buffer[x1+x][page] |= v self.buffer[pos_x1+x][page] |= value
page += 1 page += 1
def get_page_value(self, column, page): def get_page_value(self, column, page):

View File

@ -1,8 +1,10 @@
"""Common drawing functions"""
import math import math
class Pixel(object): class Pixel(object):
def __init__(self): """Pixel class"""
def __init__(self, driver):
self.options['color'] = { self.options['color'] = {
'R': 255, 'G': 255, 'B': 255 'R': 255, 'G': 255, 'B': 255
} }
@ -10,6 +12,14 @@ class Pixel(object):
'R': 0, 'G': 0, 'B': 0, 'R': 0, 'G': 0, 'B': 0,
} }
def draw_pixel(self, pos_x, pos_y):
"""dummy fuction"""
pass
def draw_line(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""dummy fuction"""
pass
def draw_rect(self, x1, y1, x2, y2): def draw_rect(self, x1, y1, x2, y2):
"""draw a rectangle""" """draw a rectangle"""
self.draw_line(x1, y1, x2, y1) self.draw_line(x1, y1, x2, y1)

View File

@ -1,3 +1,4 @@
"""NJU6450 gpio driver class"""
import time import time
import RPi.GPIO # pylint: disable=I0011,F0401 import RPi.GPIO # pylint: disable=I0011,F0401
from gfxlcd.abstract.driver import Driver from gfxlcd.abstract.driver import Driver
@ -6,6 +7,7 @@ RPi.GPIO.setmode(RPi.GPIO.BCM)
class GPIO(Driver): class GPIO(Driver):
"""GPIO driver"""
def __init__(self): def __init__(self):
self.pins = { self.pins = {
'A0': 17, 'A0': 17,

View File

@ -7,7 +7,7 @@ class NJU6450(Page, Chip):
"""Class for an LCD with NJU6450 chip""" """Class for an LCD with NJU6450 chip"""
def __init__(self, width, height, driver, auto_flush=True): def __init__(self, width, height, driver, auto_flush=True):
Chip.__init__(self, width, height, driver, auto_flush) Chip.__init__(self, width, height, driver, auto_flush)
Page.__init__(self) Page.__init__(self, driver)
def init(self): def init(self):
"""initialize display""" """initialize display"""
@ -23,7 +23,7 @@ class NJU6450(Page, Chip):
def set_xy(self, pos_x, pos_y): def set_xy(self, pos_x, pos_y):
"""set xy pos""" """set xy pos"""
if x < self.width/2: if pos_x < self.width/2:
self.driver.cmd(0xB8 | pos_y, 0) self.driver.cmd(0xB8 | pos_y, 0)
self.driver.cmd(0x00 | pos_x, 0) self.driver.cmd(0x00 | pos_x, 0)
else: else:

View File

@ -1,7 +1,7 @@
"""SPI+GPIO connection driver for SSD1306""" """SPI+GPIO connection driver for SSD1306"""
import time
import spidev # pylint: disable=I0011,F0401 import spidev # pylint: disable=I0011,F0401
import RPi.GPIO as GPIO # pylint: disable=I0011,F0401 import RPi.GPIO as GPIO # pylint: disable=I0011,F0401
import time
from gfxlcd.abstract.driver import Driver from gfxlcd.abstract.driver import Driver
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)

View File

@ -7,7 +7,7 @@ class SSD1306(Page, Chip):
"""Class for an LCD with SSD306 chip""" """Class for an LCD with SSD306 chip"""
def __init__(self, width, height, driver, auto_flush=True): def __init__(self, width, height, driver, auto_flush=True):
Chip.__init__(self, width, height, driver, auto_flush) Chip.__init__(self, width, height, driver, auto_flush)
Page.__init__(self) Page.__init__(self, driver)
def init(self): def init(self):
"""inits a device""" """inits a device"""