Skip to content

Stub AudioIn class #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frozen/pew-pewpew-lcd
Submodule pew-pewpew-lcd updated 1 files
+24 −1 pew.py
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ LONGINT_IMPL = MPZ
CIRCUITPY_AUDIOBUSIO = 0

CIRCUITPY_BITBANG_APA102 = 1

CIRCUITPY_AUDIO_IN = 1
76 changes: 76 additions & 0 deletions ports/atmel-samd/common-hal/audioio/AudioIn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifdef CIRCUITPY_AUDIO_IN

#include <stdint.h>
#include <string.h>

#include "extmod/vfs_fat.h"
#include "py/gc.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "common-hal/audioio/AudioIn.h"
#include "shared-bindings/audioio/AudioIn.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "supervisor/shared/translate/translate.h"

#include "atmel_start_pins.h"
#include "hal/include/hal_gpio.h"
#include "hpl/gclk/hpl_gclk_base.h"
#include "peripheral_clk_config.h"

#ifdef SAMD21
#include "hpl/pm/hpl_pm_base.h"
#endif

#include "audio_dma.h"
#include "timer_handler.h"

#include "samd/dma.h"
#include "samd/events.h"
#include "samd/pins.h"
#include "samd/timers.h"

// Caller validates that pins are free.
void common_hal_audioio_audioin_construct(audioio_audioin_obj_t *self,
const mcu_pin_obj_t *left_channel,
const mcu_pin_obj_t *right_channel,
uint16_t quiescent_value) {
}

bool common_hal_audioio_audioin_deinited(audioio_audioin_obj_t *self) {
return true;
}

void common_hal_audioio_audioin_deinit(audioio_audioin_obj_t *self) {
if (common_hal_audioio_audioin_deinited(self)) {
return;
}
}

#endif // CIRCUITPY_AUDIO_IN
54 changes: 54 additions & 0 deletions ports/atmel-samd/common-hal/audioio/AudioIn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOIO_AUDIOIN_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOIO_AUDIOIN_H

#ifdef CIRCUITPY_AUDIO_IN

#include "common-hal/microcontroller/Pin.h"

#include "audio_dma.h"
#include "py/obj.h"

typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *left_channel;
audio_dma_t left_dma;
#ifdef SAM_D5X_E5X
const mcu_pin_obj_t *right_channel;
audio_dma_t right_dma;
#endif
uint8_t tc_index;

uint8_t tc_to_dac_event_channel;
bool playing;
uint16_t quiescent_value;
} audioio_audioin_obj_t;

#endif // CIRCUITPY_AUDIO_IN

#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOIO_AUDIOIN_H
1 change: 1 addition & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ SRC_COMMON_HAL_ALL = \
audiobusio/I2SOut.c \
audiobusio/PDMIn.c \
audiobusio/__init__.c \
audioio/AudioIn.c \
audioio/AudioOut.c \
audioio/__init__.c \
audiopwmio/PWMAudioOut.c \
Expand Down
120 changes: 120 additions & 0 deletions shared-bindings/audioio/AudioIn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifdef CIRCUITPY_AUDIO_IN

#include <stdint.h>

#include "shared/runtime/context_manager_helpers.h"
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audioio/AudioIn.h"
#include "shared-bindings/audiocore/RawSample.h"
#include "shared-bindings/util.h"
#include "supervisor/shared/translate/translate.h"

//| class AudioIn:
//| """Read an analog audio signal"""
//|
//| def __init__(self, left_channel: microcontroller.Pin, *, right_channel: Optional[microcontroller.Pin] = None) -> None:
//| """Create a AudioIn object associated with the given pin(s). This allows you to
//| read audio signals in on the given pin(s).
//|
//| :param ~microcontroller.Pin left_channel: The pin to read the left channel from
//| :param ~microcontroller.Pin right_channel: The pin to read the right channel from
//|
//| ...
//| """
STATIC mp_obj_t audioio_audioin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_left_channel, ARG_right_channel };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_left_channel, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_right_channel, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

const mcu_pin_obj_t *left_channel_pin = validate_obj_is_free_pin(args[ARG_left_channel].u_obj);
const mcu_pin_obj_t *right_channel_pin = validate_obj_is_free_pin_or_none(args[ARG_right_channel].u_obj);

// create AudioIn object from the given pin
audioio_audioin_obj_t *self = m_new_obj(audioio_audioin_obj_t);
self->base.type = &audioio_audioin_type;
common_hal_audioio_audioin_construct(self, left_channel_pin, right_channel_pin);

return MP_OBJ_FROM_PTR(self);
}

//| def deinit(self) -> None:
//| """Deinitialises the AudioIn and releases any hardware resources for reuse."""
//| ...
//|
STATIC mp_obj_t audioio_audioin_deinit(mp_obj_t self_in) {
audioio_audioin_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audioio_audioin_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioin_deinit_obj, audioio_audioin_deinit);

//| def __enter__(self) -> AudioIn:
//| """No-op used by Context Managers."""
//| ...
//|
// Provided by context manager helper.

//| def __exit__(self) -> None:
//| """Automatically deinitializes the hardware when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
//|
STATIC mp_obj_t audioio_audioin_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_audioio_audioin_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_audioin___exit___obj, 4, 4, audioio_audioin_obj___exit__);


STATIC const mp_rom_map_elem_t audioio_audioin_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audioio_audioin_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_audioin___exit___obj) },

// Properties
};
STATIC MP_DEFINE_CONST_DICT(audioio_audioin_locals_dict, audioio_audioin_locals_dict_table);

const mp_obj_type_t audioio_audioin_type = {
{ &mp_type_type },
.name = MP_QSTR_AudioIn,
.make_new = audioio_audioin_make_new,
.locals_dict = (mp_obj_dict_t *)&audioio_audioin_locals_dict,
};

#endif // CIRCUITPY_AUDIO_IN
47 changes: 47 additions & 0 deletions shared-bindings/audioio/AudioIn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_AUDIOIN_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_AUDIOIN_H

#ifdef CIRCUITPY_AUDIO_IN

#include "common-hal/audioio/AudioIn.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/audiocore/RawSample.h"

extern const mp_obj_type_t audioio_audioin_type;

// left_channel will always be non-NULL but right_channel may be for mono output.
void common_hal_audioio_audioin_construct(audioio_audioin_obj_t *self,
const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t default_value);

void common_hal_audioio_audioin_deinit(audioio_audioin_obj_t *self);
bool common_hal_audioio_audioin_deinited(audioio_audioin_obj_t *self);

#endif // CIRCUITPY_AUDIO_IN

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_AUDIOIN_H
6 changes: 5 additions & 1 deletion shared-bindings/audioio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audioio/__init__.h"
#include "shared-bindings/audioio/AudioOut.h"
#include "shared-bindings/audioio/AudioIn.h"

//| """Support for audio output
//| """Support for audio IO
//|
//| The `audioio` module contains classes to provide access to audio IO.
//|
Expand All @@ -53,6 +54,9 @@
STATIC const mp_rom_map_elem_t audioio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audioio) },
{ MP_ROM_QSTR(MP_QSTR_AudioOut), MP_ROM_PTR(&audioio_audioout_type) },
#ifdef CIRCUITPY_AUDIO_IN
{ MP_ROM_QSTR(MP_QSTR_AudioIn), MP_ROM_PTR(&audioio_audioin_type) },
#endif // CIRCUITPY_AUDIO_IN
};

STATIC MP_DEFINE_CONST_DICT(audioio_module_globals, audioio_module_globals_table);
Expand Down