From 0bdca2f212479b5d81a0aaed289d83cd911973be Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Mon, 30 Aug 2021 17:14:34 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client b/client index 6268505..495460e 160000 --- a/client +++ b/client @@ -1 +1 @@ -Subproject commit 6268505fcc031727afb3acf44ab76b7749c6c7f8 +Subproject commit 495460ec4ff2b2fb1d0124b9401e6d4285868f68 -- Gitee From 7a5d2dc397bdb09c62ee8c4d994424cb13054206 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Mon, 30 Aug 2021 17:25:21 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20run=5Ftest4=EF=BC=8C?= =?UTF-8?q?=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 8e01c20..a43ba21 100644 --- a/main.py +++ b/main.py @@ -142,6 +142,52 @@ class FontLibTest(object): diff_time = ticks_diff(ticks_us(), start_time) / 1000 print('### show {} chars: {} ms, avg: {} ms'.format(len(chars), diff_time, diff_time / len(chars))) + def run_test4(self, chars=None): + if self.__oled is None or chars is None: + return + + from math import ceil + chars = chars.replace('\t', '') + newline_count = chars.count('\n') + chars_count = len(chars) + chars_per_row = int(self.__oled_width / self.__fontlib.font_height) + chars_per_col = int(self.__oled_height / self.__fontlib.font_height) + total_rows = ceil(chars_count / chars_per_row) + newline_count + total_pages = ceil(total_rows / chars_per_col) + x = y = 0 + width = height = self.__fontlib.font_height + buffer_size = self.__oled_width // 8 * self.__oled_height + fb_foreground = framebuf.FrameBuffer(bytearray(buffer_size)) + fb_background = framebuf.FrameBuffer(bytearray(buffer_size)) + + def __fill_page(fb): + pass + return + + for char in chars: + buffer_dict = self.__fontlib.get_characters(char) + + if ord(char) == 10: + x = 0 + y += height + continue + + buffer = memoryview(buffer_dict[ord(char)]) + + if x > ((self.__oled_width // width - 1) * width): + x = 0 + y += height + + if y > ((self.__oled_height // height - 1) * height): + x = 0 + y = 0 + sleep(1.5) + self.__oled.fill(0) + + self.__fill_buffer(buffer, x, y, self.__buffer_format) + self.__oled.show() + x += width + def __get_format(self): format = framebuf.MONO_VLSB @@ -224,8 +270,8 @@ if __name__ == '__main__': if slave_list: oled = SSD1306_I2C(128, 64, i2c) - # runner = FontLibTest(oled) - # runner.load_font('client/combined.bin') + runner = FontLibTest(oled) + runner.load_font('client/combined.bin') # runner.load_font('client/customize.bin') # runner.load_font('client/combined_hmsb.bin') # runner.load_font('client/customize_hmsb.bin') @@ -234,6 +280,7 @@ if __name__ == '__main__': # runner.run_test1(chars) # 一次性读取所有字符数据然后逐个显示 # runner.run_test2(chars) # 一次读取并显示一个字符数据 # runner.run_test3(chars) # 一次读取一屏并显示 + runner.run_test4(chars2) # 要测试 FontLibTest2 的代码,需要生成一个新字库 @@ -243,5 +290,5 @@ if __name__ == '__main__': # $ FontMaker_Cli.exe -f 幼圆 --input input.txt -o welcome.bin # # 将生成的字库和 fonts/open-iconic.bin 一起上传到开发板 - runner = FontLibTest2(oled) - runner.run_test() + # runner = FontLibTest2(oled) + # runner.run_test() -- Gitee From 49aee2ce3dce5162163dd9fbecc3b51dd4ae00c4 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Mon, 30 Aug 2021 22:17:47 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client b/client index 495460e..6dfdb12 160000 --- a/client +++ b/client @@ -1 +1 @@ -Subproject commit 495460ec4ff2b2fb1d0124b9401e6d4285868f68 +Subproject commit 6dfdb12bb247e30611510f1fde6713d710e6557c -- Gitee From 91706740cbdc5ed946e4a47fefb0f867fbc6591d Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Mon, 30 Aug 2021 23:18:46 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E7=BC=96=E5=86=99=20ru?= =?UTF-8?q?n=5Ftest4=EF=BC=8C=E5=A5=BD=E9=9A=BE=E3=80=82=E3=80=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 75 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index a43ba21..eda95c6 100644 --- a/main.py +++ b/main.py @@ -147,46 +147,71 @@ class FontLibTest(object): return from math import ceil + from machine import Timer + chars = chars.replace('\t', '') newline_count = chars.count('\n') chars_count = len(chars) chars_per_row = int(self.__oled_width / self.__fontlib.font_height) chars_per_col = int(self.__oled_height / self.__fontlib.font_height) + chars_per_page = chars_per_row * chars_per_col total_rows = ceil(chars_count / chars_per_row) + newline_count total_pages = ceil(total_rows / chars_per_col) + buffer_size = self.__oled_width // 8 * self.__oled_height + fb_foreground = framebuf.FrameBuffer(bytearray(buffer_size * 2), self.__oled_width, self.__oled_height * 2, framebuf.MONO_VLSB) + fb_background = framebuf.FrameBuffer(bytearray(buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) x = y = 0 width = height = self.__fontlib.font_height - buffer_size = self.__oled_width // 8 * self.__oled_height - fb_foreground = framebuf.FrameBuffer(bytearray(buffer_size)) - fb_background = framebuf.FrameBuffer(bytearray(buffer_size)) + current_page = 0 - def __fill_page(fb): - pass - return + buffer_dict = self.__fontlib.get_characters(chars) - for char in chars: - buffer_dict = self.__fontlib.get_characters(char) + def __fill_page(): + nonlocal x, y, height, current_page, chars_per_page, fb_foreground - if ord(char) == 10: - x = 0 - y += height - continue + while current_page < 2: + for char in chars[current_page * chars_per_page:current_page * chars_per_page + chars_per_page]: + if ord(char) == 10: + x = 0 + y += height + continue + + buffer = memoryview(buffer_dict[ord(char)]) - buffer = memoryview(buffer_dict[ord(char)]) + if x > ((self.__oled_width // width - 1) * width): + x = 0 + y += height - if x > ((self.__oled_width // width - 1) * width): - x = 0 - y += height + # if y > ((self.__oled_height // height - 1) * height * 2): + # x = y = 0 + # self.__oled.fill(0) - if y > ((self.__oled_height // height - 1) * height): - x = 0 - y = 0 - sleep(1.5) - self.__oled.fill(0) - - self.__fill_buffer(buffer, x, y, self.__buffer_format) - self.__oled.show() - x += width + fb_foreground.blit(buffer, x, y) + self.__oled.show() + x += width + current_page += 1 + + def scroll_cb(): + nonlocal y + + fb_foreground.scroll(0, -1) + y += 1 + + + __fill_page() + + x = y = 0 + self.__oled.blit(fb_foreground, x, y) + self.__oled.show() + + sleep(0.5) + + scroll_timer = Timer(1) + scroll_timer.init( + mode=Timer.PERIODIC, + period=50, + callback=scroll_cb + ) def __get_format(self): format = framebuf.MONO_VLSB -- Gitee From 5a5b9c4b447dc9e5ecb88c388e847996e947a5d7 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Tue, 31 Aug 2021 14:49:18 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20FontLibTest3=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=8D=95=E5=85=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 219 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 145 insertions(+), 74 deletions(-) diff --git a/main.py b/main.py index eda95c6..8a554ed 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ Copyright © 2021 Walkline Wang (https://walkline.wang) Gitee: https://gitee.com/walkline/micropython-new-fontlib """ from utime import ticks_us, ticks_diff, sleep -from machine import I2C, Pin +from machine import I2C, Pin, Timer from drivers.ssd1306 import SSD1306_I2C import framebuf from libs.fontlib import FontLib @@ -142,77 +142,6 @@ class FontLibTest(object): diff_time = ticks_diff(ticks_us(), start_time) / 1000 print('### show {} chars: {} ms, avg: {} ms'.format(len(chars), diff_time, diff_time / len(chars))) - def run_test4(self, chars=None): - if self.__oled is None or chars is None: - return - - from math import ceil - from machine import Timer - - chars = chars.replace('\t', '') - newline_count = chars.count('\n') - chars_count = len(chars) - chars_per_row = int(self.__oled_width / self.__fontlib.font_height) - chars_per_col = int(self.__oled_height / self.__fontlib.font_height) - chars_per_page = chars_per_row * chars_per_col - total_rows = ceil(chars_count / chars_per_row) + newline_count - total_pages = ceil(total_rows / chars_per_col) - buffer_size = self.__oled_width // 8 * self.__oled_height - fb_foreground = framebuf.FrameBuffer(bytearray(buffer_size * 2), self.__oled_width, self.__oled_height * 2, framebuf.MONO_VLSB) - fb_background = framebuf.FrameBuffer(bytearray(buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) - x = y = 0 - width = height = self.__fontlib.font_height - current_page = 0 - - buffer_dict = self.__fontlib.get_characters(chars) - - def __fill_page(): - nonlocal x, y, height, current_page, chars_per_page, fb_foreground - - while current_page < 2: - for char in chars[current_page * chars_per_page:current_page * chars_per_page + chars_per_page]: - if ord(char) == 10: - x = 0 - y += height - continue - - buffer = memoryview(buffer_dict[ord(char)]) - - if x > ((self.__oled_width // width - 1) * width): - x = 0 - y += height - - # if y > ((self.__oled_height // height - 1) * height * 2): - # x = y = 0 - # self.__oled.fill(0) - - fb_foreground.blit(buffer, x, y) - self.__oled.show() - x += width - current_page += 1 - - def scroll_cb(): - nonlocal y - - fb_foreground.scroll(0, -1) - y += 1 - - - __fill_page() - - x = y = 0 - self.__oled.blit(fb_foreground, x, y) - self.__oled.show() - - sleep(0.5) - - scroll_timer = Timer(1) - scroll_timer.init( - mode=Timer.PERIODIC, - period=50, - callback=scroll_cb - ) - def __get_format(self): format = framebuf.MONO_VLSB @@ -272,6 +201,131 @@ class FontLibTest2(object): oled.blit(buffer, x, y) +class FontLibTest3(object): + def __init__(self, oled=None): + self.__oled = oled + self.__oled_width = None + self.__oled_height = None + + if self.__oled: + self.__oled_width = self.__oled.width + self.__oled_height = self.__oled.height + + def load_font(self, font_file): + self.__fontlib = FontLib(font_file) + self.__fontlib.info() + + self.__font_width = self.__fontlib.font_height + self.__font_height = self.__fontlib.font_height + # self.__buffer_format = self.__get_format() + + def run_test(self, scroll_height=1, interval=20, chars=None, load_all=False): + if self.__oled is None or chars is None: + return + + self.__chars = chars.replace('\t', '').replace('\r\n', '\n') + self.__newline_count = self.__chars.count('\n') + self.__chars_count = len(self.__chars) + self.__chars_per_row = int(self.__oled_width / self.__fontlib.font_height) + self.__chars_per_col = int(self.__oled_height / self.__fontlib.font_height) + self.__chars_per_page = self.__chars_per_row * self.__chars_per_col + self.__total_rows = int(self.__chars_count / self.__chars_per_row) + self.__newline_count + self.__total_pages = int(self.__total_rows / self.__chars_per_col) + self.__page_prepared = False + self.__current_page = 0 + self.__buffer_size = self.__oled_width // 8 * self.__oled_height + self.__fb_foreground = framebuf.FrameBuffer(bytearray(self.__buffer_size * 2), self.__oled_width, self.__oled_height * 2, framebuf.MONO_VLSB) + self.__fb_background = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) + self.__scroll_timer = Timer(1) + self.__x = self.__y = 0 + self.__scroll_speed = scroll_height + self.__scroll_interval = interval + self.__current_page = 0 + self.__current_char_index = 0 + self.__load_all = load_all + + if self.__load_all: + self.__buffer_dict = {} if self.__method else self.__fontlib.get_characters(self.__chars) + print('get all chars: ', len(self.__buffer_dict)) + + self.__fill_page(2) + + self.__x = self.__y = 0 + self.__oled.blit(self.__fb_foreground, self.__x, self.__y) + self.__oled.show() + + sleep(1) + + self.__scroll_timer.init( + mode=Timer.PERIODIC, + period=self.__scroll_interval, + callback=self.__scroll_cb + ) + + def __fill_buffer(self, buffer): + return framebuf.FrameBuffer(bytearray(buffer), self.__font_width, self.__font_height, framebuf.MONO_VLSB) + + def __fill_page(self, num=1): + while num: + col = 0 + if not self.__load_all: + self.__buffer_dict = self.__fontlib.get_characters(self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]) + + for char in self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]: + if ord(char) == 10: + self.__x = 0 + self.__y += self.__font_height + col += 1 + self.__current_char_index += 1 + continue + + if self.__x > ((self.__oled_width // self.__font_width - 1) * self.__font_width): + self.__x = 0 + self.__y += self.__font_height + col += 1 + + if col >= self.__chars_per_col: + break + + # print(char, end='') + + buffer = memoryview(self.__buffer_dict[ord(char)]) + buffer = self.__fill_buffer(buffer) + self.__fb_foreground.blit(buffer, self.__x, self.__y) + self.__x += self.__font_width + self.__current_char_index += 1 + + num -= 1 + self.__current_page += 1 + # print('\npage: ', self.__current_page, ' filled') + + self.__page_prepared = True + self.__y = 0 + + def __scroll_cb(self, timer): + if self.__current_page > self.__total_pages: + self.__scroll_timer.deinit() + return + + if not self.__page_prepared: + return + + if self.__y >= self.__oled_height: + self.__x = 0 + self.__y -= 1 + self.__page_prepared = False + self.__fb_background.blit(self.__fb_foreground, 0, 0) + self.__fb_foreground.fill(0) + self.__fb_foreground.blit(self.__fb_background, 0, 0) + self.__fill_page() + return + + self.__fb_foreground.scroll(0, self.__scroll_speed * -1) + self.__oled.blit(self.__fb_foreground, 0, 0) + self.__oled.show() + self.__y += self.__scroll_speed + + chars =\ '''  清晨4:50,老刀穿过熙熙攘攘的步行街,去找彭蠡。   从垃圾站下班之后,老刀回家洗了个澡,换了衣服。白色衬衫和褐色裤子,这是他唯一一套体面衣服,衬衫袖口磨了边,他把袖子卷到胳膊肘。老刀四十八岁,没结婚,已经过了注意外表的年龄,又没人照顾起居,这一套衣服留着穿了很多年,每次穿一天,回家就脱了叠上。他在垃圾站上班,没必要穿得体面,偶尔参加谁家小孩的婚礼,才拿出来穿在身上。这一次他不想脏兮兮地见陌生人。他在垃圾站连续工作了五小时,很担心身上会有味道。''' @@ -287,6 +341,15 @@ chars2 =\   这似乎解答了我的疑惑。   这种事实对本人来说意义重大,相信对这个世界也是有一定意义的。''' +chars3 =\ +'''出师表 +  先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。 +  宫中府中,俱为一体;陟罚臧否,不宜异同:若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理;不宜偏私,使内外异法也。 +  侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下:愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。 +  将军向宠,性行淑均,晓畅军事,试用于昔日,先帝称之曰“能”,是以众议举宠为督:愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。 +  亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。 +  臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间:尔来二十有一年矣。先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐托付不效,以伤先帝之明;故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。 +  愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏。臣不胜受恩感激。今当远离,临表涕零,不知所言。''' if __name__ == '__main__': i2c = I2C(0, scl=Pin(18), sda=Pin(19)) @@ -296,7 +359,7 @@ if __name__ == '__main__': oled = SSD1306_I2C(128, 64, i2c) runner = FontLibTest(oled) - runner.load_font('client/combined.bin') + # runner.load_font('client/combined.bin') # runner.load_font('client/customize.bin') # runner.load_font('client/combined_hmsb.bin') # runner.load_font('client/customize_hmsb.bin') @@ -305,7 +368,6 @@ if __name__ == '__main__': # runner.run_test1(chars) # 一次性读取所有字符数据然后逐个显示 # runner.run_test2(chars) # 一次读取并显示一个字符数据 # runner.run_test3(chars) # 一次读取一屏并显示 - runner.run_test4(chars2) # 要测试 FontLibTest2 的代码,需要生成一个新字库 @@ -317,3 +379,12 @@ if __name__ == '__main__': # 将生成的字库和 fonts/open-iconic.bin 一起上传到开发板 # runner = FontLibTest2(oled) # runner.run_test() + + + runner = FontLibTest3(oled) + runner.load_font('client/chushibiao.bin') + runner.run_test(scroll_height=1, interval=20, chars=chars3) # 每次滚动 1 像素 + + # 读取所有字符数据,每次滚动 1 行 + # runner.load_font('client/combined_vlsb.bin') + # runner.run_test(runner.__font_height, 500, chars2, True) -- Gitee From e028c98728fb046357a206251b8348c2ec7fb02b Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Tue, 31 Aug 2021 17:26:39 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E4=BC=98=E5=8C=96=20Fo?= =?UTF-8?q?ntLibTest3=20=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 119 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 91 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index 8a554ed..a5e3012 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,8 @@ from machine import I2C, Pin, Timer from drivers.ssd1306 import SSD1306_I2C import framebuf from libs.fontlib import FontLib +import gc +import _thread class FontLibTest(object): @@ -223,29 +225,15 @@ class FontLibTest3(object): if self.__oled is None or chars is None: return - self.__chars = chars.replace('\t', '').replace('\r\n', '\n') - self.__newline_count = self.__chars.count('\n') - self.__chars_count = len(self.__chars) - self.__chars_per_row = int(self.__oled_width / self.__fontlib.font_height) - self.__chars_per_col = int(self.__oled_height / self.__fontlib.font_height) - self.__chars_per_page = self.__chars_per_row * self.__chars_per_col - self.__total_rows = int(self.__chars_count / self.__chars_per_row) + self.__newline_count - self.__total_pages = int(self.__total_rows / self.__chars_per_col) - self.__page_prepared = False - self.__current_page = 0 - self.__buffer_size = self.__oled_width // 8 * self.__oled_height - self.__fb_foreground = framebuf.FrameBuffer(bytearray(self.__buffer_size * 2), self.__oled_width, self.__oled_height * 2, framebuf.MONO_VLSB) - self.__fb_background = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) - self.__scroll_timer = Timer(1) - self.__x = self.__y = 0 self.__scroll_speed = scroll_height self.__scroll_interval = interval - self.__current_page = 0 - self.__current_char_index = 0 + self.__chars = chars.replace('\t', '').replace('\r\n', '\n') self.__load_all = load_all + self.__setup() + if self.__load_all: - self.__buffer_dict = {} if self.__method else self.__fontlib.get_characters(self.__chars) + self.__buffer_dict = self.__fontlib.get_characters(self.__chars) if self.__load_all else {} print('get all chars: ', len(self.__buffer_dict)) self.__fill_page(2) @@ -254,6 +242,9 @@ class FontLibTest3(object): self.__oled.blit(self.__fb_foreground, self.__x, self.__y) self.__oled.show() + self.__need_next_page = True + _thread.start_new_thread(self.__fill_page_thread, ()) + sleep(1) self.__scroll_timer.init( @@ -262,6 +253,27 @@ class FontLibTest3(object): callback=self.__scroll_cb ) + def __setup(self): + self.__chars_per_row = int(self.__oled_width / self.__fontlib.font_height) + self.__chars_per_col = int(self.__oled_height / self.__fontlib.font_height) + self.__chars_per_page = self.__chars_per_row * self.__chars_per_col + + self.__total_rows = int(len(self.__chars) / self.__chars_per_row) + self.__chars.count('\n') + self.__total_pages = int(self.__total_rows / self.__chars_per_col) + + self.__buffer_size = self.__oled_width // 8 * self.__oled_height + self.__fb_foreground = framebuf.FrameBuffer(bytearray(self.__buffer_size * 2), self.__oled_width, self.__oled_height * 2, framebuf.MONO_VLSB) + self.__fb_background = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) + self.__fb_temp = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) + + self.__scroll_timer = Timer(1) + + self.__need_next_page = False + self.__page_prepared = False + self.__current_page = 0 + self.__x = self.__y = 0 + self.__current_char_index = 0 + def __fill_buffer(self, buffer): return framebuf.FrameBuffer(bytearray(buffer), self.__font_width, self.__font_height, framebuf.MONO_VLSB) @@ -297,27 +309,32 @@ class FontLibTest3(object): num -= 1 self.__current_page += 1 + + gc.collect() # print('\npage: ', self.__current_page, ' filled') - self.__page_prepared = True - self.__y = 0 + # self.__page_prepared = True + # self.__y = 0 def __scroll_cb(self, timer): if self.__current_page > self.__total_pages: self.__scroll_timer.deinit() return - if not self.__page_prepared: - return - if self.__y >= self.__oled_height: + if not self.__page_prepared: + return + self.__x = 0 self.__y -= 1 - self.__page_prepared = False - self.__fb_background.blit(self.__fb_foreground, 0, 0) + self.__fb_temp.blit(self.__fb_foreground, 0, 0) self.__fb_foreground.fill(0) - self.__fb_foreground.blit(self.__fb_background, 0, 0) - self.__fill_page() + self.__fb_foreground.blit(self.__fb_temp, 0, 0) + self.__fb_foreground.blit(self.__fb_background, 0, self.__y) + self.__need_next_page = True + self.__page_prepared = False + # self.__fill_page() + self.__y = 0 return self.__fb_foreground.scroll(0, self.__scroll_speed * -1) @@ -325,6 +342,52 @@ class FontLibTest3(object): self.__oled.show() self.__y += self.__scroll_speed + gc.collect() + + def __fill_page_thread(self): + while self.__current_page <= self.__total_pages: + if self.__need_next_page: + col = x = y = 0 + if not self.__load_all: + self.__buffer_dict = self.__fontlib.get_characters(self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]) + + self.__fb_background.fill(0) + for char in self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]: + if ord(char) == 10: + x = 0 + y += self.__font_height + col += 1 + self.__current_char_index += 1 + continue + + if x > ((self.__oled_width // self.__font_width - 1) * self.__font_width): + x = 0 + y += self.__font_height + col += 1 + + if col >= self.__chars_per_col: + break + + # print(char, end='') + + buffer = memoryview(self.__buffer_dict[ord(char)]) + buffer = self.__fill_buffer(buffer) + self.__fb_background.blit(buffer, x, y) + x += self.__font_width + self.__current_char_index += 1 + + self.__current_page += 1 + + gc.collect() + # print('\npage: ', self.__current_page, ' filled') + + self.__page_prepared = True + self.__need_next_page = False + # self.__y = 0 + else: + sleep(0.02) + print('thread exit') + chars =\ '''  清晨4:50,老刀穿过熙熙攘攘的步行街,去找彭蠡。 @@ -383,7 +446,7 @@ if __name__ == '__main__': runner = FontLibTest3(oled) runner.load_font('client/chushibiao.bin') - runner.run_test(scroll_height=1, interval=20, chars=chars3) # 每次滚动 1 像素 + runner.run_test(scroll_height=1, interval=80, chars=chars3) # 每次滚动 1 像素 # 读取所有字符数据,每次滚动 1 行 # runner.load_font('client/combined_vlsb.bin') -- Gitee From c7ce1cc917ce18650da26a5876e1cb09850fdd1a Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Tue, 31 Aug 2021 23:30:04 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 62 ++++++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/main.py b/main.py index a5e3012..6a52f8b 100644 --- a/main.py +++ b/main.py @@ -231,11 +231,6 @@ class FontLibTest3(object): self.__load_all = load_all self.__setup() - - if self.__load_all: - self.__buffer_dict = self.__fontlib.get_characters(self.__chars) if self.__load_all else {} - print('get all chars: ', len(self.__buffer_dict)) - self.__fill_page(2) self.__x = self.__y = 0 @@ -267,6 +262,7 @@ class FontLibTest3(object): self.__fb_temp = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) self.__scroll_timer = Timer(1) + self.__buffer_dict = self.__fontlib.get_characters(self.__chars) if self.__load_all else {} self.__need_next_page = False self.__page_prepared = False @@ -279,42 +275,40 @@ class FontLibTest3(object): def __fill_page(self, num=1): while num: - col = 0 + x = y = col = 0 + current_page_chars = self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page] + if not self.__load_all: - self.__buffer_dict = self.__fontlib.get_characters(self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]) + self.__buffer_dict = self.__fontlib.get_characters(current_page_chars) - for char in self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]: + for char in current_page_chars: if ord(char) == 10: - self.__x = 0 - self.__y += self.__font_height + x = 0 + y += self.__font_height col += 1 self.__current_char_index += 1 continue - if self.__x > ((self.__oled_width // self.__font_width - 1) * self.__font_width): - self.__x = 0 - self.__y += self.__font_height + if x > ((self.__oled_width // self.__font_width - 1) * self.__font_width): + x = 0 + y += self.__font_height col += 1 if col >= self.__chars_per_col: break # print(char, end='') - - buffer = memoryview(self.__buffer_dict[ord(char)]) - buffer = self.__fill_buffer(buffer) - self.__fb_foreground.blit(buffer, self.__x, self.__y) - self.__x += self.__font_width + + buffer = self.__fill_buffer(memoryview(self.__buffer_dict[ord(char)])) + self.__fb_foreground.blit(buffer, x, y) + x += self.__font_width self.__current_char_index += 1 + gc.collect() num -= 1 self.__current_page += 1 gc.collect() - # print('\npage: ', self.__current_page, ' filled') - - # self.__page_prepared = True - # self.__y = 0 def __scroll_cb(self, timer): if self.__current_page > self.__total_pages: @@ -325,17 +319,15 @@ class FontLibTest3(object): if not self.__page_prepared: return - self.__x = 0 - self.__y -= 1 self.__fb_temp.blit(self.__fb_foreground, 0, 0) self.__fb_foreground.fill(0) self.__fb_foreground.blit(self.__fb_temp, 0, 0) - self.__fb_foreground.blit(self.__fb_background, 0, self.__y) + self.__fb_foreground.blit(self.__fb_background, 0, self.__oled_height) + self.__need_next_page = True self.__page_prepared = False - # self.__fill_page() - self.__y = 0 - return + self.__x = self.__y = 0 + # return self.__fb_foreground.scroll(0, self.__scroll_speed * -1) self.__oled.blit(self.__fb_foreground, 0, 0) @@ -348,11 +340,14 @@ class FontLibTest3(object): while self.__current_page <= self.__total_pages: if self.__need_next_page: col = x = y = 0 + current_page_chars = self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page] + if not self.__load_all: - self.__buffer_dict = self.__fontlib.get_characters(self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]) + self.__buffer_dict = self.__fontlib.get_characters(current_page_chars) self.__fb_background.fill(0) - for char in self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page]: + + for char in current_page_chars: if ord(char) == 10: x = 0 y += self.__font_height @@ -369,15 +364,14 @@ class FontLibTest3(object): break # print(char, end='') - - buffer = memoryview(self.__buffer_dict[ord(char)]) - buffer = self.__fill_buffer(buffer) + + buffer = self.__fill_buffer(memoryview(self.__buffer_dict[ord(char)])) self.__fb_background.blit(buffer, x, y) x += self.__font_width self.__current_char_index += 1 + gc.collect() self.__current_page += 1 - gc.collect() # print('\npage: ', self.__current_page, ' filled') -- Gitee From 6e20e93f4a9f5bd3d41e69240fd1344d06871e5c Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Wed, 1 Sep 2021 09:03:39 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client b/client index 6dfdb12..6fc042a 160000 --- a/client +++ b/client @@ -1 +1 @@ -Subproject commit 6dfdb12bb247e30611510f1fde6713d710e6557c +Subproject commit 6fc042a8a592160263d12fb14e526706b46284c5 -- Gitee From 202cfea9df4ce5f745d124a8537185c78a4c19a2 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Wed, 1 Sep 2021 17:31:44 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=A4=9A=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 101 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 30 deletions(-) diff --git a/main.py b/main.py index 6a52f8b..a0edbb7 100644 --- a/main.py +++ b/main.py @@ -219,9 +219,10 @@ class FontLibTest3(object): self.__font_width = self.__fontlib.font_height self.__font_height = self.__fontlib.font_height + gc.collect() # self.__buffer_format = self.__get_format() - def run_test(self, scroll_height=1, interval=20, chars=None, load_all=False): + def run_test(self, scroll_height=1, interval=20, chars=None, load_all=False, thread=False): if self.__oled is None or chars is None: return @@ -229,24 +230,31 @@ class FontLibTest3(object): self.__scroll_interval = interval self.__chars = chars.replace('\t', '').replace('\r\n', '\n') self.__load_all = load_all + self.__thread = thread self.__setup() - self.__fill_page(2) + self.__fill_page(2, True) - self.__x = self.__y = 0 self.__oled.blit(self.__fb_foreground, self.__x, self.__y) self.__oled.show() - self.__need_next_page = True - _thread.start_new_thread(self.__fill_page_thread, ()) + self.__page_prepared = False + if self.__thread: + self.__need_next_page = True + _thread.start_new_thread(self.__fill_page_thread, ()) + else: + self.__fill_page() sleep(1) - self.__scroll_timer.init( - mode=Timer.PERIODIC, - period=self.__scroll_interval, - callback=self.__scroll_cb - ) + if self.__thread: + _thread.start_new_thread(self.__scroll_thread, ()) + else: + self.__scroll_timer.init( + mode=Timer.PERIODIC, + period=self.__scroll_interval, + callback=self.__scroll_cb + ) def __setup(self): self.__chars_per_row = int(self.__oled_width / self.__fontlib.font_height) @@ -273,14 +281,17 @@ class FontLibTest3(object): def __fill_buffer(self, buffer): return framebuf.FrameBuffer(bytearray(buffer), self.__font_width, self.__font_height, framebuf.MONO_VLSB) - def __fill_page(self, num=1): + def __fill_page(self, num=1, first=False): + x = y = 0 while num: - x = y = col = 0 + col = 0 current_page_chars = self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page] if not self.__load_all: self.__buffer_dict = self.__fontlib.get_characters(current_page_chars) + self.__fb_background.fill(0) + for char in current_page_chars: if ord(char) == 10: x = 0 @@ -300,15 +311,15 @@ class FontLibTest3(object): # print(char, end='') buffer = self.__fill_buffer(memoryview(self.__buffer_dict[ord(char)])) - self.__fb_foreground.blit(buffer, x, y) + self.__fb_foreground.blit(buffer, x, y) if first else self.__fb_background.blit(buffer, x, y) x += self.__font_width self.__current_char_index += 1 gc.collect() num -= 1 self.__current_page += 1 - gc.collect() + self.__page_prepared = True def __scroll_cb(self, timer): if self.__current_page > self.__total_pages: @@ -319,26 +330,55 @@ class FontLibTest3(object): if not self.__page_prepared: return - self.__fb_temp.blit(self.__fb_foreground, 0, 0) - self.__fb_foreground.fill(0) - self.__fb_foreground.blit(self.__fb_temp, 0, 0) + # self.__fb_temp.blit(self.__fb_foreground, 0, 0) + # self.__fb_foreground.fill(0) + # self.__fb_foreground.blit(self.__fb_temp, 0, 0) self.__fb_foreground.blit(self.__fb_background, 0, self.__oled_height) + # print('page ', self.__current_page, ' used') - self.__need_next_page = True self.__page_prepared = False + self.__fill_page() self.__x = self.__y = 0 - # return + gc.collect() self.__fb_foreground.scroll(0, self.__scroll_speed * -1) self.__oled.blit(self.__fb_foreground, 0, 0) self.__oled.show() self.__y += self.__scroll_speed - gc.collect() - def __fill_page_thread(self): + def __scroll_thread(self): while self.__current_page <= self.__total_pages: + if self.__y >= self.__oled_height: + if not self.__page_prepared: + print('not') + return + + # self.__fb_temp.blit(self.__fb_foreground, 0, 0) + # self.__fb_foreground.fill(0) + # self.__fb_foreground.blit(self.__fb_temp, 0, 0) + self.__fb_foreground.blit(self.__fb_background, 0, self.__oled_height) + # print('page ', self.__current_page, ' used') + + self.__page_prepared = False + self.__need_next_page = True + self.__x = 0 + self.__y = 0 + self.__current_page += 1 + gc.collect() + + self.__fb_foreground.scroll(0, self.__scroll_speed * -1) + self.__oled.blit(self.__fb_foreground, 0, 0) + self.__oled.show() + self.__y += self.__scroll_speed + + # gc.collect() + sleep(self.__scroll_interval / 1000) + + def __fill_page_thread(self): + while self.__current_page < self.__total_pages: if self.__need_next_page: + # print('filling page ', self.__current_page) col = x = y = 0 current_page_chars = self.__chars[self.__current_char_index:self.__current_char_index + self.__chars_per_page] @@ -371,15 +411,12 @@ class FontLibTest3(object): self.__current_char_index += 1 gc.collect() - self.__current_page += 1 gc.collect() # print('\npage: ', self.__current_page, ' filled') self.__page_prepared = True self.__need_next_page = False - # self.__y = 0 - else: - sleep(0.02) + print('thread exit') @@ -408,6 +445,7 @@ chars3 =\   臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间:尔来二十有一年矣。先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐托付不效,以伤先帝之明;故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。   愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏。臣不胜受恩感激。今当远离,临表涕零,不知所言。''' + if __name__ == '__main__': i2c = I2C(0, scl=Pin(18), sda=Pin(19)) slave_list = i2c.scan() @@ -415,7 +453,7 @@ if __name__ == '__main__': if slave_list: oled = SSD1306_I2C(128, 64, i2c) - runner = FontLibTest(oled) + # runner = FontLibTest(oled) # runner.load_font('client/combined.bin') # runner.load_font('client/customize.bin') # runner.load_font('client/combined_hmsb.bin') @@ -439,9 +477,12 @@ if __name__ == '__main__': runner = FontLibTest3(oled) - runner.load_font('client/chushibiao.bin') - runner.run_test(scroll_height=1, interval=80, chars=chars3) # 每次滚动 1 像素 + runner.load_font('client/wenqy_chushibiao.bin') + runner.run_test(scroll_height=1, interval=40, chars=chars3, thread=True) # 每次滚动 1 像素 - # 读取所有字符数据,每次滚动 1 行 + # 读取所有字符数据,每次滚动 1 像素 + # runner = FontLibTest3(oled) # runner.load_font('client/combined_vlsb.bin') - # runner.run_test(runner.__font_height, 500, chars2, True) + # runner.run_test(1, 80, chars2, True) + + gc.collect() -- Gitee From d4cdbbbcaf130f0dd0c200fff23d7721c09507e2 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Wed, 1 Sep 2021 21:08:17 +0800 Subject: [PATCH 10/18] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20read=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=20seek?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fontlib.py | 17 ++++++++++++----- libs/fontlib.py | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/fontlib.py b/fontlib.py index 3dab358..31abb76 100644 --- a/fontlib.py +++ b/fontlib.py @@ -94,8 +94,10 @@ class FontLib(object): def __get_character_unicode_buffer(self, font_file, unicode_set): buffer_list = {} + chunk_size = 1000 for unicode in unicode_set: + font_file.seek(0) if self.__is_ascii(unicode): char_offset = self.__header.ascii_start + (unicode - 0x20) * self.__header.data_size elif self.__is_gb2312(unicode): @@ -114,10 +116,11 @@ class FontLib(object): else: return -1 - for offset in range(self.__header.index_table_address, self.__header.ascii_start, 1000): - font_file.seek(offset) + font_file.read(self.__header.index_table_address) + for offset in range(self.__header.index_table_address, self.__header.ascii_start, chunk_size): + # font_file.seek(offset) - char_index_offset = __seek(font_file.read(1000), gb2312_index) + char_index_offset = __seek(font_file.read(chunk_size), gb2312_index) if char_index_offset >= 0: char_index_offset += offset break @@ -125,12 +128,16 @@ class FontLib(object): buffer_list[unicode] = memoryview(self.__placeholder_buffer) continue - char_offset = self.__header.gb2312_start + (char_index_offset - self.__header.index_table_address) / 2 * self.__header.data_size + char_offset = int(self.__header.gb2312_start + (char_index_offset - self.__header.index_table_address) / 2 * self.__header.data_size) else: buffer_list[unicode] = memoryview(self.__placeholder_buffer) continue - font_file.seek(int(char_offset)) + for _ in range(0, char_offset - font_file.tell() - chunk_size, chunk_size): + font_file.read(chunk_size) + font_file.read(char_offset - font_file.tell()) + # font_file.seek(char_offset) + buffer_list[unicode] = memoryview(font_file.read(self.__header.data_size)) gc.collect() diff --git a/libs/fontlib.py b/libs/fontlib.py index 1e5e972..58d31dd 100644 --- a/libs/fontlib.py +++ b/libs/fontlib.py @@ -74,8 +74,10 @@ class FontLib(object): # @micropython.native def __get_character_unicode_buffer(self, font_file, unicode_set): buffer_list = {} + chunk_size = const(1000) for unicode in unicode_set: + font_file.seek(0) if self.__is_ascii(unicode): char_offset = self.__header.ascii_start + (unicode - FontLib.ASCII_START) * self.__header.data_size elif self.__is_gb2312(unicode): @@ -94,10 +96,11 @@ class FontLib(object): else: return -1 - for offset in range(self.__header.index_table_address, self.__header.ascii_start, 1000): - font_file.seek(offset) + font_file.read(self.__header.index_table_address) + for offset in range(self.__header.index_table_address, self.__header.ascii_start, chunk_size): + # font_file.seek(offset) - char_index_offset = __seek(font_file.read(1000), gb2312_index) + char_index_offset = __seek(font_file.read(chunk_size), gb2312_index) if char_index_offset >= 0: char_index_offset += offset break @@ -105,12 +108,16 @@ class FontLib(object): buffer_list[unicode] = memoryview(self.__placeholder_buffer) continue - char_offset = self.__header.gb2312_start + (char_index_offset - self.__header.index_table_address) / 2 * self.__header.data_size + char_offset = int(self.__header.gb2312_start + (char_index_offset - self.__header.index_table_address) / 2 * self.__header.data_size)) else: buffer_list[unicode] = memoryview(self.__placeholder_buffer) continue - font_file.seek(int(char_offset)) + for _ in range(0, char_offset - font_file.tell() - chunk_size, chunk_size): + font_file.read(chunk_size) + font_file.read(char_offset - font_file.tell()) + # font_file.seek(char_offset) + buffer_list[unicode] = memoryview(font_file.read(self.__header.data_size)) gc.collect() -- Gitee From d5c673a73e623805444bbac5b44465ed1ad74c46 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Wed, 1 Sep 2021 23:42:07 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E6=9B=B4=E6=94=B9=20=5F=5Fget=5Fcharacte?= =?UTF-8?q?r=5Funicode=5Fbuffer()=20=E6=9F=A5=E8=A1=A8=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E4=BB=8E=E6=AF=8F=E6=AC=A1=E6=9F=A5=E6=89=BE=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E6=94=B9=E4=B8=BA=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E5=AD=97=E7=AC=A6=EF=BC=8C=E5=B8=8C=E6=9C=9B?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=8F=90=E9=AB=98=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fontlib.py | 97 +++++++++++++++++++++++++++++++------------------ libs/fontlib.py | 95 +++++++++++++++++++++++++++++++----------------- 2 files changed, 123 insertions(+), 69 deletions(-) diff --git a/fontlib.py b/fontlib.py index 31abb76..4ab1e4a 100644 --- a/fontlib.py +++ b/fontlib.py @@ -94,53 +94,80 @@ class FontLib(object): def __get_character_unicode_buffer(self, font_file, unicode_set): buffer_list = {} + ascii_list = [] + gb2312_list = [] chunk_size = 1000 + def __seek(offset, data, targets): + # target: + # 0: unicode + # 1: little endian bytes + # 2: charest index offset + seeked_count = 0 + for target in targets: + if target[2] is not None: + seeked_count += 1 + continue + + seek_offset = -1 + while True: + seek_offset = data.find(target[1], seek_offset + 1) + + if seek_offset >= 0: + if seek_offset % 2 == 0: + target[2] = seek_offset + offset + break + else: + continue + else: + break + + return seeked_count == len(targets) + for unicode in unicode_set: - font_file.seek(0) if self.__is_ascii(unicode): char_offset = self.__header.ascii_start + (unicode - 0x20) * self.__header.data_size + ascii_list.append([unicode, char_offset]) elif self.__is_gb2312(unicode): - gb2312_index = struct.pack('= 0: - if seek_offset % 2 == 0: - return seek_offset - else: - continue - else: - return -1 + gb2312_list.append([unicode, struct.pack('= 0: - char_index_offset += offset - break - else: - buffer_list[unicode] = memoryview(self.__placeholder_buffer) - continue + for _ in range(0, ascii[1] - font_file.tell() - chunk_size, chunk_size): + font_file.read(chunk_size) + font_file.read(ascii[1] - font_file.tell()) + + buffer_list[ascii[0]] = memoryview(font_file.read(self.__header.data_size)) + + if len(gb2312_list): + font_file.seek(0) + font_file.read(self.__header.index_table_address) - char_offset = int(self.__header.gb2312_start + (char_index_offset - self.__header.index_table_address) / 2 * self.__header.data_size) + for offset in range(self.__header.index_table_address, self.__header.ascii_start, chunk_size): + if __seek(offset, font_file.read(chunk_size), gb2312_list): + break else: - buffer_list[unicode] = memoryview(self.__placeholder_buffer) - continue + __seek(self.__header.ascii_start - offset, font_file.read(chunk_size), gb2312_list) - for _ in range(0, char_offset - font_file.tell() - chunk_size, chunk_size): - font_file.read(chunk_size) - font_file.read(char_offset - font_file.tell()) - # font_file.seek(char_offset) + for gb2312 in gb2312_list: + font_file.seek(0) - buffer_list[unicode] = memoryview(font_file.read(self.__header.data_size)) + if gb2312[2] is None: + buffer_list[gb2312[0]] = memoryview(self.__placeholder_buffer) + else: + char_offset = int(self.__header.gb2312_start + (gb2312[2] - self.__header.index_table_address) / 2 * self.__header.data_size) + + for _ in range(0, char_offset - font_file.tell() - chunk_size, chunk_size): + font_file.read(chunk_size) + font_file.read(char_offset - font_file.tell()) + + buffer_list[gb2312[0]] = memoryview(font_file.read(self.__header.data_size)) + + gc.collect() - gc.collect() return buffer_list def get_characters(self, characters: str): @@ -301,7 +328,7 @@ def run_test(): diff_time = ticks_diff(ticks_us(), start_time) / 1000 print('### show {} chars: {} ms, avg: {} ms'.format(len(chars), diff_time, diff_time / len(chars))) else: - buffer_dict = fontlib.get_characters('爱我,中华!Hello⒉あβǚㄘB⑴■☆') + buffer_dict = fontlib.get_characters('\ue900鼽爱我,中华!Hello⒉あβǚㄘB⑴■☆') buffer_list = [] for unicode, buffer in buffer_dict.items(): diff --git a/libs/fontlib.py b/libs/fontlib.py index 58d31dd..dd96fdc 100644 --- a/libs/fontlib.py +++ b/libs/fontlib.py @@ -74,53 +74,80 @@ class FontLib(object): # @micropython.native def __get_character_unicode_buffer(self, font_file, unicode_set): buffer_list = {} + ascii_list = [] + gb2312_list = [] chunk_size = const(1000) + def __seek(offset, data, targets): + # target: + # 0: unicode + # 1: little endian bytes + # 2: charest index offset + seeked_count = 0 + for target in targets: + if target[2] is not None: + seeked_count += 1 + continue + + seek_offset = -1 + while True: + seek_offset = data.find(target[1], seek_offset + 1) + + if seek_offset >= 0: + if seek_offset % 2 == 0: + target[2] = seek_offset + offset + break + else: + continue + else: + break + + return seeked_count == len(targets) + for unicode in unicode_set: - font_file.seek(0) if self.__is_ascii(unicode): char_offset = self.__header.ascii_start + (unicode - FontLib.ASCII_START) * self.__header.data_size + ascii_list.append([unicode, char_offset]) elif self.__is_gb2312(unicode): - gb2312_index = struct.pack('= 0: - if seek_offset % 2 == 0: - return seek_offset - else: - continue - else: - return -1 + gb2312_list.append([unicode, struct.pack('= 0: - char_index_offset += offset - break - else: - buffer_list[unicode] = memoryview(self.__placeholder_buffer) - continue + for _ in range(0, ascii[1] - font_file.tell() - chunk_size, chunk_size): + font_file.read(chunk_size) + font_file.read(ascii[1] - font_file.tell()) + + buffer_list[ascii[0]] = memoryview(font_file.read(self.__header.data_size)) + + if len(gb2312_list): + font_file.seek(0) + font_file.read(self.__header.index_table_address) - char_offset = int(self.__header.gb2312_start + (char_index_offset - self.__header.index_table_address) / 2 * self.__header.data_size)) + for offset in range(self.__header.index_table_address, self.__header.ascii_start, chunk_size): + if __seek(offset, font_file.read(chunk_size), gb2312_list): + break else: - buffer_list[unicode] = memoryview(self.__placeholder_buffer) - continue + __seek(self.__header.ascii_start - offset, font_file.read(chunk_size), gb2312_list) - for _ in range(0, char_offset - font_file.tell() - chunk_size, chunk_size): - font_file.read(chunk_size) - font_file.read(char_offset - font_file.tell()) - # font_file.seek(char_offset) + for gb2312 in gb2312_list: + font_file.seek(0) - buffer_list[unicode] = memoryview(font_file.read(self.__header.data_size)) + if gb2312[2] is None: + buffer_list[gb2312[0]] = memoryview(self.__placeholder_buffer) + else: + char_offset = int(self.__header.gb2312_start + (gb2312[2] - self.__header.index_table_address) / 2 * self.__header.data_size) + + for _ in range(0, char_offset - font_file.tell() - chunk_size, chunk_size): + font_file.read(chunk_size) + font_file.read(char_offset - font_file.tell()) + + buffer_list[gb2312[0]] = memoryview(font_file.read(self.__header.data_size)) + + gc.collect() - gc.collect() return buffer_list def get_characters(self, characters: str): -- Gitee From bbf688b02b041e45697d32e8ad9ecff919fb429b Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Fri, 3 Sep 2021 00:51:35 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.txt | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/test.txt b/test.txt index 0811fd8..e5bb8dc 100644 --- a/test.txt +++ b/test.txt @@ -40,3 +40,118 @@ HZK Info: client/customize_hmsb.bin byte order : MSB characters : 1868 ### get 227 chars: 1239.848 ms, avg: 5.461885 ms + + + + + + +新方法,一次打开字库文件读取全部字模数据,使用read代替seek +__get_character_unicode_buffer() runtime: 32.91 ms +### load font file: 61.217 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +__get_character_unicode_buffer() runtime: 37584.83 ms +### get 240 chars: 38228.77 ms, avg: 159.2866 ms + + +新方法,一次打开字库文件读取全部字模数据,使用seek +__get_character_unicode_buffer() runtime: 2.773 ms +### load font file: 30.97 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +__get_character_unicode_buffer() runtime: 917.797 ms +### get 240 chars: 1561.768 ms, avg: 6.507367 ms +和 +__get_character_unicode_buffer() runtime: 2.726 ms +### load font file: 26.22 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +Traceback (most recent call last): + File "", line 387, in + File "", line 304, in run_test + File "", line 178, in get_characters + File "", line 15, in wrapper +MemoryError: memory allocation failed, allocating 55 bytes +767 chars +和 +__get_character_unicode_buffer() runtime: 2.726 ms +### load font file: 26.17 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +__get_character_unicode_buffer() runtime: 1134.455 ms +### get 227 chars: 1867.552 ms, avg: 8.227101 ms + + + + +原方法,逐字读取字模数据 +__get_character_unicode_buffer() runtime: 4.13 ms +### load font file: 27.516 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +__get_character_unicode_buffer() runtime: 2912.698 ms +### get 227 chars: 3643.97 ms, avg: 16.05273 ms +和 +__get_character_unicode_buffer() runtime: 4.132 ms +### load font file: 27.536 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +__get_character_unicode_buffer() runtime: 2504.442 ms +### get 240 chars: 3143.604 ms, avg: 13.09835 ms +和 +__get_character_unicode_buffer() runtime: 4.181 ms +### load font file: 27.623 ms +HZK Info: //client//combined.bin + file size : 500024 + font height : 18 + data size : 54 + scan mode : Vertical + byte order : LSB + characters : 8932 + +Traceback (most recent call last): + File "", line 365, in + File "", line 282, in run_test + File "", line 155, in get_characters + File "", line 15, in wrapper + File "", line 143, in __get_character_unicode_buffer +MemoryError: memory allocation failed, allocating 3112 bytes + + -- Gitee From f1067d30d6d322fbdc63e306aa71528c3f8e8f36 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Fri, 3 Sep 2021 00:51:50 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/README.md b/README.md index 1dfcf76..6ebc24e 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,74 @@ $ ab abconfig-mpy * 字符数据使用`Dict`存储,字符`Unicode`值作为关键字,取值耗时可以忽略 * 显示字符耗时未统计 +### 关于提速 + +决定显示速度的因素有两方面: + +* 从字库中读取指定字符的字模数据 +* 在 oled 中显示 + +显示部分,`MicroPython`已经提供了`SSD1306`驱动,使用`FrameBuffer`进行数据管理,我相信官方的实力,所以显示部分的速度假设已经没有提升空间了 + +数据部分,最开始的思路就是“完成功能”,所以在读取的时候是这个流程: + +1. 打开字库文件 +2. 取一个字符 +3. `分段`查找字符在`索引表`中的偏移量 +4. `定位`到偏移量读取字模数据 +5. 从第`2`步开始循环,直到读取所有字符数据 + +> `分段`的意思是:在`Python`中打开文件,它不提供查找功能,需要先读取文件内容再进行查找,对于开发板来说,一次性读取全部文件肯定会导致`内存溢出`,所以需要分段读取一段合理长度的数据再进行查找,在查找到之前就需要不停的分段读取 + + +这个方法的问题在于,每个字符都要从`索引表`的开始位置进行查找,比较浪费 + +提速的思路就是,在每次`分段`读取数据的时候查找所有字符,减少`分段`次数 + +继续查找资料,有人说使用`read`代替`seek`可以提高效率`定位`的效率,在电脑上进行了简单的测试,300M 的文件使用`read`比`seek`快大约`0.2 ms`,但是这个方法有个致命问题,虽然`read`读取到的数据并不使用,但也会分配内存空间,在开发板上运行的结果就是,更慢了 + +对比一下三种方式的读取速度(毫秒) + +字库文件信息: +* 文件大小:500024 字节 +* 字符总数:8932 个 +* 数据大小:54 字节 + +| | 原方法 | 新方法(seek) | 新方法(read) | +| :-: | :-: | :-: | :-: | +| 打开字库 | 27.51 | 26.22 | 61.21 | + +| 字符数 | 原方法 | 新方法(seek) | 新方法(read) | +| :-: | :-: | :-: | :-: | +| 240 | 13.09 | 6.50 | 159.28 | +| 227 | 16.05 | 8.22 | / | +| 767 | 内存溢出 | 内存溢出 | / | + +> 注:统计结果为**平均时间** + + +这里的字符数是包含了重复字符的总数,实际获取数据时是要去重的 + +读取速度还与字符在`索引表`中的相对位置有关,相对位置靠前的字符当然能更快被找到,反之则更慢 + +一次性读取太多的数据也会导致`内存溢出`,所以在实际使用时不建议一次性读取太多 + +> 统计运行时间使用了如下的装饰器 + +```python +from utime import ticks_us, ticks_diff + +def cal_time(fn): + def wrapper(*args,**kwargs): + starTime = ticks_us() + f = fn(*args,**kwargs) + print('%s() runtime: %s ms' % (fn.__name__, ticks_diff(ticks_us(), starTime) / 1000)) + return f + return wrapper +``` + + + ### 合作交流 * 联系邮箱: -- Gitee From 4656ef2bce75bc41316a3d63ebc548b63149f8a3 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Fri, 3 Sep 2021 00:58:17 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ebc24e..75245dd 100644 --- a/README.md +++ b/README.md @@ -266,7 +266,7 @@ $ ab abconfig-mpy 提速的思路就是,在每次`分段`读取数据的时候查找所有字符,减少`分段`次数 -继续查找资料,有人说使用`read`代替`seek`可以提高效率`定位`的效率,在电脑上进行了简单的测试,300M 的文件使用`read`比`seek`快大约`0.2 ms`,但是这个方法有个致命问题,虽然`read`读取到的数据并不使用,但也会分配内存空间,在开发板上运行的结果就是,更慢了 +继续查找资料,有人说使用`read`代替`seek`可以提高`定位`的效率,在电脑上进行了简单测试,300M 的文件使用`read`比`seek`快大约`0.2 ms`,但是这个方法有个致命问题,虽然`read`读取到的数据并不使用,但也会分配内存空间,在开发板上运行的结果就是,更慢了 对比一下三种方式的读取速度(毫秒) @@ -294,7 +294,7 @@ $ ab abconfig-mpy 一次性读取太多的数据也会导致`内存溢出`,所以在实际使用时不建议一次性读取太多 -> 统计运行时间使用了如下的装饰器 +> 统计运行时间使用的装饰器 ```python from utime import ticks_us, ticks_diff @@ -308,8 +308,6 @@ def cal_time(fn): return wrapper ``` - - ### 合作交流 * 联系邮箱: -- Gitee From 67fd78fcb1c46f68c729bebbd507b1641be76062 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Fri, 3 Sep 2021 12:58:25 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8Cread=20=E6=95=88=E7=8E=87=E4=B8=8D=E5=A4=9F=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E5=9B=9E=20seek?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fontlib.py | 45 ++++++++++++++++++++++----------------------- libs/fontlib.py | 49 +++++++++++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/fontlib.py b/fontlib.py index 4ab1e4a..0cc04e6 100644 --- a/fontlib.py +++ b/fontlib.py @@ -82,7 +82,7 @@ class FontLib(object): with open(self.__font_filename, 'rb') as font_file: self.__header = FontLibHeader(memoryview(font_file.read(FontLibHeader.LENGTH))) - self.__placeholder_buffer = self.__get_character_unicode_buffer(font_file, {ord('?')})[ord('?')] + self.__placeholder_buffer = self.__get_character_unicode_buffer(font_file, {ord('?')}, True)[0][1] gc.collect() @@ -92,8 +92,8 @@ class FontLib(object): def __is_gb2312(self, char_code): return 0x80 <= char_code <= 0xffef - def __get_character_unicode_buffer(self, font_file, unicode_set): - buffer_list = {} + def __get_character_unicode_buffer(self, font_file, unicode_set, is_placeholder=False): + buffer_list = [] ascii_list = [] gb2312_list = [] chunk_size = 1000 @@ -131,21 +131,17 @@ class FontLib(object): elif self.__is_gb2312(unicode): gb2312_list.append([unicode, struct.pack(' Date: Fri, 3 Sep 2021 12:58:48 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/fontlib.mpy | Bin 2341 -> 2662 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/libs/fontlib.mpy b/libs/fontlib.mpy index 12188752e0aaa35c12a474f9fc241673174f8825..9c9c26c971b89d6206301dc2a3ba675904566efa 100644 GIT binary patch delta 1116 zcmY*YU2NM_6ux$ToGw|@+NM8g$ELJ()1=)`lci!52|QC)rECo&ZBx2gj^ngZLo>zZ zXAj6X*{Z2%@ZtyF`i6uM4^HERX6po6n+-OGgpfcT0`W4QCJ<=_JRz=AO&!wJz1Qb_ z=iGC?@AzZ#H(LJ`@91qg)8BXf*w9cg*vl~U%LgRs$jZcWOX)V=hS?_l*!iHgZCQ1J zvu&Vt1fJ@9`S>Yq**b)UwUU*!zZSN@SYG?r+7*n70Du)wD!ZicsdPqK65f>ZE^l+7 z9ddwd9z*ttbb>lroPOis*a1jdsFT{9ty2v;;VXb_qDJ-|BM(^|&c+)FogXS+J~Pwl zgpr<}9`1^V8S+1O)IU4LGXBGCFUt;`4W2kR$M|LrbH0lcUJw9V0Z3lvLr3u%*PPcpj9w>6UInekp}PSR4dgQ#-7A{r0MqC5*djXlF6JT%P5;pS*T8Pv#}mH{muG@nH2F5#wsC;**gQ# z*Q1%)A5Z_P#^dWIRJCpFJPnH@>f`YWA9))*xK)Tnf8jbB?c72|I(6#clj6o92`-vU zb<8J*gC~af2t*NPQs++LZ}W8T)=oO5bSq|>CN>GH^YFC3F(mF5F&0;o_ zkwhg7;_NlmXE(I__Aa$ME%TQ$f+#IyGf63TJBd;<(W^8O6 zz)d9gIM_1G!oTMTSjs=zWFM<)4ZV5Z$j8e0dKj;j;ZI1%a^WAi&ADJ%uN#Fa)(u(2 zkiD^^%LA1))Dj6^T3icZ+h`PB*;GoF6h~4}gl04>^O7Q8eSNjX+l_9#xFF;NQIT?T z6a1*>IKxbOCtsKu0E6JRHc51=39w0m>mjgJT&kjO>| ztNfL(Lv|2O-__~o(fb)>`@FJ?R;7LvkA-nQ;sUjDKDJhdu_^XXHQ&(lTcl@n?gDv; zK7{C7Yy3L&eo>lExQ~4T&_GhQhL3A%rW2PHiO=Q`sRaBC`G|dAKLK^^cfx}iT9c#O dzy5!(n}8cnSqi@BvySvqDoemGv~!Lh{sSBAXygC@ delta 800 zcmY+COH30{6o&6iTc<-yDRUTA{JN8jVnVOs3PJG$5FE1dTCs zi)oMq6V1fL#Dtw2SJMXy+ky|4ZZu+CxWI*R1tz*NaS97$b&~Vnd(L;xf4$;d9qZ&8!)U_{c2dU4V-TR^#K&@Jlzv)<8-%QwPV-q=$jD`?4mx%; zNWj3a?Kj*K(r8EZWgb!1tRiP_i!yD#C5RJt<3Oa$;%|Em?=)zE^%^ulCOwsu?(njZ zn3Q-SnaoJpEayVQ?+dg!D@hY8EEa!Vpuzq2`EeF|<;OQx(U&lRR#&z*xE7>mw?;-k zg+3_pIOWrz$B&iDSGWm9pD-TRAUOgXQ;A;R$=*HI)oM@6&fM>6bX0Nc5M|M6_Wqqx z3Un<6ZY=vTy)R{|Hr9(klGSag7KRGtrML0#x_ds~$0;s$|I z(P(g~P|R(!c%`W0+EH=t5BeO&QBmYfNM}<-Zd0jRYEBBWVAa={lH|GYOrukeyf`jo z1W}eUSpv=WFw8|4pumgn>}d3Ox% zr$RYshYqdLfntfp#n`p$G4-3d)tdtvJPJ*_mk=gxbmFco@za^4lp#=F*NA=X7=ad4 lFWH1WQqPdi?k5M;Is!fYFEiH_YcZ#%#-<7MOnpp#{0CW-_znO7 -- Gitee From facedf132c5657cf8d4f37391f2175bd0008ee8f Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Fri, 3 Sep 2021 16:05:00 +0800 Subject: [PATCH 17/18] =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/main.py b/main.py index a0edbb7..04ce280 100644 --- a/main.py +++ b/main.py @@ -27,7 +27,8 @@ class FontLibTest(object): def load_font(self, font_file): start_time = ticks_us() self.__fontlib = FontLib(font_file) - print('### load font file: {} ms'.format(ticks_diff(ticks_us(), start_time) / 1000)) + diff_time = ticks_diff(ticks_us(), start_time) / 1000 + print('### load font file: {} ms'.format(diff_time)) self.__fontlib.info() self.__font_width = self.__fontlib.font_height @@ -155,7 +156,7 @@ class FontLibTest(object): def __fill_buffer(self, buffer, x, y, format): if isinstance(buffer, (bytes, memoryview)): buffer = framebuf.FrameBuffer(bytearray(buffer), self.__font_width, self.__font_height, format) - oled.blit(buffer, x, y) + self.__oled.blit(buffer, x, y) class FontLibTest2(object): @@ -219,8 +220,6 @@ class FontLibTest3(object): self.__font_width = self.__fontlib.font_height self.__font_height = self.__fontlib.font_height - gc.collect() - # self.__buffer_format = self.__get_format() def run_test(self, scroll_height=1, interval=20, chars=None, load_all=False, thread=False): if self.__oled is None or chars is None: @@ -235,10 +234,10 @@ class FontLibTest3(object): self.__setup() self.__fill_page(2, True) - self.__oled.blit(self.__fb_foreground, self.__x, self.__y) + self.__oled.blit(self.__fb_foreground, 0, 0) self.__oled.show() - self.__page_prepared = False + self.__page_prepared = True if self.__thread: self.__need_next_page = True _thread.start_new_thread(self.__fill_page_thread, ()) @@ -267,7 +266,7 @@ class FontLibTest3(object): self.__buffer_size = self.__oled_width // 8 * self.__oled_height self.__fb_foreground = framebuf.FrameBuffer(bytearray(self.__buffer_size * 2), self.__oled_width, self.__oled_height * 2, framebuf.MONO_VLSB) self.__fb_background = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) - self.__fb_temp = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) + # self.__fb_temp = framebuf.FrameBuffer(bytearray(self.__buffer_size), self.__oled_width, self.__oled_height, framebuf.MONO_VLSB) self.__scroll_timer = Timer(1) self.__buffer_dict = self.__fontlib.get_characters(self.__chars) if self.__load_all else {} @@ -314,11 +313,9 @@ class FontLibTest3(object): self.__fb_foreground.blit(buffer, x, y) if first else self.__fb_background.blit(buffer, x, y) x += self.__font_width self.__current_char_index += 1 - gc.collect() num -= 1 self.__current_page += 1 - gc.collect() self.__page_prepared = True def __scroll_cb(self, timer): @@ -330,49 +327,37 @@ class FontLibTest3(object): if not self.__page_prepared: return - # self.__fb_temp.blit(self.__fb_foreground, 0, 0) - # self.__fb_foreground.fill(0) - # self.__fb_foreground.blit(self.__fb_temp, 0, 0) self.__fb_foreground.blit(self.__fb_background, 0, self.__oled_height) # print('page ', self.__current_page, ' used') self.__page_prepared = False self.__fill_page() self.__x = self.__y = 0 - gc.collect() self.__fb_foreground.scroll(0, self.__scroll_speed * -1) self.__oled.blit(self.__fb_foreground, 0, 0) self.__oled.show() self.__y += self.__scroll_speed - gc.collect() def __scroll_thread(self): while self.__current_page <= self.__total_pages: if self.__y >= self.__oled_height: if not self.__page_prepared: - print('not') return - # self.__fb_temp.blit(self.__fb_foreground, 0, 0) - # self.__fb_foreground.fill(0) - # self.__fb_foreground.blit(self.__fb_temp, 0, 0) self.__fb_foreground.blit(self.__fb_background, 0, self.__oled_height) # print('page ', self.__current_page, ' used') self.__page_prepared = False self.__need_next_page = True - self.__x = 0 - self.__y = 0 + self.__x = self.__y = 0 self.__current_page += 1 - gc.collect() self.__fb_foreground.scroll(0, self.__scroll_speed * -1) self.__oled.blit(self.__fb_foreground, 0, 0) self.__oled.show() self.__y += self.__scroll_speed - # gc.collect() sleep(self.__scroll_interval / 1000) def __fill_page_thread(self): @@ -409,11 +394,8 @@ class FontLibTest3(object): self.__fb_background.blit(buffer, x, y) x += self.__font_width self.__current_char_index += 1 - gc.collect() - gc.collect() # print('\npage: ', self.__current_page, ' filled') - self.__page_prepared = True self.__need_next_page = False @@ -483,6 +465,6 @@ if __name__ == '__main__': # 读取所有字符数据,每次滚动 1 像素 # runner = FontLibTest3(oled) # runner.load_font('client/combined_vlsb.bin') - # runner.run_test(1, 80, chars2, True) + # runner.run_test(1, 20, chars2, True) gc.collect() -- Gitee From 1834206b943dcbdfdcc8acf688900597661fb5c4 Mon Sep 17 00:00:00 2001 From: Walkline Wang Date: Fri, 3 Sep 2021 16:05:08 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 75245dd..e8a9157 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,10 @@ $ ab abconfig-mpy 一次性读取太多的数据也会导致`内存溢出`,所以在实际使用时不建议一次性读取太多 +手动调用垃圾回收(gc.collect())的时机一定要掌握好,并不是任何时候手动调用都能起到预期的作用,有时还会起到反作用 + +对于能够自动回收的,如函数内部的变量,可以省去手动调用的操作 + > 统计运行时间使用的装饰器 ```python -- Gitee