Merge pull request #12 from bkosciow/rotate

Rotate
This commit is contained in:
Bartosz 2017-06-05 21:43:58 +02:00 committed by GitHub
commit 7f9b7c9d8f
10 changed files with 202 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 396 B

26
gfxlcd/demos/nju_3.py Normal file
View File

@ -0,0 +1,26 @@
import RPi.GPIO
import sys
sys.path.append("../../")
from gfxlcd.driver.nju6450.gpio import GPIO
from gfxlcd.driver.nju6450.nju6450 import NJU6450
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd = NJU6450(122, 32, GPIO())
lcd.rotation = 270
lcd.init()
lcd.auto_flush = False
x, y = lcd.width // 2, lcd.height // 2
lcd.draw_pixel(3,1)
lcd.draw_circle(x, y, 15)
lcd.draw_circle(x-7, y-5, 3)
lcd.draw_circle(x+7, y-5, 3)
lcd.draw_arc(x, y, 10, 45, 135)
lcd.draw_line(x, y-3, x-3, y+2)
lcd.draw_line(x, y-3, x+3, y+2)
lcd.draw_arc(x, y, 3, 45, 135)
lcd.fill_rect(0, 0, 5, 10)
lcd.flush(True)

View File

@ -0,0 +1,19 @@
import RPi.GPIO
import sys
from PIL import Image
sys.path.append("../../")
from gfxlcd.driver.nju6450.gpio import GPIO
from gfxlcd.driver.nju6450.nju6450 import NJU6450
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd_nju = NJU6450(122, 32, GPIO())
lcd_nju.rotation = 90
lcd_nju.init()
lcd_nju.auto_flush = False
image_file = Image.open("assets/20x20.png")
lcd_nju.threshold = 0
lcd_nju.draw_image(10, 0, image_file)
lcd_nju.flush(True)

25
gfxlcd/demos/ssd_3.py Normal file
View File

@ -0,0 +1,25 @@
import RPi.GPIO
import sys
import random
sys.path.append("../../")
from gfxlcd.driver.ssd1306.spi import SPI
from gfxlcd.driver.ssd1306.ssd1306 import SSD1306
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd_oled = SSD1306(128, 64, SPI())
lcd_oled.rotation = 90
lcd_oled.init()
lcd_oled.auto_flush = False
x, y = lcd_oled.width // 2, lcd_oled.height // 2
lcd_oled.draw_circle(x, y, 31)
lcd_oled.draw_circle(x-12, y-10, 7)
lcd_oled.draw_circle(x+12, y-10, 7)
lcd_oled.draw_arc(x, y, 20, 45, 135)
lcd_oled.draw_line(x, y-5, x-4, y+6)
lcd_oled.draw_line(x, y-5, x+4, y+6)
lcd_oled.draw_arc(x, y+3, 5, 45, 135)
lcd_oled.fill_rect(0, 0, 10, 10)
lcd_oled.flush(True)

View File

@ -0,0 +1,19 @@
import RPi.GPIO
import sys
from PIL import Image
sys.path.append("../../")
from gfxlcd.driver.ssd1306.spi import SPI
from gfxlcd.driver.ssd1306.ssd1306 import SSD1306
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd_oled = SSD1306(128, 64, SPI())
lcd_oled.rotation = 270
lcd_oled.init()
lcd_oled.auto_flush = False
image_file = Image.open("assets/20x20.png")
lcd_oled.threshold = 0
lcd_oled.draw_image(10, 0, image_file)
lcd_oled.flush(True)

View File

@ -11,7 +11,10 @@ class Page(Pixel, metaclass=abc.ABCMeta):
def init(self):
"""init page"""
if self.rotation == 0 or self.rotation == 180:
self.buffer = [[0] * (self.height // 8) for x in range(self.width)]
else:
self.buffer = [[0] * (self.width // 8) for x in range(self.height)]
def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y"""

View File

@ -6,6 +6,7 @@ class Pixel(object):
"""Pixel class"""
def __init__(self, driver):
self.driver = driver
self.rotation = 0
self.options['color'] = {
'R': 255, 'G': 255, 'B': 255
}
@ -36,11 +37,11 @@ class Pixel(object):
self.options['transparency_color'] = transparency_color
def draw_pixel(self, pos_x, pos_y):
"""dummy fuction"""
"""dummy function"""
pass
def draw_line(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""dummy fuction"""
"""dummy function"""
pass
def draw_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):

View File

@ -24,12 +24,16 @@ class NJU6450(Page, Chip):
def set_xy(self, pos_x, pos_y):
"""set xy pos"""
if pos_x < self.width/2:
if self.rotation == 0 or self.rotation == 180:
width = self.width
else:
width = self.height
if pos_x < width//2:
self.driver.cmd(0xB8 | pos_y, 0)
self.driver.cmd(0x00 | pos_x, 0)
else:
self.driver.cmd(0xB8 | pos_y, 1)
self.driver.cmd(0x00 | (pos_x - self.width//2), 1)
self.driver.cmd(0x00 | (pos_x - width//2), 1)
def _converted_background_color(self):
"""convert RGB background to available color"""
@ -46,10 +50,37 @@ class NJU6450(Page, Chip):
force = self.options['auto_flush']
if force:
for j in range(0, self.height//8):
for i in range(0, self.width):
if self.rotation == 0 or self.rotation == 180:
height, width = self.height, self.width
else:
width, height = self.height, self.width
for j in range(0, height//8):
for i in range(0, width):
self.set_xy(i, j)
if i < self.width/2:
if i < width//2:
self.driver.data(self.get_page_value(i, j), 0)
else:
self.driver.data(self.get_page_value(i, j), 1)
def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y"""
if self.rotation == 90:
pos_x, pos_y = self.height - pos_y - 1, pos_x
if self.rotation == 180:
pos_x, pos_y = self.width - pos_x - 1, self.height - pos_y - 1
if self.rotation == 270:
pos_x, pos_y = pos_y, self.width - pos_x - 1
Page.draw_pixel(self, pos_x, pos_y)
def fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw a filled rectangle"""
if self.rotation == 90:
pos_x1, pos_y1 = self.height - pos_y1 - 1, pos_x1
pos_x2, pos_y2 = self.height - pos_y2 - 1, pos_x2
if self.rotation == 180:
pos_x1, pos_y1 = self.width - pos_x1 - 1, self.height - pos_y1 - 1
pos_x2, pos_y2 = self.width - pos_x2 - 1, self.height - pos_y2 - 1
if self.rotation == 270:
pos_x1, pos_y1 = pos_y1 , self.width - pos_x1 - 1
pos_x2, pos_y2 = pos_y2 , self.width - pos_x2 - 1
Page.fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2)

View File

@ -5,6 +5,25 @@ from gfxlcd.abstract.chip import Chip
class SSD1306(Page, Chip):
"""Class for an LCD with SSD306 chip"""
rotations = {
0: {
'sgmt': 0xa1,
'com': 0xc8
},
90: {
'sgmt': 0xa0,
'com': 0xc8
},
180: {
'sgmt': 0xa0,
'com': 0xc0
},
270: {
'sgmt': 0xa1,
'com': 0xc0
}
}
def __init__(self, width, height, driver, auto_flush=True):
Chip.__init__(self, width, height, driver, auto_flush)
Page.__init__(self, driver)
@ -13,7 +32,7 @@ class SSD1306(Page, Chip):
def init(self):
"""inits a device"""
self.driver.init()
Page.init(self)
Page.init(self)#, self.rotate)
Chip.init(self)
self.driver.reset()
self.driver.cmd(0xae) # turn off panel
@ -27,8 +46,12 @@ 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, 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)
# a0/a1, a1 = segment 127 to 0, a0:0 to seg127
self.driver.cmd(self.rotations[self.rotation]['sgmt'])
# c8/c0 set com(N-1)to com0 c0:com0 to com(N-1)
self.driver.cmd(self.rotations[self.rotation]['com'])
self.driver.cmd(0xa6) # set normal display, a6 - normal, a7 - inverted
self.driver.cmd(0xa8) # set multiplex ratio(16to63)
@ -66,11 +89,14 @@ class SSD1306(Page, Chip):
:force - boolean|None"""
if force is None:
force = self.options['auto_flush']
if force:
for j in range(0, self.height//8):
self.set_area(0, j, self.width-1, j+1)
for i in range(0, self.width):
if self.rotation == 0 or self.rotation == 180:
height, width = self.height, self.width
else:
width, height = self.height, self.width
for j in range(0, height//8):
self.set_area(0, j, width-1, j+1)
for i in range(0, width):
self.driver.data(self.get_page_value(i, j))
def set_area(self, pos_x1, pos_y1, pos_x2, pos_y2):
@ -81,3 +107,9 @@ class SSD1306(Page, Chip):
self.driver.cmd(0x21)
self.driver.cmd(pos_x1)
self.driver.cmd(pos_x2)
def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y"""
if self.rotation == 90 or self.rotation == 270:
pos_x, pos_y = pos_y, pos_x
Page.draw_pixel(self, pos_x, pos_y)

31
gfxlcd/tests/test_chip.py Normal file
View File

@ -0,0 +1,31 @@
__author__ = 'kosci'
import sys
from nose.tools import assert_equal
sys.path.append("../../")
from gfxlcd.driver.null.null_page import NullPage
class TestChip(object):
def setUp(self):
self.lcd = NullPage(10, 16, None, False)
def test_rotate_by_0(self):
self.lcd.rotation = 0
assert_equal(10, self.lcd.width)
assert_equal(16, self.lcd.height)
def test_rotate_by_90(self):
self.lcd.rotation = 90
assert_equal(16, self.lcd.width)
assert_equal(10, self.lcd.height)
def test_rotate_by_180(self):
self.lcd.rotation = 180
assert_equal(10, self.lcd.width)
assert_equal(16, self.lcd.height)
def test_rotate_by_270(self):
self.lcd.rotation = 270
assert_equal(16, self.lcd.width)
assert_equal(10, self.lcd.height)