refactor & clean up

This commit is contained in:
Bartosz Kościów 2017-07-09 14:32:53 +02:00
parent d833436bf0
commit d085883ab7
14 changed files with 75 additions and 64 deletions

View File

@ -19,7 +19,6 @@ class Chip(metaclass=abc.ABCMeta):
"""get width""" """get width"""
if self.rotation == 0 or self.rotation == 180: if self.rotation == 0 or self.rotation == 180:
return self._width return self._width
else:
return self._height return self._height
@property @property
@ -27,7 +26,6 @@ class Chip(metaclass=abc.ABCMeta):
"""get height""" """get height"""
if self.rotation == 0 or self.rotation == 180: if self.rotation == 0 or self.rotation == 180:
return self._height return self._height
else:
return self._width return self._width
@abc.abstractmethod @abc.abstractmethod

View File

@ -3,6 +3,7 @@ import abc
class Font(metaclass=abc.ABCMeta): class Font(metaclass=abc.ABCMeta):
"""Font abstract"""
font = [] # Dictionary with hex that describe each char font = [] # Dictionary with hex that describe each char
size = (0, 0) size = (0, 0)
@ -12,8 +13,3 @@ class Font(metaclass=abc.ABCMeta):
def get(self, letter): def get(self, letter):
"""return array with letter""" """return array with letter"""
return self.font[ord(letter)] return self.font[ord(letter)]
@property
def size(self):
"""get font size"""
return self.size

View File

@ -1,12 +1,13 @@
"""Touch panel interface""" """Touch panel interface"""
import spidev # pylint: disable=I0011,F0401
import abc import abc
import RPi.GPIO import spidev # pylint: disable=I0011,F0401
import RPi.GPIO # pylint: disable=I0011,F0401
class Touch(metaclass=abc.ABCMeta): class Touch(metaclass=abc.ABCMeta):
"""Touch class""" """Touch class"""
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): def __init__(self, width, height, int_pin=None,
callback=None, cs_pin=None, spi=0, speed=1000000):
self.width = width self.width = width
self.height = height self.height = height
self.spi = spidev.SpiDev() self.spi = spidev.SpiDev()
@ -30,7 +31,8 @@ class Touch(metaclass=abc.ABCMeta):
if self.int_pin: if self.int_pin:
RPi.GPIO.setup(self.int_pin, RPi.GPIO.IN) RPi.GPIO.setup(self.int_pin, RPi.GPIO.IN)
RPi.GPIO.add_event_detect( RPi.GPIO.add_event_detect(
self.int_pin, RPi.GPIO.BOTH, callback=self._interrupt, bouncetime=self.bouncetime self.int_pin, RPi.GPIO.BOTH, callback=self._interrupt,
bouncetime=self.bouncetime
) )
if self.cs_pin: if self.cs_pin:
RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT) RPi.GPIO.setup(self.cs_pin, RPi.GPIO.OUT)
@ -53,7 +55,7 @@ class Touch(metaclass=abc.ABCMeta):
def _calculate_avr(self, points): def _calculate_avr(self, points):
"""calculate x,y by average""" """calculate x,y by average"""
if len(points) == 0: if not points:
return None return None
sum_x = 0 sum_x = 0
sum_y = 0 sum_y = 0
@ -67,5 +69,4 @@ class Touch(metaclass=abc.ABCMeta):
"""checks if point is in range""" """checks if point is in range"""
if self.rotate == 0 or self.rotate == 180: if self.rotate == 0 or self.rotate == 180:
return 0 <= pos_x <= self.width and 0 <= pos_y <= self.height return 0 <= pos_x <= self.width and 0 <= pos_y <= self.height
else:
return 0 <= pos_y <= self.width and 0 <= pos_x <= self.height return 0 <= pos_y <= self.width and 0 <= pos_x <= self.height

View File

@ -1,3 +1,4 @@
"""Page drawing algorithm"""
import abc import abc
from gfxlcd.drawing.pixel import Pixel from gfxlcd.drawing.pixel import Pixel

View File

@ -160,4 +160,3 @@ class Pixel(object):
pos_x + bit, pos_y + row, self.background_color pos_x + bit, pos_y + row, self.background_color
) )
data >>= 1 data >>= 1

View File

@ -1,10 +1,12 @@
import RPi.GPIO """Driver for AD7843 touch panel"""
import RPi.GPIO # pylint: disable=I0011,F0401
from gfxlcd.abstract.touch import Touch from gfxlcd.abstract.touch import Touch
class AD7843(Touch): class AD7843(Touch):
"""AD7843 class""" """AD7843 class"""
def __init__(self, width, height, int_pin=None, callback=None, cs_pin=None, spi=0, speed=1000000): def __init__(self, width, height, int_pin=None,
callback=None, cs_pin=None, spi=0, speed=1000000):
super().__init__(width, height, int_pin, callback, cs_pin, spi, speed) super().__init__(width, height, int_pin, callback, cs_pin, spi, speed)
self.correction = { self.correction = {
'x': 364, 'x': 364,
@ -16,20 +18,32 @@ class AD7843(Touch):
def _get_xy(self, offset_x, offset_y): def _get_xy(self, offset_x, offset_y):
"""correct x and y""" """correct x and y"""
if self.rotate == 0: if self.rotate == 0:
return int((offset_x - self.correction['x']) / self.correction['ratio_x']), \ return int(
int((offset_y - self.correction['y']) / self.correction['ratio_y']) (offset_x - self.correction['x']) / self.correction['ratio_x']
), int(
(offset_y - self.correction['y']) / self.correction['ratio_y']
)
if self.rotate == 90: if self.rotate == 90:
return self.height - int((offset_y - self.correction['y']) / self.correction['ratio_y']), \ return self.height - int(
int((offset_x - self.correction['x']) / self.correction['ratio_x']) (offset_y - self.correction['y']) / self.correction['ratio_y']
), int(
(offset_x - self.correction['x']) / self.correction['ratio_x']
)
if self.rotate == 180: if self.rotate == 180:
return self.width - int((offset_x - self.correction['x']) / self.correction['ratio_x']), \ return self.width - int(
self.height - int((offset_y - self.correction['y']) / self.correction['ratio_y']) (offset_x - self.correction['x']) / self.correction['ratio_x']
), self.height - int(
(offset_y - self.correction['y']) / self.correction['ratio_y']
)
if self.rotate == 270: if self.rotate == 270:
return int((offset_y - self.correction['y']) / self.correction['ratio_y']), \ return int(
self.width - int((offset_x - self.correction['x']) / self.correction['ratio_x']) (offset_y - self.correction['y']) / self.correction['ratio_y']
), self.width - int(
(offset_x - self.correction['x']) / self.correction['ratio_x']
)
def get_position(self): def get_position(self):
"""get touch coords""" """get touch coords"""

View File

@ -6,6 +6,7 @@ from charlcd.abstract.flush_event_interface import FlushEvent
class HD44780(BaseDriver, FlushEvent): class HD44780(BaseDriver, FlushEvent):
"""HD44780 driver for GfxLCD"""
def __init__(self, gfxlcd, lcd_flush=False): def __init__(self, gfxlcd, lcd_flush=False):
"""Class init""" """Class init"""
self.gfxlcd = gfxlcd self.gfxlcd = gfxlcd
@ -39,11 +40,11 @@ class HD44780(BaseDriver, FlushEvent):
if char < 100: if char < 100:
return return
char -= 100 char -= 100
y = char // self.width pos_y = char // self.width
x = char - (y*self.width) pos_x = char - (pos_y * self.width)
self.position = { self.position = {
'x': x * self.font.size[0], 'x': pos_x * self.font.size[0],
'y': y * self.font.size[1] 'y': pos_y * self.font.size[1]
} }
def shutdown(self): def shutdown(self):

View File

@ -1,5 +1,4 @@
"""ILI9486 chip driver""" """ILI9486 chip driver"""
import time
from gfxlcd.drawing.area import Area from gfxlcd.drawing.area import Area
from gfxlcd.abstract.chip import Chip from gfxlcd.abstract.chip import Chip

View File

@ -1,6 +1,6 @@
"""SPI communication driver""" """SPI communication driver"""
import time import time
import spidev import spidev # pylint: disable=I0011,F0401
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
RPi.GPIO.setmode(RPi.GPIO.BCM) RPi.GPIO.setmode(RPi.GPIO.BCM)

View File

@ -3,7 +3,6 @@ from gfxlcd.drawing.page import Page
from gfxlcd.abstract.chip import Chip from gfxlcd.abstract.chip import Chip
class NJU6450(Page, Chip): 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=False): def __init__(self, width, height, driver, auto_flush=False):

View File

@ -1,5 +1,4 @@
"""Area driver """ """Area driver """
import time
from gfxlcd.abstract.driver import Driver from gfxlcd.abstract.driver import Driver
@ -32,7 +31,9 @@ class AreaDriver(Driver):
def data(self, data, enable): def data(self, data, enable):
"""send data to display""" """send data to display"""
app_x, app_y = self.pointer app_x, app_y = self.pointer
self.buffer[self.area['start_x'] + app_x][self.area['start_y'] + app_y] = data self.buffer[
self.area['start_x'] + app_x][self.area['start_y'] + app_y
] = data
self._inc_pointer() self._inc_pointer()
def _inc_pointer(self): def _inc_pointer(self):
@ -47,4 +48,3 @@ class AreaDriver(Driver):
app_y = 0 app_y = 0
self.pointer = (app_x, app_y) self.pointer = (app_x, app_y)

View File

@ -1,4 +1,5 @@
import RPi.GPIO """XPT2046 touch pabel driver"""
import RPi.GPIO # pylint: disable=I0011,F0401
from gfxlcd.abstract.touch import Touch from gfxlcd.abstract.touch import Touch

View File

@ -1 +1,2 @@
__author__ = 'kosci' """font module"""
__author__ = 'Bartosz Kosciow'

View File

@ -1,3 +1,4 @@
"""font 8x8"""
from gfxlcd.abstract.font import Font from gfxlcd.abstract.font import Font