From af1ae95a5353e4741a328dc58189579e053c909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Tue, 25 Apr 2017 20:22:17 +0200 Subject: [PATCH] refactoring pep8 & pylint --- gfxlcd/abstract/driver.py | 3 +- gfxlcd/demos/ili.py | 1 + gfxlcd/demos/ili_2.py | 7 +++-- gfxlcd/demos/nju.py | 1 + gfxlcd/demos/nju_2.py | 5 ++-- gfxlcd/demos/ssd.py | 6 ++-- gfxlcd/demos/ssd_2.py | 3 +- gfxlcd/drawing/area.py | 50 ++++++++++++++++++-------------- gfxlcd/driver/ili9325/gpio.py | 15 ++++------ gfxlcd/driver/ili9325/ili9325.py | 19 +++++++----- gfxlcd/driver/nju6450/gpio.py | 12 ++++---- gfxlcd/driver/nju6450/nju6450.py | 13 +++++---- gfxlcd/driver/ssd1306/spi.py | 13 ++++----- gfxlcd/driver/ssd1306/ssd1306.py | 13 +++++---- 14 files changed, 87 insertions(+), 74 deletions(-) diff --git a/gfxlcd/abstract/driver.py b/gfxlcd/abstract/driver.py index f928f24..f868593 100644 --- a/gfxlcd/abstract/driver.py +++ b/gfxlcd/abstract/driver.py @@ -1,3 +1,4 @@ +"""Interface for communication driver""" import abc @@ -20,4 +21,4 @@ class Driver(metaclass=abc.ABCMeta): @abc.abstractmethod def data(self, data, enable): """sends data to device""" - pass \ No newline at end of file + pass diff --git a/gfxlcd/demos/ili.py b/gfxlcd/demos/ili.py index 631b2a2..aa89fd6 100644 --- a/gfxlcd/demos/ili.py +++ b/gfxlcd/demos/ili.py @@ -29,6 +29,7 @@ def draw_net(o): o.draw_line(0, s, o.width-1, s) s += 10 + drv = GPIO() o = ILI9325(240, 320, drv) diff --git a/gfxlcd/demos/ili_2.py b/gfxlcd/demos/ili_2.py index 8280d2b..6bc7fd1 100644 --- a/gfxlcd/demos/ili_2.py +++ b/gfxlcd/demos/ili_2.py @@ -1,10 +1,10 @@ import RPi.GPIO import sys -RPi.GPIO.setmode(RPi.GPIO.BCM) sys.path.append("../../") from gfxlcd.driver.ili9325.gpio import GPIO as ILIGPIO from gfxlcd.driver.ili9325.ili9325 import ILI9325 import random +RPi.GPIO.setmode(RPi.GPIO.BCM) def hole(o, x, y): @@ -24,7 +24,7 @@ def hole(o, x, y): def draw_points(o): for _ in range(0, 50): - hole(o, random.randint(2,o.width - 10), random.randint(2,o.height-10)) + hole(o, random.randint(2, o.width-10), random.randint(2, o.height-10)) def draw_net(o): @@ -37,6 +37,7 @@ def draw_net(o): o.draw_line(0, s, o.width-1, s) s += 10 + lcd_tft = ILI9325(240, 320, ILIGPIO()) lcd_tft.init() @@ -95,4 +96,4 @@ lcd_tft.draw_line(131, 227, 135, 238) lcd_tft.draw_arc(131, 235, 5, 45, 135) lcd_tft.fill_rect(195, 204, 205, 210) lcd_tft.draw_rect(180, 210, 220, 225) -lcd_tft.fill_rect(180, 226, 220, 259) \ No newline at end of file +lcd_tft.fill_rect(180, 226, 220, 259) diff --git a/gfxlcd/demos/nju.py b/gfxlcd/demos/nju.py index 0c9f609..7ad9b09 100644 --- a/gfxlcd/demos/nju.py +++ b/gfxlcd/demos/nju.py @@ -17,6 +17,7 @@ def hole(x, y): o.draw_pixel(x, y + 3) o.draw_pixel(x+4, y + 3) + drv = GPIO() o = NJU6450(122, 32, drv) diff --git a/gfxlcd/demos/nju_2.py b/gfxlcd/demos/nju_2.py index 03736b9..52483c0 100644 --- a/gfxlcd/demos/nju_2.py +++ b/gfxlcd/demos/nju_2.py @@ -1,10 +1,10 @@ import RPi.GPIO import sys -RPi.GPIO.setmode(RPi.GPIO.BCM) sys.path.append("../../") from gfxlcd.driver.nju6450.gpio import GPIO from gfxlcd.driver.nju6450.nju6450 import NJU6450 import random +RPi.GPIO.setmode(RPi.GPIO.BCM) def hole(o, x, y): @@ -24,7 +24,7 @@ def hole(o, x, y): def draw_points(o): for _ in range(0, 50): - hole(o, random.randint(2,o.width - 10), random.randint(2,o.height-10)) + hole(o, random.randint(2, o.width-10), random.randint(2, o.height-10)) def draw_net(o): @@ -37,6 +37,7 @@ def draw_net(o): o.draw_line(0, s, o.width-1, s) s += 10 + lcd_nju = NJU6450(122, 32, GPIO()) lcd_nju.init() lcd_nju.auto_flush = False diff --git a/gfxlcd/demos/ssd.py b/gfxlcd/demos/ssd.py index bc8c230..1ccd70d 100644 --- a/gfxlcd/demos/ssd.py +++ b/gfxlcd/demos/ssd.py @@ -3,7 +3,6 @@ from driver.ssd1306.spi import SPI from driver.ssd1306.ssd1306 import SSD1306 - def hole(x, y): o.draw_pixel(x+1, y) o.draw_pixel(x+2, y) @@ -18,13 +17,15 @@ def hole(x, y): o.draw_pixel(x, y + 3) o.draw_pixel(x+4, y + 3) + drv = SPI() o = SSD1306(128, 64, drv) o.init() o.auto_flush = False for _ in range(0, 50): - hole(random.randint(2,120), random.randint(2,56)) + hole(random.randint(2, 120), random.randint(2, 56)) + hole(10, 10) hole(15, 13) hole(18, 23) @@ -87,4 +88,3 @@ o.flush(True) # o.draw_pixels(46, 9, 25) # o.draw_pixels(47, 9, 5) # o.draw_pixels(48, 9, 3) - diff --git a/gfxlcd/demos/ssd_2.py b/gfxlcd/demos/ssd_2.py index a5faba5..e01ca28 100644 --- a/gfxlcd/demos/ssd_2.py +++ b/gfxlcd/demos/ssd_2.py @@ -24,7 +24,7 @@ def hole(o, x, y): def draw_points(o): for _ in range(0, 50): - hole(o, random.randint(2,o.width - 10), random.randint(2,o.height-10)) + hole(o, random.randint(2, o.width- 10), random.randint(2, o.height-10)) def draw_net(o): @@ -37,6 +37,7 @@ def draw_net(o): o.draw_line(0, s, o.width-1, s) s += 10 + lcd_oled = SSD1306(128, 64, SPI()) lcd_oled.init() lcd_oled.auto_flush = False diff --git a/gfxlcd/drawing/area.py b/gfxlcd/drawing/area.py index 4bdb7a1..7c0127f 100644 --- a/gfxlcd/drawing/area.py +++ b/gfxlcd/drawing/area.py @@ -1,41 +1,49 @@ +"""Area drawing algorithm""" import itertools from gfxlcd.drawing.pixel import Pixel class Area(Pixel): - """Page drawing algorithm""" - def __init__(self): + """Area drawing algorithm""" + def __init__(self, driver): + self.driver = driver Pixel.__init__(self) def init(self): + """additional initialization""" pass - def draw_pixel(self, x, y): + def draw_pixel(self, pos_x, pos_y): """draw one pixel""" - self._set_area(x, y, x, y) + 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): """select area to work with""" - self.driver.cmd_data(0x0020, x1) - self.driver.cmd_data(0x0021, y1) - self.driver.cmd_data(0x0050, x1) - self.driver.cmd_data(0x0052, y1) - self.driver.cmd_data(0x0051, x2) - self.driver.cmd_data(0x0053, y2) + self.driver.cmd(0x0020, None) + self.driver.data(x1, None) + self.driver.cmd(0x0021, None) + self.driver.data(y1, None) + self.driver.cmd(0x0050, None) + self.driver.data(x1, None) + self.driver.cmd(0x0052, None) + self.driver.data(y1, None) + self.driver.cmd(0x0051, None) + self.driver.data(x2, None) + self.driver.cmd(0x0053, None) + self.driver.data(y2, None) self.driver.cmd(0x0022, None) - - def _draw_vertical_line(self, x, y, length): + def _draw_vertical_line(self, pos_x, pos_y, length): """draw vertical line""" - self._set_area(x, y, x, y + length) + self._set_area(pos_x, pos_y, pos_x, pos_y + length) color = self._converted_color() for _ in itertools.repeat(None, length): self.driver.data(color, None) - def _draw_horizontal_line(self, x, y, length): + def _draw_horizontal_line(self, pos_x, pos_y, length): """draw horizontal line""" - self._set_area(x, y, x + length, y) + self._set_area(pos_x, pos_y, pos_x + length, pos_y) color = self._converted_color() for _ in itertools.repeat(None, length): self.driver.data(color, None) @@ -83,23 +91,23 @@ class Area(Pixel): 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: self._draw_horizontal_line( - int(x1 + dx), + int(x1 + delta_x), int(y1 + (idx * offset_y)), int(step) ) - dx += step * offset_x + delta_x += step * offset_x else: self._draw_vertical_line( int(x1 + (idx * offset_x)), - int(y1 + dy), + int(y1 + delta_y), int(step) ) - dy += step * offset_y + delta_y += step * offset_y def fill_rect(self, x1, y1, x2, y2): """fill an area""" diff --git a/gfxlcd/driver/ili9325/gpio.py b/gfxlcd/driver/ili9325/gpio.py index afdf757..fa733d7 100644 --- a/gfxlcd/driver/ili9325/gpio.py +++ b/gfxlcd/driver/ili9325/gpio.py @@ -1,11 +1,12 @@ +"""GPIO communication driver""" import time -import RPi.GPIO +import RPi.GPIO # pylint: disable=I0011,F0401 from gfxlcd.abstract.driver import Driver - RPi.GPIO.setmode(RPi.GPIO.BCM) class GPIO(Driver): + """GPIO communication driver""" def __init__(self): self.pins = { 'RS': 27, @@ -46,10 +47,10 @@ class GPIO(Driver): RPi.GPIO.output(self.pins[pin], value) bits >>= 1 - def cmd(self, char, enable): + def cmd(self, data, enable): """send command to display""" RPi.GPIO.output(self.pins['RS'], 0) - self.send(char, enable) + self.send(data, enable) def send(self, char, enable): """send 16bit as 2*8bit""" @@ -64,9 +65,3 @@ class GPIO(Driver): """send data to display""" RPi.GPIO.output(self.pins['RS'], 1) self.send(data, enable) - - def cmd_data(self, cmd, data): - RPi.GPIO.output(self.pins['RS'], 0) - self.send(cmd, None) - RPi.GPIO.output(self.pins['RS'], 1) - self.send(data, None) \ No newline at end of file diff --git a/gfxlcd/driver/ili9325/ili9325.py b/gfxlcd/driver/ili9325/ili9325.py index 8282c8a..b260f01 100644 --- a/gfxlcd/driver/ili9325/ili9325.py +++ b/gfxlcd/driver/ili9325/ili9325.py @@ -1,27 +1,30 @@ +"""ILI9325 chip driver""" import time from gfxlcd.drawing.area import Area from gfxlcd.abstract.chip import Chip class ILI9325(Area, Chip): - """CLass for ILI9325 based LCD""" + """Class for ILI9325 based LCD""" def __init__(self, width, height, driver): Chip.__init__(self, width, height, driver, True) - Area.__init__(self) + Area.__init__(self, driver) def _converted_background_color(self): """color from 8-8-8 to 5-6-5""" rgb = self.options['background_color']['R'] << 16 | \ - self.options['background_color']['G'] << 8 | \ - self.options['background_color']['B'] - return ((rgb & 0x00f80000) >> 8) | ((rgb & 0x0000fc00) >> 5) | ((rgb & 0x000000f8) >> 3) + self.options['background_color']['G'] << 8 | \ + self.options['background_color']['B'] + return ((rgb & 0x00f80000) >> 8) |\ + ((rgb & 0x0000fc00) >> 5) | ((rgb & 0x000000f8) >> 3) def _converted_color(self): """color from 8-8-8 to 5-6-5""" rgb = self.options['color']['R'] << 16 | \ - self.options['color']['G'] << 8 | \ - self.options['color']['B'] - return ((rgb & 0x00f80000) >> 8) | ((rgb & 0x0000fc00) >> 5) | ((rgb & 0x000000f8) >> 3) + self.options['color']['G'] << 8 | \ + self.options['color']['B'] + return ((rgb & 0x00f80000) >> 8) |\ + ((rgb & 0x0000fc00) >> 5) | ((rgb & 0x000000f8) >> 3) def init(self): """init display""" diff --git a/gfxlcd/driver/nju6450/gpio.py b/gfxlcd/driver/nju6450/gpio.py index 7f1a27a..56633e2 100644 --- a/gfxlcd/driver/nju6450/gpio.py +++ b/gfxlcd/driver/nju6450/gpio.py @@ -1,5 +1,5 @@ import time -import RPi.GPIO +import RPi.GPIO # pylint: disable=I0011,F0401 from gfxlcd.abstract.driver import Driver RPi.GPIO.setmode(RPi.GPIO.BCM) @@ -44,15 +44,15 @@ class GPIO(Driver): RPi.GPIO.output(self.pins['E1'], 1) RPi.GPIO.output(self.pins['E2'], 1) - def cmd(self, char, enable): + def cmd(self, data, enable): """send command""" RPi.GPIO.output(self.pins['A0'], 0) - self.send(char, enable) + self.send(data, enable) - def data(self, char, enable): + def data(self, data, enable): """send data""" RPi.GPIO.output(self.pins['A0'], 1) - self.send(char, enable) + self.send(data, enable) def send(self, data, enable): """Write to gpio""" @@ -66,4 +66,4 @@ class GPIO(Driver): RPi.GPIO.output(self.pins['E'+str(enable+1)], 1) time.sleep(0.00025) RPi.GPIO.output(self.pins['E1'], 0) - RPi.GPIO.output(self.pins['E2'], 0) \ No newline at end of file + RPi.GPIO.output(self.pins['E2'], 0) diff --git a/gfxlcd/driver/nju6450/nju6450.py b/gfxlcd/driver/nju6450/nju6450.py index cb8128e..6395d19 100644 --- a/gfxlcd/driver/nju6450/nju6450.py +++ b/gfxlcd/driver/nju6450/nju6450.py @@ -1,3 +1,4 @@ +"""NJU6450 chip""" from gfxlcd.drawing.page import Page from gfxlcd.abstract.chip import Chip @@ -20,14 +21,14 @@ class NJU6450(Page, Chip): self.driver.cmd(cmd, 0) self.driver.cmd(cmd, 1) - def set_xy(self, x, y): + def set_xy(self, pos_x, pos_y): """set xy pos""" if x < self.width/2: - self.driver.cmd(0xB8 | y, 0) - self.driver.cmd(0x00 | x, 0) + self.driver.cmd(0xB8 | pos_y, 0) + self.driver.cmd(0x00 | pos_x, 0) else: - self.driver.cmd(0xB8 | y, 1) - self.driver.cmd(0x00 | (x - self.width//2), 1) + self.driver.cmd(0xB8 | pos_y, 1) + self.driver.cmd(0x00 | (pos_x - self.width//2), 1) def _converted_background_color(self): """convert RGB background to available color""" @@ -50,4 +51,4 @@ class NJU6450(Page, Chip): if i < self.width/2: self.driver.data(self.get_page_value(i, j), 0) else: - self.driver.data(self.get_page_value(i, j), 1) \ No newline at end of file + self.driver.data(self.get_page_value(i, j), 1) diff --git a/gfxlcd/driver/ssd1306/spi.py b/gfxlcd/driver/ssd1306/spi.py index 38e8e2c..c4d6f6e 100644 --- a/gfxlcd/driver/ssd1306/spi.py +++ b/gfxlcd/driver/ssd1306/spi.py @@ -1,13 +1,13 @@ -import spidev -import RPi.GPIO as GPIO - - -GPIO.setmode(GPIO.BCM) +"""SPI+GPIO connection driver for SSD1306""" +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) class SPI(Driver): + """SPI driver""" def __init__(self): self.pins = { 'RST': 13, @@ -22,7 +22,7 @@ class SPI(Driver): GPIO.output(self.pins[pin], 0) spi = spidev.SpiDev() - spi.open(0,0) + spi.open(0, 0) spi.max_speed_hz = 8000000 spi.mode = 0 self.spi = spi @@ -46,4 +46,3 @@ class SPI(Driver): GPIO.output(self.pins['DC'], 1) self.spi.xfer2([data]) GPIO.output(self.pins['DC'], 0) - diff --git a/gfxlcd/driver/ssd1306/ssd1306.py b/gfxlcd/driver/ssd1306/ssd1306.py index 52574f9..942abae 100644 --- a/gfxlcd/driver/ssd1306/ssd1306.py +++ b/gfxlcd/driver/ssd1306/ssd1306.py @@ -1,3 +1,4 @@ +"""SSD1306 chip driver""" from gfxlcd.drawing.page import Page from gfxlcd.abstract.chip import Chip @@ -25,7 +26,7 @@ class SSD1306(Page, Chip): self.driver.cmd(0xb0) # set page address self.driver.cmd(0x81) # set contrast control register self.driver.cmd(0xff) - self.driver.cmd(0xa1) # a0/a1 set segment re-map 127 to 0 a0:0 to seg127 + self.driver.cmd(0xa1) # a0/a1, a1 = segment 127 to 0, a0:0 to seg127 self.driver.cmd(0xc8) # c8/c0 set com(N-1)to com0 c0:com0 to com(N-1) self.driver.cmd(0xa6) # set normal display, a6 - normal, a7 - inverted @@ -70,11 +71,11 @@ class SSD1306(Page, Chip): for i in range(0, self.width): self.driver.data(self.get_page_value(i, j)) - def set_area(self, x1, y1, x2, y2): + def set_area(self, pos_x1, pos_y1, pos_x2, pos_y2): """set area to work on""" self.driver.cmd(0x22) - self.driver.cmd(0xb0 + y1) - self.driver.cmd(0xb0 + y2) + self.driver.cmd(0xb0 + pos_y1) + self.driver.cmd(0xb0 + pos_y2) self.driver.cmd(0x21) - self.driver.cmd(x1) - self.driver.cmd(x2) + self.driver.cmd(pos_x1) + self.driver.cmd(pos_x2)