LCD display and buttons in Python (tutorial and code)

Raspberry Pi related support

Re: LCD display and buttons in Python (tutorial and code)

Postby XploD » 17 Jun 2015, 11:21

I'm using the old Raspberry Pi and I newer tried it on the Pi 2 but seems that the problem is due to using the old RPi.GPIO, which doesn't support RPi 2. Frankly, I don't even know why I used that old version :D I'm using 0.5.2a in tutorial and newest one is 0.5.11; they added support for RPi 2 in 0.5.10.

So try to install the newest one:

First, you have to download it. Position yourself on desired directory (for example "cd /root")

Code: Select all
wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz


Extract it with:

Code: Select all
tar -xvf RPi.GPIO-0.5.11.tar.gz


And install it with:

Code: Select all
cd RPi.GPIO-0.5.11


Code: Select all
python2 setup.py install


If you experience some errors because an older version is already installed, you can first try to remove the older version. I don't know exactly how to do it and currently don't have time to test, but it should be something like:

Code: Select all
pacman -Rns RPi.GPIO


Currently I don't have time to test this by myself, but you can try it yourself and let me know if it works! If it does, then I will test it with older Pi as soon as I get some free time and update the tutorial.

I hope it will work :)

NOTE

Later I noticed that something is wrong with the buttons. The may work for a while but sooner or later it will bring up an error and buttons will stop working. But display will continue to work. Currently I have final exams on my college so until I finish with it, I can't work on this, and there's a lot of things to fix. Sorry for that.

But I'm using it since I created it with IR remote and LCD display and it works good. As I said, it consume a lot of resources (about 20-30% CPU constantly on older RPi and it's the process with highest CPU usage) but I haven't noticed any problems or performance decrease because of that. It will work probably even better on RPi 2 which is more powerfull. Unfortunately, I wasn't able to decrease this CPU usage. The problem is writting to display which takes a lot of CPU usage and since it scrolls the text, it writes to display almost all the time making such high CPU usage. And the faster the speed of scrolling is, the more CPU usage it takes. I tried all libraries for LCD I could find but all of them consume a lot of CPU power, some of them more, some of them less. This Adafruit one is not the lightest one but works the best. There's a library which consumes less CPU power, but on song change the scroll slows down a lot.
Music taste: xplodmusic @ Last.fm
Audio source: Raspberry Pi 2 + Sabre ES9023 DAC + WD 500 GB HDD + TP-Link TL-WN722N Wi-Fi + D-LINK Dub H7 USB hub
Hi-Fi: Phillips DCD-7010
Mixer: Nady Audio MM-242
Misc: Tube Stereo Vumeter (Magic Eye)
User avatar
XploD
 
Posts: 77
Joined: 24 Mar 2015, 18:32
Location: Croatia

Re: LCD display and buttons in Python (tutorial and code)

Postby hondagx35 » 17 Jun 2015, 14:47

phuongdk wrote:Dear XploD,
I did all step without problem.
But when I run "python2 RA_LCD_Buttons.py" to execute your code, it has an error as images below:

Untitled.png


I'm using Raspberry PI 2 (lastest version).
Please help me, thanks ;)



Hi phuongdk,

you have to do this steps on your Raspberry Pi, not on your PC.
It seems as you have forgotten to ssh to your pi.

SH is built into Linux distributions and Mac OS, and a third-party SSH client is available for Windows. See the following guides for using SSH depending on the operating system used by the computer you are connecting from:
Linux & Mac OS
Windows

Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: LCD display and buttons in Python (tutorial and code)

Postby XploD » 17 Jun 2015, 15:14

Oh I didn't even see the whole picture, I took a look only for an error and didn't see it was in console of PC instead SSH.
Music taste: xplodmusic @ Last.fm
Audio source: Raspberry Pi 2 + Sabre ES9023 DAC + WD 500 GB HDD + TP-Link TL-WN722N Wi-Fi + D-LINK Dub H7 USB hub
Hi-Fi: Phillips DCD-7010
Mixer: Nady Audio MM-242
Misc: Tube Stereo Vumeter (Magic Eye)
User avatar
XploD
 
Posts: 77
Joined: 24 Mar 2015, 18:32
Location: Croatia

Re: LCD display and buttons in Python (tutorial and code)

Postby Hairybiker » 01 Jul 2015, 15:34

Thanks for the code.
I have hacked it about to use my i2c LCD's (both 2x16 &4x20 currently only uses the 1st 2 lines but that could be changed)
If anyone is interested I will post the mods and new library (to interface with the i2c controller)
DRM is to rights management like DDT is to pest management.
Happy Linux Mint User. Check it out at www.linuxmint.com for a COMPLETELY FREE OS.
User avatar
Hairybiker
 
Posts: 14
Joined: 01 Jul 2015, 15:31

Re: LCD display and buttons in Python (tutorial and code)

Postby XploD » 01 Jul 2015, 16:41

You're welcome. I just ordered 2 4x20 LCDs from eBay and a few paralel to I2C converters from eBay and the semester on my college is over so I'm going to continue to work on this. Have you noticed any better performance on I2C? If you want, you can post your code for I2C so I can see which one did you use. I hope that it should work better on I2C since it's hardware implemented on Pi and it should reduce CPU load.

Unfortunately, it will took some time for the LCDs to arrive (15-25 days) but in the meantime, I will try to fix buttons, auto-start on boot and I will experiment with another informations for lower 2 rows. Also, I will try to connect LIRC and Python to receive commands from IR remote so I will try to achieve turning on/off shuffle/repeat and other as well as changing playlists/folders with remote.

Also, tomorrow I'm going to buy transistors and resistors so I can be able to turn-off LCD backlight or even control it's brightness using PWM on Pi. When paused or stopped, after few seconds the backlight will go off automatically so I could sleep during night :mrgreen:

And yes, I ordered parts for hardware SD cards switcher, since is not possible (at least no one succeded AFAIK) to do a dual boot with OSMC so I will use a microcontroller to switch between 2 different cards.
Music taste: xplodmusic @ Last.fm
Audio source: Raspberry Pi 2 + Sabre ES9023 DAC + WD 500 GB HDD + TP-Link TL-WN722N Wi-Fi + D-LINK Dub H7 USB hub
Hi-Fi: Phillips DCD-7010
Mixer: Nady Audio MM-242
Misc: Tube Stereo Vumeter (Magic Eye)
User avatar
XploD
 
Posts: 77
Joined: 24 Mar 2015, 18:32
Location: Croatia

Re: LCD display and buttons in Python (tutorial and code)

Postby Hairybiker » 01 Jul 2015, 18:16

OK
The i2c modules I have (currently several of them :D ) is this one:
ebay link

99p delivered or £2 if you want one from the UK. (on Amazon)

Program:
Link to source code for i2c_lcd driver & interface program.
The i2c needs to be enabled:
edit /etc/modules-load.d/raspberrypi.conf
And add:
Code: Select all
i2c-bcm2708
i2c-dev


to it.

Add the python modules:
Code: Select all
pacman install python2-i2c-tools python-mpd2


Then shutdown and install the lcd:
Wiring:
Code: Select all
LCD     PI
-ve     pin 6 (5V you MAY get away with 3V3 Pin 1)
+ve     pin 2
sda     pin 3
sdc     pin 5

(All pins are for a Pi B, don't know if they changed on the B+/2)

Boot up and test:
i2cdetect -y 1

It should show this:

Code: Select all
[root@runeaudio latest]# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- 



27 is where the lcd is.
Ignore the UU.

Edit the program to set your lcd size (RA_I2C_LCD.py)
look for
lcd_columns = 20
lcd_rows = 4

and change if needed.
To run the program :
python2 RA_I2C_LCD.py

(I didn't use any buttons so you can add them if you need to.)
and it should display the artist & track.
If you don't see anything or a black display adjust the blue module on the back (with a small screwdriver SLOWLY) till you can read the screen.

You have various options in the I2C library - I hacked it about from another web site (see link below), adding extra bits.
Things like cursor/blink on/off, scroll[line #] (not as nice as your bi-directional scroll), line[line #] (to display on line #, variable scroll speed, backlight(on/off), display at a position on any line (x,y).
Will try and document them one of these days, but still hacking it at the moment. :P

Only just got my LCD working yesterday using the base library (HERE) which I have been hacking to introduce extra functions I wanted.

If you want to hack the driver about be my guest, I am still learning Python so it is probably not optimised at all.
NB it doesn't like /n, no support for spanning using cr. Still looking into that one.
DRM is to rights management like DDT is to pest management.
Happy Linux Mint User. Check it out at www.linuxmint.com for a COMPLETELY FREE OS.
User avatar
Hairybiker
 
Posts: 14
Joined: 01 Jul 2015, 15:31

Re: LCD display and buttons in Python (tutorial and code)

Postby Hairybiker » 02 Jul 2015, 17:02

I have been looking at your scrolling routine, trying to make sense of it :D

You reuse the same code several times in the Artist/Title scroll, wouldn't it be better to make a function for that?

I also couldn't figure out how your scroll actually works :?:
Is this right?
counter[1] is the x position
direction[0] is 0 - normal 1 - back

but couldn't figure out HOW it works, can you enlighten me please.
DRM is to rights management like DDT is to pest management.
Happy Linux Mint User. Check it out at www.linuxmint.com for a COMPLETELY FREE OS.
User avatar
Hairybiker
 
Posts: 14
Joined: 01 Jul 2015, 15:31

Re: LCD display and buttons in Python (tutorial and code)

Postby XploD » 04 Jul 2015, 08:43

Thank you for your code and explanation! I ordered that same i2c module (3 pieces), now waiting for it and the display to arrive.

My code is a mess for a few reasons: I'm new to Python and new to all this (coding for GPIO on Pi) and when I'm working on something new, I'm not careful with code tidiness. I use try-and-see-what-happens method :D After I finish with everything, I'll work on making the code more tidy.

So yes, I have a reused part of code; the same thing is for artist and title. direction, counter and framebuffer are arrays with 2 elements: 0 is for first line and 1 is for second line. counter is a position, yes, and direction is 0 for normal and 1 for back as you said. The code is absoluetly the same for both lines so I will explain on first one. First I check if artist name is longer than 16, if it is, then I check if currently a file is playing, or if the radio is playing, I check if radio scroll is enabled. Then I take a 16 charachters long part of the name, starting from position[0] till position[0] + 16 into framebuffer[0]. If direction[0] is 0, then we scroll forwards and first I check if I reached the end (artist_length - 16) and if I did, I change the direction. Otherwise I increase counter[0]. So, depending on the counter, in first pass it will take a part of the artist from 0-15, in the next pass 1-16, then 2-17 and so on.

If artist name length is exactly 16, then I took the whole name into framebuffer. And last, if it's smaller than 16, then I took the whole name and fit empty places with spaces. At the end, I print both framebuffers into first and second line repsectively, and let it sleep for a while (scrolling period).
Music taste: xplodmusic @ Last.fm
Audio source: Raspberry Pi 2 + Sabre ES9023 DAC + WD 500 GB HDD + TP-Link TL-WN722N Wi-Fi + D-LINK Dub H7 USB hub
Hi-Fi: Phillips DCD-7010
Mixer: Nady Audio MM-242
Misc: Tube Stereo Vumeter (Magic Eye)
User avatar
XploD
 
Posts: 77
Joined: 24 Mar 2015, 18:32
Location: Croatia

Re: LCD display and buttons in Python (tutorial and code)

Postby Hairybiker » 04 Jul 2015, 10:02

Cheers for that, yep I am new to Python as well, I used to program in C about 20 years ago, but not done much recently. I wanted to learn Python and this looked like a good way to do it.

I have rebuilt my library to make it more consistent and will try and add your bi-dir scolling then up load it again.
DRM is to rights management like DDT is to pest management.
Happy Linux Mint User. Check it out at www.linuxmint.com for a COMPLETELY FREE OS.
User avatar
Hairybiker
 
Posts: 14
Joined: 01 Jul 2015, 15:31

Re: LCD display and buttons in Python (tutorial and code)

Postby XploD » 04 Jul 2015, 11:27

To my shame, I'm an engineering student and a programmer :D But I usually program in PHP, Java or C. But I'm usually lazy so as I said, when working with something new, I'm to lazy to create functions, classes or similar so I just write a code in a way I think. Once when I know what I'm doing, then I rewrite the code to be more clean.

While waiting for display, I will try to make the code cleaner and I will experiment with LCD backlight dimming and with fetching other infos from MPD.

Also, my laziness is the reason why my hardware is on a proto board. I created it like this to test it and now I'm too lazy to solder it onto a board and create a chasis for all together :mrgreen:
Music taste: xplodmusic @ Last.fm
Audio source: Raspberry Pi 2 + Sabre ES9023 DAC + WD 500 GB HDD + TP-Link TL-WN722N Wi-Fi + D-LINK Dub H7 USB hub
Hi-Fi: Phillips DCD-7010
Mixer: Nady Audio MM-242
Misc: Tube Stereo Vumeter (Magic Eye)
User avatar
XploD
 
Posts: 77
Joined: 24 Mar 2015, 18:32
Location: Croatia

support RuneAudio Donate with PayPal

PreviousNext

Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 12 guests