Black Lives Matter - Action and Equality. ... Adafruit is part of the Stop Hate for Profit campaign. Adafruit is open and shipping.
0

Pyportal ValueError: SCK in use
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pyportal ValueError: SCK in use

by slowjim on Sat Apr 11, 2020 3:50 am

Hi

I have been progressing through a big project (for me) on my PyPortal Titano. While my code has been working to start with, it crashes as after a few minutes because it can't allocate enough memory (the main code is 700+ lines long). To alleviate the issue I have been practicing using the SD card to store the libraries, images and some code to free up space. In my practice code (posted below) I have added a small script to append a path to the sd card and the new lib folder (as per the https://learn.adafruit.com/micropython- ... on-sd-card learn guide) also posted below.

Now my test code is based off the ISS tracker and when the code goes to look for the local time, I get the following error:

Traceback (most recent call last):
File "code.py", line 47, in <module>
File "adafruit_pyportal.py", line 271, in __init__
ValueError: SCK in use

Here are my two scripts (sorry the main code is long and mostly useless in this test, I was just using it for as I had used it as basis for my other project)

Code: Select all | TOGGLE FULL SIZE
mport sys
import sdmount_lib
import adafruit_sdcard
import adafruit_bus_device

import time
import math
import board
import displayio
from terminalio import FONT
from adafruit_pyportal import PyPortal
from adafruit_display_shapes.circle import Circle
from adafruit_display_text.label import Label




#--| USER CONFIG |--------------------------
MARK_SIZE = 10           # marker radius
MARK_COLOR = 0xFF3030    # marker color
MARK_THICKNESS = 5       # marker thickness
TRAIL_LENGTH = 200       # trail length
TRAIL_COLOR = 0xFFFF00   # trail color
DATE_COLOR = 0xFFFFFF    # date color
TIME_COLOR = 0xFFFFFF   # time color
LAT_MAX = 80             # latitude (deg) of map top/bottom edge
UPDATE_RATE = 10         # update rate in seconds
#-------------------------------------------

DATA_SOURCE = "http://api.open-notify.org/iss-now.json"
DATA_LOCATION = ["iss_position"]

WIDTH = board.DISPLAY.width
HEIGHT = board.DISPLAY.height

# determine the current working directory needed so we know where to find files
cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=DATA_LOCATION,
                    status_neopixel=board.NEOPIXEL)
#                    default_bg=cwd+"/map.bmp")

# Connect to the internet and get local time
pyportal.get_local_time()

# Date and time label
date_label = Label(FONT, text="0000-00-00", color=DATE_COLOR, x=0, y=20)
time_label = Label(FONT, text="00:00:00", color=TIME_COLOR, x=0, y=70)
pyportal.splash.append(date_label)
pyportal.splash.append(time_label)

# ISS trail
trail_bitmap = displayio.Bitmap(3, 3, 1)
trail_palette = displayio.Palette(1)
trail_palette[0] = TRAIL_COLOR
trail = displayio.Group(max_size=TRAIL_LENGTH)
pyportal.splash.append(trail)

# ISS location marker
marker = displayio.Group(max_size=MARK_THICKNESS)
for r in range(MARK_SIZE - MARK_THICKNESS, MARK_SIZE):
    marker.append(Circle(0, 0, r, outline=MARK_COLOR))
pyportal.splash.append(marker)

def get_location(width=WIDTH, height=HEIGHT):
    """Fetch current lat/lon, convert to (x, y) tuple scaled to width/height."""

    # Get location
    try:
        location = pyportal.fetch()
    except RuntimeError:
        return None, None

    # Compute (x, y) coordinates
    lat = float(location["latitude"])   # degrees, -90 to 90
    lon = float(location["longitude"])  # degrees, -180 to 180

    # Scale latitude for cropped map
    lat *= 90 / LAT_MAX

    # Mercator projection math
    # https://stackoverflow.com/a/14457180
    # https://en.wikipedia.org/wiki/Mercator_projection#Alternative_expressions
    x = lon + 180
    x = width * x / 360

    y = math.radians(lat)
    y = math.tan(math.pi / 4 + y / 2)
    y = math.log(y)
    y = (width * y) / (2 * math.pi)
    y = height / 2 - y

    return int(x), int(y)

def update_display(current_time, update_iss=False):
    """Update the display with current info."""

    # ISS location
    if update_iss:
        x, y = get_location()
        if x and y:
            marker.x = x
            marker.y = y
            if len(trail) >= TRAIL_LENGTH:
                trail.pop(0)
            trail.append(displayio.TileGrid(trail_bitmap,
                                            pixel_shader=trail_palette,
                                            x = x - 1,
                                            y = y - 1) )


    # Date and time
    date_label.text = "{:04}-{:02}-{:02}".format(current_time.tm_year,
                                                 current_time.tm_mon,
                                                 current_time.tm_mday)
    time_label.text = "{:02}:{:02}:{:02}".format(current_time.tm_hour,
                                                 current_time.tm_min,
                                                 current_time.tm_sec)

    try:
        board.DISPLAY.refresh(target_frames_per_second=60)
    except AttributeError:
        board.DISPLAY.refresh_soon()


# Initial refresh
update_display(time.localtime(), True)
last_update = time.monotonic()

# Run forever
while True:
    now = time.monotonic()
    new_position = False
    if now - last_update > UPDATE_RATE:
        new_position = True
        last_update = now
    update_display(time.localtime(), new_position)
    time.sleep(0.5)


Sd mount code:
Code: Select all | TOGGLE FULL SIZE
import sys
 
import adafruit_sdcard
import board
import busio
import digitalio
import storage
 
# Connect to the card and mount the filesystem.
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
spi = board.SPI()
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
sys.path.append("/sd")
sys.path.append("/sd/lib")


If anyone can help, id really appreciate it.

Yours in isolation, Cheers

slowjim

slowjim
 
Posts: 25
Joined: Fri Sep 27, 2019 3:49 am

Re: Pyportal ValueError: SCK in use

by tannewt on Mon Apr 13, 2020 6:20 pm

Hi! The PyPortal code will automatically mount the SD card for you I believe.

Also, memory errors are a result of RAM filling up. Moving source files to an SD card will not reduce the amount of RAM used to run your code. The libraries are loaded into RAM on import.

tannewt
 
Posts: 1683
Joined: Thu Oct 06, 2016 8:48 pm

Re: Pyportal ValueError: SCK in use

by slowjim on Wed Apr 15, 2020 3:20 am

Hi tannewt

Thanks for your reply. Yes, it all seems obvious when you say it. The mind needs a little jogging sometimes. I think I might be trying to work my PyPortal a little too hard. I will find a way to share the workload with another board.

Cheers

SlowJim

slowjim
 
Posts: 25
Joined: Fri Sep 27, 2019 3:49 am

Please be positive and constructive with your questions and comments.