Writing from SD Card to Flash Memery

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Nicolebendo
 
Posts: 11
Joined: Wed May 11, 2022 6:32 am

Writing from SD Card to Flash Memery

Post by Nicolebendo »

Hi, i'm using a raspberry Pico running on CircuitPython,
I'm trying to play multiple sounds at the same time using this piece of code:

Code: Select all

import time
import board
import audiocore
import audiomixer
from audiopwmio import PWMAudioOut as AudioOut

num_voices = 2

audio = AudioOut(board.GP18, right_channel=board.GP19)
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=22050, channel_count=1,
                         bits_per_sample=16, samples_signed=True)
audio.play(mixer)

mixer.voice[0].level = 1.0
mixer.voice[1].level = 1.0

wave0 = audiocore.WaveFile(open("/drumsacuff_22k_s16.wav","rb"))
mixer.voice[0].play( wave0, loop=True )

wave1 = audiocore.WaveFile(open("/snowpeaks_22k_s16.wav","rb"))
mixer.voice[1].play( wave1, loop=True )
This works perfectly when the files are stored directly on the pico. But when I try with files stored on a sd card it doesn't work anymore (note that it works fine with only 1 file from the sd card, or 1 file from the sd card and multiple files from the raspberry pico main direcrtory)

Couldn't I just temporary store a .wav file from the sd card into a variable or someting like this, so that it is temporary stored on the raspberry pico ?
Or can I copy the some .wav files from sd card to the raspberry main directory in order to play them and then delete them when I'm finished with them ?

Thank you for your hekp ! :)

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

Re: Writing from SD Card to Flash Memery

Post by mikeysklar »

Is there any reason not keep all your files on the Pico USB drive and just skip the SD card? If space is an issue maybe using MP3? I suspect you have a constraint that requires the SD card, but just checking.

You should be able to write to CircuitPython flash from your code by modifying the boot.py. Take a look at the example boot.py and code.py here for making a datalogger to flash space.

https://github.com/edgecollective/circu ... m0_express

boot.py:

Code: Select all

import digitalio
import board
import storage

switch=digitalio.DigitalInOut(board.D6)
switch.switch_to_input()

storage.remount("/",not switch.value) # switch.value==False means datalogging mode:  allow circuitpython code to write to flash, while making USB read-only.  So: pull D6 high for datalogging, leave D6 at ground for USB write access (reprogramming).
code.py

Code: Select all

import digitalio
import board
import time

switch=digitalio.DigitalInOut(board.D6)
switch.switch_to_input()

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

if switch.value==True: # if pin D6 is pulled high, then datalog mode -- no USB write allowed
    for i in range(10): # keep number of writes low for testing
        f=open('data.txt','a')
        f.write('{:03d}\n'.format(i))
        f.close()
        led.value = not led.value # blink to indicate successful write
        time.sleep(1)
        
else:
    while True:
        led.value = not led.value #faster blink to indicate in USB read-write mode
        time.sleep(.2)
Once you get a writable flash space then you need to figure out the equivalent of copy. I'll think about how to do that.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”