Skip to content

Instantly share code, notes, and snippets.

@C47D
Created June 7, 2019 23:00
Show Gist options
  • Save C47D/ffd5e0f555694a5b95cd515c1a6c4c0b to your computer and use it in GitHub Desktop.
Save C47D/ffd5e0f555694a5b95cd515c1a6c4c0b to your computer and use it in GitHub Desktop.
circuitpython notes
# Add i2s to nrf52
Go to ports/nrf/common-hal/audiobusio/I2SOut.h y I2SOut.c
revisar el common-hal de atmel-samd para referencia.
Implementar las funciones definidas en shared-bindings/audiobusio/I2SOut.h y I2SOut.c / PDMIn.h y PDMIn.c
NOTA: shared-bindings son la relacion entre python y el código en C
La hal de los chips de nordic se basa en: https://github.com/adafruit/nrfx
Incluir "nrf_gpio.h" para tener acceso a la hal de nrfx de los gpio
Incluir "nrf_i2s.h" para tener acceso a los registros del i2s del nordic
¿Que indica audiobusio_i2sout_obj_t?
typedef struct {
mp_obj_base_t base;
bool left_justified;
const mcu_pin_obj_t *bit_clock;
const mcu_pin_obj_t *word_select;
const mcu_pin_obj_t *data;
uint8_t clock_unit;
uint8_t serializer;
uint8_t gclk;
bool playing;
audio_dma_t dma;
} audiobusio_i2sout_obj_t;
Necesitamos:
mcu_pin_obj_t para word_select, bit_clock y data
clock unit
serializer <- NO SE QUE ES ESTO!
gclk
playing <- flag
audio_dma_t
## Buscar documentación de:
- claim_pin()
- never_reset_pin_number()
- common_hal_digitalio_digitalinout_deinited()
- reset_pin_number()
- common_hal_digitalio_set_pull()
## Ejemplo de como utilizar audioio
import time
import array
import math
import audioio
import board
import digitalio
button = digitalio.DigitalInOut(board.A1)
button.switch_to_input(pull=digitalio.Pull.UP)
tone_volume = 0.1 # Increase this to increase the volume of the tone.
frequency = 440 # Set this to the Hz of the tone you want to generate.
length = 8000 // frequency
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
audio = audioio.AudioOut(board.A0)
sine_wave_sample = audioio.RawSample(sine_wave)
while True:
if not button.value:
audio.play(sine_wave_sample, loop=True)
time.sleep(1)
audio.stop()
## Ejemplo para reproducir un WAV
import time
import audioio
import board
import digitalio
button = digitalio.DigitalInOut(board.A1)
button.switch_to_input(pull=digitalio.Pull.UP)
wave_file = open("StreetChicken.wav", "rb")
wave = audioio.WaveFile(wave_file)
audio = audioio.AudioOut(board.A0)
while True:
audio.play(wave)
# This allows you to do other things while the audio plays!
t = time.monotonic()
while time.monotonic() - t < 6:
pass
audio.pause()
print("Waiting for button press to continue!")
while button.value:
pass
audio.resume()
while audio.playing:
pass
print("Done!")
## Para añadir soporte para reproducir MP3 seria algo mas o menos parecido a WavFile:
audio = audioio.MP3File()
a audio.play se le pasan samples, por lo tanto MP3File debe de retornar un sample?
## Debemos de entender:
- audiosample_sample_rate()
- audio_dma_setup_playback()
- audio_dma_stop()
- set_timer_frequency()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment