Re: Python2 instalacja na Bookworm / ( moduł : smbus ) ?
: 03 stycznia 2025, 17:59
Dzięki za info i namiary ...w chwili wolnej będę walczył może skumam jak to usprawnić na przyszłość. I oczywiście podam rozwiązanie.
Polski portal użytkowników dystrybucji Debian GNU/Linux, dyskusje, artykuły, nowości, blog, porady, pomoc.
https://www.debian.pl/
Kod: Zaznacz cały
apt install python3-full
Kod: Zaznacz cały
apt install python3-smbus2
Kod: Zaznacz cały
apt install python3-smbus
Kod: Zaznacz cały
2to3 skrypt1 [opcje] skrypt2
Kod: Zaznacz cały
/usr/bin/2to3:3: DeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
from lib2to3.main import main
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored start.py
--- start.py (original)
+++ start.py (refactored)
@@ -95,7 +95,7 @@
'Ź': 'Z',
'Ż': 'Z',
}
- for f, t in pl.items():
+ for f, t in list(pl.items()):
chars = chars.replace(f,t)
return chars
@@ -111,7 +111,7 @@
ekran1 = replace_pl(ekran1)
statusLines = ekran1.split('\n')
ekran1 = statusLines[1]
- if ekran1 <> '':
+ if ekran1 != '':
ekran1L1 = ekran1.split(' ',1)[0].strip()
ekran1L2temp = ekran1.split(' ',1)[1].strip()
ekran1L2 = ekran1L2temp.split(' ',1)[0].strip()
RefactoringTool: Can't parse start1.py: IndentationError: unindent does not match any outer indentation level (<tokenize>, line 114)
RefactoringTool: Files that need to be modified:
RefactoringTool: start.py
RefactoringTool: There was 1 error:
RefactoringTool: Can't parse start1.py: IndentationError: unindent does not match any outer indentation level (<tokenize>, line 114)
Kod: Zaznacz cały
root@orangepi3-lts:~# python3 start2.py
Kod: Zaznacz cały
Traceback (most recent call last):
File "/root/start2.py", line 177, in <module>
main()
File "/root/start2.py", line 111, in main
ekran1 = replace_pl(ekran1)
^^^^^^^^^^^^^^^^^^
File "/root/start2.py", line 99, in replace_pl
chars = chars.replace(f,t)
^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Kod: Zaznacz cały
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smbus
import time
import subprocess
from time import sleep, strftime
from datetime import datetime
from subprocess import *
# Define some device parameters
I2C_ADDR = 0x3f # I2C device address
LCD_WIDTH = 40 # Maximum characters per line
LCD_HEIGHT = 2
# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x0C # Off
ENABLE = 0b00000100 # Enable bit
# Timing constants
E_PULSE = 0.005
E_DELAY = 0.005
#Open I2C interface
bus = smbus.SMBus(0) # Rev 1 Pi uses 0 (and Orange PI PC, for pins 3 and 5)
#bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def lcd_byte(bits, mode):
# Send byte to data pins
# bits = the data
# mode = 1 for data
# 0 for command
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
# High bits
bus.write_byte(I2C_ADDR, bits_high)
lcd_toggle_enable(bits_high)
# Low bits
bus.write_byte(I2C_ADDR, bits_low)
lcd_toggle_enable(bits_low)
def lcd_toggle_enable(bits):
# Toggle enable
time.sleep(E_DELAY)
bus.write_byte(I2C_ADDR, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
time.sleep(E_DELAY)
def lcd_string(message,line):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
def replace_pl(chars):
pl = { 'ą': 'a',
'ć': 'c',
'ę': 'e',
'ł': 'l',
'ń': 'n',
'ó': 'o',
'ś': 's',
'ź': 'z',
'ż': 'z',
'Ą': 'A',
'Ć': 'C',
'Ę': 'E',
'Ł': 'L',
'Ń': 'N',
'Ó': 'O',
'Ś': 'S',
'Ź': 'Z',
'Ż': 'Z',
}
for f, t in list(pl.items()):
chars = chars.replace(f,t)
return chars
def main():
counter = 0
while True:
# Get current status and playtime
process = subprocess.Popen('sudo mpc -v', shell=True, stdout=subprocess.PIPE)
ekran1 = process.communicate()[0]
ekran1 = replace_pl(ekran1)
statusLines = ekran1.split('\n')
ekran1 = statusLines[1]
if ekran1 != '':
ekran1L1 = ekran1.split(' ',1)[0].strip()
ekran1L2temp = ekran1.split(' ',1)[1].strip()
ekran1L2 = ekran1L2temp.split(' ',1)[0].strip()
process = subprocess.Popen('sudo mpc -f %name%', shell=True, stdout=subprocess.PIPE)
ekran2 = process.communicate()[0]
ekran2 = replace_pl(ekran2)
statusLines = ekran2.split('\n')
ekran2 = statusLines[0]
ekran2L1 = ekran2[0:40]
ekran2L2 = ekran2[40:80]
process = subprocess.Popen('sudo mpc -f %artist%', shell=True, stdout=subprocess.PIPE)
ekran3 = process.communicate()[0]
ekran3 = replace_pl(ekran3)
statusLines = ekran3.split('\n')
ekran3 = statusLines[0]
ekran3L1 = ekran3[0:40]
ekran3L2 = ekran3[40:80]
process = subprocess.Popen('sudo mpc -f %title%', shell=True, stdout=subprocess.PIPE)
ekran4 = process.communicate()[0]
ekran4 = replace_pl(ekran4)
statusLines = ekran4.split('\n')
ekran4 = statusLines[0]
ekran4L1 = ekran4[0:40]
ekran4L2 = ekran4[40:80]
if counter == 1:
if ekran1 == "":
counter = 7
else:
lcd_string(ekran1L1,LCD_LINE_1)
lcd_string(ekran1L2,LCD_LINE_2)
if counter == 7:
if ekran2 == "":
counter = 14
else:
lcd_string(ekran2L1,LCD_LINE_1)
lcd_string(ekran2L2,LCD_LINE_2)
if counter == 14:
if ekran3 == "":
counter = 30
else:
lcd_string(ekran3L1,LCD_LINE_1)
lcd_string(ekran3L2,LCD_LINE_2)
if counter == 30:
if ekran4 == "":
counter = 46
else:
lcd_string(ekran4L1,LCD_LINE_1)
lcd_string(ekran4L2,LCD_LINE_2)
sleep(0.1)
counter = counter + 1
if counter == 53:
counter = 1
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
lcd_byte(0x00, LCD_CMD)
lcd_byte(0x01, LCD_CMD)
lcd_byte(0x08, LCD_CMD)
Kod: Zaznacz cały
Traceback (most recent call last):
File "/root/start2.py", line 39, in <module>
bus = smbus.SMBus(0) # Rev 1 Pi uses 0 (and Orange PI PC, for pins 3 and 5)
^^^^^
NameError: name 'smbus' is not defined. Did you mean: 'smbus2'?
Kod: Zaznacz cały
apt install python3-smbus python3-dev i2c-tools
Kod: Zaznacz cały
nano /etc/modules
Kod: Zaznacz cały
i2cdetect -y 1
Kod: Zaznacz cały
#!/usr/bin/python
# -*- coding: utf-8 -*-
#--------------------------------------
# ___ ___ _ ____
# / _ \/ _ \(_) __/__ __ __
# / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/ /_/___/ .__/\_, /
# /_/ /___/
#
# lcd_i2c.py
# LCD test script using I2C backpack.
# Supports 16x2 and 20x4 screens.
#
# Author : Matt Hawkins
# Date : 20/09/2015
#
# http://www.raspberrypi-spy.co.uk/
#
# Copyright 2015 Matt Hawkins
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#--------------------------------------
try:
import smbus2 as smbus
except ImportError:
import smbus
import time
import subprocess
from time import sleep, strftime
from datetime import datetime
from subprocess import *
# Define some device parameters
I2C_ADDR = 0x3f # I2C device address
LCD_WIDTH = 16 # Maximum characters per line
# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
LCD_BACKLIGHT = 0x08 # On
#LCD_BACKLIGHT = 0x00 # Off
ENABLE = 0b00000100 # Enable bit
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
#Open I2C interface
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(0) # Rev 2 Pi uses 1
def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)
def lcd_byte(bits, mode):
# Send byte to data pins
# bits = the data
# mode = 1 for data
# 0 for command
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
# High bits
bus.write_byte(I2C_ADDR, bits_high)
lcd_toggle_enable(bits_high)
# Low bits
bus.write_byte(I2C_ADDR, bits_low)
lcd_toggle_enable(bits_low)
def lcd_toggle_enable(bits):
# Toggle enable
time.sleep(E_DELAY)
bus.write_byte(I2C_ADDR, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
time.sleep(E_DELAY)
def lcd_string(message,line):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
def replace_pl(chars):
pl = { 'ą': 'a',
'ć': 'c',
'ę': 'e',
'ł': 'l',
'ń': 'n',
'ó': 'o',
'ś': 's',
'ź': 'z',
'ż': 'z',
'Ą': 'A',
'Ć': 'C',
'Ę': 'E',
'Ł': 'L',
'Ń': 'N',
'Ó': 'O',
'Ś': 'S',
'Ź': 'Z',
'Ż': 'Z',
}
for f, t in list(pl.items()):
chars = chars.replace(f,t)
return chars
def main():
counter = 0
while True:
# Get current status and playtime
process = subprocess.Popen('sudo mpc -v', shell=True, stdout=subprocess.PIPE)
ekran1 = process.communicate()[0]
ekran1 = replace_pl(ekran1)
statusLines = ekran1.split('\n')
ekran1 = statusLines[1]
if ekran1 > '':
ekran1L1 = ekran1.split(' ',1)[0].strip()
ekran1L2temp = ekran1.split(' ',1)[1].strip()
ekran1L2 = ekran1L2temp.split(' ',1)[0].strip()
process = subprocess.Popen('sudo mpc -f %name%', shell=True, stdout=subprocess.PIPE)
ekran2 = process.communicate()[0]
ekran2 = replace_pl(ekran2)
statusLines = ekran2.split('\n')
ekran2 = statusLines[0]
ekran2L1 = ekran2[0:16]
ekran2L2 = ekran2[16:31]
process = subprocess.Popen('sudo mpc -f %artist%', shell=True, stdout=subprocess.PIPE)
ekran3 = process.communicate()[0]
ekran3 = replace_pl(ekran3)
statusLines = ekran3.split('\n')
ekran3 = statusLines[0]
ekran3L1 = ekran3[0:16]
ekran3L2 = ekran3[16:31]
process = subprocess.Popen('sudo mpc -f %title%', shell=True, stdout=subprocess.PIPE)
ekran4 = process.communicate()[0]
ekran4 = replace_pl(ekran4)
statusLines = ekran4.split('\n')
ekran4 = statusLines[0]
ekran4L1 = ekran4[0:16]
ekran4L2 = ekran4[16:31]
if counter == 1:
if ekran1 == "":
counter = 7
else:
lcd_string(ekran1L1,LCD_LINE_1)
lcd_string(ekran1L2,LCD_LINE_2)
if counter == 7:
if ekran2 == "":
counter = 14
else:
lcd_string(ekran2L1,LCD_LINE_1)
lcd_string(ekran2L2,LCD_LINE_2)
if counter == 14:
if ekran3 == "":
counter = 25
else:
lcd_string(ekran3L1,LCD_LINE_1)
lcd_string(ekran3L2,LCD_LINE_2)
if counter == 25:
if ekran4 == "":
counter = 35
else:
lcd_string(ekran4L1,LCD_LINE_1)
lcd_string(ekran4L2,LCD_LINE_2)
sleep(0.1)
counter = counter + 1
if counter == 40:
counter = 1
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
lcd_byte(0x00, LCD_CMD)
lcd_byte(0x01, LCD_CMD)
lcd_byte(0x08, LCD_CMD)
Kod: Zaznacz cały
nano /etc/modules
Kod: Zaznacz cały
overlays= i2c0 i2c1 i2c2
olk777 pisze: 05 stycznia 2025, 21:10 Zaraz sprawdzę :-) ... ciekawe czy takie internetowe konwertery są naprawdę pomocne ? Tylko nie będąc "specem" w pythonie martwi mnie użycie modułów czyli ciągle wałkowanego tu smbus który jest po prostu niezbędny do uruchomienia LCD z tym skryptem. Jak widzę wzrokowo to konwerter internetowy zmienił baaaaaaardzo niewiele :-) ...jeśli to zadziała to aż wstyd że nie umie się tego samemu poprawić. Ale czy aby w skrypcie nie trzeba teraz samemu zmienić smbus na smbus2 ? bo konwerter tego nie wychwycił ? a wątpię żeby to zadziałało bez obecności w systemie modułu smbus którego nie można zainstalować nie mając pythona2 itd :-) ...
Ps. niestety ale to wymaga raczej większej wiedzy ... jak wspomniałem być obeznanym w pythonie3 i żadne konwertery tego nie zrobią poprawnie tak mi się wydaje. Efekt z konwertowanym plikiem i modułem smbus a także zainstalowanymi : apt install python3-smbus / apt install python3-smbus2Kod: Zaznacz cały
root@orangepi3-lts:~# python3 start2.py
może zamieszczę tu prze-konwertowany skryptKod: Zaznacz cały
Traceback (most recent call last): File "/root/start2.py", line 177, in <module> main() File "/root/start2.py", line 111, in main ekran1 = replace_pl(ekran1) ^^^^^^^^^^^^^^^^^^ File "/root/start2.py", line 99, in replace_pl chars = chars.replace(f,t) ^^^^^^^^^^^^^^^^^^ TypeError: a bytes-like object is required, not 'str'
jak zmieniłem w skrypcie tylko : import smbus ...na import smbus2 to wywala:Kod: Zaznacz cały
#!/usr/bin/python # -*- coding: utf-8 -*- import smbus import time import subprocess from time import sleep, strftime from datetime import datetime from subprocess import * # Define some device parameters I2C_ADDR = 0x3f # I2C device address LCD_WIDTH = 40 # Maximum characters per line LCD_HEIGHT = 2 # Define some device constants LCD_CHR = 1 # Mode - Sending data LCD_CMD = 0 # Mode - Sending command LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line LCD_BACKLIGHT = 0x08 # On #LCD_BACKLIGHT = 0x0C # Off ENABLE = 0b00000100 # Enable bit # Timing constants E_PULSE = 0.005 E_DELAY = 0.005 #Open I2C interface bus = smbus.SMBus(0) # Rev 1 Pi uses 0 (and Orange PI PC, for pins 3 and 5) #bus = smbus.SMBus(1) # Rev 2 Pi uses 1 def lcd_byte(bits, mode): # Send byte to data pins # bits = the data # mode = 1 for data # 0 for command bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT # High bits bus.write_byte(I2C_ADDR, bits_high) lcd_toggle_enable(bits_high) # Low bits bus.write_byte(I2C_ADDR, bits_low) lcd_toggle_enable(bits_low) def lcd_toggle_enable(bits): # Toggle enable time.sleep(E_DELAY) bus.write_byte(I2C_ADDR, (bits | ENABLE)) time.sleep(E_PULSE) bus.write_byte(I2C_ADDR,(bits & ~ENABLE)) time.sleep(E_DELAY) def lcd_string(message,line): # Send string to display message = message.ljust(LCD_WIDTH," ") lcd_byte(line, LCD_CMD) for i in range(LCD_WIDTH): lcd_byte(ord(message[i]),LCD_CHR) def replace_pl(chars): pl = { 'ą': 'a', 'ć': 'c', 'ę': 'e', 'ł': 'l', 'ń': 'n', 'ó': 'o', 'ś': 's', 'ź': 'z', 'ż': 'z', 'Ą': 'A', 'Ć': 'C', 'Ę': 'E', 'Ł': 'L', 'Ń': 'N', 'Ó': 'O', 'Ś': 'S', 'Ź': 'Z', 'Ż': 'Z', } for f, t in list(pl.items()): chars = chars.replace(f,t) return chars def main(): counter = 0 while True: # Get current status and playtime process = subprocess.Popen('sudo mpc -v', shell=True, stdout=subprocess.PIPE) ekran1 = process.communicate()[0] ekran1 = replace_pl(ekran1) statusLines = ekran1.split('\n') ekran1 = statusLines[1] if ekran1 != '': ekran1L1 = ekran1.split(' ',1)[0].strip() ekran1L2temp = ekran1.split(' ',1)[1].strip() ekran1L2 = ekran1L2temp.split(' ',1)[0].strip() process = subprocess.Popen('sudo mpc -f %name%', shell=True, stdout=subprocess.PIPE) ekran2 = process.communicate()[0] ekran2 = replace_pl(ekran2) statusLines = ekran2.split('\n') ekran2 = statusLines[0] ekran2L1 = ekran2[0:40] ekran2L2 = ekran2[40:80] process = subprocess.Popen('sudo mpc -f %artist%', shell=True, stdout=subprocess.PIPE) ekran3 = process.communicate()[0] ekran3 = replace_pl(ekran3) statusLines = ekran3.split('\n') ekran3 = statusLines[0] ekran3L1 = ekran3[0:40] ekran3L2 = ekran3[40:80] process = subprocess.Popen('sudo mpc -f %title%', shell=True, stdout=subprocess.PIPE) ekran4 = process.communicate()[0] ekran4 = replace_pl(ekran4) statusLines = ekran4.split('\n') ekran4 = statusLines[0] ekran4L1 = ekran4[0:40] ekran4L2 = ekran4[40:80] if counter == 1: if ekran1 == "": counter = 7 else: lcd_string(ekran1L1,LCD_LINE_1) lcd_string(ekran1L2,LCD_LINE_2) if counter == 7: if ekran2 == "": counter = 14 else: lcd_string(ekran2L1,LCD_LINE_1) lcd_string(ekran2L2,LCD_LINE_2) if counter == 14: if ekran3 == "": counter = 30 else: lcd_string(ekran3L1,LCD_LINE_1) lcd_string(ekran3L2,LCD_LINE_2) if counter == 30: if ekran4 == "": counter = 46 else: lcd_string(ekran4L1,LCD_LINE_1) lcd_string(ekran4L2,LCD_LINE_2) sleep(0.1) counter = counter + 1 if counter == 53: counter = 1 if __name__ == '__main__': try: main() except KeyboardInterrupt: pass finally: lcd_byte(0x00, LCD_CMD) lcd_byte(0x01, LCD_CMD) lcd_byte(0x08, LCD_CMD)
pewnie używając smbus2 trzeba inaczej zdefiniować w skrypcie : smbus.SMBus(0) ...bo to pozmieniali :-)Kod: Zaznacz cały
Traceback (most recent call last): File "/root/start2.py", line 39, in <module> bus = smbus.SMBus(0) # Rev 1 Pi uses 0 (and Orange PI PC, for pins 3 and 5) ^^^^^ NameError: name 'smbus' is not defined. Did you mean: 'smbus2'?
ale i tak nie wiadomo co jeszcze ...szkoda czasu. Trzeba by od podstaw liznąć pytona3 :-)