diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ebd6193..1718944 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,32 +10,35 @@ jobs: build: strategy: matrix: - os: [ubuntu-18.04] + # macos-13 is x86_64, and macos-14 is arm64 + os: [ubuntu-22.04, macos-13, macos-14] fail-fast: false runs-on: ${{ matrix.os }} name: build.py ${{ matrix.os }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: true - - name: Install toolchain (Ubuntu) - if: runner.os == 'Linux' - run: | - sudo add-apt-repository -y ppa:team-gcc-arm-embedded/ppa - sudo apt-get update -qq - sudo apt-get install -y gcc-arm-embedded - sudo apt-get install -y cmake + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - uses: carlosperate/arm-none-eabi-gcc-action@v1 + with: + release: 10.3-2021.10 + - name: Install CMake v3.22 via PyPI + run: python -m pip install cmake==3.28.3 - name: Check Versions run: | arm-none-eabi-gcc --version cmake --version - - name: Build - run: | - make -C lib/micropython/mpy-cross -j2 - cd src - make -j2 + python --version + uname -a + - name: Build mpy-cross + run: make -C lib/micropython/mpy-cross -j2 + - name: Build MicroPython + run: make -C src -j2 - name: Upload hex file - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4 with: - name: MICROBIT-MICROPYTHON-${{ github.sha }}.hex + name: MICROBIT-MICROPYTHON-${{ github.sha }}-${{ matrix.os }}.hex path: src/MICROBIT.hex diff --git a/.gitignore b/.gitignore index 9d5fcdd..c107393 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ src/build src/MICROBIT.hex src/codal_port/build -src/codal_port/libmicropython.a diff --git a/lib/codal b/lib/codal index 18aa65b..871d0fc 160000 --- a/lib/codal +++ b/lib/codal @@ -1 +1 @@ -Subproject commit 18aa65b99a100f1923f14f1cd7bc8fc3b38afdc6 +Subproject commit 871d0fccaf385a57f7cd2c2d61c644ec04107914 diff --git a/lib/micropython b/lib/micropython index da4b38e..a61c446 160000 --- a/lib/micropython +++ b/lib/micropython @@ -1 +1 @@ -Subproject commit da4b38e7562dfa451917f9d7f344a7f26de8c7bd +Subproject commit a61c446c0b34e82aeb54b9770250d267656f2b7f diff --git a/src/addlayouttable.py b/src/addlayouttable.py index 3e32ab1..4e7fe1e 100755 --- a/src/addlayouttable.py +++ b/src/addlayouttable.py @@ -124,7 +124,7 @@ def parse_map_file(filename, symbols): line = line.strip() if line == "Linker script and memory map": parse_symbols = True - elif parse_symbols and line.startswith("0x00"): + elif parse_symbols and line.startswith(("0x00", "0x20")): line = line.split() if len(line) >= 2 and line[1] in symbols: symbols[line[1]] = int(line[0], 16) @@ -228,11 +228,11 @@ def main(): # Print information. if args.output is not sys.stdout: - fmt = "{:13} 0x{:05x}..0x{:05x}" - print(fmt.format("SoftDevice", sd_start, sd_end)) - print(fmt.format("MicroPython", mp_start, mp_end)) - print(fmt.format("Layout table", layout_addr, layout_addr + len(layout.data))) - print(fmt.format("Filesystem", fs_start, fs_end)) + fmt = "{:13} 0x{:05x}..0x{:05x} {:6} bytes" + print(fmt.format("SoftDevice", sd_start, sd_end, sd_end - sd_start)) + print(fmt.format("MicroPython", mp_start, mp_end, mp_end - mp_start)) + print(fmt.format("Layout table", layout_addr, layout_addr + len(layout.data), len(layout.data))) + print(fmt.format("Filesystem", fs_start, fs_end, fs_end - fs_start)) # Output the new firmware as a hex file. output_firmware(args.output, firmware, layout_addr, layout.data) diff --git a/src/codal.patch b/src/codal.patch index 7bca328..6b6a65c 100644 --- a/src/codal.patch +++ b/src/codal.patch @@ -20,6 +20,6 @@ index b89e1d0..b53224b 100644 +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PROJECT_SOURCE_DIR}/../../src/codal_port/filesystem.ld") +add_library(micropython STATIC IMPORTED) +set_target_properties(micropython PROPERTIES -+ IMPORTED_LOCATION "${PROJECT_SOURCE_DIR}/../../src/codal_port/libmicropython.a" ++ IMPORTED_LOCATION "${PROJECT_SOURCE_DIR}/../../src/codal_port/build/libmicropython.a" +) +target_link_libraries("MICROBIT" micropython) diff --git a/src/codal_app/codal.json b/src/codal_app/codal.json index 2b8048b..9444ac5 100644 --- a/src/codal_app/codal.json +++ b/src/codal_app/codal.json @@ -2,7 +2,7 @@ "target": { "name": "codal-microbit-v2", "url": "https://github.com/lancaster-university/codal-microbit-v2", - "branch": "v0.2.43", + "branch": "v0.2.67", "type": "git", "test_ignore": true } , @@ -12,6 +12,7 @@ "MICROBIT_BLE_PAIRING_MODE": 1, "MICROBIT_BLE_PARTIAL_FLASHING" : 1, "MICROBIT_BLE_SECURITY_MODE": 2, + "MICROBIT_BLE_UTILITY_SERVICE_PAIRING": 1, "MICROBIT_USB_SERIAL_WAKE": 1, "LEVEL_DETECTOR_SPL_8BIT_000_POINT": 52.0 }, diff --git a/src/codal_app/microbithal.cpp b/src/codal_app/microbithal.cpp index d032ba3..23c4231 100644 --- a/src/codal_app/microbithal.cpp +++ b/src/codal_app/microbithal.cpp @@ -25,11 +25,23 @@ */ #include "main.h" +#include "microbithal.h" #include "MicroBitDevice.h" #include "neopixel.h" #define HAL_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +// It's not possible to include the CODAL header file that defines the SFX_DEFAULT_xxx +// constants in C code, because that CODAL header file is C++. Instead we define our +// own MICROBIT_HAL_SFX_DEFAULT_xxx versions of the constants in a C-compatible header +// file, and assert here that they have the same value as the CODAL constants. +static_assert(MICROBIT_HAL_SFX_DEFAULT_VIBRATO_PARAM == SFX_DEFAULT_VIBRATO_PARAM, ""); +static_assert(MICROBIT_HAL_SFX_DEFAULT_VIBRATO_STEPS == SFX_DEFAULT_VIBRATO_STEPS, ""); +static_assert(MICROBIT_HAL_SFX_DEFAULT_TREMOLO_PARAM == SFX_DEFAULT_TREMOLO_PARAM, ""); +static_assert(MICROBIT_HAL_SFX_DEFAULT_TREMOLO_STEPS == SFX_DEFAULT_TREMOLO_STEPS, ""); +static_assert(MICROBIT_HAL_SFX_DEFAULT_WARBLE_PARAM == SFX_DEFAULT_WARBLE_PARAM, ""); +static_assert(MICROBIT_HAL_SFX_DEFAULT_WARBLE_STEPS == SFX_DEFAULT_WARBLE_STEPS, ""); + NRF52Pin *const pin_obj[] = { &uBit.io.P0, &uBit.io.P1, @@ -50,7 +62,7 @@ NRF52Pin *const pin_obj[] = { &uBit.io.P16, &uBit.io.P19, // external I2C SCL &uBit.io.P20, // external I2C SDA - &uBit.io.face, + &uBit.io.logo, &uBit.io.speaker, &uBit.io.runmic, &uBit.io.microphone, @@ -78,12 +90,11 @@ static const PullMode pin_pull_mode_mapping[] = { }; static uint8_t pin_pull_state[32 + 6]; +static uint16_t touch_state[4]; static uint16_t button_state[2]; extern "C" { -#include "microbithal.h" - void microbit_hal_background_processing(void) { // This call takes about 200us. Event(DEVICE_ID_SCHEDULER, DEVICE_SCHEDULER_EVT_IDLE); @@ -94,7 +105,7 @@ void microbit_hal_idle(void) { __WFI(); } -void microbit_hal_reset(void) { +__attribute__((noreturn)) void microbit_hal_reset(void) { microbit_reset(); } @@ -202,11 +213,36 @@ void microbit_hal_pin_write_analog_u10(int pin, int value) { pin_obj[pin]->setAnalogValue(value); } -int microbit_hal_pin_is_touched(int pin) { - if (pin == MICROBIT_HAL_PIN_FACE) { - // For touch on the face/logo, delegate to the TouchButton instance. - return uBit.logo.buttonActive(); +void microbit_hal_pin_touch_calibrate(int pin) { + pin_obj[pin]->touchCalibrate(); +} + +int microbit_hal_pin_touch_state(int pin, int *was_touched, int *num_touches) { + if (was_touched != NULL || num_touches != NULL) { + int pin_state_index; + if (pin == MICROBIT_HAL_PIN_LOGO) { + pin_state_index = 3; + } else { + pin_state_index = pin; // pin0/1/2 + } + int t = pin_obj[pin]->wasTouched(); + uint16_t state = touch_state[pin_state_index]; + if (t) { + // Update state based on number of touches since last call. + // Low bit is "was touched at least once", upper bits are "number of touches". + state = (state + (t << 1)) | 1; + } + if (was_touched != NULL) { + *was_touched = state & 1; + state &= ~1; + } + if (num_touches != NULL) { + *num_touches = state >> 1; + state &= 1; + } + touch_state[pin_state_index] = state; } + return pin_obj[pin]->isTouched(); } @@ -215,10 +251,13 @@ void microbit_hal_pin_write_ws2812(int pin, const uint8_t *buf, size_t len) { } int microbit_hal_i2c_init(int scl, int sda, int freq) { - // TODO set pins - int ret = uBit.i2c.setFrequency(freq); + int ret = uBit.i2c.redirect(*pin_obj[sda], *pin_obj[scl]); + if (ret != DEVICE_OK) { + return ret; + } + ret = uBit.i2c.setFrequency(freq); if (ret != DEVICE_OK) { - return ret;; + return ret; } return 0; } @@ -255,11 +294,16 @@ int microbit_hal_uart_init(int tx, int rx, int baudrate, int bits, int parity, i static NRF52SPI *spi = NULL; int microbit_hal_spi_init(int sclk, int mosi, int miso, int frequency, int bits, int mode) { - if (spi != NULL) { - delete spi; + int ret; + if (spi == NULL) { + spi = new NRF52SPI(*pin_obj[mosi], *pin_obj[miso], *pin_obj[sclk], NRF_SPIM2); + } else { + ret = spi->redirect(*pin_obj[mosi], *pin_obj[miso], *pin_obj[sclk]); + if (ret != DEVICE_OK) { + return ret; + } } - spi = new NRF52SPI(*pin_obj[mosi], *pin_obj[miso], *pin_obj[sclk], NRF_SPIM2); - int ret = spi->setFrequency(frequency); + ret = spi->setFrequency(frequency); if (ret != DEVICE_OK) { return ret; } @@ -311,10 +355,6 @@ void microbit_hal_display_enable(int value) { } } -void microbit_hal_display_clear(void) { - uBit.display.clear(); -} - int microbit_hal_display_get_pixel(int x, int y) { uint32_t pixel = uBit.display.image.getPixelValue(x, y); if (pixel == 255) { @@ -339,6 +379,16 @@ int microbit_hal_display_read_light_level(void) { return uBit.display.readLightLevel(); } +void microbit_hal_display_rotate(unsigned int rotation) { + static DisplayRotation angle_map[4] = { + MATRIX_DISPLAY_ROTATION_0, + MATRIX_DISPLAY_ROTATION_90, + MATRIX_DISPLAY_ROTATION_180, + MATRIX_DISPLAY_ROTATION_270, + }; + uBit.display.rotateTo(angle_map[rotation & 3]); +} + void microbit_hal_accelerometer_get_sample(int axis[3]) { Sample3D sample = uBit.accelerometer.getSample(); axis[0] = sample.x; diff --git a/src/codal_app/microbithal.h b/src/codal_app/microbithal.h index fa9fe5c..78d455f 100644 --- a/src/codal_app/microbithal.h +++ b/src/codal_app/microbithal.h @@ -59,7 +59,7 @@ extern "C" { #define MICROBIT_HAL_PIN_P16 (16) #define MICROBIT_HAL_PIN_P19 (17) #define MICROBIT_HAL_PIN_P20 (18) -#define MICROBIT_HAL_PIN_FACE (19) +#define MICROBIT_HAL_PIN_LOGO (19) #define MICROBIT_HAL_PIN_SPEAKER (20) #define MICROBIT_HAL_PIN_USB_TX (30) #define MICROBIT_HAL_PIN_USB_RX (31) @@ -90,6 +90,7 @@ extern "C" { // Microphone events, passed to microbit_hal_level_detector_callback(). #define MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW (1) #define MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH (2) +#define MICROBIT_HAL_MICROPHONE_EVT_CLAP (8) // Threshold kind, passed to microbit_hal_microphone_set_threshold(). #define MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW (0) @@ -102,9 +103,17 @@ extern "C" { #define MICROBIT_HAL_LOG_TIMESTAMP_HOURS (36000) #define MICROBIT_HAL_LOG_TIMESTAMP_DAYS (864000) +// These default fx values are the same as defined by CODAL, but here in a C-compatible header. +#define MICROBIT_HAL_SFX_DEFAULT_VIBRATO_PARAM (2) +#define MICROBIT_HAL_SFX_DEFAULT_VIBRATO_STEPS (512) +#define MICROBIT_HAL_SFX_DEFAULT_TREMOLO_PARAM (3) +#define MICROBIT_HAL_SFX_DEFAULT_TREMOLO_STEPS (900) +#define MICROBIT_HAL_SFX_DEFAULT_WARBLE_PARAM (2) +#define MICROBIT_HAL_SFX_DEFAULT_WARBLE_STEPS (700) + void microbit_hal_idle(void); -void microbit_hal_reset(void); +__attribute__((noreturn)) void microbit_hal_reset(void); void microbit_hal_panic(int); int microbit_hal_temperature(void); @@ -123,7 +132,8 @@ int microbit_hal_pin_read(int pin); void microbit_hal_pin_write(int pin, int value); int microbit_hal_pin_read_analog_u10(int pin); void microbit_hal_pin_write_analog_u10(int pin, int value); -int microbit_hal_pin_is_touched(int pin); +void microbit_hal_pin_touch_calibrate(int pin); +int microbit_hal_pin_touch_state(int pin, int *was_touched, int *num_touches); void microbit_hal_pin_write_ws2812(int pin, const uint8_t *buf, size_t len); int microbit_hal_i2c_init(int scl, int sda, int freq); @@ -138,10 +148,10 @@ int microbit_hal_spi_transfer(size_t len, const uint8_t *src, uint8_t *dest); int microbit_hal_button_state(int button, int *was_pressed, int *num_presses); void microbit_hal_display_enable(int value); -void microbit_hal_display_clear(void); int microbit_hal_display_get_pixel(int x, int y); void microbit_hal_display_set_pixel(int x, int y, int bright); int microbit_hal_display_read_light_level(void); +void microbit_hal_display_rotate(unsigned int rotation); void microbit_hal_accelerometer_get_sample(int axis[3]); int microbit_hal_accelerometer_get_gesture(void); @@ -157,6 +167,7 @@ int microbit_hal_compass_get_heading(void); void microbit_hal_microphone_init(void); void microbit_hal_microphone_set_threshold(int kind, int value); int microbit_hal_microphone_get_level(void); +float microbit_hal_microphone_get_level_db(void); const uint8_t *microbit_hal_get_font_data(char c); diff --git a/src/codal_app/microbithal_audio.cpp b/src/codal_app/microbithal_audio.cpp index a590274..d27b429 100644 --- a/src/codal_app/microbithal_audio.cpp +++ b/src/codal_app/microbithal_audio.cpp @@ -33,6 +33,7 @@ class AudioSource : public DataSource { DataSink *sink; ManagedBuffer buf; void (*callback)(void); + MixerChannel *channel; AudioSource() : started(false) { @@ -76,11 +77,7 @@ void microbit_hal_audio_select_speaker(bool enable) { // Input value has range 0-255 inclusive. void microbit_hal_audio_set_volume(int value) { - if (value >= 255) { - uBit.audio.setVolume(128); - } else { - uBit.audio.setVolume(value / 2); - } + uBit.audio.setVolume(value); } void microbit_hal_sound_synth_callback(int event) { @@ -113,7 +110,9 @@ void microbit_hal_audio_init(uint32_t sample_rate) { MicroBitAudio::requestActivation(); data_source.started = true; data_source.callback = microbit_hal_audio_ready_callback; - uBit.audio.mixer.addChannel(data_source, sample_rate, 255); + data_source.channel = uBit.audio.mixer.addChannel(data_source, sample_rate, 255); + } else { + data_source.channel->setSampleRate(sample_rate); } } @@ -130,7 +129,9 @@ void microbit_hal_audio_speech_init(uint32_t sample_rate) { MicroBitAudio::requestActivation(); speech_source.started = true; speech_source.callback = microbit_hal_audio_speech_ready_callback; - uBit.audio.mixer.addChannel(speech_source, sample_rate, 255); + speech_source.channel = uBit.audio.mixer.addChannel(speech_source, sample_rate, 255); + } else { + speech_source.channel->setSampleRate(sample_rate); } } diff --git a/src/codal_app/microbithal_microphone.cpp b/src/codal_app/microbithal_microphone.cpp index 0cceb16..e530432 100644 --- a/src/codal_app/microbithal_microphone.cpp +++ b/src/codal_app/microbithal_microphone.cpp @@ -59,4 +59,11 @@ int microbit_hal_microphone_get_level(void) { return value; } +float microbit_hal_microphone_get_level_db(void) { + uBit.audio.levelSPL->setUnit(LEVEL_DETECTOR_SPL_DB); + float value = uBit.audio.levelSPL->getValue(); + uBit.audio.levelSPL->setUnit(LEVEL_DETECTOR_SPL_8BIT); + return value; +} + } diff --git a/src/codal_port/Makefile b/src/codal_port/Makefile index 5f07b38..c6b4643 100644 --- a/src/codal_port/Makefile +++ b/src/codal_port/Makefile @@ -14,6 +14,13 @@ QSTR_DEFS = qstrdefsport.h # Include py core make definitions. include $(TOP)/py/py.mk +include $(TOP)/extmod/extmod.mk + +# The micropython-lib submodule is not needed by this project, but the MicroPython +# build system requires it if FROZEN_MANIFEST is set (which it is below). To avoid +# needing to check out the micropython-lib submodule, point MPY_LIB_DIR to a dummy +# location that has a README.md file. +MPY_LIB_DIR = $(TOP) MP_VER_FILE = $(HEADER_BUILD)/mpversion.h MBIT_VER_FILE = $(HEADER_BUILD)/microbitversion.h @@ -38,7 +45,8 @@ INC += -I$(BUILD) CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized CFLAGS_ARCH += -DNRF52833_XXAA -CFLAGS = $(INC) $(CWARN) -std=c99 $(CFLAGS_MOD) $(CFLAGS_ARCH) $(COPT) $(CFLAGS_EXTRA) +CFLAGS += $(INC) $(CWARN) -std=c99 $(CFLAGS_MOD) $(CFLAGS_ARCH) $(COPT) $(CFLAGS_EXTRA) +CXXFLAGS += $(filter-out -std=c99,$(CFLAGS)) # Debugging/Optimization ifdef DEBUG @@ -49,7 +57,7 @@ endif CFLAGS += -g LDFLAGS_ARCH = -Wl,-map,$@.map -LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA) +LDFLAGS += $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA) SRC_C += \ drv_display.c \ @@ -91,7 +99,6 @@ SRC_C += \ modradio.c \ modspeech.c \ modthis.c \ - modutime.c \ mphalport.c \ SRC_C += \ @@ -107,7 +114,7 @@ SRC_C += \ $(abspath $(LOCAL_LIB_DIR)/sam/debug.c) \ SRC_O += \ - shared/runtime/gchelper_m3.o \ + shared/runtime/gchelper_thumb2.o \ OBJ = $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_O)) @@ -128,12 +135,9 @@ all: lib $(MBIT_VER_FILE) # Also rebuild MicroPython version header in correct directory to pick up git hash. $(MBIT_VER_FILE): FORCE (cd $(TOP) && $(PYTHON) py/makeversionhdr.py $(abspath $(MP_VER_FILE))) - $(PYTHON) $(TOP)/py/makeversionhdr.py $(MBIT_VER_FILE).pre - $(CAT) $(MBIT_VER_FILE).pre | $(SED) s/MICROPY_/MICROBIT_/ > $(MBIT_VER_FILE) + $(PYTHON) make_microbit_version_hdr.py $(MBIT_VER_FILE) # Suppress warnings from SAM library. $(BUILD)/$(abspath $(LOCAL_LIB_DIR))/sam/sam.o: CWARN += -Wno-array-bounds include $(TOP)/py/mkrules.mk - -CLEAN_EXTRA += $(LIBMICROPYTHON) diff --git a/src/codal_port/drv_display.c b/src/codal_port/drv_display.c index db700d8..e2c5f75 100644 --- a/src/codal_port/drv_display.c +++ b/src/codal_port/drv_display.c @@ -44,7 +44,7 @@ static mp_uint_t async_delay = 1000; static mp_uint_t async_tick = 0; static bool async_clear = false; -STATIC void async_stop(void) { +static void async_stop(void) { async_iterator = NULL; async_mode = ASYNC_MODE_STOPPED; async_tick = 0; @@ -62,7 +62,7 @@ void microbit_display_stop(void) { MP_STATE_PORT(display_data) = NULL; } -STATIC void wait_for_event() { +static void wait_for_event() { while (!wakeup_event) { // allow CTRL-C to stop the animation if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) { @@ -142,6 +142,8 @@ void microbit_display_update(void) { } void microbit_display_clear(void) { + // Reset repeat state, cancel animation and clear screen. + // The actual screen clearing will be done by microbit_display_update. wakeup_event = false; async_mode = ASYNC_MODE_CLEAR; async_tick = async_delay - MILLISECONDS_PER_MACRO_TICK; @@ -190,3 +192,5 @@ void microbit_display_animate(mp_obj_t iterable, mp_int_t delay, bool clear, boo wait_for_event(); } } + +MP_REGISTER_ROOT_POINTER(void *display_data); diff --git a/src/codal_port/drv_image.c b/src/codal_port/drv_image.c index 03f9e15..4a1877c 100644 --- a/src/codal_port/drv_image.c +++ b/src/codal_port/drv_image.c @@ -35,7 +35,7 @@ const monochrome_5by5_t microbit_blank_image = { { 0, 0, 0 } }; -STATIC uint8_t monochrome_get_pixel(monochrome_5by5_t *self, mp_int_t x, mp_int_t y) { +static uint8_t monochrome_get_pixel(monochrome_5by5_t *self, mp_int_t x, mp_int_t y) { unsigned int index = y*5+x; if (index == 24) return self->pixel44; @@ -43,7 +43,7 @@ STATIC uint8_t monochrome_get_pixel(monochrome_5by5_t *self, mp_int_t x, mp_int_ } greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) { - greyscale_t *result = m_new_obj_var(greyscale_t, uint8_t, (w*h+1)>>1); + greyscale_t *result = m_new_obj_var(greyscale_t, byte_data, uint8_t, (w*h+1)>>1); result->base.type = µbit_image_type; result->five = 0; result->width = w; diff --git a/src/codal_port/drv_radio.c b/src/codal_port/drv_radio.c index 1452ae1..ed8ef5b 100644 --- a/src/codal_port/drv_radio.c +++ b/src/codal_port/drv_radio.c @@ -283,3 +283,5 @@ void microbit_radio_pop(void) { // Re-enable the radio IRQ. NVIC_EnableIRQ(RADIO_IRQn); } + +MP_REGISTER_ROOT_POINTER(uint8_t *radio_buf); diff --git a/src/codal_port/drv_softtimer.c b/src/codal_port/drv_softtimer.c index e58ee26..b780470 100644 --- a/src/codal_port/drv_softtimer.c +++ b/src/codal_port/drv_softtimer.c @@ -32,7 +32,7 @@ static bool microbit_soft_timer_paused = false; -STATIC int microbit_soft_timer_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) { +static int microbit_soft_timer_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) { microbit_soft_timer_entry_t *e1 = (microbit_soft_timer_entry_t *)n1; microbit_soft_timer_entry_t *e2 = (microbit_soft_timer_entry_t *)n2; return TICKS_DIFF(e1->expiry_ms, e2->expiry_ms) < 0; @@ -98,3 +98,5 @@ uint32_t microbit_soft_timer_get_ms_to_next_expiry(void) { } return dt; } + +MP_REGISTER_ROOT_POINTER(struct _microbit_soft_timer_entry_t *soft_timer_heap); diff --git a/src/codal_port/iters.c b/src/codal_port/iters.c index 8869307..b199095 100644 --- a/src/codal_port/iters.c +++ b/src/codal_port/iters.c @@ -42,12 +42,12 @@ static mp_obj_t microbit_repeat_iter_next(mp_obj_t iter_in) { return mp_obj_subscr(iter->iterable, MP_OBJ_NEW_SMALL_INT(iter->index), MP_OBJ_SENTINEL); } -const mp_obj_type_t microbit_repeat_iterator_type = { - .base = { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = microbit_repeat_iter_next, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_repeat_iterator_type, + MP_QSTR_iterator, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, + iter, microbit_repeat_iter_next + ); mp_obj_t microbit_repeat_iterator(mp_obj_t iterable) { repeat_iterator_t *result = m_new_obj(repeat_iterator_t); diff --git a/src/codal_port/main.c b/src/codal_port/main.c index 500b9ad..88d5ba9 100644 --- a/src/codal_port/main.c +++ b/src/codal_port/main.c @@ -36,7 +36,7 @@ #include "shared/readline/readline.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" -#include "ports/nrf/modules/uos/microbitfs.h" +#include "ports/nrf/modules/os/microbitfs.h" #include "drv_softtimer.h" #include "drv_system.h" #include "drv_display.h" @@ -96,7 +96,7 @@ void mp_main(void) { } } -STATIC void microbit_display_exception(mp_obj_t exc_in) { +static void microbit_display_exception(mp_obj_t exc_in) { // Construct the message string ready for display. mp_uint_t n, *values; mp_obj_exception_get_traceback(exc_in, &n, &values); @@ -136,7 +136,7 @@ void microbit_pyexec_file(const char *filename) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { // Parse and comple the file. - mp_lexer_t *lex = mp_lexer_new_from_file(filename); + mp_lexer_t *lex = mp_lexer_new_from_file(qstr_from_str(filename)); qstr source_name = lex->source_name; mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT); mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false); @@ -194,16 +194,16 @@ void microbit_file_opened_for_writing(const char *name, size_t name_len) { } #if MICROPY_MBFS -mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - return uos_mbfs_new_reader(filename); +mp_lexer_t *mp_lexer_new_from_file(qstr filename) { + return os_mbfs_new_reader(qstr_str(filename)); } mp_import_stat_t mp_import_stat(const char *path) { - return uos_mbfs_import_stat(path); + return os_mbfs_import_stat(path); } mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - return uos_mbfs_open(n_args, args); + return os_mbfs_open(n_args, args); } MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); #endif diff --git a/src/codal_port/make_microbit_version_hdr.py b/src/codal_port/make_microbit_version_hdr.py new file mode 100644 index 0000000..3d532ea --- /dev/null +++ b/src/codal_port/make_microbit_version_hdr.py @@ -0,0 +1,120 @@ +""" +Generate header file with macros defining micro:bit version info. + +Adapted from MicroPython's py/makeversionhdr.py file at v1.21.0. +""" + +import argparse +import sys +import os +import datetime +import subprocess + + +def get_version_info_from_git(repo_path): + # Note: git describe doesn't work if no tag is available + try: + git_tag = subprocess.check_output( + ["git", "describe", "--tags", "--dirty", "--always", "--match", "v[1-9].*"], + cwd=repo_path, + stderr=subprocess.STDOUT, + universal_newlines=True, + ).strip() + except subprocess.CalledProcessError as er: + if er.returncode == 128: + # git exit code of 128 means no repository found + return None + git_tag = "" + except OSError: + return None + try: + git_hash = subprocess.check_output( + ["git", "rev-parse", "--short", "HEAD"], + cwd=repo_path, + stderr=subprocess.STDOUT, + universal_newlines=True, + ).strip() + except subprocess.CalledProcessError: + git_hash = "unknown" + except OSError: + return None + + try: + # Check if there are any modified files. + subprocess.check_call( + ["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], + cwd=repo_path, + stderr=subprocess.STDOUT, + ) + # Check if there are any staged files. + subprocess.check_call( + ["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], + cwd=repo_path, + stderr=subprocess.STDOUT, + ) + except subprocess.CalledProcessError: + git_hash += "-dirty" + except OSError: + return None + + return git_tag, git_hash + + +def make_version_header(repo_path, filename): + info = None + if "MICROBIT_GIT_TAG" in os.environ: + info = [os.environ["MICROBIT_GIT_TAG"], os.environ["MICROBIT_GIT_HASH"]] + if info is None: + info = get_version_info_from_git(repo_path) + + git_tag, git_hash = info + + build_date = datetime.date.today() + if "SOURCE_DATE_EPOCH" in os.environ: + build_date = datetime.datetime.utcfromtimestamp( + int(os.environ["SOURCE_DATE_EPOCH"]) + ).date() + + # Generate the file with the git and version info + file_data = """\ +// This file was generated by py/makeversionhdr.py +#define MICROBIT_GIT_TAG "%s" +#define MICROBIT_GIT_HASH "%s" +#define MICROBIT_BUILD_DATE "%s" +""" % ( + git_tag, + git_hash, + build_date.strftime("%Y-%m-%d"), + ) + + # Check if the file contents changed from last time + write_file = True + if os.path.isfile(filename): + with open(filename, "r") as f: + existing_data = f.read() + if existing_data == file_data: + write_file = False + + # Only write the file if we need to + if write_file: + print("GEN %s" % filename) + with open(filename, "w") as f: + f.write(file_data) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "-r", + "--repo-path", + default=os.path.join(os.path.dirname(sys.argv[0]), ".."), + help="path to git repo to query for version", + ) + parser.add_argument("dest", nargs=1, help="output file path") + args = parser.parse_args() + + make_version_header(args.repo_path, args.dest[0]) + + +if __name__ == "__main__": + main() diff --git a/src/codal_port/microbit_accelerometer.c b/src/codal_port/microbit_accelerometer.c index 761401b..1ef1d61 100644 --- a/src/codal_port/microbit_accelerometer.c +++ b/src/codal_port/microbit_accelerometer.c @@ -36,11 +36,11 @@ typedef struct _microbit_accelerometer_obj_t { } microbit_accelerometer_obj_t; volatile bool accelerometer_up_to_date = false; -STATIC volatile uint16_t gesture_state = 0; // 1 bit per gesture -STATIC volatile uint8_t gesture_list_cur = 0; // index into gesture_list -STATIC volatile uint8_t gesture_list[GESTURE_LIST_SIZE] = {0}; // list of pending gestures, 4-bits per element +static volatile uint16_t gesture_state = 0; // 1 bit per gesture +static volatile uint8_t gesture_list_cur = 0; // index into gesture_list +static volatile uint8_t gesture_list[GESTURE_LIST_SIZE] = {0}; // list of pending gestures, 4-bits per element -STATIC const qstr gesture_name_map[] = { +static const qstr gesture_name_map[] = { [MICROBIT_HAL_ACCELEROMETER_EVT_NONE] = MP_QSTR_, [MICROBIT_HAL_ACCELEROMETER_EVT_TILT_UP] = MP_QSTR_up, [MICROBIT_HAL_ACCELEROMETER_EVT_TILT_DOWN] = MP_QSTR_down, @@ -56,7 +56,7 @@ STATIC const qstr gesture_name_map[] = { [MICROBIT_HAL_ACCELEROMETER_EVT_SHAKE] = MP_QSTR_shake, }; -STATIC uint32_t gesture_from_obj(mp_obj_t gesture_in) { +static uint32_t gesture_from_obj(mp_obj_t gesture_in) { qstr gesture = mp_obj_str_get_qstr(gesture_in); for (uint i = 0; i < MP_ARRAY_SIZE(gesture_name_map); ++i) { if (gesture == gesture_name_map[i]) { @@ -66,7 +66,7 @@ STATIC uint32_t gesture_from_obj(mp_obj_t gesture_in) { mp_raise_ValueError(MP_ERROR_TEXT("invalid gesture")); } -STATIC void update_for_gesture(void) { +static void update_for_gesture(void) { if (!accelerometer_up_to_date) { accelerometer_up_to_date = true; int axis[3]; @@ -90,31 +90,31 @@ void microbit_hal_gesture_callback(int value) { } } -STATIC mp_obj_t microbit_accelerometer_get_x(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_get_x(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_accelerometer_get_sample(axis); return mp_obj_new_int(axis[0]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_x_obj, microbit_accelerometer_get_x); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_x_obj, microbit_accelerometer_get_x); -STATIC mp_obj_t microbit_accelerometer_get_y(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_get_y(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_accelerometer_get_sample(axis); return mp_obj_new_int(axis[1]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_y_obj, microbit_accelerometer_get_y); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_y_obj, microbit_accelerometer_get_y); -STATIC mp_obj_t microbit_accelerometer_get_z(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_get_z(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_accelerometer_get_sample(axis); return mp_obj_new_int(axis[2]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_z_obj, microbit_accelerometer_get_z); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_z_obj, microbit_accelerometer_get_z); -STATIC mp_obj_t microbit_accelerometer_get_values(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_get_values(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_accelerometer_get_sample(axis); @@ -125,33 +125,33 @@ STATIC mp_obj_t microbit_accelerometer_get_values(mp_obj_t self_in) { }; return mp_obj_new_tuple(3, tuple); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_values_obj, microbit_accelerometer_get_values); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_values_obj, microbit_accelerometer_get_values); -STATIC mp_obj_t microbit_accelerometer_get_strength(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_get_strength(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_accelerometer_get_sample(axis); int strength = sqrtf(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]); return mp_obj_new_int(strength); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_strength_obj, microbit_accelerometer_get_strength); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_strength_obj, microbit_accelerometer_get_strength); -STATIC mp_obj_t microbit_accelerometer_current_gesture(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_current_gesture(mp_obj_t self_in) { (void)self_in; update_for_gesture(); return MP_OBJ_NEW_QSTR(gesture_name_map[microbit_hal_accelerometer_get_gesture()]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_current_gesture_obj, microbit_accelerometer_current_gesture); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_current_gesture_obj, microbit_accelerometer_current_gesture); -STATIC mp_obj_t microbit_accelerometer_is_gesture(mp_obj_t self_in, mp_obj_t gesture_in) { +static mp_obj_t microbit_accelerometer_is_gesture(mp_obj_t self_in, mp_obj_t gesture_in) { (void)self_in; uint32_t gesture = gesture_from_obj(gesture_in); update_for_gesture(); return mp_obj_new_bool(microbit_hal_accelerometer_get_gesture() == gesture); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_is_gesture_obj, microbit_accelerometer_is_gesture); +static MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_is_gesture_obj, microbit_accelerometer_is_gesture); -STATIC mp_obj_t microbit_accelerometer_was_gesture(mp_obj_t self_in, mp_obj_t gesture_in) { +static mp_obj_t microbit_accelerometer_was_gesture(mp_obj_t self_in, mp_obj_t gesture_in) { (void)self_in; uint32_t gesture = gesture_from_obj(gesture_in); update_for_gesture(); @@ -160,9 +160,9 @@ STATIC mp_obj_t microbit_accelerometer_was_gesture(mp_obj_t self_in, mp_obj_t ge gesture_list_cur = 0; return result; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_was_gesture_obj, microbit_accelerometer_was_gesture); +static MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_was_gesture_obj, microbit_accelerometer_was_gesture); -STATIC mp_obj_t microbit_accelerometer_get_gestures(mp_obj_t self_in) { +static mp_obj_t microbit_accelerometer_get_gestures(mp_obj_t self_in) { (void)self_in; update_for_gesture(); if (gesture_list_cur == 0) { @@ -176,16 +176,16 @@ STATIC mp_obj_t microbit_accelerometer_get_gestures(mp_obj_t self_in) { gesture_list_cur = 0; return o; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_gestures_obj, microbit_accelerometer_get_gestures); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_gestures_obj, microbit_accelerometer_get_gestures); -STATIC mp_obj_t microbit_accelerometer_set_range(mp_obj_t self_in, mp_obj_t g) { +static mp_obj_t microbit_accelerometer_set_range(mp_obj_t self_in, mp_obj_t g) { (void)self_in; microbit_hal_accelerometer_set_range(mp_obj_get_int(g)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_set_range_obj, microbit_accelerometer_set_range); +static MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_set_range_obj, microbit_accelerometer_set_range); -STATIC const mp_rom_map_elem_t microbit_accelerometer_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_accelerometer_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_get_x), MP_ROM_PTR(µbit_accelerometer_get_x_obj) }, { MP_ROM_QSTR(MP_QSTR_get_y), MP_ROM_PTR(µbit_accelerometer_get_y_obj) }, { MP_ROM_QSTR(MP_QSTR_get_z), MP_ROM_PTR(µbit_accelerometer_get_z_obj) }, @@ -197,13 +197,14 @@ STATIC const mp_rom_map_elem_t microbit_accelerometer_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_get_gestures), MP_ROM_PTR(µbit_accelerometer_get_gestures_obj) }, { MP_ROM_QSTR(MP_QSTR_set_range), MP_ROM_PTR(µbit_accelerometer_set_range_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_accelerometer_locals_dict, microbit_accelerometer_locals_dict_table); - -const mp_obj_type_t microbit_accelerometer_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitAccelerometer, - .locals_dict = (mp_obj_dict_t *)µbit_accelerometer_locals_dict, -}; +static MP_DEFINE_CONST_DICT(microbit_accelerometer_locals_dict, microbit_accelerometer_locals_dict_table); + +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_accelerometer_type, + MP_QSTR_MicroBitAccelerometer, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_accelerometer_locals_dict + ); const microbit_accelerometer_obj_t microbit_accelerometer_obj = { { µbit_accelerometer_type }, diff --git a/src/codal_port/microbit_button.c b/src/codal_port/microbit_button.c index bbc6339..ce3eab3 100644 --- a/src/codal_port/microbit_button.c +++ b/src/codal_port/microbit_button.c @@ -56,19 +56,20 @@ mp_obj_t microbit_button_was_pressed(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(microbit_button_was_pressed_obj, microbit_button_was_pressed); -STATIC const mp_map_elem_t microbit_button_locals_dict_table[] = { +static const mp_map_elem_t microbit_button_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_is_pressed), (mp_obj_t)µbit_button_is_pressed_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_was_pressed), (mp_obj_t)µbit_button_was_pressed_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_get_presses), (mp_obj_t)µbit_button_get_presses_obj }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_button_locals_dict, microbit_button_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_button_locals_dict, microbit_button_locals_dict_table); -const mp_obj_type_t microbit_button_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitButton, - .locals_dict = (mp_obj_dict_t *)µbit_button_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_button_type, + MP_QSTR_MicroBitButton, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_button_locals_dict + ); const microbit_button_obj_t microbit_button_a_obj = { .base = { µbit_button_type }, diff --git a/src/codal_port/microbit_compass.c b/src/codal_port/microbit_compass.c index dab165a..5d5985a 100644 --- a/src/codal_port/microbit_compass.c +++ b/src/codal_port/microbit_compass.c @@ -32,63 +32,63 @@ typedef struct _microbit_compass_obj_t { mp_obj_base_t base; } microbit_compass_obj_t; -STATIC mp_obj_t microbit_compass_is_calibrated(mp_obj_t self_in) { +static mp_obj_t microbit_compass_is_calibrated(mp_obj_t self_in) { (void)self_in; return mp_obj_new_bool(microbit_hal_compass_is_calibrated()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_is_calibrated_obj, microbit_compass_is_calibrated); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_is_calibrated_obj, microbit_compass_is_calibrated); -STATIC mp_obj_t microbit_compass_calibrate(mp_obj_t self_in) { +static mp_obj_t microbit_compass_calibrate(mp_obj_t self_in) { (void)self_in; microbit_hal_compass_calibrate(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_calibrate_obj, microbit_compass_calibrate); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_calibrate_obj, microbit_compass_calibrate); -STATIC mp_obj_t microbit_compass_clear_calibration(mp_obj_t self_in) { +static mp_obj_t microbit_compass_clear_calibration(mp_obj_t self_in) { (void)self_in; microbit_hal_compass_clear_calibration(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_clear_calibration_obj, microbit_compass_clear_calibration); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_clear_calibration_obj, microbit_compass_clear_calibration); -STATIC mp_obj_t microbit_compass_heading(mp_obj_t self_in) { +static mp_obj_t microbit_compass_heading(mp_obj_t self_in) { (void)self_in; return mp_obj_new_int(microbit_hal_compass_get_heading()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_heading_obj, microbit_compass_heading); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_heading_obj, microbit_compass_heading); -STATIC mp_obj_t microbit_compass_get_x(mp_obj_t self_in) { +static mp_obj_t microbit_compass_get_x(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_compass_get_sample(axis); return mp_obj_new_int(axis[0]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_x_obj, microbit_compass_get_x); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_x_obj, microbit_compass_get_x); -STATIC mp_obj_t microbit_compass_get_y(mp_obj_t self_in) { +static mp_obj_t microbit_compass_get_y(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_compass_get_sample(axis); return mp_obj_new_int(axis[1]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_y_obj, microbit_compass_get_y); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_y_obj, microbit_compass_get_y); -STATIC mp_obj_t microbit_compass_get_z(mp_obj_t self_in) { +static mp_obj_t microbit_compass_get_z(mp_obj_t self_in) { (void)self_in; int axis[3]; microbit_hal_compass_get_sample(axis); return mp_obj_new_int(axis[2]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_z_obj, microbit_compass_get_z); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_z_obj, microbit_compass_get_z); -STATIC mp_obj_t microbit_compass_get_field_strength(mp_obj_t self_in) { +static mp_obj_t microbit_compass_get_field_strength(mp_obj_t self_in) { (void)self_in; return mp_obj_new_int(microbit_hal_compass_get_field_strength()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_field_strength_obj, microbit_compass_get_field_strength); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_field_strength_obj, microbit_compass_get_field_strength); -STATIC const mp_rom_map_elem_t microbit_compass_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_compass_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_heading), MP_ROM_PTR(µbit_compass_heading_obj) }, { MP_ROM_QSTR(MP_QSTR_is_calibrated), MP_ROM_PTR(µbit_compass_is_calibrated_obj) }, { MP_ROM_QSTR(MP_QSTR_calibrate), MP_ROM_PTR(µbit_compass_calibrate_obj) }, @@ -98,13 +98,14 @@ STATIC const mp_rom_map_elem_t microbit_compass_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_get_z), MP_ROM_PTR(µbit_compass_get_z_obj) }, { MP_ROM_QSTR(MP_QSTR_get_field_strength), MP_ROM_PTR(µbit_compass_get_field_strength_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_compass_locals_dict, microbit_compass_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_compass_locals_dict, microbit_compass_locals_dict_table); -STATIC const mp_obj_type_t microbit_compass_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitCompass, - .locals_dict = (mp_obj_dict_t *)µbit_compass_locals_dict, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_compass_type, + MP_QSTR_MicroBitCompass, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_compass_locals_dict + ); const microbit_compass_obj_t microbit_compass_obj = { { µbit_compass_type }, diff --git a/src/codal_port/microbit_display.c b/src/codal_port/microbit_display.c index fb41017..e9e3f04 100644 --- a/src/codal_port/microbit_display.c +++ b/src/codal_port/microbit_display.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include #include "py/runtime.h" #include "py/objstr.h" #include "py/mphal.h" @@ -163,7 +164,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_read_light_level_obj, microbit_displa mp_obj_t microbit_display_clear_func(mp_obj_t self) { (void)self; - microbit_hal_display_clear(); + microbit_display_clear(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(microbit_display_clear_obj, microbit_display_clear_func); @@ -178,7 +179,7 @@ void microbit_display_set_pixel(microbit_display_obj_t *display, mp_int_t x, mp_ microbit_hal_display_set_pixel(x, y, bright); } -STATIC mp_obj_t microbit_display_set_pixel_func(mp_uint_t n_args, const mp_obj_t *args) { +static mp_obj_t microbit_display_set_pixel_func(mp_uint_t n_args, const mp_obj_t *args) { (void)n_args; microbit_display_obj_t *self = (microbit_display_obj_t*)args[0]; microbit_display_set_pixel(self, mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3])); @@ -193,13 +194,31 @@ mp_int_t microbit_display_get_pixel(microbit_display_obj_t *display, mp_int_t x, return microbit_hal_display_get_pixel(x, y); } -STATIC mp_obj_t microbit_display_get_pixel_func(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) { +static mp_obj_t microbit_display_get_pixel_func(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) { microbit_display_obj_t *self = (microbit_display_obj_t*)self_in; return MP_OBJ_NEW_SMALL_INT(microbit_display_get_pixel(self, mp_obj_get_int(x_in), mp_obj_get_int(y_in))); } MP_DEFINE_CONST_FUN_OBJ_3(microbit_display_get_pixel_obj, microbit_display_get_pixel_func); -STATIC const mp_rom_map_elem_t microbit_display_locals_dict_table[] = { +static mp_obj_t microbit_display_rotate(mp_obj_t self_in, mp_obj_t angle_in) { + (void)self_in; + mp_float_t angle_degrees = mp_obj_get_float(angle_in); + + // Round angle towards nearest multiple of 90 degrees, within 0..359. + angle_degrees = MICROPY_FLOAT_C_FUN(fmod)(angle_degrees, 360); + if (angle_degrees < 0) { + angle_degrees += 360; + } + unsigned int rotation = (unsigned int)((angle_degrees + 45) / 90); + + // Set the display rotation. + microbit_hal_display_rotate(rotation); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(microbit_display_rotate_obj, microbit_display_rotate); + +static const mp_rom_map_elem_t microbit_display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(µbit_display_get_pixel_obj) }, { MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(µbit_display_set_pixel_obj) }, { MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(µbit_display_show_obj) }, @@ -209,15 +228,16 @@ STATIC const mp_rom_map_elem_t microbit_display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(µbit_display_off_obj) }, { MP_ROM_QSTR(MP_QSTR_is_on), MP_ROM_PTR(µbit_display_is_on_obj) }, { MP_ROM_QSTR(MP_QSTR_read_light_level),MP_ROM_PTR(µbit_display_read_light_level_obj) }, + { MP_ROM_QSTR(MP_QSTR_rotate),MP_ROM_PTR(µbit_display_rotate_obj) }, }; - -STATIC MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table); - -STATIC const mp_obj_type_t microbit_display_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitDisplay, - .locals_dict = (mp_obj_dict_t*)µbit_display_locals_dict, -}; +static MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table); + +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_display_type, + MP_QSTR_MicroBitDisplay, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_display_locals_dict + ); microbit_display_obj_t microbit_display_obj = { .base = {µbit_display_type}, diff --git a/src/codal_port/microbit_i2c.c b/src/codal_port/microbit_i2c.c index 9f64104..b8c1888 100644 --- a/src/codal_port/microbit_i2c.c +++ b/src/codal_port/microbit_i2c.c @@ -31,9 +31,11 @@ typedef struct _microbit_i2c_obj_t { mp_obj_base_t base; + const microbit_pin_obj_t *scl; + const microbit_pin_obj_t *sda; } microbit_i2c_obj_t; -STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_freq, ARG_sda, ARG_scl }; static const mp_arg_t allowed_args[] = { { MP_QSTR_freq, MP_ARG_INT, {.u_int = 100000} }, @@ -55,8 +57,12 @@ STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp scl = microbit_obj_get_pin(args[ARG_scl].u_obj); } + // Acquire new pins and free the previous ones. + microbit_obj_pin_acquire_and_free(µbit_i2c_obj.scl, scl, microbit_pin_mode_i2c); + microbit_obj_pin_acquire_and_free(µbit_i2c_obj.sda, sda, microbit_pin_mode_i2c); + // Initialise the I2C bus. - int ret =microbit_hal_i2c_init(scl->name, sda->name, args[ARG_freq].u_int); + int ret = microbit_hal_i2c_init(scl->name, sda->name, args[ARG_freq].u_int); if (ret != 0) { mp_raise_OSError(ret); @@ -66,7 +72,7 @@ STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_init_obj, 1, microbit_i2c_init); -STATIC mp_obj_t microbit_i2c_scan(mp_obj_t self_in) { +static mp_obj_t microbit_i2c_scan(mp_obj_t self_in) { mp_obj_t list = mp_obj_new_list(0, NULL); // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved for (int addr = 0x08; addr < 0x78; ++addr) { @@ -79,7 +85,7 @@ STATIC mp_obj_t microbit_i2c_scan(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(microbit_i2c_scan_obj, microbit_i2c_scan); -STATIC mp_obj_t microbit_i2c_read(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_i2c_read(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_addr, ARG_n, ARG_repeat }; static const mp_arg_t allowed_args[] = { { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, @@ -103,11 +109,11 @@ STATIC mp_obj_t microbit_i2c_read(mp_uint_t n_args, const mp_obj_t *pos_args, mp } // Return bytes object with read data. - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_read_obj, 1, microbit_i2c_read); -STATIC mp_obj_t microbit_i2c_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_i2c_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_addr, ARG_buf, ARG_repeat }; static const mp_arg_t allowed_args[] = { { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, @@ -134,20 +140,23 @@ STATIC mp_obj_t microbit_i2c_write(mp_uint_t n_args, const mp_obj_t *pos_args, m } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_write_obj, 1, microbit_i2c_write); -STATIC const mp_rom_map_elem_t microbit_i2c_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_i2c_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(µbit_i2c_init_obj) }, { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(µbit_i2c_scan_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(µbit_i2c_read_obj) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(µbit_i2c_write_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_i2c_locals_dict, microbit_i2c_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_i2c_locals_dict, microbit_i2c_locals_dict_table); -STATIC const mp_obj_type_t microbit_i2c_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitI2C, - .locals_dict = (mp_obj_dict_t *)µbit_i2c_locals_dict, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_i2c_type, + MP_QSTR_MicroBitI2C, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_i2c_locals_dict + ); -const microbit_i2c_obj_t microbit_i2c_obj = { +microbit_i2c_obj_t microbit_i2c_obj = { { µbit_i2c_type }, + .scl = µbit_p19_obj, + .sda = µbit_p20_obj, }; diff --git a/src/codal_port/microbit_image.c b/src/codal_port/microbit_image.c index 6aafaa5..f44a5b5 100644 --- a/src/codal_port/microbit_image.c +++ b/src/codal_port/microbit_image.c @@ -29,7 +29,7 @@ #include "py/mphal.h" #include "modmicrobit.h" -STATIC void microbit_image_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +static void microbit_image_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { microbit_image_obj_t *self = (microbit_image_obj_t*)self_in; mp_printf(print, "Image("); if (kind == PRINT_STR) { @@ -51,7 +51,7 @@ STATIC void microbit_image_print(const mp_print_t *print, mp_obj_t self_in, mp_p mp_printf(print, ")"); } -STATIC microbit_image_obj_t *image_from_parsed_str(const char *s, mp_int_t len) { +static microbit_image_obj_t *image_from_parsed_str(const char *s, mp_int_t len) { mp_int_t w = 0; mp_int_t h = 0; mp_int_t line_len = 0; @@ -108,7 +108,7 @@ STATIC microbit_image_obj_t *image_from_parsed_str(const char *s, mp_int_t len) } -STATIC mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { +static mp_obj_t microbit_image_make_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { (void)type_in; mp_arg_check_num(n_args, n_kw, 0, 3, false); @@ -177,7 +177,7 @@ greyscale_t *image_shift(microbit_image_obj_t *self, mp_int_t x, mp_int_t y) { return result; } -STATIC microbit_image_obj_t *image_crop(microbit_image_obj_t *img, mp_int_t x, mp_int_t y, mp_int_t w, mp_int_t h) { +static microbit_image_obj_t *image_crop(microbit_image_obj_t *img, mp_int_t x, mp_int_t y, mp_int_t w, mp_int_t h) { if (w < 0) { w = 0; } @@ -338,7 +338,7 @@ mp_obj_t microbit_image_invert(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(microbit_image_invert_obj, microbit_image_invert); -STATIC const mp_rom_map_elem_t microbit_image_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_image_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(µbit_image_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(µbit_image_height_obj) }, { MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(µbit_image_get_pixel_obj) }, @@ -420,9 +420,9 @@ STATIC const mp_rom_map_elem_t microbit_image_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_SNAKE), MP_ROM_PTR(µbit_const_image_snake_obj) }, { MP_ROM_QSTR(MP_QSTR_SCISSORS), MP_ROM_PTR(µbit_const_image_scissors_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_image_locals_dict, microbit_image_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_image_locals_dict, microbit_image_locals_dict_table); -STATIC const unsigned char *get_font_data_from_char(char c) { +static const unsigned char *get_font_data_from_char(char c) { const uint8_t *data = microbit_hal_get_font_data(c); if (data == NULL) { data = microbit_hal_get_font_data('?'); @@ -430,12 +430,12 @@ STATIC const unsigned char *get_font_data_from_char(char c) { return data; } -STATIC mp_int_t get_pixel_from_font_data(const unsigned char *data, int x, int y) { +static mp_int_t get_pixel_from_font_data(const unsigned char *data, int x, int y) { // The following logic belongs in MicroBitFont return ((data[y] >> (4 - x)) & 1); } -STATIC void microbit_image_set_from_char(greyscale_t *img, char c) { +static void microbit_image_set_from_char(greyscale_t *img, char c) { const unsigned char *data = get_font_data_from_char(c); for (int x = 0; x < MICROBIT_DISPLAY_WIDTH; ++x) { for (int y = 0; y < MICROBIT_DISPLAY_HEIGHT; ++y) { @@ -464,7 +464,7 @@ microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t f return (microbit_image_obj_t *)result; } -STATIC microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add) { +static microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, microbit_image_obj_t *rhs, bool add) { mp_int_t h = image_height(lhs); mp_int_t w = image_width(lhs); if (image_height(rhs) != h || image_width(rhs) != w) { @@ -487,7 +487,7 @@ STATIC microbit_image_obj_t *microbit_image_sum(microbit_image_obj_t *lhs, micro return (microbit_image_obj_t *)result; } -STATIC mp_obj_t image_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { +static mp_obj_t image_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { if (mp_obj_get_type(lhs_in) != µbit_image_type) { return MP_OBJ_NULL; // op not supported } @@ -509,15 +509,15 @@ STATIC mp_obj_t image_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs return microbit_image_sum(lhs, (microbit_image_obj_t *)rhs_in, op == MP_BINARY_OP_ADD); } - -const mp_obj_type_t microbit_image_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitImage, - .print = microbit_image_print, - .make_new = microbit_image_make_new, - .binary_op = image_binary_op, - .locals_dict = (mp_obj_dict_t*)µbit_image_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_image_type, + MP_QSTR_MicroBitImage, + MP_TYPE_FLAG_NONE, + make_new, microbit_image_make_new, + print, microbit_image_print, + binary_op, image_binary_op, + locals_dict, µbit_image_locals_dict + ); /////////////////////////////////////////////////////////////////////////////////// @@ -558,7 +558,7 @@ mp_obj_t scrolling_string_image_iterable(const char* str, mp_uint_t len, mp_obj_ return result; } -STATIC int font_column_non_blank(const unsigned char *font_data, unsigned int col) { +static int font_column_non_blank(const unsigned char *font_data, unsigned int col) { for (int y = 0; y < MICROBIT_DISPLAY_HEIGHT; ++y) { if (get_pixel_from_font_data(font_data, col, y)) { return 1; @@ -568,7 +568,7 @@ STATIC int font_column_non_blank(const unsigned char *font_data, unsigned int co } /* Not strictly the rightmost non-blank column, but the rightmost in columns 2,3 or 4. */ -STATIC unsigned int rightmost_non_blank_column(const unsigned char *font_data) { +static unsigned int rightmost_non_blank_column(const unsigned char *font_data) { if (font_column_non_blank(font_data, 4)) { return 4; } @@ -594,7 +594,7 @@ static void restart(scrolling_string_iterator_t *iter) { } } -STATIC mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { +static mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { (void)iter_buf; // not big enough to hold scrolling_string_iterator_t scrolling_string_t *str = (scrolling_string_t *)o_in; scrolling_string_iterator_t *result = m_new_obj(scrolling_string_iterator_t); @@ -609,7 +609,7 @@ STATIC mp_obj_t get_microbit_scrolling_string_iter(mp_obj_t o_in, mp_obj_iter_bu return result; } -STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { +static mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { scrolling_string_iterator_t *iter = (scrolling_string_iterator_t *)o_in; if (iter->next_char == iter->end && iter->offset == 5) { if (iter->repeat) { @@ -656,18 +656,19 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) { return iter->img; } -const mp_obj_type_t microbit_scrolling_string_type = { - { &mp_type_type }, - .name = MP_QSTR_ScrollingString, - .getiter = get_microbit_scrolling_string_iter, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_scrolling_string_type, + MP_QSTR_ScrollingString, + MP_TYPE_FLAG_ITER_IS_GETITER, + iter, get_microbit_scrolling_string_iter + ); -const mp_obj_type_t microbit_scrolling_string_iterator_type = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = microbit_scrolling_string_iter_next, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_scrolling_string_iterator_type, + MP_QSTR_iterator, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, + iter, microbit_scrolling_string_iter_next + ); /////////////////////////////////////////////////////////////////////////////////// @@ -708,13 +709,14 @@ static mp_obj_t facade_unary_op(mp_unary_op_t op, mp_obj_t self_in) { static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf); -const mp_obj_type_t string_image_facade_type = { - { &mp_type_type }, - .name = MP_QSTR_Facade, - .unary_op = facade_unary_op, - .subscr = string_image_facade_subscr, - .getiter = microbit_facade_iterator, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + string_image_facade_type, + MP_QSTR_Facade, + MP_TYPE_FLAG_ITER_IS_GETITER, + unary_op, facade_unary_op, + subscr, string_image_facade_subscr, + iter, microbit_facade_iterator + ); /////////////////////////////////////////////////////////////////////////////////// @@ -745,12 +747,12 @@ static mp_obj_t microbit_facade_iter_next(mp_obj_t iter_in) { return iter->image; } -const mp_obj_type_t microbit_facade_iterator_type = { - { &mp_type_type }, - .name = MP_QSTR_iterator, - .getiter = mp_identity_getiter, - .iternext = microbit_facade_iter_next, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_facade_iterator_type, + MP_QSTR_iterator, + MP_TYPE_FLAG_ITER_IS_ITERNEXT, + iter, microbit_facade_iter_next + ); static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) { assert(sizeof(facade_iterator_t) <= sizeof(mp_obj_iter_buf_t)); diff --git a/src/codal_port/microbit_microphone.c b/src/codal_port/microbit_microphone.c index 59a43d5..cf37069 100644 --- a/src/codal_port/microbit_microphone.c +++ b/src/codal_port/microbit_microphone.c @@ -32,6 +32,7 @@ #define SOUND_EVENT_QUIET (0) #define SOUND_EVENT_LOUD (1) +#define SOUND_EVENT_CLAP (2) typedef struct _microbit_microphone_obj_t { mp_obj_base_t base; @@ -40,6 +41,7 @@ typedef struct _microbit_microphone_obj_t { static const mp_const_obj_t sound_event_obj_map[] = { [SOUND_EVENT_QUIET] = MP_ROM_PTR(µbit_soundevent_quiet_obj), [SOUND_EVENT_LOUD] = MP_ROM_PTR(µbit_soundevent_loud_obj), + [SOUND_EVENT_CLAP] = MP_ROM_PTR(µbit_soundevent_clap_obj), }; static uint8_t sound_event_current = SOUND_EVENT_QUIET; @@ -54,6 +56,8 @@ void microbit_hal_level_detector_callback(int value) { ev = SOUND_EVENT_QUIET; } else if (value == MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH) { ev = SOUND_EVENT_LOUD; + } else if (value == MICROBIT_HAL_MICROPHONE_EVT_CLAP) { + ev = SOUND_EVENT_CLAP; } else { // Ignore unknown events. return; @@ -67,11 +71,11 @@ void microbit_hal_level_detector_callback(int value) { } } -STATIC void microphone_init(void) { +static void microphone_init(void) { microbit_hal_microphone_init(); } -STATIC uint8_t sound_event_from_obj(mp_obj_t sound) { +static uint8_t sound_event_from_obj(mp_obj_t sound) { for (uint8_t i = 0; i < MP_ARRAY_SIZE(sound_event_obj_map); ++i) { if (sound == sound_event_obj_map[i]) { return i; @@ -80,7 +84,7 @@ STATIC uint8_t sound_event_from_obj(mp_obj_t sound) { mp_raise_ValueError(MP_ERROR_TEXT("invalid sound")); } -STATIC mp_obj_t microbit_microphone_set_threshold(mp_obj_t self_in, mp_obj_t sound_in, mp_obj_t value_in) { +static mp_obj_t microbit_microphone_set_threshold(mp_obj_t self_in, mp_obj_t sound_in, mp_obj_t value_in) { (void)self_in; uint8_t sound = sound_event_from_obj(sound_in); int kind; @@ -96,31 +100,38 @@ STATIC mp_obj_t microbit_microphone_set_threshold(mp_obj_t self_in, mp_obj_t sou microbit_hal_microphone_set_threshold(kind, value); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(microbit_microphone_set_threshold_obj, microbit_microphone_set_threshold); +static MP_DEFINE_CONST_FUN_OBJ_3(microbit_microphone_set_threshold_obj, microbit_microphone_set_threshold); -STATIC mp_obj_t microbit_microphone_sound_level(mp_obj_t self_in) { +static mp_obj_t microbit_microphone_sound_level(mp_obj_t self_in) { (void)self_in; microphone_init(); return MP_OBJ_NEW_SMALL_INT(microbit_hal_microphone_get_level()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_sound_level_obj, microbit_microphone_sound_level); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_sound_level_obj, microbit_microphone_sound_level); -STATIC mp_obj_t microbit_microphone_current_event(mp_obj_t self_in) { +static mp_obj_t microbit_microphone_sound_level_db(mp_obj_t self_in) { + (void)self_in; + microphone_init(); + return mp_obj_new_float_from_f(microbit_hal_microphone_get_level_db()); +} +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_sound_level_db_obj, microbit_microphone_sound_level_db); + +static mp_obj_t microbit_microphone_current_event(mp_obj_t self_in) { (void)self_in; microphone_init(); return (mp_obj_t)sound_event_obj_map[sound_event_current]; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_current_event_obj, microbit_microphone_current_event); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_current_event_obj, microbit_microphone_current_event); -STATIC mp_obj_t microbit_microphone_is_event(mp_obj_t self_in, mp_obj_t sound_in) { +static mp_obj_t microbit_microphone_is_event(mp_obj_t self_in, mp_obj_t sound_in) { (void)self_in; microphone_init(); uint8_t sound = sound_event_from_obj(sound_in); return mp_obj_new_bool(sound == sound_event_current); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_microphone_is_event_obj, microbit_microphone_is_event); +static MP_DEFINE_CONST_FUN_OBJ_2(microbit_microphone_is_event_obj, microbit_microphone_is_event); -STATIC mp_obj_t microbit_microphone_was_event(mp_obj_t self_in, mp_obj_t sound_in) { +static mp_obj_t microbit_microphone_was_event(mp_obj_t self_in, mp_obj_t sound_in) { (void)self_in; microphone_init(); uint8_t sound = sound_event_from_obj(sound_in); @@ -129,9 +140,9 @@ STATIC mp_obj_t microbit_microphone_was_event(mp_obj_t self_in, mp_obj_t sound_i sound_event_history_index = 0; return result; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_microphone_was_event_obj, microbit_microphone_was_event); +static MP_DEFINE_CONST_FUN_OBJ_2(microbit_microphone_was_event_obj, microbit_microphone_was_event); -STATIC mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) { +static mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) { (void)self_in; microphone_init(); if (sound_event_history_index == 0) { @@ -145,23 +156,25 @@ STATIC mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) { sound_event_history_index = 0; return o; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events); -STATIC const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_set_threshold), MP_ROM_PTR(µbit_microphone_set_threshold_obj) }, { MP_ROM_QSTR(MP_QSTR_sound_level), MP_ROM_PTR(µbit_microphone_sound_level_obj) }, + { MP_ROM_QSTR(MP_QSTR_sound_level_db), MP_ROM_PTR(µbit_microphone_sound_level_db_obj) }, { MP_ROM_QSTR(MP_QSTR_current_event), MP_ROM_PTR(µbit_microphone_current_event_obj) }, { MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(µbit_microphone_is_event_obj) }, { MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(µbit_microphone_was_event_obj) }, { MP_ROM_QSTR(MP_QSTR_get_events), MP_ROM_PTR(µbit_microphone_get_events_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_microphone_locals_dict, microbit_microphone_locals_dict_table); - -const mp_obj_type_t microbit_microphone_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitMicrophone, - .locals_dict = (mp_obj_dict_t *)µbit_microphone_locals_dict, -}; +static MP_DEFINE_CONST_DICT(microbit_microphone_locals_dict, microbit_microphone_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + microbit_microphone_type, + MP_QSTR_MicroBitMicrophone, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_microphone_locals_dict + ); const microbit_microphone_obj_t microbit_microphone_obj = { { µbit_microphone_type }, diff --git a/src/codal_port/microbit_pin.c b/src/codal_port/microbit_pin.c index 2b807a4..c330341 100644 --- a/src/codal_port/microbit_pin.c +++ b/src/codal_port/microbit_pin.c @@ -48,9 +48,16 @@ const microbit_pin_obj_t microbit_p16_obj = {{µbit_dig_pin_type}, 16, MICR const microbit_pin_obj_t microbit_p19_obj = {{µbit_dig_pin_type}, 19, MICROBIT_HAL_PIN_P19, MODE_I2C}; const microbit_pin_obj_t microbit_p20_obj = {{µbit_dig_pin_type}, 20, MICROBIT_HAL_PIN_P20, MODE_I2C}; -const microbit_pin_obj_t microbit_pin_logo_obj = {{µbit_touch_only_pin_type}, 30, MICROBIT_HAL_PIN_FACE, MODE_UNUSED}; +const microbit_pin_obj_t microbit_pin_logo_obj = {{µbit_touch_only_pin_type}, 30, MICROBIT_HAL_PIN_LOGO, MODE_UNUSED}; const microbit_pin_obj_t microbit_pin_speaker_obj = {{µbit_dig_pin_type}, 31, MICROBIT_HAL_PIN_SPEAKER, MODE_UNUSED}; +static mp_obj_t microbit_pin_touch_calibrate(mp_obj_t self_in) { + microbit_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); + microbit_hal_pin_touch_calibrate(self->name); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_touch_calibrate_obj, microbit_pin_touch_calibrate); + static mp_obj_t microbit_pin_get_mode_func(mp_obj_t self_in) { microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in; return MP_OBJ_NEW_QSTR(microbit_pin_get_mode(self)->name); @@ -155,17 +162,35 @@ mp_obj_t microbit_pin_get_analog_period_microseconds(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_get_analog_period_microseconds_obj, microbit_pin_get_analog_period_microseconds); -mp_obj_t microbit_pin_is_touched(mp_obj_t self_in) { +static int microbit_pin_get_touch_state(mp_obj_t self_in, int *was_touched, int *num_touches) { microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in; const microbit_pinmode_t *mode = microbit_pin_get_mode(self); if (mode != microbit_pin_mode_touch && mode != microbit_pin_mode_button) { microbit_obj_pin_acquire(self, microbit_pin_mode_touch); // set NO_PULL on pin } - return mp_obj_new_bool(microbit_hal_pin_is_touched(self->name)); + return microbit_hal_pin_touch_state(self->name, was_touched, num_touches); +} + +mp_obj_t microbit_pin_is_touched(mp_obj_t self_in) { + return mp_obj_new_bool(microbit_pin_get_touch_state(self_in, NULL, NULL)); } MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_is_touched_obj, microbit_pin_is_touched); +mp_obj_t microbit_pin_was_touched(mp_obj_t self_in) { + int was_touched; + microbit_pin_get_touch_state(self_in, &was_touched, NULL); + return mp_obj_new_bool(was_touched); +} +MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_was_touched_obj, microbit_pin_was_touched); + +mp_obj_t microbit_pin_get_touches(mp_obj_t self_in) { + int num_touches; + microbit_pin_get_touch_state(self_in, NULL, &num_touches); + return MP_OBJ_NEW_SMALL_INT(num_touches); +} +MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_get_touches_obj, microbit_pin_get_touches); + mp_obj_t microbit_pin_set_touch_mode(mp_obj_t self_in, mp_obj_t mode_in) { microbit_pin_obj_t *self = (microbit_pin_obj_t *)self_in; const microbit_pinmode_t *mode = microbit_pin_get_mode(self); @@ -186,7 +211,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(microbit_pin_set_touch_mode_obj, microbit_pin_set_touc { MP_ROM_QSTR(MP_QSTR_RESISTIVE), MP_ROM_INT(MICROBIT_HAL_PIN_TOUCH_RESISTIVE) }, \ { MP_ROM_QSTR(MP_QSTR_CAPACITIVE), MP_ROM_INT(MICROBIT_HAL_PIN_TOUCH_CAPACITIVE) } -STATIC const mp_rom_map_elem_t microbit_dig_pin_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_dig_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write_digital), MP_ROM_PTR(µbit_pin_write_digital_obj) }, { MP_ROM_QSTR(MP_QSTR_read_digital), MP_ROM_PTR(µbit_pin_read_digital_obj) }, { MP_ROM_QSTR(MP_QSTR_write_analog), MP_ROM_PTR(µbit_pin_write_analog_obj) }, @@ -198,15 +223,16 @@ STATIC const mp_rom_map_elem_t microbit_dig_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_get_mode), MP_ROM_PTR(µbit_pin_get_mode_obj) }, PULL_CONSTANTS, }; -STATIC MP_DEFINE_CONST_DICT(microbit_dig_pin_locals_dict, microbit_dig_pin_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_dig_pin_locals_dict, microbit_dig_pin_locals_dict_table); -const mp_obj_type_t microbit_dig_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitDigitalPin, - .locals_dict = (mp_obj_dict_t *)µbit_dig_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_dig_pin_type, + MP_QSTR_MicroBitDigitalPin, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_dig_pin_locals_dict + ); -STATIC const mp_rom_map_elem_t microbit_ann_pin_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_ann_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write_digital), MP_ROM_PTR(µbit_pin_write_digital_obj) }, { MP_ROM_QSTR(MP_QSTR_read_digital), MP_ROM_PTR(µbit_pin_read_digital_obj) }, { MP_ROM_QSTR(MP_QSTR_write_analog), MP_ROM_PTR(µbit_pin_write_analog_obj) }, @@ -219,15 +245,17 @@ STATIC const mp_rom_map_elem_t microbit_ann_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_get_mode), MP_ROM_PTR(µbit_pin_get_mode_obj) }, PULL_CONSTANTS, }; -STATIC MP_DEFINE_CONST_DICT(microbit_ann_pin_locals_dict, microbit_ann_pin_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_ann_pin_locals_dict, microbit_ann_pin_locals_dict_table); -const mp_obj_type_t microbit_ad_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitAnalogDigitalPin, - .locals_dict = (mp_obj_dict_t *)µbit_ann_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_ad_pin_type, + MP_QSTR_MicroBitAnalogDigitalPin, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_ann_pin_locals_dict + ); -STATIC const mp_rom_map_elem_t microbit_touch_pin_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_touch_pin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_touch_calibrate), MP_ROM_PTR(µbit_pin_touch_calibrate_obj) }, { MP_ROM_QSTR(MP_QSTR_write_digital), MP_ROM_PTR(µbit_pin_write_digital_obj) }, { MP_ROM_QSTR(MP_QSTR_read_digital), MP_ROM_PTR(µbit_pin_read_digital_obj) }, { MP_ROM_QSTR(MP_QSTR_write_analog), MP_ROM_PTR(µbit_pin_write_analog_obj) }, @@ -236,6 +264,8 @@ STATIC const mp_rom_map_elem_t microbit_touch_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_set_analog_period_microseconds), MP_ROM_PTR(µbit_pin_set_analog_period_microseconds_obj) }, { MP_ROM_QSTR(MP_QSTR_get_analog_period_microseconds), MP_ROM_PTR(µbit_pin_get_analog_period_microseconds_obj) }, { MP_ROM_QSTR(MP_QSTR_is_touched), MP_ROM_PTR(µbit_pin_is_touched_obj) }, + { MP_ROM_QSTR(MP_QSTR_was_touched), MP_ROM_PTR(µbit_pin_was_touched_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_touches), MP_ROM_PTR(µbit_pin_get_touches_obj) }, { MP_ROM_QSTR(MP_QSTR_get_pull), MP_ROM_PTR(µbit_pin_get_pull_obj) }, { MP_ROM_QSTR(MP_QSTR_set_pull), MP_ROM_PTR(µbit_pin_set_pull_obj) }, { MP_ROM_QSTR(MP_QSTR_get_mode), MP_ROM_PTR(µbit_pin_get_mode_obj) }, @@ -243,26 +273,31 @@ STATIC const mp_rom_map_elem_t microbit_touch_pin_locals_dict_table[] = { PULL_CONSTANTS, TOUCH_CONSTANTS, }; -STATIC MP_DEFINE_CONST_DICT(microbit_touch_pin_locals_dict, microbit_touch_pin_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_touch_pin_locals_dict, microbit_touch_pin_locals_dict_table); -const mp_obj_type_t microbit_touch_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitTouchPin, - .locals_dict = (mp_obj_dict_t *)µbit_touch_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_touch_pin_type, + MP_QSTR_MicroBitTouchPin, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_touch_pin_locals_dict + ); -STATIC const mp_rom_map_elem_t microbit_touch_only_pin_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_touch_only_pin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_touch_calibrate), MP_ROM_PTR(µbit_pin_touch_calibrate_obj) }, { MP_ROM_QSTR(MP_QSTR_is_touched), MP_ROM_PTR(µbit_pin_is_touched_obj) }, + { MP_ROM_QSTR(MP_QSTR_was_touched), MP_ROM_PTR(µbit_pin_was_touched_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_touches), MP_ROM_PTR(µbit_pin_get_touches_obj) }, { MP_ROM_QSTR(MP_QSTR_set_touch_mode), MP_ROM_PTR(µbit_pin_set_touch_mode_obj) }, TOUCH_CONSTANTS, }; -STATIC MP_DEFINE_CONST_DICT(microbit_touch_only_pin_locals_dict, microbit_touch_only_pin_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_touch_only_pin_locals_dict, microbit_touch_only_pin_locals_dict_table); -const mp_obj_type_t microbit_touch_only_pin_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitTouchOnlyPin, - .locals_dict = (mp_obj_dict_t *)µbit_touch_only_pin_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_touch_only_pin_type, + MP_QSTR_MicroBitTouchOnlyPin, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_touch_only_pin_locals_dict + ); const microbit_pin_obj_t *microbit_obj_get_pin(mp_const_obj_t o) { const mp_obj_type_t *type = mp_obj_get_type(o); diff --git a/src/codal_port/microbit_pinaudio.c b/src/codal_port/microbit_pinaudio.c index 69f67d9..708ff5e 100644 --- a/src/codal_port/microbit_pinaudio.c +++ b/src/codal_port/microbit_pinaudio.c @@ -28,7 +28,7 @@ #include "modmicrobit.h" // The currently selected pin output for the audio. -STATIC const microbit_pin_obj_t *audio_routed_pin = NULL; +static const microbit_pin_obj_t *audio_routed_pin = NULL; void microbit_pin_audio_select(mp_const_obj_t select, const microbit_pinmode_t *pinmode) { // Work out which pins are requested for the audio output. diff --git a/src/codal_port/microbit_pinmode.c b/src/codal_port/microbit_pinmode.c index ceb5a8f..e79849e 100644 --- a/src/codal_port/microbit_pinmode.c +++ b/src/codal_port/microbit_pinmode.c @@ -73,6 +73,14 @@ bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinm } } +void microbit_obj_pin_acquire_and_free(const microbit_pin_obj_t **old_pin, const microbit_pin_obj_t *new_pin, const microbit_pinmode_t *new_mode) { + microbit_obj_pin_acquire(new_pin, new_mode); + if (*old_pin != new_pin) { + microbit_obj_pin_free(*old_pin); + *old_pin = new_pin; + } +} + static void noop(const microbit_pin_obj_t *pin) { (void)pin; } diff --git a/src/codal_port/microbit_sound.c b/src/codal_port/microbit_sound.c index 06ef823..aac72a1 100644 --- a/src/codal_port/microbit_sound.c +++ b/src/codal_port/microbit_sound.c @@ -28,7 +28,7 @@ #include "modmicrobit.h" #define SOUND(name, ...) \ - STATIC const microbit_sound_obj_t microbit_sound_ ## name ## _obj = { \ + static const microbit_sound_obj_t microbit_sound_ ## name ## _obj = { \ { µbit_sound_type }, \ MP_STRINGIFY(name) \ } @@ -46,12 +46,12 @@ SOUND(yawn); #undef SOUND -STATIC void microbit_sound_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +static void microbit_sound_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { const microbit_sound_obj_t *self = (const microbit_sound_obj_t *)self_in; mp_printf(print, "Sound('%s')", self->name); } -STATIC const mp_rom_map_elem_t microbit_sound_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_sound_locals_dict_table[] = { #define SOUND(NAME, name) { MP_ROM_QSTR(MP_QSTR_ ## NAME), MP_ROM_PTR(µbit_sound_ ## name ## _obj) } SOUND(GIGGLE, giggle), @@ -67,11 +67,12 @@ STATIC const mp_rom_map_elem_t microbit_sound_locals_dict_table[] = { #undef SOUND }; -STATIC MP_DEFINE_CONST_DICT(microbit_sound_locals_dict, microbit_sound_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_sound_locals_dict, microbit_sound_locals_dict_table); -const mp_obj_type_t microbit_sound_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitSound, - .print = microbit_sound_print, - .locals_dict = (mp_obj_dict_t *)µbit_sound_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_sound_type, + MP_QSTR_MicroBitSound, + MP_TYPE_FLAG_NONE, + print, microbit_sound_print, + locals_dict, µbit_sound_locals_dict + ); diff --git a/src/codal_port/microbit_soundeffect.c b/src/codal_port/microbit_soundeffect.c index 0e00f79..70bc969 100644 --- a/src/codal_port/microbit_soundeffect.c +++ b/src/codal_port/microbit_soundeffect.c @@ -25,6 +25,7 @@ */ #include "py/runtime.h" +#include "microbithal.h" #include "modmicrobit.h" #include "modaudio.h" @@ -90,7 +91,7 @@ typedef struct _soundeffect_attr_t { uint8_t length; } soundeffect_attr_t; -STATIC const uint16_t waveform_to_qstr_table[5] = { +static const uint16_t waveform_to_qstr_table[5] = { [SOUND_EFFECT_WAVEFORM_SINE] = MP_QSTR_WAVEFORM_SINE, [SOUND_EFFECT_WAVEFORM_SAWTOOTH] = MP_QSTR_WAVEFORM_SAWTOOTH, [SOUND_EFFECT_WAVEFORM_TRIANGLE] = MP_QSTR_WAVEFORM_TRIANGLE, @@ -98,14 +99,14 @@ STATIC const uint16_t waveform_to_qstr_table[5] = { [SOUND_EFFECT_WAVEFORM_NOISE] = MP_QSTR_WAVEFORM_NOISE, }; -STATIC const uint16_t fx_to_qstr_table[4] = { +static const uint16_t fx_to_qstr_table[4] = { [SOUND_EFFECT_FX_NONE] = MP_QSTR_FX_NONE, [SOUND_EFFECT_FX_TREMOLO] = MP_QSTR_FX_TREMOLO, [SOUND_EFFECT_FX_VIBRATO] = MP_QSTR_FX_VIBRATO, [SOUND_EFFECT_FX_WARBLE] = MP_QSTR_FX_WARBLE, }; -STATIC const soundeffect_attr_t soundeffect_attr_table[] = { +static const soundeffect_attr_t soundeffect_attr_table[] = { { MP_QSTR_freq_start, SOUND_EXPR_FREQUENCY_START_OFFSET, SOUND_EXPR_FREQUENCY_START_LENGTH }, { MP_QSTR_freq_end, SOUND_EXPR_FREQUENCY_END_OFFSET, SOUND_EXPR_FREQUENCY_END_LENGTH }, { MP_QSTR_duration, SOUND_EXPR_DURATION_OFFSET, SOUND_EXPR_DURATION_LENGTH }, @@ -116,12 +117,24 @@ STATIC const soundeffect_attr_t soundeffect_attr_table[] = { { MP_QSTR_shape, SOUND_EXPR_SHAPE_OFFSET, SOUND_EXPR_SHAPE_LENGTH }, }; +static const uint8_t fx_default_param[] = { + [SOUND_EFFECT_FX_VIBRATO] = MICROBIT_HAL_SFX_DEFAULT_VIBRATO_PARAM, + [SOUND_EFFECT_FX_TREMOLO] = MICROBIT_HAL_SFX_DEFAULT_TREMOLO_PARAM, + [SOUND_EFFECT_FX_WARBLE] = MICROBIT_HAL_SFX_DEFAULT_WARBLE_PARAM, +}; + +static const uint16_t fx_default_steps[] = { + [SOUND_EFFECT_FX_VIBRATO] = MICROBIT_HAL_SFX_DEFAULT_VIBRATO_STEPS, + [SOUND_EFFECT_FX_TREMOLO] = MICROBIT_HAL_SFX_DEFAULT_TREMOLO_STEPS, + [SOUND_EFFECT_FX_WARBLE] = MICROBIT_HAL_SFX_DEFAULT_WARBLE_STEPS, +}; + const char *microbit_soundeffect_get_sound_expr_data(mp_obj_t self_in) { const microbit_soundeffect_obj_t *self = MP_OBJ_TO_PTR(self_in); return &self->sound_expr[0]; } -STATIC void sound_expr_encode(microbit_soundeffect_obj_t *self, size_t offset, size_t length, unsigned int value) { +static void sound_expr_encode(microbit_soundeffect_obj_t *self, size_t offset, size_t length, unsigned int value) { if (offset == SOUND_EXPR_VOLUME_START_OFFSET || offset == SOUND_EXPR_VOLUME_END_OFFSET) { if (value > 255) { mp_raise_ValueError(MP_ERROR_TEXT("maximum value is 255")); @@ -143,7 +156,7 @@ STATIC void sound_expr_encode(microbit_soundeffect_obj_t *self, size_t offset, s } } -STATIC unsigned int sound_expr_decode(const microbit_soundeffect_obj_t *self, size_t offset, size_t length) { +static unsigned int sound_expr_decode(const microbit_soundeffect_obj_t *self, size_t offset, size_t length) { unsigned int value = 0; for (size_t i = 0; i < length; ++i) { value = value * 10 + self->sound_expr[offset + i] - '0'; @@ -154,7 +167,7 @@ STATIC unsigned int sound_expr_decode(const microbit_soundeffect_obj_t *self, si return value; } -STATIC void microbit_soundeffect_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +static void microbit_soundeffect_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { const microbit_soundeffect_obj_t *self = MP_OBJ_TO_PTR(self_in); unsigned int freq_start = sound_expr_decode(self, SOUND_EXPR_FREQUENCY_START_OFFSET, SOUND_EXPR_FREQUENCY_START_LENGTH); @@ -208,7 +221,7 @@ STATIC void microbit_soundeffect_print(const mp_print_t *print, mp_obj_t self_in // Constructor: // SoundEffect(freq_start, freq_end, duration, vol_start, vol_end, waveform, fx, shape) -STATIC mp_obj_t microbit_soundeffect_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args_in) { +static mp_obj_t microbit_soundeffect_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args_in) { enum { ARG_freq_start, ARG_freq_end, ARG_duration, ARG_vol_start, ARG_vol_end, ARG_waveform, ARG_fx, ARG_shape }; static const mp_arg_t allowed_args[] = { { MP_QSTR_freq_start, MP_ARG_INT, {.u_int = SOUND_EFFECT_DEFAULT_FREQ_START} }, @@ -250,7 +263,7 @@ STATIC mp_obj_t microbit_soundeffect_make_new(const mp_obj_type_t *type, size_t return MP_OBJ_FROM_PTR(self); } -STATIC void microbit_soundeffect_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { +static void microbit_soundeffect_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { microbit_soundeffect_obj_t *self = MP_OBJ_TO_PTR(self_in); const soundeffect_attr_t *soundeffect_attr = NULL; for (size_t i = 0; i < MP_ARRAY_SIZE(soundeffect_attr_table); ++i) { @@ -273,12 +286,17 @@ STATIC void microbit_soundeffect_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des if (self->is_mutable) { unsigned int value = mp_obj_get_int(dest[1]); sound_expr_encode(self, soundeffect_attr->offset, soundeffect_attr->length, value); + if (soundeffect_attr->offset == SOUND_EXPR_FX_CHOICE_OFFSET) { + // Changing the fx choice, so also update the fx parameters for that choice. + sound_expr_encode(self, SOUND_EXPR_FX_PARAM_OFFSET, SOUND_EXPR_FX_PARAM_LENGTH, fx_default_param[value]); + sound_expr_encode(self, SOUND_EXPR_FX_STEPS_OFFSET, SOUND_EXPR_FX_STEPS_LENGTH, fx_default_steps[value]); + } dest[0] = MP_OBJ_NULL; // Indicate store succeeded. } } } -STATIC mp_obj_t microbit_soundeffect_from_string(mp_obj_t str_in) { +static mp_obj_t microbit_soundeffect_from_string(mp_obj_t str_in) { microbit_soundeffect_obj_t *self = m_new_obj(microbit_soundeffect_obj_t); self->base.type = µbit_soundeffect_type; self->is_mutable = true; @@ -294,10 +312,10 @@ STATIC mp_obj_t microbit_soundeffect_from_string(mp_obj_t str_in) { return MP_OBJ_FROM_PTR(self); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_soundeffect_from_string_obj, microbit_soundeffect_from_string); -STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(microbit_soundeffect_from_string_staticmethod_obj, MP_ROM_PTR(µbit_soundeffect_from_string_obj)); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_soundeffect_from_string_obj, microbit_soundeffect_from_string); +static MP_DEFINE_CONST_STATICMETHOD_OBJ(microbit_soundeffect_from_string_staticmethod_obj, MP_ROM_PTR(µbit_soundeffect_from_string_obj)); -STATIC mp_obj_t microbit_soundeffect_copy(mp_obj_t self_in) { +static mp_obj_t microbit_soundeffect_copy(mp_obj_t self_in) { microbit_soundeffect_obj_t *self = MP_OBJ_TO_PTR(self_in); microbit_soundeffect_obj_t *copy = m_new_obj(microbit_soundeffect_obj_t); copy->base.type = self->base.type; @@ -306,9 +324,9 @@ STATIC mp_obj_t microbit_soundeffect_copy(mp_obj_t self_in) { return MP_OBJ_FROM_PTR(copy); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_soundeffect_copy_obj, microbit_soundeffect_copy); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_soundeffect_copy_obj, microbit_soundeffect_copy); -STATIC const mp_rom_map_elem_t microbit_soundeffect_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_soundeffect_locals_dict_table[] = { // Static methods. { MP_ROM_QSTR(MP_QSTR__from_string), MP_ROM_PTR(µbit_soundeffect_from_string_staticmethod_obj) }, @@ -335,13 +353,14 @@ STATIC const mp_rom_map_elem_t microbit_soundeffect_locals_dict_table[] = { #undef C }; -STATIC MP_DEFINE_CONST_DICT(microbit_soundeffect_locals_dict, microbit_soundeffect_locals_dict_table); - -const mp_obj_type_t microbit_soundeffect_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitSoundEffect, - .print = microbit_soundeffect_print, - .make_new = microbit_soundeffect_make_new, - .attr = microbit_soundeffect_attr, - .locals_dict = (mp_obj_dict_t *)µbit_soundeffect_locals_dict, -}; +static MP_DEFINE_CONST_DICT(microbit_soundeffect_locals_dict, microbit_soundeffect_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + microbit_soundeffect_type, + MP_QSTR_MicroBitSoundEffect, + MP_TYPE_FLAG_NONE, + make_new, microbit_soundeffect_make_new, + print, microbit_soundeffect_print, + attr, microbit_soundeffect_attr, + locals_dict, µbit_soundeffect_locals_dict + ); diff --git a/src/codal_port/microbit_soundevent.c b/src/codal_port/microbit_soundevent.c index 5edeec3..71b7b20 100644 --- a/src/codal_port/microbit_soundevent.c +++ b/src/codal_port/microbit_soundevent.c @@ -42,20 +42,27 @@ const microbit_soundevent_obj_t microbit_soundevent_quiet_obj = { MP_QSTR_quiet, }; -STATIC void microbit_soundevent_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +const microbit_soundevent_obj_t microbit_soundevent_clap_obj = { + { µbit_soundevent_type }, + MP_QSTR_clap, +}; + +static void microbit_soundevent_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { microbit_soundevent_obj_t *self = (microbit_soundevent_obj_t *)self_in; mp_printf(print, "SoundEvent('%q')", self->name); } -STATIC const mp_rom_map_elem_t microbit_soundevent_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_soundevent_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_LOUD), MP_ROM_PTR(µbit_soundevent_loud_obj) }, { MP_ROM_QSTR(MP_QSTR_QUIET), MP_ROM_PTR(µbit_soundevent_quiet_obj) }, + { MP_ROM_QSTR(MP_QSTR_CLAP), MP_ROM_PTR(µbit_soundevent_clap_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_soundevent_locals_dict, microbit_soundevent_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_soundevent_locals_dict, microbit_soundevent_locals_dict_table); -const mp_obj_type_t microbit_soundevent_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitSoundEvent, - .print = microbit_soundevent_print, - .locals_dict = (mp_obj_dict_t *)µbit_soundevent_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + microbit_soundevent_type, + MP_QSTR_MicroBitSoundEvent, + MP_TYPE_FLAG_NONE, + print, microbit_soundevent_print, + locals_dict, µbit_soundevent_locals_dict + ); diff --git a/src/codal_port/microbit_speaker.c b/src/codal_port/microbit_speaker.c index 5c413d2..936cea3 100644 --- a/src/codal_port/microbit_speaker.c +++ b/src/codal_port/microbit_speaker.c @@ -32,31 +32,32 @@ typedef struct _microbit_speaker_obj_t { mp_obj_base_t base; } microbit_speaker_obj_t; -STATIC mp_obj_t microbit_speaker_off(mp_obj_t self_in) { +static mp_obj_t microbit_speaker_off(mp_obj_t self_in) { (void)self_in; microbit_hal_audio_select_speaker(false); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_speaker_off_obj, microbit_speaker_off); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_speaker_off_obj, microbit_speaker_off); -STATIC mp_obj_t microbit_speaker_on(mp_obj_t self_in) { +static mp_obj_t microbit_speaker_on(mp_obj_t self_in) { (void)self_in; microbit_hal_audio_select_speaker(true); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_speaker_on_obj, microbit_speaker_on); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_speaker_on_obj, microbit_speaker_on); -STATIC const mp_rom_map_elem_t microbit_speaker_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_speaker_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(µbit_speaker_off_obj) }, { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(µbit_speaker_on_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_speaker_locals_dict, microbit_speaker_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_speaker_locals_dict, microbit_speaker_locals_dict_table); -STATIC const mp_obj_type_t microbit_speaker_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitSpeaker, - .locals_dict = (mp_obj_dict_t *)µbit_speaker_locals_dict, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_speaker_type, + MP_QSTR_MicroBitSpeaker, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_speaker_locals_dict + ); const microbit_speaker_obj_t microbit_speaker_obj = { { µbit_speaker_type }, diff --git a/src/codal_port/microbit_spi.c b/src/codal_port/microbit_spi.c index b420043..3685f4e 100644 --- a/src/codal_port/microbit_spi.c +++ b/src/codal_port/microbit_spi.c @@ -32,17 +32,20 @@ typedef struct _microbit_spi_obj_t { mp_obj_base_t base; + const microbit_pin_obj_t *sclk; + const microbit_pin_obj_t *mosi; + const microbit_pin_obj_t *miso; } microbit_spi_obj_t; -STATIC bool microbit_spi_initialised = false; +static bool microbit_spi_initialised = false; -STATIC void microbit_spi_check_initialised(void) { +static void microbit_spi_check_initialised(void) { if (!microbit_spi_initialised) { mp_raise_ValueError(MP_ERROR_TEXT("SPI not initialised")); } } -STATIC mp_obj_t microbit_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_baudrate, ARG_bits, ARG_mode, ARG_sclk, ARG_mosi, ARG_miso }; static const mp_arg_t allowed_args[] = { { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 1000000} }, @@ -71,12 +74,10 @@ STATIC mp_obj_t microbit_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp miso = microbit_obj_get_pin(args[ARG_miso].u_obj); } - // Initialise the pins. - // Note: the pins are not freed, so init'ing the SPI a second time on - // different pins will leave the old pins still in SPI mode. - microbit_obj_pin_acquire(sclk, microbit_pin_mode_spi); - microbit_obj_pin_acquire(mosi, microbit_pin_mode_spi); - microbit_obj_pin_acquire(miso, microbit_pin_mode_spi); + // Acquire new pins and free the previous ones. + microbit_obj_pin_acquire_and_free(µbit_spi_obj.sclk, sclk, microbit_pin_mode_spi); + microbit_obj_pin_acquire_and_free(µbit_spi_obj.mosi, mosi, microbit_pin_mode_spi); + microbit_obj_pin_acquire_and_free(µbit_spi_obj.miso, miso, microbit_pin_mode_spi); // Initialise the SPI bus. int ret = microbit_hal_spi_init(sclk->name, mosi->name, miso->name, @@ -92,7 +93,7 @@ STATIC mp_obj_t microbit_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_spi_init_obj, 1, microbit_spi_init); -STATIC mp_obj_t microbit_spi_write(mp_obj_t self_in, mp_obj_t buf_in) { +static mp_obj_t microbit_spi_write(mp_obj_t self_in, mp_obj_t buf_in) { microbit_spi_check_initialised(); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); @@ -101,10 +102,14 @@ STATIC mp_obj_t microbit_spi_write(mp_obj_t self_in, mp_obj_t buf_in) { } MP_DEFINE_CONST_FUN_OBJ_2(microbit_spi_write_obj, microbit_spi_write); -STATIC mp_obj_t microbit_spi_read(size_t n_args, const mp_obj_t *args) { +static mp_obj_t microbit_spi_read(size_t n_args, const mp_obj_t *args) { microbit_spi_check_initialised(); + mp_int_t nbytes = mp_obj_get_int(args[1]); + if (nbytes < 0) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid number of bytes")); + } vstr_t vstr; - vstr_init_len(&vstr, mp_obj_get_int(args[1])); + vstr_init_len(&vstr, nbytes); uint8_t byte_out = 0; if (n_args == 3) { byte_out = mp_obj_get_int(args[2]); @@ -114,11 +119,11 @@ STATIC mp_obj_t microbit_spi_read(size_t n_args, const mp_obj_t *args) { if (ret != 0) { mp_raise_OSError(ret); } - return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return mp_obj_new_bytes_from_vstr(&vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_spi_read_obj, 2, 3, microbit_spi_read); -STATIC mp_obj_t microbit_spi_write_readinto(mp_obj_t self_in, mp_obj_t write_buf, mp_obj_t read_buf) { +static mp_obj_t microbit_spi_write_readinto(mp_obj_t self_in, mp_obj_t write_buf, mp_obj_t read_buf) { microbit_spi_check_initialised(); mp_buffer_info_t write_bufinfo; mp_get_buffer_raise(write_buf, &write_bufinfo, MP_BUFFER_READ); @@ -132,20 +137,24 @@ STATIC mp_obj_t microbit_spi_write_readinto(mp_obj_t self_in, mp_obj_t write_buf } MP_DEFINE_CONST_FUN_OBJ_3(microbit_spi_write_readinto_obj, microbit_spi_write_readinto); -STATIC const mp_rom_map_elem_t microbit_spi_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_spi_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(µbit_spi_init_obj) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(µbit_spi_write_obj) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(µbit_spi_read_obj) }, { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(µbit_spi_write_readinto_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_spi_locals_dict, microbit_spi_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_spi_locals_dict, microbit_spi_locals_dict_table); -STATIC const mp_obj_type_t microbit_spi_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitSPI, - .locals_dict = (mp_obj_dict_t *)µbit_spi_locals_dict, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_spi_type, + MP_QSTR_MicroBitSPI, + MP_TYPE_FLAG_NONE, + locals_dict, µbit_spi_locals_dict + ); -const microbit_spi_obj_t microbit_spi_obj = { +microbit_spi_obj_t microbit_spi_obj = { { µbit_spi_type }, + .sclk = µbit_p13_obj, + .mosi = µbit_p15_obj, + .miso = µbit_p14_obj, }; diff --git a/src/codal_port/microbit_uart.c b/src/codal_port/microbit_uart.c index 0691864..80cc403 100644 --- a/src/codal_port/microbit_uart.c +++ b/src/codal_port/microbit_uart.c @@ -39,9 +39,9 @@ typedef struct _microbit_uart_obj_t { } microbit_uart_obj_t; // timeout (in ms) to wait between characters when reading -STATIC uint16_t microbit_uart_timeout_char = 0; +static uint16_t microbit_uart_timeout_char = 0; -STATIC mp_obj_t microbit_uart_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_uart_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_pins, ARG_tx, ARG_rx }; static const mp_arg_t allowed_args[] = { { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 9600} }, @@ -93,7 +93,7 @@ STATIC mp_obj_t microbit_uart_init(mp_uint_t n_args, const mp_obj_t *pos_args, m } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_uart_init_obj, 1, microbit_uart_init); -STATIC mp_obj_t microbit_uart_any(mp_obj_t self_in) { +static mp_obj_t microbit_uart_any(mp_obj_t self_in) { (void)self_in; if (mp_hal_stdio_poll(MP_STREAM_POLL_RD)) { return mp_const_true; @@ -103,7 +103,7 @@ STATIC mp_obj_t microbit_uart_any(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(microbit_uart_any_obj, microbit_uart_any); -STATIC const mp_rom_map_elem_t microbit_uart_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(µbit_uart_init_obj) }, { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(µbit_uart_any_obj) }, @@ -115,11 +115,11 @@ STATIC const mp_rom_map_elem_t microbit_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_INT(1) }, { MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_INT(0) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_uart_locals_dict, microbit_uart_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_uart_locals_dict, microbit_uart_locals_dict_table); // Waits at most timeout_ms for at least 1 char to become ready for reading. // Returns true if something available, false if not. -STATIC bool microbit_uart_rx_wait(uint32_t timeout_ms) { +static bool microbit_uart_rx_wait(uint32_t timeout_ms) { uint32_t start = mp_hal_ticks_ms(); for (;;) { if (mp_hal_stdio_poll(MP_STREAM_POLL_RD)) { @@ -132,7 +132,7 @@ STATIC bool microbit_uart_rx_wait(uint32_t timeout_ms) { } } -STATIC mp_uint_t microbit_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { +static mp_uint_t microbit_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { (void)self_in; byte *buf = (byte*)buf_in; (void)errcode; @@ -159,7 +159,7 @@ STATIC mp_uint_t microbit_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t si } } -STATIC mp_uint_t microbit_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { +static mp_uint_t microbit_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { (void)self_in; const char *buf = (const char*)buf_in; (void)errcode; @@ -167,18 +167,19 @@ STATIC mp_uint_t microbit_uart_write(mp_obj_t self_in, const void *buf_in, mp_ui return size; } -STATIC const mp_stream_p_t microbit_uart_stream_p = { +static const mp_stream_p_t microbit_uart_stream_p = { .read = microbit_uart_read, .write = microbit_uart_write, .is_text = false, }; -STATIC const mp_obj_type_t microbit_uart_type = { - { &mp_type_type }, - .name = MP_QSTR_MicroBitUART, - .protocol = µbit_uart_stream_p, - .locals_dict = (mp_obj_dict_t *)µbit_uart_locals_dict, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_uart_type, + MP_QSTR_MicroBitUART, + MP_TYPE_FLAG_NONE, + protocol, µbit_uart_stream_p, + locals_dict, µbit_uart_locals_dict + ); const microbit_uart_obj_t microbit_uart_obj = { { µbit_uart_type }, diff --git a/src/codal_port/microbitfs.c b/src/codal_port/microbitfs.c index f6b8831..0ddff1c 100644 --- a/src/codal_port/microbitfs.c +++ b/src/codal_port/microbitfs.c @@ -1,5 +1,5 @@ /* - * This file is part of the Micro Python project, http://micropython.org/ + * This file is part of the MicroPython project, http://micropython.org/ * * The MIT License (MIT) * @@ -25,7 +25,7 @@ * THE SOFTWARE. */ -// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with: +// This is a copy of the file micropython:ports/nrf/modules/os/microbitfs.c with: // - a call to `microbit_file_opened_for_writing` added in `microbit_file_open` // - a fix to `find_chunk_and_erase` to sweep the filesystem if any free chunks are found @@ -33,7 +33,7 @@ #include #include -#include "modules/uos/microbitfs.h" +#include "modules/os/microbitfs.h" #include "drivers/flash.h" #include "drivers/rng.h" #include "py/obj.h" @@ -121,17 +121,17 @@ typedef struct _persistent_config_t { uint8_t marker; // Should always be PERSISTENT_DATA_MARKER } persistent_config_t; -extern const mp_obj_type_t uos_mbfs_fileio_type; -extern const mp_obj_type_t uos_mbfs_textio_type; +extern const mp_obj_type_t os_mbfs_fileio_type; +extern const mp_obj_type_t os_mbfs_textio_type; // Page indexes count down from the end of ROM. -STATIC uint8_t first_page_index; -STATIC uint8_t last_page_index; -// The number of useable chunks in the file system. -STATIC uint8_t chunks_in_file_system; +static uint8_t first_page_index; +static uint8_t last_page_index; +// The number of usable chunks in the file system. +static uint8_t chunks_in_file_system; // Index of chunk to start searches. This is randomised to even out wear. -STATIC uint8_t start_index; -STATIC file_chunk *file_system_chunks; +static uint8_t start_index; +static file_chunk *file_system_chunks; // Defined by the linker extern byte _fs_start[]; @@ -140,25 +140,25 @@ extern byte _fs_end[]; STATIC_ASSERT((sizeof(file_chunk) == CHUNK_SIZE)); // From micro:bit memory.h -STATIC inline byte *rounddown(byte *addr, uint32_t align) { +static inline byte *rounddown(byte *addr, uint32_t align) { return (byte*)(((uint32_t)addr)&(-align)); } // From micro:bit memory.h -STATIC inline byte *roundup(byte *addr, uint32_t align) { +static inline byte *roundup(byte *addr, uint32_t align) { return (byte*)((((uint32_t)addr)+align-1)&(-align)); } -STATIC inline void *first_page(void) { +static inline void *first_page(void) { return _fs_end - FLASH_PAGESIZE * first_page_index; } -STATIC inline void *last_page(void) { +static inline void *last_page(void) { return _fs_end - FLASH_PAGESIZE * last_page_index; } -STATIC void init_limits(void) { +static void init_limits(void) { // First determine where to end byte *end = _fs_end; end = rounddown(end, FLASH_PAGESIZE)-FLASH_PAGESIZE; @@ -173,7 +173,7 @@ STATIC void init_limits(void) { chunks_in_file_system = (end-start)>>MBFS_LOG_CHUNK_SIZE; } -STATIC void randomise_start_index(void) { +static void randomise_start_index(void) { start_index = rng_generate_random_word() % chunks_in_file_system + 1; } @@ -191,7 +191,7 @@ void microbit_filesystem_init(void) { } } -STATIC void copy_page(void *dest, void *src) { +static void copy_page(void *dest, void *src) { DEBUG(("FILE DEBUG: Copying page from %lx to %lx.\r\n", (uint32_t)src, (uint32_t)dest)); flash_page_erase((uint32_t)dest); file_chunk *src_chunk = src; @@ -214,7 +214,7 @@ STATIC void copy_page(void *dest, void *src) { // Then all the pages are copied, one by one, into the adjacent newly unused page. // Finally, the persistent data is saved back to the opposite end of the filesystem from whence it came. // -STATIC void filesystem_sweep(void) { +static void filesystem_sweep(void) { persistent_config_t config; uint8_t *page; uint8_t *end_page; @@ -244,11 +244,11 @@ STATIC void filesystem_sweep(void) { } -STATIC inline byte *seek_address(file_descriptor_obj *self) { +static inline byte *seek_address(file_descriptor_obj *self) { return (byte*)&(file_system_chunks[self->seek_chunk].data[self->seek_offset]); } -STATIC uint8_t microbit_find_file(const char *name, int name_len) { +static uint8_t microbit_find_file(const char *name, int name_len) { for (uint8_t index = 1; index <= chunks_in_file_system; index++) { const file_chunk *p = &file_system_chunks[index]; if (p->marker != FILE_START) @@ -272,7 +272,7 @@ STATIC uint8_t microbit_find_file(const char *name, int name_len) { // 3a. Sweep the filesystem and restart. // 3b. Otherwise, fail and return FILE_NOT_FOUND. // -STATIC uint8_t find_chunk_and_erase(void) { +static uint8_t find_chunk_and_erase(void) { // Start search at a random chunk to spread the wear more evenly. // Search for unused chunk uint8_t index = start_index; @@ -320,13 +320,13 @@ STATIC uint8_t find_chunk_and_erase(void) { return find_chunk_and_erase(); } -STATIC mp_obj_t microbit_file_name(file_descriptor_obj *fd) { +static mp_obj_t microbit_file_name(file_descriptor_obj *fd) { return mp_obj_new_str(&(file_system_chunks[fd->start_chunk].header.filename[0]), file_system_chunks[fd->start_chunk].header.name_len); } -STATIC file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary); +static file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary); -STATIC void clear_file(uint8_t chunk) { +static void clear_file(uint8_t chunk) { do { flash_write_byte((uint32_t)&(file_system_chunks[chunk].marker), FREED_CHUNK); DEBUG(("FILE DEBUG: Freeing chunk %d.\n", chunk)); @@ -334,7 +334,7 @@ STATIC void clear_file(uint8_t chunk) { } while (chunk <= chunks_in_file_system); } -STATIC file_descriptor_obj *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) { +static file_descriptor_obj *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) { if (name_len > MAX_FILENAME_LENGTH) { return NULL; } @@ -360,13 +360,8 @@ STATIC file_descriptor_obj *microbit_file_open(const char *name, size_t name_len return microbit_file_descriptor_new(index, write, binary); } -STATIC file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary) { - file_descriptor_obj *res = m_new_obj(file_descriptor_obj); - if (binary) { - res->base.type = &uos_mbfs_fileio_type; - } else { - res->base.type = &uos_mbfs_textio_type; - } +static file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary) { + file_descriptor_obj *res = mp_obj_malloc(file_descriptor_obj, binary ? &os_mbfs_fileio_type : &os_mbfs_textio_type); res->start_chunk = start_chunk; res->seek_chunk = start_chunk; res->seek_offset = file_system_chunks[start_chunk].header.name_len+2; @@ -376,7 +371,7 @@ STATIC file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bo return res; } -STATIC mp_obj_t microbit_remove(mp_obj_t filename) { +static mp_obj_t microbit_remove(mp_obj_t filename) { size_t name_len; const char *name = mp_obj_str_get_data(filename, &name_len); mp_uint_t index = microbit_find_file(name, name_len); @@ -387,13 +382,13 @@ STATIC mp_obj_t microbit_remove(mp_obj_t filename) { return mp_const_none; } -STATIC void check_file_open(file_descriptor_obj *self) { +static void check_file_open(file_descriptor_obj *self) { if (!self->open) { mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file")); } } -STATIC int advance(file_descriptor_obj *self, uint32_t n, bool write) { +static int advance(file_descriptor_obj *self, uint32_t n, bool write) { DEBUG(("FILE DEBUG: Advancing from chunk %d, offset %d.\r\n", self->seek_chunk, self->seek_offset)); self->seek_offset += n; if (self->seek_offset == DATA_PER_CHUNK) { @@ -415,7 +410,7 @@ STATIC int advance(file_descriptor_obj *self, uint32_t n, bool write) { return 0; } -STATIC mp_uint_t microbit_file_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) { +static mp_uint_t microbit_file_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) { file_descriptor_obj *self = (file_descriptor_obj *)obj; check_file_open(self); if (self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) { @@ -445,7 +440,7 @@ STATIC mp_uint_t microbit_file_read(mp_obj_t obj, void *buf, mp_uint_t size, int return bytes_read; } -STATIC mp_uint_t microbit_file_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) { +static mp_uint_t microbit_file_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) { file_descriptor_obj *self = (file_descriptor_obj *)obj; check_file_open(self); if (!self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) { @@ -468,14 +463,14 @@ STATIC mp_uint_t microbit_file_write(mp_obj_t obj, const void *buf, mp_uint_t si return size; } -STATIC void microbit_file_close(file_descriptor_obj *fd) { +static void microbit_file_close(file_descriptor_obj *fd) { if (fd->writable) { flash_write_byte((uint32_t)&(file_system_chunks[fd->start_chunk].header.end_offset), fd->seek_offset); } fd->open = false; } -STATIC mp_obj_t microbit_file_list(void) { +static mp_obj_t microbit_file_list(void) { mp_obj_t res = mp_obj_new_list(0, NULL); for (uint8_t index = 1; index <= chunks_in_file_system; index++) { if (file_system_chunks[index].marker == FILE_START) { @@ -486,7 +481,7 @@ STATIC mp_obj_t microbit_file_list(void) { return res; } -STATIC mp_obj_t microbit_file_size(mp_obj_t filename) { +static mp_obj_t microbit_file_size(mp_obj_t filename) { size_t name_len; const char *name = mp_obj_str_get_data(filename, &name_len); uint8_t chunk = microbit_find_file(name, name_len); @@ -505,7 +500,7 @@ STATIC mp_obj_t microbit_file_size(mp_obj_t filename) { return mp_obj_new_int(len); } -STATIC mp_uint_t file_read_byte(file_descriptor_obj *fd) { +static mp_uint_t file_read_byte(file_descriptor_obj *fd) { if (file_system_chunks[fd->seek_chunk].next_chunk == UNUSED_CHUNK) { uint8_t end_offset = file_system_chunks[fd->start_chunk].header.end_offset; if (end_offset == UNUSED_CHUNK || fd->seek_offset == end_offset) { @@ -517,9 +512,9 @@ STATIC mp_uint_t file_read_byte(file_descriptor_obj *fd) { return res; } -// Now follows the code to integrate this filesystem into the uos module. +// Now follows the code to integrate this filesystem into the os module. -mp_lexer_t *uos_mbfs_new_reader(const char *filename) { +mp_lexer_t *os_mbfs_new_reader(const char *filename) { file_descriptor_obj *fd = microbit_file_open(filename, strlen(filename), false, false); if (fd == NULL) { mp_raise_OSError(MP_ENOENT); @@ -531,7 +526,7 @@ mp_lexer_t *uos_mbfs_new_reader(const char *filename) { return mp_lexer_new(qstr_from_str(filename), reader); } -mp_import_stat_t uos_mbfs_import_stat(const char *path) { +mp_import_stat_t os_mbfs_import_stat(const char *path) { uint8_t chunk = microbit_find_file(path, strlen(path)); if (chunk == FILE_NOT_FOUND) { return MP_IMPORT_STAT_NO_EXIST; @@ -540,38 +535,38 @@ mp_import_stat_t uos_mbfs_import_stat(const char *path) { } } -STATIC mp_obj_t uos_mbfs_file_name(mp_obj_t self) { +static mp_obj_t os_mbfs_file_name(mp_obj_t self) { file_descriptor_obj *fd = (file_descriptor_obj*)self; return microbit_file_name(fd); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_file_name_obj, uos_mbfs_file_name); +static MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_file_name_obj, os_mbfs_file_name); -STATIC mp_obj_t uos_mbfs_file_close(mp_obj_t self) { +static mp_obj_t os_mbfs_file_close(mp_obj_t self) { file_descriptor_obj *fd = (file_descriptor_obj*)self; microbit_file_close(fd); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_file_close_obj, uos_mbfs_file_close); +static MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_file_close_obj, os_mbfs_file_close); -STATIC mp_obj_t uos_mbfs_remove(mp_obj_t name) { +static mp_obj_t os_mbfs_remove(mp_obj_t name) { return microbit_remove(name); } -MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_remove_obj, uos_mbfs_remove); +MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_remove_obj, os_mbfs_remove); -STATIC mp_obj_t uos_mbfs_file___exit__(size_t n_args, const mp_obj_t *args) { +static mp_obj_t os_mbfs_file___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - return uos_mbfs_file_close(args[0]); + return os_mbfs_file_close(args[0]); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uos_mbfs_file___exit___obj, 4, 4, uos_mbfs_file___exit__); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_mbfs_file___exit___obj, 4, 4, os_mbfs_file___exit__); typedef struct { mp_obj_base_t base; mp_fun_1_t iternext; uint8_t index; -} uos_mbfs_ilistdir_it_t; +} os_mbfs_ilistdir_it_t; -STATIC mp_obj_t uos_mbfs_ilistdir_it_iternext(mp_obj_t self_in) { - uos_mbfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); +static mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) { + os_mbfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); // Read until the next FILE_START chunk. for (; self->index <= chunks_in_file_system; self->index++) { @@ -595,28 +590,27 @@ STATIC mp_obj_t uos_mbfs_ilistdir_it_iternext(mp_obj_t self_in) { return MP_OBJ_STOP_ITERATION; } -STATIC mp_obj_t uos_mbfs_ilistdir(void) { - uos_mbfs_ilistdir_it_t *iter = m_new_obj(uos_mbfs_ilistdir_it_t); - iter->base.type = &mp_type_polymorph_iter; - iter->iternext = uos_mbfs_ilistdir_it_iternext; +static mp_obj_t os_mbfs_ilistdir(void) { + os_mbfs_ilistdir_it_t *iter = mp_obj_malloc(os_mbfs_ilistdir_it_t, &mp_type_polymorph_iter); + iter->iternext = os_mbfs_ilistdir_it_iternext; iter->index = 1; return MP_OBJ_FROM_PTR(iter); } -MP_DEFINE_CONST_FUN_OBJ_0(uos_mbfs_ilistdir_obj, uos_mbfs_ilistdir); +MP_DEFINE_CONST_FUN_OBJ_0(os_mbfs_ilistdir_obj, os_mbfs_ilistdir); -MP_DEFINE_CONST_FUN_OBJ_0(uos_mbfs_listdir_obj, microbit_file_list); +MP_DEFINE_CONST_FUN_OBJ_0(os_mbfs_listdir_obj, microbit_file_list); -STATIC mp_obj_t microbit_file_writable(mp_obj_t self) { +static mp_obj_t microbit_file_writable(mp_obj_t self) { return mp_obj_new_bool(((file_descriptor_obj *)self)->writable); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable); -STATIC const mp_map_elem_t uos_mbfs_file_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&uos_mbfs_file_close_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)&uos_mbfs_file_name_obj }, +static const mp_map_elem_t os_mbfs_file_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&os_mbfs_file_close_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)&os_mbfs_file_name_obj }, { MP_ROM_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj }, - { MP_ROM_QSTR(MP_QSTR___exit__), (mp_obj_t)&uos_mbfs_file___exit___obj }, + { MP_ROM_QSTR(MP_QSTR___exit__), (mp_obj_t)&os_mbfs_file___exit___obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_writable), (mp_obj_t)µbit_file_writable_obj }, /* Stream methods */ { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, @@ -624,37 +618,39 @@ STATIC const mp_map_elem_t uos_mbfs_file_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj}, }; -STATIC MP_DEFINE_CONST_DICT(uos_mbfs_file_locals_dict, uos_mbfs_file_locals_dict_table); +static MP_DEFINE_CONST_DICT(os_mbfs_file_locals_dict, os_mbfs_file_locals_dict_table); -STATIC const mp_stream_p_t textio_stream_p = { +static const mp_stream_p_t textio_stream_p = { .read = microbit_file_read, .write = microbit_file_write, .is_text = true, }; -const mp_obj_type_t uos_mbfs_textio_type = { - { &mp_type_type }, - .name = MP_QSTR_TextIO, - .protocol = &textio_stream_p, - .locals_dict = (mp_obj_dict_t*)&uos_mbfs_file_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + os_mbfs_textio_type, + MP_QSTR_TextIO, + MP_TYPE_FLAG_NONE, + protocol, &textio_stream_p, + locals_dict, &os_mbfs_file_locals_dict + ); -STATIC const mp_stream_p_t fileio_stream_p = { +static const mp_stream_p_t fileio_stream_p = { .read = microbit_file_read, .write = microbit_file_write, }; -const mp_obj_type_t uos_mbfs_fileio_type = { - { &mp_type_type }, - .name = MP_QSTR_FileIO, - .protocol = &fileio_stream_p, - .locals_dict = (mp_obj_dict_t*)&uos_mbfs_file_locals_dict, -}; +MP_DEFINE_CONST_OBJ_TYPE( + os_mbfs_fileio_type, + MP_QSTR_FileIO, + MP_TYPE_FLAG_NONE, + protocol, &fileio_stream_p, + locals_dict, &os_mbfs_file_locals_dict + ); // From micro:bit fileobj.c -mp_obj_t uos_mbfs_open(size_t n_args, const mp_obj_t *args) { +mp_obj_t os_mbfs_open(size_t n_args, const mp_obj_t *args) { /// -1 means default; 0 explicitly false; 1 explicitly true. int read = -1; int text = -1; @@ -688,7 +684,7 @@ mp_obj_t uos_mbfs_open(size_t n_args, const mp_obj_t *args) { mp_raise_ValueError(MP_ERROR_TEXT("illegal mode")); } -STATIC mp_obj_t uos_mbfs_stat(mp_obj_t filename) { +static mp_obj_t os_mbfs_stat(mp_obj_t filename) { mp_obj_t file_size = microbit_file_size(filename); mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); @@ -704,6 +700,6 @@ STATIC mp_obj_t uos_mbfs_stat(mp_obj_t filename) { t->items[9] = MP_OBJ_NEW_SMALL_INT(0); // st_ctime return MP_OBJ_FROM_PTR(t); } -MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_stat_obj, uos_mbfs_stat); +MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_stat_obj, os_mbfs_stat); #endif // MICROPY_MBFS diff --git a/src/codal_port/modantigravity.c b/src/codal_port/modantigravity.c index 5de9ee4..842c2c4 100644 --- a/src/codal_port/modantigravity.c +++ b/src/codal_port/modantigravity.c @@ -31,12 +31,12 @@ #define GET_PIXEL(x, y) microbit_hal_display_get_pixel(x, y) #define SET_PIXEL(x, y, v) microbit_hal_display_set_pixel(x, y, v) -STATIC void antigravity_output_char(char c) { +static void antigravity_output_char(char c) { MP_PLAT_PRINT_STRN((char *)&c, 1); } // NOTE: string has to be plain ASCII -STATIC void antigravity_print_rle(const char *s) { +static void antigravity_print_rle(const char *s) { /* RLE encoding format (2 characters, [0] (first) and [1] (second)): * [0] the amount of times to output the specified character (max 127), * bitwise or'ed with 0x80 (to set the last bit, ie: bit 7) @@ -67,7 +67,7 @@ STATIC void antigravity_print_rle(const char *s) { */ //#define ANTIGRAVITY_COMIC_LARGE -STATIC void antigravity(uint8_t interval_ms) { +static void antigravity(uint8_t interval_ms) { /* move all of the LEDs upwards (we can move them in other directions in the * future). * first, output the traditional XKCD comic (either in full or micro:size :) @@ -194,19 +194,21 @@ STATIC void antigravity(uint8_t interval_ms) { } } -STATIC mp_obj_t antigravity__init__(void) { +static mp_obj_t antigravity__init__(void) { antigravity(200); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(antigravity___init___obj, antigravity__init__); +static MP_DEFINE_CONST_FUN_OBJ_0(antigravity___init___obj, antigravity__init__); -STATIC const mp_rom_map_elem_t antigravity_module_globals_table[] = { +static const mp_rom_map_elem_t antigravity_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_antigravity) }, { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&antigravity___init___obj) }, }; -STATIC MP_DEFINE_CONST_DICT(antigravity_module_globals, antigravity_module_globals_table); +static MP_DEFINE_CONST_DICT(antigravity_module_globals, antigravity_module_globals_table); const mp_obj_module_t antigravity_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&antigravity_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_antigravity, antigravity_module); diff --git a/src/codal_port/modaudio.c b/src/codal_port/modaudio.c index 6a4b6c4..5962c22 100644 --- a/src/codal_port/modaudio.c +++ b/src/codal_port/modaudio.c @@ -54,9 +54,10 @@ static inline bool audio_is_running(void) { void microbit_audio_stop(void) { audio_source_iter = NULL; + microbit_hal_audio_stop_expression(); } -STATIC void audio_buffer_ready(void) { +static void audio_buffer_ready(void) { uint32_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); audio_output_state_t old_state = audio_output_state; audio_output_state = AUDIO_OUTPUT_STATE_DATA_READY; @@ -66,7 +67,7 @@ STATIC void audio_buffer_ready(void) { } } -STATIC void audio_data_fetcher(void) { +static void audio_data_fetcher(void) { audio_fetcher_scheduled = false; if (audio_source_iter == NULL) { return; @@ -108,11 +109,11 @@ STATIC void audio_data_fetcher(void) { } } -STATIC mp_obj_t audio_data_fetcher_wrapper(mp_obj_t arg) { +static mp_obj_t audio_data_fetcher_wrapper(mp_obj_t arg) { audio_data_fetcher(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(audio_data_fetcher_wrapper_obj, audio_data_fetcher_wrapper); +static MP_DEFINE_CONST_FUN_OBJ_1(audio_data_fetcher_wrapper_obj, audio_data_fetcher_wrapper); void microbit_hal_audio_ready_callback(void) { if (audio_output_state == AUDIO_OUTPUT_STATE_DATA_READY) { @@ -204,13 +205,14 @@ void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, ui } } -STATIC mp_obj_t stop(void) { +static mp_obj_t stop(void) { microbit_audio_stop(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_stop_obj, stop); -STATIC mp_obj_t play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + // Note: the return_pin argument is for compatibility with micro:bit v1 and is ignored on v2. static const mp_arg_t allowed_args[] = { { MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_wait, MP_ARG_BOOL, {.u_bool = true} }, @@ -221,11 +223,6 @@ STATIC mp_obj_t play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - // The return_pin argument from micro:bit v1 is no longer supported. - if (args[3].u_obj != mp_const_none) { - mp_raise_ValueError(MP_ERROR_TEXT("return_pin not supported")); - } - mp_obj_t src = args[0].u_obj; microbit_audio_play_source(src, args[2].u_obj, args[1].u_bool, DEFAULT_SAMPLE_RATE); return mp_const_none; @@ -241,7 +238,7 @@ mp_obj_t is_playing(void) { } MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_is_playing_obj, is_playing); -STATIC const mp_rom_map_elem_t audio_globals_table[] = { +static const mp_rom_map_elem_t audio_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audio) }, { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(µbit_audio_stop_obj) }, { MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(µbit_audio_play_obj) }, @@ -249,24 +246,26 @@ STATIC const mp_rom_map_elem_t audio_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_AudioFrame), MP_ROM_PTR(µbit_audio_frame_type) }, { MP_ROM_QSTR(MP_QSTR_SoundEffect), MP_ROM_PTR(µbit_soundeffect_type) }, }; -STATIC MP_DEFINE_CONST_DICT(audio_module_globals, audio_globals_table); +static MP_DEFINE_CONST_DICT(audio_module_globals, audio_globals_table); const mp_obj_module_t audio_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&audio_module_globals, }; +MP_REGISTER_MODULE(MP_QSTR_audio, audio_module); + /******************************************************************************/ // AudioFrame class -STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { +static mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { (void)type_in; (void)args; mp_arg_check_num(n_args, n_kw, 0, 0, false); return microbit_audio_frame_make_new(); } -STATIC mp_obj_t audio_frame_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value_in) { +static mp_obj_t audio_frame_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value_in) { microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in; mp_int_t index = mp_obj_get_int(index_in); if (index < 0 || index >= AUDIO_CHUNK_SIZE) { @@ -377,7 +376,7 @@ static void mult(microbit_audio_frame_obj_t *self, float f) { } } -STATIC mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { +static mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { if (mp_obj_get_type(lhs_in) != µbit_audio_frame_type) { return MP_OBJ_NULL; // op not supported } @@ -403,21 +402,22 @@ STATIC mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj } } -STATIC const mp_map_elem_t microbit_audio_frame_locals_dict_table[] = { +static const mp_map_elem_t microbit_audio_frame_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_copyfrom), (mp_obj_t)©from_obj }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_audio_frame_locals_dict, microbit_audio_frame_locals_dict_table); - -const mp_obj_type_t microbit_audio_frame_type = { - { &mp_type_type }, - .name = MP_QSTR_AudioFrame, - .make_new = microbit_audio_frame_new, - .unary_op = audio_frame_unary_op, - .binary_op = audio_frame_binary_op, - .subscr = audio_frame_subscr, - .buffer_p = { .get_buffer = audio_frame_get_buffer }, - .locals_dict = (mp_obj_dict_t*)µbit_audio_frame_locals_dict, -}; +static MP_DEFINE_CONST_DICT(microbit_audio_frame_locals_dict, microbit_audio_frame_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + microbit_audio_frame_type, + MP_QSTR_AudioFrame, + MP_TYPE_FLAG_NONE, + make_new, microbit_audio_frame_new, + unary_op, audio_frame_unary_op, + binary_op, audio_frame_binary_op, + subscr, audio_frame_subscr, + buffer, audio_frame_get_buffer, + locals_dict, µbit_audio_frame_locals_dict + ); microbit_audio_frame_obj_t *microbit_audio_frame_make_new(void) { microbit_audio_frame_obj_t *res = m_new_obj(microbit_audio_frame_obj_t); @@ -425,3 +425,5 @@ microbit_audio_frame_obj_t *microbit_audio_frame_make_new(void) { memset(res->data, 128, AUDIO_CHUNK_SIZE); return res; } + +MP_REGISTER_ROOT_POINTER(void *audio_source); diff --git a/src/codal_port/modaudio.h b/src/codal_port/modaudio.h index 3f3f477..7111715 100644 --- a/src/codal_port/modaudio.h +++ b/src/codal_port/modaudio.h @@ -40,6 +40,7 @@ typedef struct _microbit_audio_frame_obj_t { } microbit_audio_frame_obj_t; extern const mp_obj_type_t microbit_audio_frame_type; +extern const mp_obj_module_t audio_module; void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, uint32_t sample_rate); void microbit_audio_stop(void); diff --git a/src/codal_port/modlog.c b/src/codal_port/modlog.c index 69daa8a..548c9c2 100644 --- a/src/codal_port/modlog.c +++ b/src/codal_port/modlog.c @@ -31,7 +31,7 @@ #define TIMESTAMP_DEFAULT_FORMAT (MICROBIT_HAL_LOG_TIMESTAMP_SECONDS) -STATIC void log_check_error(int result) { +static void log_check_error(int result) { if (result == MICROBIT_HAL_DEVICE_NO_RESOURCES) { mp_raise_OSError(MP_ENOSPC); } else if (result != MICROBIT_HAL_DEVICE_OK) { @@ -39,13 +39,13 @@ STATIC void log_check_error(int result) { } } -STATIC mp_obj_t log___init__(void) { +static mp_obj_t log___init__(void) { microbit_hal_log_set_timestamp(TIMESTAMP_DEFAULT_FORMAT); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(log___init___obj, log___init__); -STATIC mp_obj_t log_set_labels(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t log_set_labels(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_timestamp }; static const mp_arg_t allowed_args[] = { { MP_QSTR_timestamp, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(TIMESTAMP_DEFAULT_FORMAT)} }, @@ -73,15 +73,15 @@ STATIC mp_obj_t log_set_labels(size_t n_args, const mp_obj_t *pos_args, mp_map_t return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(log_set_labels_obj, 0, log_set_labels); +static MP_DEFINE_CONST_FUN_OBJ_KW(log_set_labels_obj, 0, log_set_labels); -STATIC mp_obj_t log_set_mirroring(mp_obj_t serial) { +static mp_obj_t log_set_mirroring(mp_obj_t serial) { microbit_hal_log_set_mirroring(mp_obj_is_true(serial)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(log_set_mirroring_obj, log_set_mirroring); +static MP_DEFINE_CONST_FUN_OBJ_1(log_set_mirroring_obj, log_set_mirroring); -STATIC mp_obj_t log_delete(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t log_delete(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_full }; static const mp_arg_t allowed_args[] = { { MP_QSTR_full, MP_ARG_BOOL, {.u_bool = false} }, @@ -94,9 +94,9 @@ STATIC mp_obj_t log_delete(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(log_delete_obj, 0, log_delete); +static MP_DEFINE_CONST_FUN_OBJ_KW(log_delete_obj, 0, log_delete); -STATIC mp_obj_t log_add(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t log_add(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { // Get the dict to add as a row. mp_map_t *map; if (n_args == 0) { @@ -132,9 +132,9 @@ STATIC mp_obj_t log_add(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(log_add_obj, 0, log_add); +static MP_DEFINE_CONST_FUN_OBJ_KW(log_add_obj, 0, log_add); -STATIC const mp_rom_map_elem_t log_module_globals_table[] = { +static const mp_rom_map_elem_t log_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_log) }, { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&log___init___obj) }, @@ -149,9 +149,11 @@ STATIC const mp_rom_map_elem_t log_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_HOURS), MP_ROM_INT(MICROBIT_HAL_LOG_TIMESTAMP_HOURS) }, { MP_ROM_QSTR(MP_QSTR_DAYS), MP_ROM_INT(MICROBIT_HAL_LOG_TIMESTAMP_DAYS) }, }; -STATIC MP_DEFINE_CONST_DICT(log_module_globals, log_module_globals_table); +static MP_DEFINE_CONST_DICT(log_module_globals, log_module_globals_table); const mp_obj_module_t log_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&log_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_log, log_module); diff --git a/src/codal_port/modlove.c b/src/codal_port/modlove.c index ccca73d..541decf 100644 --- a/src/codal_port/modlove.c +++ b/src/codal_port/modlove.c @@ -28,11 +28,11 @@ #include "py/mphal.h" #include "modmicrobit.h" -STATIC const mp_float_t bright_levels[7] = { +static const mp_float_t bright_levels[7] = { 0.0, 1.0 / 9, 2.0 / 9, 4.0 / 9, 6.0 / 9, 7.0 / 9, 1.0, }; -STATIC void love(int interval_ms) { +static void love(int interval_ms) { microbit_image_obj_t *hearts[MP_ARRAY_SIZE(bright_levels)]; for (uint i = 0; i < MP_ARRAY_SIZE(bright_levels); i++) { hearts[i] = microbit_image_dim(HEART_IMAGE, bright_levels[i]); @@ -57,22 +57,24 @@ STATIC void love(int interval_ms) { microbit_display_clear(); } -STATIC mp_obj_t love_badaboom(void) { +static mp_obj_t love_badaboom(void) { // make love(25); // ! war return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(love_badaboom_obj, love_badaboom); +static MP_DEFINE_CONST_FUN_OBJ_0(love_badaboom_obj, love_badaboom); -STATIC const mp_map_elem_t love_module_globals_table[] = { +static const mp_map_elem_t love_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_love) }, { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&love_badaboom_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_badaboom), (mp_obj_t)&love_badaboom_obj }, }; -STATIC MP_DEFINE_CONST_DICT(love_module_globals, love_module_globals_table); +static MP_DEFINE_CONST_DICT(love_module_globals, love_module_globals_table); const mp_obj_module_t love_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&love_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_love, love_module); diff --git a/src/codal_port/modmachine.c b/src/codal_port/modmachine.c index 4b34670..3b786e3 100644 --- a/src/codal_port/modmachine.c +++ b/src/codal_port/modmachine.c @@ -24,46 +24,47 @@ * THE SOFTWARE. */ -#include "py/mpconfig.h" -#include "extmod/machine_mem.h" -#include "extmod/machine_pulse.h" +#include "py/runtime.h" #include "modmicrobit.h" -#if MICROPY_PY_MACHINE +#undef MICROPY_PY_MACHINE +#define MICROPY_PY_MACHINE (1) +#include "extmod/machine_mem.c" +#undef MICROPY_PY_MACHINE // Returns a string of 8 bytes (64 bits), which is the unique ID for the MCU -STATIC mp_obj_t machine_unique_id(void) { +static mp_obj_t machine_unique_id(void) { uint32_t dev_id[2]; mp_hal_unique_id(dev_id); return mp_obj_new_bytes((const void*)&dev_id, 8); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); +static MP_DEFINE_CONST_FUN_OBJ_0(microbit_unique_id_obj, machine_unique_id); // Get the MCU frequency -STATIC mp_obj_t machine_freq(void) { +static mp_obj_t machine_freq(void) { return MP_OBJ_NEW_SMALL_INT(64000000); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); +static MP_DEFINE_CONST_FUN_OBJ_0(microbit_freq_obj, machine_freq); // Disable interrupt requests -STATIC mp_obj_t machine_disable_irq(void) { +static mp_obj_t machine_disable_irq(void) { return mp_obj_new_int(mp_hal_disable_irq()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq); +static MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq); // Enable interrupt requests -STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) { +static mp_obj_t machine_enable_irq(mp_obj_t state_in) { uint32_t state = mp_obj_get_int(state_in); mp_hal_enable_irq(state); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); +static MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); -STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { +static const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) }, - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, + { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(µbit_unique_id_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(µbit_reset_obj) }, - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, + { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(µbit_freq_obj) }, { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, @@ -74,11 +75,11 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); +static MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); const mp_obj_module_t machine_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&machine_module_globals, }; -#endif // MICROPY_PY_MACHINE +MP_REGISTER_MODULE(MP_QSTR_machine, machine_module); diff --git a/src/codal_port/modmicrobit.c b/src/codal_port/modmicrobit.c index 07afdc9..98d1086 100644 --- a/src/codal_port/modmicrobit.c +++ b/src/codal_port/modmicrobit.c @@ -29,17 +29,18 @@ #include "py/mphal.h" #include "drv_softtimer.h" #include "drv_system.h" +#include "modaudio.h" #include "modmicrobit.h" -STATIC mp_obj_t microbit_run_every_new(uint32_t period_ms); +static mp_obj_t microbit_run_every_new(uint32_t period_ms); -STATIC mp_obj_t microbit_reset_(void) { +static mp_obj_t microbit_reset_(void) { microbit_hal_reset(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(microbit_reset_obj, microbit_reset_); -STATIC mp_obj_t microbit_sleep(mp_obj_t ms_in) { +static mp_obj_t microbit_sleep(mp_obj_t ms_in) { mp_int_t ms; if (mp_obj_is_integer(ms_in)) { ms = mp_obj_get_int(ms_in); @@ -53,12 +54,12 @@ STATIC mp_obj_t microbit_sleep(mp_obj_t ms_in) { } MP_DEFINE_CONST_FUN_OBJ_1(microbit_sleep_obj, microbit_sleep); -STATIC mp_obj_t microbit_running_time(void) { +static mp_obj_t microbit_running_time(void) { return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms()); } MP_DEFINE_CONST_FUN_OBJ_0(microbit_running_time_obj, microbit_running_time); -STATIC mp_obj_t microbit_panic(mp_uint_t n_args, const mp_obj_t *args) { +static mp_obj_t microbit_panic(mp_uint_t n_args, const mp_obj_t *args) { if (n_args == 0) { // TODO the docs don't mention this, so maybe remove it? microbit_hal_panic(999); @@ -69,12 +70,12 @@ STATIC mp_obj_t microbit_panic(mp_uint_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_panic_obj, 0, 1, microbit_panic); -STATIC mp_obj_t microbit_temperature(void) { +static mp_obj_t microbit_temperature(void) { return mp_obj_new_int(microbit_hal_temperature()); } MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature); -STATIC mp_obj_t microbit_set_volume(mp_obj_t volume_in) { +static mp_obj_t microbit_set_volume(mp_obj_t volume_in) { mp_int_t volume = mp_obj_get_int(volume_in); if (volume < 0) { volume = 0; @@ -84,18 +85,18 @@ STATIC mp_obj_t microbit_set_volume(mp_obj_t volume_in) { microbit_hal_audio_set_volume(volume); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_set_volume_obj, microbit_set_volume); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_set_volume_obj, microbit_set_volume); -STATIC mp_obj_t microbit_ws2812_write(mp_obj_t pin_in, mp_obj_t buf_in) { +static mp_obj_t microbit_ws2812_write(mp_obj_t pin_in, mp_obj_t buf_in) { uint8_t pin = microbit_obj_get_pin(pin_in)->name; mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); microbit_hal_pin_write_ws2812(pin, bufinfo.buf, bufinfo.len); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_ws2812_write_obj, microbit_ws2812_write); +static MP_DEFINE_CONST_FUN_OBJ_2(microbit_ws2812_write_obj, microbit_ws2812_write); -STATIC mp_obj_t microbit_run_every(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_run_every(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_callback, ARG_days, ARG_h, ARG_min, ARG_s, ARG_ms }; static const mp_arg_t allowed_args[] = { { MP_QSTR_callback, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, @@ -122,12 +123,12 @@ STATIC mp_obj_t microbit_run_every(size_t n_args, const mp_obj_t *pos_args, mp_m return run_every; } else { // Start the timer now. - return mp_obj_get_type(run_every)->call(run_every, 1, 0, &args[ARG_callback].u_obj); + return MP_OBJ_TYPE_GET_SLOT(mp_obj_get_type(run_every), call)(run_every, 1, 0, &args[ARG_callback].u_obj); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(microbit_run_every_obj, 0, microbit_run_every); +static MP_DEFINE_CONST_FUN_OBJ_KW(microbit_run_every_obj, 0, microbit_run_every); -STATIC mp_obj_t microbit_scale(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_scale(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_value, ARG_from_, ARG_to }; static const mp_arg_t allowed_args[] = { { MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, @@ -160,9 +161,9 @@ STATIC mp_obj_t microbit_scale(size_t n_args, const mp_obj_t *pos_args, mp_map_t return mp_obj_new_int(MICROPY_FLOAT_C_FUN(nearbyint)(to_value)); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(microbit_scale_obj, 0, microbit_scale); +static MP_DEFINE_CONST_FUN_OBJ_KW(microbit_scale_obj, 0, microbit_scale); -STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = { +static const mp_rom_map_elem_t microbit_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_microbit) }, { MP_ROM_QSTR(MP_QSTR_Image), (mp_obj_t)µbit_image_type }, @@ -216,13 +217,15 @@ STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_pin_speaker), MP_ROM_PTR(µbit_pin_speaker_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_module_globals, microbit_module_globals_table); +static MP_DEFINE_CONST_DICT(microbit_module_globals, microbit_module_globals_table); const mp_obj_module_t microbit_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)µbit_module_globals, }; +MP_REGISTER_MODULE(MP_QSTR_microbit, microbit_module); + /******************************************************************************/ // run_every object @@ -231,7 +234,7 @@ typedef struct _microbit_run_every_obj_t { mp_obj_t user_callback; } microbit_run_every_obj_t; -STATIC mp_obj_t microbit_run_every_callback(mp_obj_t self_in) { +static mp_obj_t microbit_run_every_callback(mp_obj_t self_in) { microbit_run_every_obj_t *self = MP_OBJ_TO_PTR(self_in); if (self->user_callback == MP_OBJ_NULL) { @@ -261,9 +264,9 @@ STATIC mp_obj_t microbit_run_every_callback(mp_obj_t self_in) { return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_run_every_callback_obj, microbit_run_every_callback); +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_run_every_callback_obj, microbit_run_every_callback); -STATIC mp_obj_t microbit_run_every_obj_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { +static mp_obj_t microbit_run_every_obj_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { microbit_run_every_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_arg_check_num(n_args, n_kw, 1, 1, false); self->timer.py_callback = MP_OBJ_FROM_PTR(µbit_run_every_callback_obj); @@ -272,13 +275,14 @@ STATIC mp_obj_t microbit_run_every_obj_call(mp_obj_t self_in, size_t n_args, siz return self_in; } -STATIC const mp_obj_type_t microbit_run_every_obj_type = { - { &mp_type_type }, - .name = MP_QSTR_run_every, - .call = microbit_run_every_obj_call, -}; +static MP_DEFINE_CONST_OBJ_TYPE( + microbit_run_every_obj_type, + MP_QSTR_run_every, + MP_TYPE_FLAG_NONE, + call, microbit_run_every_obj_call + ); -STATIC mp_obj_t microbit_run_every_new(uint32_t period_ms) { +static mp_obj_t microbit_run_every_new(uint32_t period_ms) { microbit_run_every_obj_t *self = m_new_obj(microbit_run_every_obj_t); self->timer.pairheap.base.type = µbit_run_every_obj_type; self->timer.flags = MICROBIT_SOFT_TIMER_FLAG_PY_CALLBACK | MICROBIT_SOFT_TIMER_FLAG_GC_ALLOCATED; diff --git a/src/codal_port/modmicrobit.h b/src/codal_port/modmicrobit.h index 455cae6..b4a5da4 100644 --- a/src/codal_port/modmicrobit.h +++ b/src/codal_port/modmicrobit.h @@ -111,9 +111,9 @@ extern const struct _microbit_pin_obj_t microbit_p20_obj; extern const struct _microbit_pin_obj_t microbit_pin_logo_obj; extern const struct _microbit_pin_obj_t microbit_pin_speaker_obj; -extern const struct _microbit_i2c_obj_t microbit_i2c_obj; +extern struct _microbit_i2c_obj_t microbit_i2c_obj; extern const struct _microbit_uart_obj_t microbit_uart_obj; -extern const struct _microbit_spi_obj_t microbit_spi_obj; +extern struct _microbit_spi_obj_t microbit_spi_obj; extern const struct _monochrome_5by5_t microbit_const_image_heart_obj; extern const struct _monochrome_5by5_t microbit_const_image_heart_small_obj; @@ -184,6 +184,7 @@ extern const struct _monochrome_5by5_t microbit_const_image_scissors_obj; extern const microbit_soundevent_obj_t microbit_soundevent_loud_obj; extern const microbit_soundevent_obj_t microbit_soundevent_quiet_obj; +extern const microbit_soundevent_obj_t microbit_soundevent_clap_obj; extern struct _microbit_display_obj_t microbit_display_obj; extern const struct _microbit_accelerometer_obj_t microbit_accelerometer_obj; @@ -220,6 +221,9 @@ bool microbit_obj_pin_can_be_acquired(const microbit_pin_obj_t *pin); // Not safe to call in an interrupt as it may raise if pin can't be acquired. bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode); +// Acquire a new pin and free a previous one, for the given mode. +void microbit_obj_pin_acquire_and_free(const microbit_pin_obj_t **old_pin, const microbit_pin_obj_t *new_pin, const microbit_pinmode_t *new_mode); + // Change the mode of a pin. This does not check the existing mode, nor release any // resources, and should only be used if pin resources are managed by the caller. void microbit_pin_set_mode(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode); diff --git a/src/codal_port/modmusic.c b/src/codal_port/modmusic.c index d0bbdc8..13603a5 100644 --- a/src/codal_port/modmusic.c +++ b/src/codal_port/modmusic.c @@ -66,13 +66,13 @@ typedef struct _music_data_t { mp_obj_t async_note; } music_data_t; -STATIC uint32_t start_note(const char *note_str, size_t note_len); +static uint32_t start_note(const char *note_str, size_t note_len); -STATIC void music_output_amplitude(uint32_t amplitude) { +static void music_output_amplitude(uint32_t amplitude) { microbit_hal_pin_write_analog_u10(MICROBIT_HAL_PIN_MIXER, amplitude); } -STATIC int music_output_period_us(uint32_t period) { +static int music_output_period_us(uint32_t period) { return microbit_hal_pin_set_analog_period_us(MICROBIT_HAL_PIN_MIXER, period); } @@ -132,7 +132,7 @@ void microbit_music_tick(void) { } } -STATIC void wait_async_music_idle(void) { +static void wait_async_music_idle(void) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { // Wait for the async music state to become idle. @@ -149,7 +149,7 @@ STATIC void wait_async_music_idle(void) { } } -STATIC uint32_t start_note(const char *note_str, size_t note_len) { +static uint32_t start_note(const char *note_str, size_t note_len) { music_output_amplitude(MUSIC_OUTPUT_AMPLITUDE_ON); // [NOTE](#|b)(octave)(:length) @@ -158,9 +158,9 @@ STATIC uint32_t start_note(const char *note_str, size_t note_len) { // array of us periods // these are the periods of note4 (the octave ascending from middle c) from A->B then C->G - STATIC uint16_t periods_us[] = {2273, 2025, 3822, 3405, 3034, 2863, 2551}; + static uint16_t periods_us[] = {2273, 2025, 3822, 3405, 3034, 2863, 2551}; // A#, -, C#, D#, -, F#, G# - STATIC uint16_t periods_sharps_us[] = {2145, 0, 3608, 3214, 0, 2703, 2408}; + static uint16_t periods_sharps_us[] = {2145, 0, 3608, 3214, 0, 2703, 2408}; // we'll represent the note as an integer (A=0, G=6) // TODO: validate the note @@ -259,7 +259,7 @@ STATIC uint32_t start_note(const char *note_str, size_t note_len) { return gap_ms; } -STATIC mp_obj_t microbit_music_reset(void) { +static mp_obj_t microbit_music_reset(void) { music_data->bpm = DEFAULT_BPM; music_data->ticks = DEFAULT_TICKS; music_data->last_octave = DEFAULT_OCTAVE; @@ -268,7 +268,7 @@ STATIC mp_obj_t microbit_music_reset(void) { } MP_DEFINE_CONST_FUN_OBJ_0(microbit_music_reset_obj, microbit_music_reset); -STATIC mp_obj_t microbit_music_get_tempo(void) { +static mp_obj_t microbit_music_get_tempo(void) { mp_obj_t tempo_tuple[2]; tempo_tuple[0] = mp_obj_new_int(music_data->bpm); tempo_tuple[1] = mp_obj_new_int(music_data->ticks); @@ -276,7 +276,7 @@ STATIC mp_obj_t microbit_music_get_tempo(void) { } MP_DEFINE_CONST_FUN_OBJ_0(microbit_music_get_tempo_obj, microbit_music_get_tempo); -STATIC mp_obj_t microbit_music_stop(mp_uint_t n_args, const mp_obj_t *args) { +static mp_obj_t microbit_music_stop(mp_uint_t n_args, const mp_obj_t *args) { mp_const_obj_t pin; if (n_args == 0) { pin = MUSIC_OUTPUT_DEFAULT_PIN; @@ -296,7 +296,7 @@ STATIC mp_obj_t microbit_music_stop(mp_uint_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_music_stop_obj, 0, 1, microbit_music_stop); -STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { static const mp_arg_t allowed_args[] = { { MP_QSTR_music, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_pin, MP_ARG_OBJ, {.u_obj = (mp_obj_t)MUSIC_OUTPUT_DEFAULT_PIN} }, @@ -352,7 +352,7 @@ STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args, } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_music_play_obj, 0, microbit_music_play); -STATIC mp_obj_t microbit_music_pitch(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_music_pitch(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { static const mp_arg_t allowed_args[] = { { MP_QSTR_frequency, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_duration, MP_ARG_INT, {.u_int = -1} }, @@ -400,7 +400,7 @@ STATIC mp_obj_t microbit_music_pitch(mp_uint_t n_args, const mp_obj_t *pos_args, } MP_DEFINE_CONST_FUN_OBJ_KW(microbit_music_pitch_obj, 0, microbit_music_pitch); -STATIC mp_obj_t microbit_music_set_tempo(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t microbit_music_set_tempo(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { static const mp_arg_t allowed_args[] = { { MP_QSTR_ticks, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_bpm, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, @@ -434,7 +434,7 @@ static mp_obj_t music_init(void) { } MP_DEFINE_CONST_FUN_OBJ_0(music___init___obj, music_init); -STATIC const mp_rom_map_elem_t microbit_music_locals_dict_table[] = { +static const mp_rom_map_elem_t microbit_music_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_music) }, { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&music___init___obj) }, @@ -467,9 +467,14 @@ STATIC const mp_rom_map_elem_t microbit_music_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_POWER_UP), MP_ROM_PTR(µbit_music_tune_power_up_obj) }, { MP_ROM_QSTR(MP_QSTR_POWER_DOWN), MP_ROM_PTR(µbit_music_tune_power_down_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(microbit_music_locals_dict, microbit_music_locals_dict_table); +static MP_DEFINE_CONST_DICT(microbit_music_locals_dict, microbit_music_locals_dict_table); const mp_obj_module_t music_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)µbit_music_locals_dict, }; + +MP_REGISTER_MODULE(MP_QSTR_music, music_module); + +#undef music_data +MP_REGISTER_ROOT_POINTER(struct _music_data_t *music_data); diff --git a/src/codal_port/modos.c b/src/codal_port/modos.c index b629de2..d70fb36 100644 --- a/src/codal_port/modos.c +++ b/src/codal_port/modos.c @@ -27,7 +27,7 @@ #include "py/obj.h" #include "py/objtuple.h" #include "py/objstr.h" -#include "ports/nrf/modules/uos/microbitfs.h" +#include "ports/nrf/modules/os/microbitfs.h" // Include MicroPython and micro:bit version information. #include "genhdr/mpversion.h" @@ -40,17 +40,17 @@ const char microbit_release_string[] = MICROBIT_RELEASE; const char microbit_version_string[] = MICROBIT_VERSION; -STATIC const qstr os_uname_info_fields[] = { +static const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine }; -STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, MICROPY_PY_SYS_PLATFORM); -STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, MICROPY_PY_SYS_PLATFORM); -STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, microbit_release_string); -STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, microbit_version_string); -STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROBIT_BOARD_NAME " with " MICROPY_HW_MCU_NAME); +static const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, MICROPY_PY_SYS_PLATFORM); +static const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, MICROPY_PY_SYS_PLATFORM); +static const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, microbit_release_string); +static const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, microbit_version_string); +static const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROBIT_BOARD_NAME " with " MICROPY_HW_MCU_NAME); -STATIC MP_DEFINE_ATTRTUPLE( +static MP_DEFINE_ATTRTUPLE( os_uname_info_obj, os_uname_info_fields, 5, @@ -61,37 +61,39 @@ STATIC MP_DEFINE_ATTRTUPLE( MP_ROM_PTR(&os_uname_info_machine_obj) ); -STATIC mp_obj_t os_uname(void) { +static mp_obj_t os_uname(void) { return MP_OBJ_FROM_PTR(&os_uname_info_obj); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); +static MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); #if MICROPY_MBFS -STATIC mp_obj_t os_size(mp_obj_t filename) { - mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(uos_mbfs_stat_obj.fun._1(filename)); +static mp_obj_t os_size(mp_obj_t filename) { + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(os_mbfs_stat_obj.fun._1(filename)); return tuple->items[6]; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_size_obj, os_size); +static MP_DEFINE_CONST_FUN_OBJ_1(os_size_obj, os_size); #endif -STATIC const mp_rom_map_elem_t os_module_globals_table[] = { +static const mp_rom_map_elem_t os_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) }, { MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) }, #if MICROPY_MBFS - { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&uos_mbfs_listdir_obj) }, - { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&uos_mbfs_ilistdir_obj) }, - { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&uos_mbfs_remove_obj) }, - { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&uos_mbfs_stat_obj) }, + { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&os_mbfs_listdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&os_mbfs_ilistdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&os_mbfs_remove_obj) }, + { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_mbfs_stat_obj) }, // micro:bit v1 specific { MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&os_size_obj) }, #endif }; -STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); +static MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); const mp_obj_module_t os_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&os_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_os, os_module); diff --git a/src/codal_port/modpower.c b/src/codal_port/modpower.c index a707e4a..2cec449 100644 --- a/src/codal_port/modpower.c +++ b/src/codal_port/modpower.c @@ -28,7 +28,7 @@ #include "py/mphal.h" #include "drv_softtimer.h" -STATIC size_t get_array(mp_obj_t *src, mp_obj_t **items) { +static size_t get_array(mp_obj_t *src, mp_obj_t **items) { if (*src == mp_const_none) { // None, so an array of length 0. *items = NULL; @@ -45,13 +45,13 @@ STATIC size_t get_array(mp_obj_t *src, mp_obj_t **items) { } } -STATIC mp_obj_t power_off(void) { +static mp_obj_t power_off(void) { microbit_hal_power_off(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(power_off_obj, power_off); +static MP_DEFINE_CONST_FUN_OBJ_0(power_off_obj, power_off); -STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_ms, ARG_wake_on, ARG_run_every }; static const mp_arg_t allowed_args[] = { { MP_QSTR_ms, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, @@ -136,17 +136,19 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(power_deep_sleep_obj, 0, power_deep_sleep); +static MP_DEFINE_CONST_FUN_OBJ_KW(power_deep_sleep_obj, 0, power_deep_sleep); -STATIC const mp_rom_map_elem_t power_module_globals_table[] = { +static const mp_rom_map_elem_t power_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_power) }, { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&power_off_obj) }, { MP_ROM_QSTR(MP_QSTR_deep_sleep), MP_ROM_PTR(&power_deep_sleep_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(power_module_globals, power_module_globals_table); +static MP_DEFINE_CONST_DICT(power_module_globals, power_module_globals_table); const mp_obj_module_t power_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&power_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_power, power_module); diff --git a/src/codal_port/modradio.c b/src/codal_port/modradio.c index 09e63e6..d49e256 100644 --- a/src/codal_port/modradio.c +++ b/src/codal_port/modradio.c @@ -32,24 +32,24 @@ #include "py/smallint.h" #include "drv_radio.h" -STATIC microbit_radio_config_t radio_config; +static microbit_radio_config_t radio_config; -STATIC mp_obj_t mod_radio_reset(void); +static mp_obj_t mod_radio_reset(void); -STATIC void ensure_enabled(void) { +static void ensure_enabled(void) { if (MP_STATE_PORT(radio_buf) == NULL) { mp_raise_ValueError(MP_ERROR_TEXT("radio is not enabled")); } } -STATIC mp_obj_t mod_radio___init__(void) { +static mp_obj_t mod_radio___init__(void) { mod_radio_reset(); microbit_radio_enable(&radio_config); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio___init___obj, mod_radio___init__); -STATIC mp_obj_t mod_radio_reset(void) { +static mp_obj_t mod_radio_reset(void) { radio_config.max_payload = MICROBIT_RADIO_DEFAULT_MAX_PAYLOAD; radio_config.queue_len = MICROBIT_RADIO_DEFAULT_QUEUE_LEN; radio_config.channel = MICROBIT_RADIO_DEFAULT_CHANNEL; @@ -61,7 +61,7 @@ STATIC mp_obj_t mod_radio_reset(void) { } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_reset_obj, mod_radio_reset); -STATIC mp_obj_t mod_radio_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t mod_radio_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { (void)pos_args; // unused if (n_args != 0) { @@ -160,19 +160,19 @@ STATIC mp_obj_t mod_radio_config(size_t n_args, const mp_obj_t *pos_args, mp_map } MP_DEFINE_CONST_FUN_OBJ_KW(mod_radio_config_obj, 0, mod_radio_config); -STATIC mp_obj_t mod_radio_on(void) { +static mp_obj_t mod_radio_on(void) { microbit_radio_enable(&radio_config); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_on_obj, mod_radio_on); -STATIC mp_obj_t mod_radio_off(void) { +static mp_obj_t mod_radio_off(void) { microbit_radio_disable(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_off_obj, mod_radio_off); -STATIC mp_obj_t mod_radio_send_bytes(mp_obj_t buf_in) { +static mp_obj_t mod_radio_send_bytes(mp_obj_t buf_in) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); ensure_enabled(); @@ -181,7 +181,7 @@ STATIC mp_obj_t mod_radio_send_bytes(mp_obj_t buf_in) { } MP_DEFINE_CONST_FUN_OBJ_1(mod_radio_send_bytes_obj, mod_radio_send_bytes); -STATIC mp_obj_t mod_radio_receive_bytes(void) { +static mp_obj_t mod_radio_receive_bytes(void) { ensure_enabled(); const uint8_t *buf = microbit_radio_peek(); if (buf == NULL) { @@ -194,7 +194,7 @@ STATIC mp_obj_t mod_radio_receive_bytes(void) { } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_receive_bytes_obj, mod_radio_receive_bytes); -STATIC mp_obj_t mod_radio_send(mp_obj_t buf_in) { +static mp_obj_t mod_radio_send(mp_obj_t buf_in) { mp_uint_t len; const char *data = mp_obj_str_get_data(buf_in, &len); ensure_enabled(); @@ -203,7 +203,7 @@ STATIC mp_obj_t mod_radio_send(mp_obj_t buf_in) { } MP_DEFINE_CONST_FUN_OBJ_1(mod_radio_send_obj, mod_radio_send); -STATIC mp_obj_t mod_radio_receive(void) { +static mp_obj_t mod_radio_receive(void) { ensure_enabled(); const uint8_t *buf = microbit_radio_peek(); if (buf == NULL) { @@ -221,7 +221,7 @@ STATIC mp_obj_t mod_radio_receive(void) { } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_receive_obj, mod_radio_receive); -STATIC mp_obj_t mod_radio_receive_bytes_into(mp_obj_t buf_in) { +static mp_obj_t mod_radio_receive_bytes_into(mp_obj_t buf_in) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); ensure_enabled(); @@ -237,7 +237,7 @@ STATIC mp_obj_t mod_radio_receive_bytes_into(mp_obj_t buf_in) { } MP_DEFINE_CONST_FUN_OBJ_1(mod_radio_receive_bytes_into_obj, mod_radio_receive_bytes_into); -STATIC mp_obj_t mod_radio_receive_full(void) { +static mp_obj_t mod_radio_receive_full(void) { ensure_enabled(); const uint8_t *buf = microbit_radio_peek(); if (buf == NULL) { @@ -252,7 +252,7 @@ STATIC mp_obj_t mod_radio_receive_full(void) { mp_obj_t tuple[3] = { mp_obj_new_bytes(buf + 1, len), MP_OBJ_NEW_SMALL_INT(rssi), - MP_OBJ_NEW_SMALL_INT(timestamp_us & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) + MP_OBJ_NEW_SMALL_INT(timestamp_us & (MICROPY_PY_TIME_TICKS_PERIOD - 1)) }; microbit_radio_pop(); return mp_obj_new_tuple(3, tuple); @@ -260,7 +260,7 @@ STATIC mp_obj_t mod_radio_receive_full(void) { } MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_receive_full_obj, mod_radio_receive_full); -STATIC const mp_map_elem_t radio_module_globals_table[] = { +static const mp_map_elem_t radio_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_radio) }, { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&mod_radio___init___obj }, @@ -284,9 +284,11 @@ STATIC const mp_map_elem_t radio_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_RATE_2MBIT), MP_OBJ_NEW_SMALL_INT(RADIO_MODE_MODE_Nrf_2Mbit) }, }; -STATIC MP_DEFINE_CONST_DICT(radio_module_globals, radio_module_globals_table); +static MP_DEFINE_CONST_DICT(radio_module_globals, radio_module_globals_table); const mp_obj_module_t radio_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&radio_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_radio, radio_module); diff --git a/src/codal_port/modspeech.c b/src/codal_port/modspeech.c index 4c4bcb2..ed587c0 100644 --- a/src/codal_port/modspeech.c +++ b/src/codal_port/modspeech.c @@ -28,6 +28,7 @@ #include #include +#include "py/mphal.h" #include "py/obj.h" #include "py/objtuple.h" #include "py/objstr.h" @@ -37,6 +38,17 @@ #include "sam/reciter.h" #include "sam/sam.h" +// The default `mode` for say/pronounce/sing can be configured externally. +// The allowed values are: +// - 0 = traditional micro:bit v1, 15625Hz (not supported with USE_DEDICATED_AUDIO_CHANNEL) +// - 1 = standard sampling fidelity, 19000Hz +// - 2 = standard sampling fidelity, 19000Hz, with sample smoothing +// - 3 = higher sampling fidelity, 38000Hz +// - 4 = higher sampling fidelity, 38000Hz, with sample smoothing +#ifndef MICROPY_PY_SPEECH_DEFAULT_MODE +#define MICROPY_PY_SPEECH_DEFAULT_MODE (1) +#endif + // If disabled, pipe speech through audio module output. // If enabled, use a dedicated audio mixer channer with a double buffer. #define USE_DEDICATED_AUDIO_CHANNEL (1) @@ -92,7 +104,7 @@ void microbit_hal_audio_speech_ready_callback(void) { #endif } -STATIC void sam_output_reset(microbit_audio_frame_obj_t *src_frame) { +static void sam_output_reset(microbit_audio_frame_obj_t *src_frame) { sam_output_frame = src_frame; buf_start_pos = 0; last_pos = 0; @@ -111,7 +123,7 @@ STATIC void sam_output_reset(microbit_audio_frame_obj_t *src_frame) { #endif } -STATIC void speech_wait_output_drained(void) { +static void speech_wait_output_drained(void) { #if USE_DEDICATED_AUDIO_CHANNEL while (speech_output_read >= 0) { mp_handle_pending(true); @@ -135,7 +147,7 @@ STATIC void speech_wait_output_drained(void) { } #if USE_DEDICATED_AUDIO_CHANNEL -STATIC void speech_output_sample(uint8_t b) { +static void speech_output_sample(uint8_t b) { speech_output_buffer[OUT_CHUNK_SIZE * speech_output_write + speech_output_buffer_idx++] = b; if (speech_output_buffer_idx >= OUT_CHUNK_SIZE) { speech_wait_output_drained(); @@ -315,7 +327,7 @@ void SamOutputByte(unsigned int pos, unsigned char b) { // This iterator assumes that the speech renderer can generate samples // at least as fast as we can consume them. -STATIC mp_obj_t next(mp_obj_t iter) { +static mp_obj_t next(mp_obj_t iter) { if (exhausted) { return MP_OBJ_STOP_ITERATION; } @@ -332,14 +344,14 @@ STATIC mp_obj_t next(mp_obj_t iter) { } } -STATIC const mp_obj_type_t speech_iterator_type = { +static const mp_obj_type_t speech_iterator_type = { { &mp_type_type }, .name = MP_QSTR_iterator, .getiter = mp_identity_getiter, .iternext = next, }; -STATIC mp_obj_t make_speech_iter(void) { +static mp_obj_t make_speech_iter(void) { speech_iterator_t *result = m_new_obj(speech_iterator_t); result->base.type = &speech_iterator_type; result->empty = microbit_audio_frame_make_new(); @@ -349,7 +361,7 @@ STATIC mp_obj_t make_speech_iter(void) { #endif -STATIC mp_obj_t translate(mp_obj_t words) { +static mp_obj_t translate(mp_obj_t words) { mp_uint_t len, outlen; const char *txt = mp_obj_str_get_data(words, &len); // Reciter truncates *output* at about 120 characters. @@ -379,14 +391,14 @@ STATIC mp_obj_t translate(mp_obj_t words) { } MP_DEFINE_CONST_FUN_OBJ_1(translate_obj, translate); -STATIC mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool sing) { +static mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool sing) { static const mp_arg_t allowed_args[] = { { MP_QSTR_pitch, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_PITCH} }, { MP_QSTR_speed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPEED} }, { MP_QSTR_mouth, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_MOUTH} }, { MP_QSTR_throat, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_THROAT} }, { MP_QSTR_debug, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, - { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, + { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_SPEECH_DEFAULT_MODE} }, { MP_QSTR_volume, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 4} }, { MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(µbit_pin_default_audio_obj)} }, }; @@ -458,32 +470,36 @@ STATIC mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t * return mp_const_none; } -STATIC mp_obj_t say(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t say(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { mp_obj_t phonemes = translate(pos_args[0]); return articulate(phonemes, n_args-1, pos_args+1, kw_args, false); } MP_DEFINE_CONST_FUN_OBJ_KW(say_obj, 1, say); -STATIC mp_obj_t pronounce(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t pronounce(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { return articulate(pos_args[0], n_args-1, pos_args+1, kw_args, false); } MP_DEFINE_CONST_FUN_OBJ_KW(pronounce_obj, 1, pronounce); -STATIC mp_obj_t sing(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +static mp_obj_t sing(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { return articulate(pos_args[0], n_args-1, pos_args+1, kw_args, true); } MP_DEFINE_CONST_FUN_OBJ_KW(sing_obj, 1, sing); -STATIC const mp_map_elem_t _globals_table[] = { +static const mp_map_elem_t _globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_speech) }, { MP_OBJ_NEW_QSTR(MP_QSTR_say), (mp_obj_t)&say_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_sing), (mp_obj_t)&sing_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_pronounce), (mp_obj_t)&pronounce_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_translate), (mp_obj_t)&translate_obj }, }; -STATIC MP_DEFINE_CONST_DICT(_globals, _globals_table); +static MP_DEFINE_CONST_DICT(_globals, _globals_table); const mp_obj_module_t speech_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_speech, speech_module); + +MP_REGISTER_ROOT_POINTER(void *speech_data); diff --git a/src/codal_port/modthis.c b/src/codal_port/modthis.c index e57a347..105b6dd 100644 --- a/src/codal_port/modthis.c +++ b/src/codal_port/modthis.c @@ -26,7 +26,7 @@ #include "py/runtime.h" -STATIC const char *this_text = +static const char *this_text = "The Zen of MicroPython, by Nicholas H. Tollervey\n" "\n" "Code,\n" @@ -41,33 +41,35 @@ STATIC const char *this_text = "Happy hacking! :-)\n"; // If you contribute code to this project, add your name here. -STATIC const char *authors_text = +static const char *authors_text = "MicroPython on the micro:bit is brought to you by:\n" "Damien P. George, Mark Shannon, Radomir Dopieralski, Matthew Else,\n" "Carol Willing, Tom Viner, Alan Jackson, Nick Coghlan, Joseph Haig,\n" "Alex Chan, Andrea Grandi, Paul Egan, Piotr Kasprzyk, Andrew Mulholland,\n" "Matt Wheeler, Joe Glancy, Abbie Brooks and Nicholas H. Tollervey.\n"; -STATIC mp_obj_t this__init__(void) { +static mp_obj_t this__init__(void) { mp_printf(&mp_plat_print, "%s", this_text); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(this___init___obj, this__init__); -STATIC mp_obj_t this_authors(void) { +static mp_obj_t this_authors(void) { mp_printf(&mp_plat_print, "%s", authors_text); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(this_authors_obj, this_authors); -STATIC const mp_rom_map_elem_t this_module_globals_table[] = { +static const mp_rom_map_elem_t this_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_this) }, { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&this___init___obj) }, { MP_ROM_QSTR(MP_QSTR_authors), MP_ROM_PTR(&this_authors_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(this_module_globals, this_module_globals_table); +static MP_DEFINE_CONST_DICT(this_module_globals, this_module_globals_table); const mp_obj_module_t this_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&this_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_this, this_module); diff --git a/src/codal_port/modutime.c b/src/codal_port/modutime.c deleted file mode 100644 index a0b72e9..0000000 --- a/src/codal_port/modutime.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013-2017 Damien P. George - * - * 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. - */ - -#include "extmod/utime_mphal.h" - -STATIC const mp_rom_map_elem_t utime_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, - - { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, - { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(utime_module_globals, utime_module_globals_table); - -const mp_obj_module_t utime_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&utime_module_globals, -}; diff --git a/src/codal_port/mpconfigport.h b/src/codal_port/mpconfigport.h index e4333f1..cca2378 100644 --- a/src/codal_port/mpconfigport.h +++ b/src/codal_port/mpconfigport.h @@ -59,9 +59,6 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_STREAMS_NON_BLOCK (1) #define MICROPY_MODULE_BUILTIN_INIT (1) -#define MICROPY_MODULE_WEAK_LINKS (1) -#define MICROPY_MODULE_FROZEN_MPY (1) -#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool #define MICROPY_USE_INTERNAL_ERRNO (1) #define MICROPY_ENABLE_SCHEDULER (1) @@ -81,19 +78,18 @@ #define MICROPY_PY_SYS_PLATFORM "microbit" // Extended modules -#define MICROPY_PY_UERRNO (1) -#define MICROPY_PY_UTIME_MP_HAL (1) -#define MICROPY_PY_URANDOM (1) -#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (rng_generate_random_word()) -#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) -#define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_ERRNO (1) +#define MICROPY_PY_RANDOM (1) +#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (rng_generate_random_word()) +#define MICROPY_PY_RANDOM_EXTRA_FUNCS (1) +#define MICROPY_PY_TIME (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_MBFS (1) // Custom errno list. -#define MICROPY_PY_UERRNO_LIST \ +#define MICROPY_PY_ERRNO_LIST \ X(EPERM) \ X(ENOENT) \ X(EIO) \ @@ -125,61 +121,6 @@ #define MP_STATE_PORT MP_STATE_VM -extern const struct _mp_obj_module_t antigravity_module; -extern const struct _mp_obj_module_t audio_module; -extern const struct _mp_obj_module_t log_module; -extern const struct _mp_obj_module_t love_module; -extern const struct _mp_obj_module_t machine_module; -extern const struct _mp_obj_module_t microbit_module; -extern const struct _mp_obj_module_t music_module; -extern const struct _mp_obj_module_t os_module; -extern const struct _mp_obj_module_t power_module; -extern const struct _mp_obj_module_t radio_module; -extern const struct _mp_obj_module_t speech_module; -extern const struct _mp_obj_module_t this_module; -extern const struct _mp_obj_module_t utime_module; - -#define MICROPY_PORT_BUILTIN_MODULES \ - { MP_ROM_QSTR(MP_QSTR_antigravity), MP_ROM_PTR(&antigravity_module) }, \ - { MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&audio_module) }, \ - { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&log_module) }, \ - { MP_ROM_QSTR(MP_QSTR_love), MP_ROM_PTR(&love_module) }, \ - { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&machine_module) }, \ - { MP_ROM_QSTR(MP_QSTR_microbit), MP_ROM_PTR(µbit_module) }, \ - { MP_ROM_QSTR(MP_QSTR_music), MP_ROM_PTR(&music_module) }, \ - { MP_ROM_QSTR(MP_QSTR_os), MP_ROM_PTR(&os_module) }, \ - { MP_ROM_QSTR(MP_QSTR_power), MP_ROM_PTR(&power_module) }, \ - { MP_ROM_QSTR(MP_QSTR_radio), MP_ROM_PTR(&radio_module) }, \ - { MP_ROM_QSTR(MP_QSTR_speech), MP_ROM_PTR(&speech_module) }, \ - { MP_ROM_QSTR(MP_QSTR_this), MP_ROM_PTR(&this_module) }, \ - { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&utime_module) }, \ - -#define MICROPY_PORT_ROOT_POINTERS \ - const char *readline_hist[8]; \ - void *display_data; \ - uint8_t *radio_buf; \ - void *audio_source; \ - void *speech_data; \ - struct _music_data_t *music_data; \ - struct _microbit_soft_timer_entry_t *soft_timer_heap; \ - -// These functions allow nested calls. -extern void target_disable_irq(void); -extern void target_enable_irq(void); - -static inline uint32_t disable_irq(void) { - target_disable_irq(); - return 0; -} - -static inline void enable_irq(uint32_t state) { - (void)state; - target_enable_irq(); -} - -#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() -#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((uint32_t)(p) | 1)) #define MP_SSIZE_MAX (0x7fffffff) @@ -192,7 +133,7 @@ typedef long mp_off_t; // We need to provide a declaration/definition of alloca() #include -// Needed for MICROPY_PY_URANDOM_SEED_INIT_FUNC. +// Needed for MICROPY_PY_RANDOM_SEED_INIT_FUNC. extern uint32_t rng_generate_random_word(void); // Needed for microbitfs.c:microbit_file_open. diff --git a/src/codal_port/mphalport.h b/src/codal_port/mphalport.h index 86d965a..a4d5405 100644 --- a/src/codal_port/mphalport.h +++ b/src/codal_port/mphalport.h @@ -28,8 +28,31 @@ #include "microbithal.h" #include "nrf.h" -// Not implemented and not exposed in utime module. -#define mp_hal_ticks_cpu() (0) +#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() +#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) + +// These functions allow nested calls. +extern void target_disable_irq(void); +extern void target_enable_irq(void); + +static inline uint32_t disable_irq(void) { + target_disable_irq(); + return 0; +} + +static inline void enable_irq(uint32_t state) { + (void)state; + target_enable_irq(); +} + +static inline mp_uint_t mp_hal_ticks_cpu(void) { + if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) || !(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) { + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + DWT->CYCCNT = 0; + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; + } + return DWT->CYCCNT; +} void mp_hal_set_interrupt_char(int c); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy