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
class Chip(metaclass=abc.ABCMeta):
"""Chip class"""
def __init__(self, width, height, driver, auto_flush):
self.options = {}
self._width = width
@ -31,20 +33,24 @@ class Chip(metaclass=abc.ABCMeta):
@property
def color(self):
"""get RGB colour"""
return self.options['color']
@color.setter
def color(self, rgb):
"""set (R, G, B) colour """
self.options['color'] = {
'R': rgb[0], 'G': rgb[1], 'B': rgb[2]
}
@property
def background_color(self):
"""get background colour"""
return self.options['background_color']
@background_color.setter
def background_color(self, rgb):
"""set (R, G, B) background colour """
self.options['background_color'] = {
'R': rgb[0], 'G': rgb[1], 'B': rgb[2]
}
@ -65,31 +71,31 @@ class Chip(metaclass=abc.ABCMeta):
pass
@abc.abstractmethod
def draw_pixel(self, x, y):
def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y"""
pass
@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"""
pass
@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"""
pass
@abc.abstractmethod
def draw_circle(self, x, y, r):
def draw_circle(self, pos_x, pos_y, radius):
"""draw a circle"""
pass
@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"""
pass
@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"""
pass

View File

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

View File

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

View File

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

View File

@ -1,8 +1,10 @@
"""Common drawing functions"""
import math
class Pixel(object):
def __init__(self):
"""Pixel class"""
def __init__(self, driver):
self.options['color'] = {
'R': 255, 'G': 255, 'B': 255
}
@ -10,6 +12,14 @@ class Pixel(object):
'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):
"""draw a rectangle"""
self.draw_line(x1, y1, x2, y1)

View File

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

View File

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

View File

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

View File

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