From 968780ef376502ae191d88c215c65d6ca4901aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Sun, 2 Jul 2017 20:56:32 +0200 Subject: [PATCH 1/3] driver for hd44780 and CharLCD --- gfxlcd/demos/ili9325_hd44780.py | 36 ++++++++++++++++ gfxlcd/driver/hd44780.py | 73 +++++++++++++++++++++++++++++++++ gfxlcd/tests/test_hd44780.py | 42 +++++++++++++++++++ setup.py | 2 +- 4 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 gfxlcd/demos/ili9325_hd44780.py create mode 100644 gfxlcd/driver/hd44780.py create mode 100644 gfxlcd/tests/test_hd44780.py diff --git a/gfxlcd/demos/ili9325_hd44780.py b/gfxlcd/demos/ili9325_hd44780.py new file mode 100644 index 0000000..99bf905 --- /dev/null +++ b/gfxlcd/demos/ili9325_hd44780.py @@ -0,0 +1,36 @@ +import sys +sys.path.append("../../") +import RPi.GPIO as GPIO # NOQA pylint: disable=I0011,F0401 +from charlcd.buffered import CharLCD # NOQA +from gfxlcd.driver.ili9325.gpio import GPIO as ILIGPIO +from gfxlcd.driver.ili9325.ili9325 import ILI9325 +from gfxlcd.driver.hd44780 import HD44780 +GPIO.setmode(GPIO.BCM) + + +def test1(): + """demo """ + ili_drv = ILIGPIO() + ili_drv.pins['LED'] = 6 + ili_drv.pins['CS'] = 18 + lcd = ILI9325(240, 320, ili_drv) + lcd.auto_flush = False + lcd.rotation = 0 + + drv = HD44780(lcd) + lcd = CharLCD(drv.width, drv.height, drv, 0, 0) + lcd.init() + + lcd.write('-!Second blarg!') + lcd.write("-second line", 0, 1) + lcd.flush() + + lcd.write('/* ', 19, 0) + lcd.write('|*|', 19, 1) + lcd.write(' */', 19, 2) + lcd.flush() + + lcd.write('BUM', 19, 5) + lcd.flush() + +test1() diff --git a/gfxlcd/driver/hd44780.py b/gfxlcd/driver/hd44780.py new file mode 100644 index 0000000..1599a31 --- /dev/null +++ b/gfxlcd/driver/hd44780.py @@ -0,0 +1,73 @@ +"""Driver for CharLCD module +allows graphical LCD to work as character LCD +""" +from charlcd.drivers.base import BaseDriver +from charlcd.abstract import lcd as char_lcd + + +class HD44780(BaseDriver): + def __init__(self, gfxlcd): + """Class init""" + self.gfxlcd = gfxlcd + self.mode = 0 + self.initialized = False + self.font = self.gfxlcd.options['font'] + self.width = self.gfxlcd.width // self.font.size[0] + self.height = self.gfxlcd.height // self.font.size[1] + self.pins = { + 'E2': None + } + self.position = { + 'x': 0, + 'y': 0 + } + + def init(self): + """init function""" + if self.initialized: + return + char_lcd.LCD_LINES = [] + for address in range(self.height): + char_lcd.LCD_LINES.append(100 + (address * self.width)) + + self.gfxlcd.init() + self.initialized = True + + def cmd(self, char, enable=0): + """write command - set cursor position""" + if char < 100: + return + char -= 100 + y = char // self.width + x = char - (y*self.width) + self.position = { + 'x': x * self.font.size[0], + 'y': y * self.font.size[1] + } + + def shutdown(self): + """shutdown procedure""" + pass + + def send(self, enable=0): + """send ack command""" + pass + + def write(self, char, enable=0): + """write data to lcd""" + pass + + def char(self, char, enable=0): + """write char to lcd""" + self.gfxlcd.draw_text( + self.position['x'], self.position['y'], char + ) + self._increase_x() + + def set_mode(self, mode): + """sets driver mode. 4/8 bit""" + self.mode = mode + + def _increase_x(self): + self.position['x'] += self.font.size[0] + diff --git a/gfxlcd/tests/test_hd44780.py b/gfxlcd/tests/test_hd44780.py new file mode 100644 index 0000000..84ac825 --- /dev/null +++ b/gfxlcd/tests/test_hd44780.py @@ -0,0 +1,42 @@ +__author__ = 'kosci' + +import sys +from nose.tools import assert_equal +sys.path.append("../../") +from gfxlcd.driver.null.null_page import NullPage +from gfxlcd.driver.hd44780 import HD44780 +from charlcd.buffered import CharLCD + + +class TestChip(object): + def setUp(self): + self.gfx_lcd = NullPage(132, 16, None, False) + self.drv = HD44780(self.gfx_lcd) + self.output = [" ".ljust(16, " ") for i in range(0, 2)] + + def get_lcd(self): + lcd = CharLCD(self.drv.width, self.drv.height, self.drv, 0, 0) + lcd.init() + return lcd + + def test_get_size_in_chars(self): + assert_equal(16, self.drv.width) + assert_equal(2, self.drv.height) + + def test_init_small_hd44780(self): + lcd = CharLCD(self.drv.width, self.drv.height, self.drv, 0, 0) + lcd.init() + + def test_write_to_buffer(self): + lcd = self.get_lcd() + lcd.write('Hello') + lcd.write(' world', 0, 1) + self.output[0] = "Hello" + " ".ljust(11, " ") + self.output[1] = " world" + " ".ljust(6, " ") + assert_equal(lcd.buffer, self.output) + + def test_flush(self): + lcd = self.get_lcd() + lcd.write('Hello') + lcd.write(' world', 0, 1) + lcd.flush() diff --git a/setup.py b/setup.py index 52f6792..8f46c71 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def read(*paths): setup( name='gfxlcd', - version='0.6.0', + version='0.7.0', description='gfxlcd is a handler for graphical lcds: ILI9328, SSD1306, NJU6450, touch panel: AD7843 @ Raspberry Pi.', keywords=[ 'gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd', 'touch panel', 'ad7843', From 30f18a685ac299ac5b426c498020b58a2530a79a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Mon, 3 Jul 2017 20:37:33 +0200 Subject: [PATCH 2/3] docs --- CHANGELOG.txt | 2 ++ readme.md | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 06eafef..3f676ee 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,5 @@ +0.8.0 + - HD44780 emulation via driver 0.7.0 - add draw_text function - add font 8x8 as default diff --git a/readme.md b/readme.md index c575843..8b892ab 100644 --- a/readme.md +++ b/readme.md @@ -15,12 +15,18 @@ And for touch panels: - ad7843 via SPI, uses irq or not - ad7846/xpt2046 +Bonus + +- HD44780 emulation (works with CharLCD) + + On NJU and SSD uses buffer to keep current content as help for page operations. Wiring is below Demos are in demos directory + LCD initialization === ## SSD1306 @@ -269,3 +275,23 @@ Default: G9 ----------------- LCD_CS G7 ----------------- TP_CS + +HD44780 emulation +=== + +This driver can work with CharLCD and emulate char LCD + + ili_drv = ILIGPIO() + ili_drv.pins['LED'] = 6 + ili_drv.pins['CS'] = 18 + lcd = ILI9325(240, 320, ili_drv) + lcd.auto_flush = False + lcd.rotation = 0 + + drv = HD44780(lcd) + lcd = CharLCD(drv.width, drv.height, drv, 0, 0) + lcd.init() + + lcd.write('-!Second blarg!') + lcd.write("-second line", 0, 1) + lcd.flush() From 8d0b02cdf10fd2fa9d48b2bd840b8892ec9560f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Ko=C5=9Bci=C3=B3w?= Date: Mon, 3 Jul 2017 20:43:48 +0200 Subject: [PATCH 3/3] cfg --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8f46c71..5cf32cc 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def read(*paths): setup( name='gfxlcd', - version='0.7.0', + version='0.8.0', description='gfxlcd is a handler for graphical lcds: ILI9328, SSD1306, NJU6450, touch panel: AD7843 @ Raspberry Pi.', keywords=[ 'gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd', 'touch panel', 'ad7843',