Rasp-Hack-Afficheur-LCD-Python

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche

Code Python

The Python code for Adafruit's Character LCDs on the Pi is available on Github at https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code

There are two files we will be working with:

  • Adafruit_CharLCD.py : contains python class for LCD control
  • Adafruit_CharLCD_IPclock_example.py : code for IP address and date/time calls methods from Adafruit_CharLCD.py


The first file Adafruit_CharLCD.py is a mashup from two different sources of LCD code. Github user lrvick put together a nice python class. His work is the baseline that is slowly being changed as we are bringing in more elements from the Arduino LiquidCrystal library.

The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros. Simply run the following commands from an appropriate location (ex. "/home/pi"):

apt-get install git
git clone git://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code
cd Adafruit_CharLCD

Tester

You can test the wiring from the previous step by simply running the Adafruit_CharLCD.py python code, as it has a little code it in that will simply display a test message when wired correctly

If you're using a Version 2 Raspberry Pi, pin #21 has been replaced with pin #27 so edit Adafruit_CharLCD.py and change:

def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None):

to

def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None):


you can use nano Adafruit_CharLCD.py to edit

chmod +x Adafruit_CharLCD.py
sudo ./Adafruit_CharLCD.py

L'exemple Horloge + IP

#!/usr/bin/python
     
from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime
     
lcd = Adafruit_CharLCD()
     
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
     
lcd.begin(16,1)
     
def run_cmd(cmd):
    p = Popen(cmd, shell=True, stdout=PIPE)
    output = p.communicate()[0]
    return output
     
while 1:
    lcd.clear()
    ipaddr = run_cmd(cmd)
    lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
    lcd.message('IP %s' % ( ipaddr ) )
    sleep(2)

Executer le code

Démarrer l'exemple IP + horloge

$ sudo ./Adafruit_CharLCD_IPclock_example.py 

Résultat

Ce que vous devriez voir:

Rasp-Hack-Afficheur-LCD-Python-01.jpg


Source: Character LCD with Raspberry Pi or BeagleBone Black écrit par Tony Dicola pour Adafruit Industries.

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.