博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用RPLCD滚动文本
阅读量:2522 次
发布时间:2019-05-11

本文共 4179 字,大约阅读时间需要 13 分钟。

RPLCD库 (The RPLCD Library)

Last year when I started playing around with HD44780 based character LCDs on the Raspberry Pi, I did not find any software library for it that I liked. There were some C++ libraries around and also some Python scripts, but they were a bit messy and not as clean and consistent as I’m used to from the Python community. So I wrote my own library: .

去年,当我开始在Raspberry Pi上玩基于HD44780的字符LCD时,没有找到我喜欢的任何软件库。 周围有一些C ++库和一些Python脚本,但是它们有点混乱,不像我从Python社区习惯的那样干净和一致。 所以我写了我自己的库: 。

In contrast to other libraries, RPLCD tries to be “Pythonic”, which means that I don’t want to simply copy the C implementations, but use Python features where appropriate. The result of this is that I’m using a clean modular approach and make use of language features like properties and context managers. Here’s a small example on how to write a text to a 20×4 LCD:

与其他库相比,RPLCD试图成为“ Pythonic”,这意味着我不想简单地复制C实现,而是在适当的地方使用Python功能。 结果是我使用了一种干净的模块化方法,并利用了诸如特性和上下文管理器之类的语言功能。 这是一个有关如何将文本写入20×4 LCD的小例子:

>>> >>> from from RPLCD RPLCD import import CharLCDCharLCD>>> >>> lcd lcd = = CharLCDCharLCD ()()>>> >>> lcdlcd .. write_stringwrite_string (( u'Raspberry Pi HD44780'u'Raspberry Pi HD44780' ))>>> >>> lcdlcd .. cursor_pos cursor_pos = = (( 22 , , 00 ))>>> >>> lcdlcd .. write_stringwrite_string (( u'http://github.com/u'http://github.com/ nrnr dbrgn/RPLCD'dbrgn/RPLCD' ))

The result:

结果:

HD44780 LCD模块的照片显示一些文本。

If you want to know more, please refer to the .

如果您想了解更多信息,请参阅 。

滚动文字 (Scrolling Text)

Writing a string to the display with RPLCD is easy. But what about more dynamic things, like scrolling text? That’s not hard to implement by using a “frame buffer” data structure.

使用RPLCD将字符串写到显示器很容易。 但是,诸如滚动文本之类的更具动态性的东西呢? 通过使用“帧缓冲区”数据结构不难实现。

The idea is that you create a data structure which contains all the characters that should be written to the display later on. This data structure can then be manipulated with standard Python tools. A good choice here would be a 2-dimensional list, representing rows and columns. In the case of a 2×16 LCD:

想法是创建一个数据结构,其中包含以后应写入显示的所有字符。 然后可以使用标准Python工具来操纵此数据结构。 一个不错的选择是二维列表,它代表行和列。 如果是2×16 LCD:

Then we need a function to actually write the framebuffer to the device. It should properly truncate the strings to the max column length.

然后,我们需要一个函数将帧缓冲区实际写入设备。 它应该正确地将字符串截断为最大列长度。

>>> >>> def def write_to_lcdwrite_to_lcd (( lcdlcd , , framebufferframebuffer , , num_colsnum_cols ):):...     ...     """Write the framebuffer out to the specified LCD.""""""Write the framebuffer out to the specified LCD."""...     ...     lcdlcd .. homehome ()()...     ...     for for row row in in framebufferframebuffer ::...         ...         lcdlcd .. write_stringwrite_string (( rowrow .. ljustljust (( num_colsnum_cols )[:)[: num_colsnum_cols ])])...         ...         lcdlcd .. write_stringwrite_string (( '' rnrn '' ))

Write the framebuffer to the LCD:

将帧缓冲区写入LCD:

Now we have a framebuffer that simply writes “Hello!” to the first row of the LCD. But we want a scrolling text across the second row… To achieve this, we need to manipulate the framebuffer, write it to the display, sleep for a short while and repeat. A simple version might look like this:

现在我们有了一个帧缓冲区,它只写了“ Hello!”。 到LCD的第一行。 但是,我们希望在第二行上滚动文本……要实现这一点,我们需要操纵帧缓冲区,将其写入显示器,睡眠一会儿然后重复。 一个简单的版本可能如下所示:

>>> >>> import import timetime>>> >>> long_string long_string = = 'This string is too long to fit''This string is too long to fit'>>> >>> for for i i in in rangerange (( lenlen (( long_stringlong_string ) ) - - 16 16 + + 11 ):):...     ...     framebufferframebuffer [[ 11 ] ] = = long_stringlong_string [[ ii :: ii ++ 1616 ]]...     ...     write_to_lcdwrite_to_lcd (( lcdlcd , , framebufferframebuffer , , 1616 ))...     ...     timetime .. sleepsleep (( 0.20.2 ))

This simply scrolls the text into the visible area and stops. To get infinite scrolling, simply add 16 blank characters at the beginning and end of the string and repeat. Here’s the extended version of the above code, wrapped in a function:

这只是将文本滚动到可见区域并停止。 要无限滚动,只需在字符串的开头和结尾添加16个空白字符,然后重复。 这是上述代码的扩展版本,包含在一个函数中:

Now you got infinite scrolling text on the second row, while the first row stays static.

现在,您在第二行获得了无限滚动文本,而第一行保持不变。

翻译自:

转载地址:http://siqwd.baihongyu.com/

你可能感兴趣的文章
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>