• Benvenuti su RaspberryItaly!
Benvenuto ospite! Login Login con Facebook Registrati Login with Facebook


Valutazione discussione:
  • 0 voto(i) - 0 media
  • 1
  • 2
  • 3
  • 4
  • 5

[-]
Tags
gpio i2c e display

Display I2c e GPIO
#1
Salve... I problemi non vengono mai da soli Big Grin

Ora, complice il prezzo notevolmente basso, ho acquistato un display 16x2 i2c come quello in foto 
[Immagine: 8553_3.jpg]
[Immagine: 8553_2_2.jpg]

Iil venditore dichiara il  funzionamento a 5V.
Il mio problema nasce dal fatto che su alcune guide si parla di level shift, ma non so se questo sia da usare con il display che il venditore dichiara a 5V, ma, qualora lo fosse, dovrei far passare tutti e 4 i pin per lo shift level secondo questo schema?


[Immagine: Raspberry-Pi-16x2-i2c-lcd-wiring.jpg]

Grazie anticipatamente...
Risposta
#2
Il convertitore di livello ti serve solo se vuoi "leggere" quello che c'è scritto sul display, visto che ha uscita a 5V. Ma siccome è un display, normalmente ci si deve solo scrivere, e per evitare di avere 5V di ritorno, si mette a massa il pin 5 (RW) del display..
Vedi (ad esempio) questa guida
Lo ho anche io, e prima o poi conto di trovare il tempo di giocarci e scrivere una guida..
Risposta
#3
La guida che hai riportato, però non fa riferimento al display i2c con 4 pin, ma a quello senza il PIC con i collegamenti tramite 12 PIN... Ecco perchè  io parlavo del level shift... Ho visto prima questa  guida, poi questa guida e mi ha confuso le idee... Un altro quesito è quale linguaggio mi consigli? Python, C o altri (quali?), il tutto è sempre finalizzato al famoso audio streamer...  Cool
Risposta
#4
Io sono arrivato al Pi per avere una scusa per utilizzare Python, quindi..
Ps: esistono una marea di script già fatti per usare display simili con Volumio, ad esempio.
Pps: un streamer non è qualcosa che trasmette in streaming on rete? Tu, se non ho capito male, vuoi fare un player di musica liquida
Risposta
#5
Big GrinSi... ho sbagliato da streamer a Player...  Big Grin
Risposta
#6
Allora... Peggio di un'odissea... (la mia e di conseguenza la vostra...  Big Grin Big Grin  Big Grin )
Ho seguito questa guida per l'installazione della libreria i2c...

Ho scoperto digitando
Codice:
lsmod | grep i2c

ottengo come risposta:

[Immagine: gpio_raspy.jpg]

da cui si evince che probabilmente il mio processore è un 2385, e me lo dimostra il comando

Codice:
cat /code/cpuinfo

[Immagine: cpuinfo.jpg]
quindi, prima di fare sciocchezze Rolleyes  dovrei digitare in

Codice:
sudo nano /etc/modules
Codice:
i2c-bcm2385
i2c-dev
per poi caricare le librerie... Ma a questo punto, carico quelle su python-simbus 12c?
Risposta
#7
Si, certo.
Risposta
#8
Scusate, ma ogni tanto il lavoro mi tiene lontano dal raspy e dal forum...
Allora per cercare di andare avanti, e provare il display dovrei caricare la libreria Python. Quale, però? Ce ne sono milioni!!!  Huh Huh Huh  e come caricarla e renderla eseguibile?
Poi, cosa più importante (per me  Big Grin ), come la relaziono con i dati di MPD e non con dati tipo temperatura CPU ed altre varie ed eventuali? 

Credo che poi, per la prosecuzione e la comprensione del tutto convenga spostare il tutto (forse) se un mod lo ritiene opportuno nella sezione più opportuna...
Risposta
#9
Una libreria, da sola, non fa nulla. Permette di facilitare le potenzialità di Python, ma devi sempre essere in grado di scrivere un programma in Python, che vada a utilizzare quella libreria.
Risposta
#10
Ecco... Proprio lì andavo a parare... Allora... Googlando (sono ore che lo faccio) ho trovato questo script:
Codice:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
import textwrap
import time
import signal
import sys

import Adafruit_CharLCD as LCD

# Raspberry Pi gpio pin configuration
lcd_rs        = 5
lcd_en        = 6
lcd_d4        = 17
lcd_d5        = 23
lcd_d6        = 24
lcd_d7        = 27
lcd_backlight = 26

# define LCD column and row size for LCD
lcd_columns = 24
lcd_rows    = 2

# init the LCD using the pins above
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

def sigterm_handler(signal, frame):
 lcd.set_cursor(0,0)
 line1 = ('      Volumio 1.55      ')[0:lcd_columns]
 line2 = ('    shutting down...    ')[0:lcd_columns]
 lcd.message(line1 + '\n' + line2)
 sys.exit(0)

signal.signal(signal.SIGTERM, sigterm_handler)

def replace_specialchars(message):
 try:
   # message = message.encode('utf-8')
   message = message.replace('ä', chr(228))
   message = message.replace('ö', chr(246))
   message = message.replace('ü', chr(252))
   # message = message.replace('Ä', chr(196))
   # message = message.replace('Ö', chr(214))
   # message = message.replace('Ü', chr(220))
   # message = message.replace('ß', chr(223))
   # message = message.replace('°', chr(223))
   # message = message.replace('µ', chr(228))
   # message = message.replace('´', chr(96))
   # message = message.replace('€', chr(227))
   # message = message.replace('–', '-')
   # message = message.replace('“', '"')
   # message = message.replace('”', '"')
   # message = message.replace('„', '"')
   # message = message.replace('’', '\'')
   # message = message.replace('‘', '\'')
   # message = message.replace('è', '232');
   # message = message.replace('é', '233');
   # message = message.replace('ê', 'e');
   # message = message.replace('á', '225');
   # message = message.replace('à', '224');
   # message = message.prelace('â', 'a');
 except:
   return message;
 return message

def main():

 # print welcome message
 line1 = '      Volumio 1.55      '
 line2 = '  initializing system   '
 lcd.message(line1 + '\n' + line2)

 # create custom char (...) = three dots in one character
 char_threeDots = [0b00000,0b00000,0b00000,0b00000,0b00000,0b00000,0b10101,0b00000]
 # create in ram position 0
 lcd.create_char(0, char_threeDots)

 # wait 2 seconds
 time.sleep(2.0)

 # clear display and turn on backlight
 lcd.clear()
 # lcd.backlight(lcd.ON)

 songArtistPrevious = ''
 stoppedSet = 0
 z = 0

 while True:
   # get current status and playtime
   process = subprocess.Popen('mpc', shell=True, stdout=subprocess.PIPE)
   status = process.communicate()[0]
   statusLines = status.split('\n')

   # check if mpc returns more that one line plus an extra, in that case we dont have stopped the music and can parse additional data
   if len(statusLines) > 2:
     # extract the song name / artist name (first line)
     # web radio has station name before artist / song
     songArtist = statusLines[0]
     if ":" in songArtist:
       songArtist = statusLines[0].split(':',1)[1].strip()
     if ":" not in songArtist:
       songArtist = statusLines[0]
           
     # extract player status (playing/stopped)
     infoPlayerStatus = statusLines[1].split(' ',1)[0].strip()

     # check for song change; only write new data to display if there is a change cause this is nicer to cpu
     if songArtist != songArtistPrevious:
       songArtistPrevious = songArtist
       stoppedSet = 0
       z = 0

       infoArtist = songArtist.split(' - ',1)[0].strip()
       # shorten artist name if longer than lcd_columns
       if len(infoArtist) > lcd_columns:
         infoArtist = (infoArtist)[0:lcd_columns - 1] + '\x00'
       # center
       while len(infoArtist) < (lcd_columns -1):
         infoArtist = ' ' + infoArtist + ' '
       infoArtist = infoArtist + '        '

       infoSong = songArtist.split(' - ',1)[1].strip()
       # shorten song name if longer than lcd_columns
       if len(infoSong) > lcd_columns:
         infoSong = (infoSong)[0:lcd_columns - 1] + '\x00'
       # center
       while len(infoSong) < (lcd_columns -1):
         infoSong = ' ' + infoSong + ' '
       infoSong = infoSong + '        '

       # extract a string like '2:01/3:43 (54%)' from the string '[playing] #2/8   2:01/4:38 (50%)'
       infoLine = statusLines[1].split(']',1)[1].strip()
       # remove first character ('#')
       # format aftwerwards: 2/8   2:01/4:38 (50%)
       infoLine = infoLine[1:]
       infoTrack = infoLine.split(' ',1)[0].strip()
       infoTimes = infoLine.split(' ',1)[1].strip()
       # split of (50%)
       infoTimes = infoTimes.split('(',1)[0].strip()
       infoTotalPlaytime = infoTimes.split('/',1)[1].strip()
           
     # toggle artist / track info
     if z > 40:
       z = 0

     if z == 0:            
       # show song title and total play time / track
       lcd.set_cursor(0,0)
       line1 = (infoSong)[0:lcd_columns]
       line2 = ('Track: '+infoTrack+' Time: '+infoTotalPlaytime)[0:lcd_columns]
       while len(line2) < (lcd_columns -1):
         line2 = ' ' + line2 + ' '
       if infoPlayerStatus == '[paused]':
         line2 = '    [playing paused]    '
       # clean special chars
       line1 = replace_specialchars(line1)
       # line2 = replace_specialchars(line2)
       lcd.message(line1 + '\n' + line2)
           
     if z == 10:
       # show song title and artist
       lcd.set_cursor(0,0)
       line1 = (infoSong)[0:lcd_columns]
       line2 = (infoArtist)[0:lcd_columns]
       # clean special chars
       line1 = replace_specialchars(line1)
       line2 = replace_specialchars(line2)
       lcd.message(line1 + '\n' + line2)

   else:
     # message when stopped
     # set message only once, not in a loop (cpu friendly)
     if stoppedSet != 1:
       lcd.set_cursor(0,0)
       line1 = ('      Volumio 1.55      ')[0:lcd_columns]
       line2 = ('   [no music playing]   ')[0:lcd_columns]
       lcd.message(line1 + '\n' + line2)
       songArtistPrevious = ''
       stoppedSet = 1

   # sleep 0.5s
   time.sleep(0.5)
   z += 1

if __name__ == '__main__':

che però sembra essere scritto per un display NON i2c ma che avrebbe i requisiti che a me occorrono...
Ora chiedo a voi esperti:

girovagando in rete ho trovato questo script:
Codice:
# Credit for this code goes to "natbett" of the Raspberry Pi Forum 18/02/13

# https://www.raspberrypi.org/forums/viewtopic.php?t=34261&p=378524
# Before running this code make sure to run sudo i2cdetect -y 1
# and match the LCD address of your device

import smbus
from time import *

# LCD Address
ADDRESS = 0x27

# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
]LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80

# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00

# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00

# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00

# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00

# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00

En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit

class i2c_device:
   def __init__(self, addr, port=1):
      self.addr = addr
      self.bus = smbus.SMBus(port)

# Write a single command
   def write_cmd(self, cmd):
      self.bus.write_byte(self.addr, cmd)
      sleep(0.0001)

class lcd:
   #initializes objects and lcd
   def __init__(self):
      self.lcd_device = i2c_device(ADDRESS)

      self.lcd_write(0x03)
      self.lcd_write(0x03)
      self.lcd_write(0x03)
      self.lcd_write(0x02)

      self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
      self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
      self.lcd_write(LCD_CLEARDISPLAY)
      self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
      sleep(0.2)

   # clocks EN to latch command
   def lcd_strobe(self, data):
      self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
      sleep(.0005)
      self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
      sleep(.0001)

   def lcd_write_four_bits(self, data):
      self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
      self.lcd_strobe(data)

   # write a command to lcd
   def lcd_write(self, cmd, mode=0):
      self.lcd_write_four_bits(mode | (cmd & 0xF0))
      self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))

   # put string function
   def lcd_display_string(self, string, line):
      if line == 1:
         self.lcd_write(0x80)
      if line == 2:
         self.lcd_write(0xC0)
      if line == 3:
         self.lcd_write(0x94)
      if line == 4:
         self.lcd_write(0xD4)

      for char in string:
         self.lcd_write(ord(char), Rs)

   # clear lcd and set to home
   def lcd_clear(self):
      self.lcd_write(LCD_CLEARDISPLAY)
      self.lcd_write(LCD_RETURNHOME)

display = lcd()
display.lcd_display_string("RaspiNews - 16x2", 1)
display.lcd_display_string("I2C LCD Demo..", 2)
mi scuso per il color ed il font, ma non riesco a cancellarli... tel o ho ripulito io. Zzed
Poichè, però, io sono leggermente capra nella programmazione chiedo: è possibile "aprire" lo script per poterlo confrontare ed eventualmente editare per arrivare ad una cosa similare a quello riportato sopra? e come?
E per provare se lo script funziona, come faccio?
Grazie.
Risposta
  


Vai al forum:


Navigazione: 1 Ospite(i)
Forum con nuovi Post
Forum senza nuovi post
Forum bloccato
Forum Redirect