LCD display and buttons in Python (tutorial and code)

Raspberry Pi related support

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

Postby Hairybiker » 04 Jul 2015, 11:39

I'm lazy as well, that's why I don't like too much typing :D
If I am re-using code then it goes into a procedure and is forgotten about :lol:

Given Python's strict layout issues, I find it is simpler to dump into a proc and just call it rather than paste/edit.
Been looking into MPD but there doesn't seem to be much you can read from it, no info like playing/left time etc.
Might be better reading it from the rune webpage and scraping the result.
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, 12:53

Actually, there are lots of of commands for MPD. I'm using MPD Python library, here you find all the commands: http://pythonhosted.org/python-mpd2/top ... mands.html

On the same way I'm getting artist and title, I think it's possible to get all other (current time, total time, bitrate, filename, size etc etc) but I will implement my own counter for elapsed time which will be syncronized on each change (new track, play/stop/pause). So it will be reseted to zero on song change and on pressing stop. And while playing, it will count independently. Because, it's not very good for optimization to "spam" MPD every 1 second to get elapsed time from it.

Since my college semester is done, I moved to my parents house during summer and now I'm facing a small problem: I have only one ethernet cable in my room for connecting my laptop and Wi-Fi signal is very poor in my room so I'm not able to listen to internet radio and SSH and RuneUI are pretty slow due to this. I will have to get an ethernet switch or something to connect it via cable or get a Wi-Fi repeater.

Here is my current setup: http://i.imgur.com/qe1mlA8.jpg :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

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

Postby Hairybiker » 04 Jul 2015, 12:54

Cheap switch is the best way, unless all your stuff is wifi.
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 » 05 Jul 2015, 12:05

UPDATE

05.07.2015

- Fixed problems with buttons; MPD was closing connection after 60 seconds of inactivity -> fixed with pinging MPD every 50 seconds
- Added dynamic backlight control: don't know if it will be useful for somebody, but I had problems due to display lightning up my room so I wasn't able to sleep :D now the backlight will go off when you stop or pause playback, after specified time, you can enable/disable this and specify the time between pause/stop and going off


HOW TO UPDATE FROM LATEST VERSION BEFORE THIS

To update to this version from latest before this, you only have to get the newest RA_LCD_Buttons.py. You can do this with GUI (if you have Samba enabled or another app like WinSCP, links are here:

TAR Archive: http://buzzthisnow.com/RA_LCD_Buttons.tar (TAR archive)
RAR Archive: http://buzzthisnow.com/RA_LCD_Buttons.rar (RAR archive)

Or you can do it in terminal (via SSH). First, remove the old one. Position yourself where your RA_LCD_Buttons folder is and remove it with:

Code: Select all
rm RA_LCD_Buttons


Then get a new one:

Code: Select all
wget http://buzzthisnow.com/RA_LCD_Buttons.tar


And then use this command to extract it:

Code: Select all
tar -xvf RA_LCD_Buttons.tar


Now you can delete the archive if you want, to free up some space, by executing:

Code: Select all
rm RA_LCD_Buttons.tar


It will create a folder named RA_LCD_Buttons, execute this command to enter it:

Code: Select all
cd RA_LCD_Buttons


Now you can start it with:

Code: Select all
nohup python2 RA_LCD_Buttons.py >/dev/null 2>&1 &



HARDWARE MODIFICATION FOR BACKLIGHT

If you want to use this feature for dynamic LCD backlight, a hardware modification is needed. You will need a BD139 transistor and a 10 kilo ohm resistor. Or if you know what you're doing, you can use any other suitable transistor-resistor combination. I used this one. The schematic is attached in this post. Then in RA_LCD_Buttons.py you have to change LCD_BACKLIGHT pin according to where you connected it and you have to specify backlight_timeout. If you put it to 0, it will be disabled.

COMMENTS

I'm currently struggling with PWM. This thing with backlight is just a beggining, I'm working on creating a dimmable backlight: you would be able to adjust backlight brightness with your remote, and also turn it off with a key press instead of this automatic turn off when paused/stopped.
Attachments
FMCPZRTG8LWOHW9.LARGE.jpg
FMCPZRTG8LWOHW9.LARGE.jpg (228.58 KiB) Viewed 3140 times
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 XploD » 21 Jul 2015, 13:28

Hairybiker wrote: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.



Correction: when installing, it's not python-i2c-tools, the correct one is i2c-tools only, so "pacman -S i2c-tools".

Also, if you're using Raspberry Pi 2, see here to enable I2C: how-to-enable-i2c-t1287.html
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 » 21 Jul 2015, 14:12

It's python2-i2c-tools if you are using python2 like I was, i2c-tools will install the python3 version, since python3 is the default in Arch Linux.
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 » 21 Jul 2015, 16:17

Hmm, this package doesn't exist:

Code: Select all
[root@runeaudio RA_LCD]# pacman -S python2-i2c-tools
error: target not found: python2-i2c-tools


Also, I installed it with "pacman -S i2c-tools" and it works with python2. I run my program with "python2 test.py" and the LCD works great.
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 schinken » 22 Jul 2015, 14:55

Hello!

This looks very promising and I orderer the i2c-interface and an 20x4 LCD.
While surfing ebay, I found a http://www.ebay.com/itm/121513512643 color LCD, too with SPI interface for same price as the 20x4+i2c.

Would be cool if we get this one running, showing the CD-cover or weather or whatever.

What do you all think?
schinken
 
Posts: 8
Joined: 13 May 2015, 10:25

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

Postby XploD » 23 Jul 2015, 11:18

Hello,

For a TFT screen, take a look at this topic: configuration-of-a-pitft-touchscreen-t731.html

I was thinking about buying a color screen as well but they are mostly to small or to expensive, so for now, I'm using a 20x4.

I'm currently rewriting the whole program for this to have more options, to work with 20x4 and I2C interface and for the code to be more tidy (I'll turn it from procedural to object-oriented). Once I finish, I will publish the code and the video to show it. Also, I put the code to github for easier access and updates.

Some screens so far:
Attachments
DSC_1548.JPG
DSC_1548.JPG (53.57 KiB) Viewed 3014 times
DSC_1549.JPG
DSC_1549.JPG (54.36 KiB) Viewed 3014 times
DSC_1547.JPG
DSC_1547.JPG (66.31 KiB) Viewed 3014 times
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 XploD » 23 Jul 2015, 11:19

And more (due to only 3 files per post):
Attachments
DSC_1545.JPG
DSC_1545.JPG (70.3 KiB) Viewed 3014 times
DSC_1546.JPG
DSC_1546.JPG (59.57 KiB) Viewed 3014 times
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 6 guests