rotate nju

This commit is contained in:
Bartosz Kościów 2017-06-05 17:49:07 +02:00
parent 5c7fca57d7
commit 578ae67c44
6 changed files with 87 additions and 44 deletions

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)

View File

@ -6,41 +6,6 @@ from gfxlcd.driver.ssd1306.spi import SPI
from gfxlcd.driver.ssd1306.ssd1306 import SSD1306 from gfxlcd.driver.ssd1306.ssd1306 import SSD1306
RPi.GPIO.setmode(RPi.GPIO.BCM) RPi.GPIO.setmode(RPi.GPIO.BCM)
def hole(o, x, y):
o.draw_pixel(x+1, y)
o.draw_pixel(x+2, y)
o.draw_pixel(x+3, y)
o.draw_pixel(x+1, y + 4)
o.draw_pixel(x+2, y + 4)
o.draw_pixel(x+3, y + 4)
o.draw_pixel(x, y + 1)
o.draw_pixel(x+4, y + 1)
o.draw_pixel(x, y + 2)
o.draw_pixel(x+4, y + 2)
o.draw_pixel(x, y + 3)
o.draw_pixel(x+4, y + 3)
def draw_points(o):
for _ in range(0, 50):
hole(o,
random.randint(2, o.width - 10),
random.randint(2, o.height - 10)
)
def draw_net(o):
s = 0
while s < o.width-1:
o.draw_line(s, 0, s, o.height-1)
s += 10
s = 0
while s < o.height-1:
o.draw_line(0, s, o.width-1, s)
s += 10
lcd_oled = SSD1306(128, 64, SPI()) lcd_oled = SSD1306(128, 64, SPI())
lcd_oled.rotation = 90 lcd_oled.rotation = 90
lcd_oled.init() lcd_oled.init()

View File

@ -11,7 +11,6 @@ class Page(Pixel, metaclass=abc.ABCMeta):
def init(self): def init(self):
"""init page""" """init page"""
#self.rotate = rotate
if self.rotation == 0 or self.rotation == 180: if self.rotation == 0 or self.rotation == 180:
self.buffer = [[0] * (self.height // 8) for x in range(self.width)] self.buffer = [[0] * (self.height // 8) for x in range(self.width)]
else: else:
@ -19,8 +18,6 @@ class Page(Pixel, metaclass=abc.ABCMeta):
def draw_pixel(self, pos_x, pos_y): def draw_pixel(self, pos_x, pos_y):
"""draw a pixel at x,y""" """draw a pixel at x,y"""
if self.rotation == 90 or self.rotation == 270:
pos_x, pos_y = pos_y, pos_x
self.buffer[pos_x][pos_y//8] |= 1 << (pos_y % 8) self.buffer[pos_x][pos_y//8] |= 1 << (pos_y % 8)
self.flush() self.flush()

View File

@ -24,12 +24,16 @@ 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 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(0xB8 | pos_y, 0)
self.driver.cmd(0x00 | pos_x, 0) self.driver.cmd(0x00 | pos_x, 0)
else: else:
self.driver.cmd(0xB8 | pos_y, 1) 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): def _converted_background_color(self):
"""convert RGB background to available color""" """convert RGB background to available color"""
@ -46,10 +50,37 @@ class NJU6450(Page, Chip):
force = self.options['auto_flush'] force = self.options['auto_flush']
if force: if force:
for j in range(0, self.height//8): if self.rotation == 0 or self.rotation == 180:
for i in range(0, self.width): 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) self.set_xy(i, j)
if i < self.width/2: if i < width//2:
self.driver.data(self.get_page_value(i, j), 0) self.driver.data(self.get_page_value(i, j), 0)
else: else:
self.driver.data(self.get_page_value(i, j), 1) 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

@ -101,10 +101,15 @@ class SSD1306(Page, Chip):
def set_area(self, pos_x1, pos_y1, pos_x2, pos_y2): def set_area(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""set area to work on""" """set area to work on"""
self.driver.cmd(0x22) self.driver.cmd(0x22)
self.driver.cmd(0xb0 + pos_y1) self.driver.cmd(0xb0 + pos_y1)
self.driver.cmd(0xb0 + pos_y2) self.driver.cmd(0xb0 + pos_y2)
self.driver.cmd(0x21) self.driver.cmd(0x21)
self.driver.cmd(pos_x1) self.driver.cmd(pos_x1)
self.driver.cmd(pos_x2) 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)