Re: i2c 16x2 LCD Pi Plate example code fails with "Syntax Error"

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Post Reply
User avatar
jharris1993
 
Posts: 83
Joined: Sun Dec 23, 2018 10:27 pm

Re: i2c 16x2 LCD Pi Plate example code fails with "Syntax Error"

Post by jharris1993 »

System:
Raspberry Pi 4 8g
Raspberry Pi O/S Buster
I have installed the Adafruit_Blinka library, adafruit-circuitpython-charlcd and the "Full Example Code" (listed below), which I am attempting to run in Thonny 3.3.10

Viz.: "Full Example Code"

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Simple test for I2C RGB character LCD shield kit"""
import time
import board
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd

# Modify this if you have a different sized Character LCD
lcd_columns = 16
lcd_rows = 2

# Initialise I2C bus.
i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller

# Initialise the LCD class
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)

lcd.clear()
# Set LCD color to red
lcd.color = [100, 0, 0]
time.sleep(1)
# Print two line message
lcd.message = "Hello\nCircuitPython"
# Wait 5s
time.sleep(5)
# Set LCD color to blue
lcd.color = [0, 100, 0]
time.sleep(1)
# Set LCD color to green
lcd.color = [0, 0, 100]
time.sleep(1)
# Set LCD color to purple
lcd.color = [50, 0, 50]
time.sleep(1)
lcd.clear()
# Print two line message right to left
lcd.text_direction = lcd.RIGHT_TO_LEFT
lcd.message = "Hello\nCircuitPython"
# Wait 5s
time.sleep(5)
# Return text direction to left to right
lcd.text_direction = lcd.LEFT_TO_RIGHT
# Display cursor
lcd.clear()
lcd.cursor = True
lcd.message = "Cursor! "
# Wait 5s
time.sleep(5)
# Display blinking cursor
lcd.clear()
lcd.blink = True
lcd.message = "Blinky Cursor!"
# Wait 5s
time.sleep(5)
lcd.blink = False
lcd.clear()
# Create message to scroll
scroll_msg = "<-- Scroll"
lcd.message = scroll_msg
# Scroll to the left
for i in range(len(scroll_msg)):
    time.sleep(0.5)
    lcd.move_left()
lcd.clear()
time.sleep(1)
lcd.message = "Going to sleep\nCya later!"
time.sleep(5)
# Turn off LCD backlights and clear text
lcd.color = [0, 0, 0]
lcd.clear()
The attempt to run fails with a syntax error in one of the libraries as follows:

Thonny Error Messages:

Code: Select all

Python 3.7.3 (/usr/bin/python3)
>>> %Run adafruit_display.py
Traceback (most recent call last):
  File "/home/pi/Desktop/adafruit_display.py", line 7, in <module>
    import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd
  File "/usr/local/lib/python3.7/dist-packages/adafruit_character_lcd/character_lcd_rgb_i2c.py", line 46, in <module>
    from adafruit_mcp230xx.mcp23017 import MCP23017
  File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp230xx/mcp23017.py", line 30, in <module>
    from .mcp230xx import MCP230XX
  File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp230xx/mcp230xx.py", line 16, in <module>
    from .mcp23xxx import MCP23XXX
  File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp230xx/mcp23xxx.py", line 14, in <module>
    from adafruit_bus_device import i2c_device, spi_device
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 15, in <module>
    from circuitpython_typing import ReadableBuffer, WriteableBuffer
  File "/usr/local/lib/python3.7/dist-packages/circuitpython_typing/__init__.py", line 79
    def read(self, count: Optional[int] = None, /) -> Optional[bytes]:
                                                ^
SyntaxError: invalid syntax
>>>
Subsequent searches both here, (and on the internet in general), have produced absolutely nothing. Note that I did try to "RTFM", but the "FM" provided no guidance whatsoever.

I am absolutely sure that there is something stupid/simple/obvious that I am overlooking here as I am also confident that Lady Ada's code is virtually bulletproof and well tested by legions of users. Ergo, I have a bad case of "foot-in-mouth disease".

I am totally out of my league here and I have no idea how to proceed. Any help would be gratefully appreciated.

Thanks!

User avatar
mikeysklar
 
Posts: 14791
Joined: Mon Aug 01, 2016 8:10 pm

Re: i2c 16x2 LCD Pi Plate example code fails with "Syntax Error"

Post by mikeysklar »

This looks like an error not from the example code itself, but from an underlying library not being compatible with the version of python.

I believe the problem is in the circuitpython_typing initialization:

Code: Select all

sudo nano /usr/local/lib/python3.7/dist-packages/circuitpython_typing/__init__.py
this line:

Code: Select all

def read(self, count: Optional[int] = None, /) -> Optional[bytes]:
change to:

Code: Select all

def read(self, count: Optional[int] = None) -> Optional[bytes]:
This could be an issue with the python release compatibility and might not be a problem with python3.8 or running Bookworm.

Post Reply
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”