|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Jeff Epler for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/floppyio/__init__.h" |
| 28 | +#include "shared-bindings/digitalio/DigitalInOut.h" |
| 29 | +#include "common-hal/floppyio/__init__.h" |
| 30 | + |
| 31 | +#include <stdint.h> |
| 32 | + |
| 33 | +#include "py/binary.h" |
| 34 | +#include "py/enum.h" |
| 35 | +#include "py/obj.h" |
| 36 | +#include "py/runtime.h" |
| 37 | + |
| 38 | +//| def flux_readinto(buffer: WriteableBuffer, data: DigitalInOut, index: DigitalInOut) -> int: |
| 39 | +//| """Read flux transition information into the buffer. |
| 40 | +//| |
| 41 | +//| The function returns when the buffer has filled, or when the index input |
| 42 | +//| indicates that one full revolution of data has been recorded. Due to |
| 43 | +//| technical limitations, this process may not be interruptible by |
| 44 | +//| KeyboardInterrupt. |
| 45 | +//| |
| 46 | +//| :param buffer: Read data into this buffer. Each element represents the time between successive zero-to-one transitions. |
| 47 | +//| :param data: Pin on which the flux data appears |
| 48 | +//| :param index: Pin on which the index pulse appears |
| 49 | +//| :return: The actual number of bytes of read |
| 50 | +//| """ |
| 51 | +//| ... |
| 52 | +//| |
| 53 | +STATIC mp_obj_t floppyio_flux_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 54 | + enum { ARG_buffer, ARG_data, ARG_index }; |
| 55 | + static const mp_arg_t allowed_args[] = { |
| 56 | + { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 57 | + { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 58 | + { MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 59 | + }; |
| 60 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 61 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 62 | + |
| 63 | + mp_buffer_info_t bufinfo; |
| 64 | + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE); |
| 65 | + digitalio_digitalinout_obj_t *data = assert_digitalinout(args[ARG_data].u_obj); |
| 66 | + digitalio_digitalinout_obj_t *index = assert_digitalinout(args[ARG_index].u_obj); |
| 67 | + |
| 68 | + return MP_OBJ_NEW_SMALL_INT(common_hal_floppyio_flux_readinto(bufinfo.buf, bufinfo.len, data, index)); |
| 69 | +} |
| 70 | +MP_DEFINE_CONST_FUN_OBJ_KW(floppyio_flux_readinto_obj, 0, floppyio_flux_readinto); |
| 71 | + |
| 72 | +//| def mfm_readinto(buffer: WriteableBuffer, data: DigitalInOut, index: DigitalInOut) -> int: |
| 73 | +//| """Read mfm blocks into the buffer. |
| 74 | +//| |
| 75 | +//| The track is assumed to consist of 512-byte sectors. |
| 76 | +//| |
| 77 | +//| The function returns when all sectors have been successfully read, or |
| 78 | +//| a number of index pulses have occurred. Due to technical limitations, this |
| 79 | +//| process may not be interruptible by KeyboardInterrupt. |
| 80 | +//| |
| 81 | +//| :param buffer: Read data into this buffer. Must be a multiple of 512. |
| 82 | +//| :param data: Pin on which the mfm data appears |
| 83 | +//| :param index: Pin on which the index pulse appears |
| 84 | +//| :return: The actual number of sectors read |
| 85 | +//| """ |
| 86 | +//| ... |
| 87 | +//| |
| 88 | +STATIC mp_obj_t floppyio_mfm_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 89 | + enum { ARG_buffer, ARG_data, ARG_index }; |
| 90 | + static const mp_arg_t allowed_args[] = { |
| 91 | + { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 92 | + { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 93 | + { MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 94 | + }; |
| 95 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 96 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 97 | + |
| 98 | + mp_buffer_info_t bufinfo; |
| 99 | + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE); |
| 100 | + digitalio_digitalinout_obj_t *data = assert_digitalinout(args[ARG_data].u_obj); |
| 101 | + digitalio_digitalinout_obj_t *index = assert_digitalinout(args[ARG_index].u_obj); |
| 102 | + |
| 103 | + if (bufinfo.len % 512 != 0) { |
| 104 | + mp_raise_ValueError(translate("Buffer must be a multiple of 512 bytes")); |
| 105 | + } |
| 106 | + size_t n_sectors = bufinfo.len / 512; |
| 107 | + return MP_OBJ_NEW_SMALL_INT(common_hal_floppyio_mfm_readinto(bufinfo.buf, n_sectors, data, index)); |
| 108 | +} |
| 109 | +MP_DEFINE_CONST_FUN_OBJ_KW(floppyio_mfm_readinto_obj, 0, floppyio_mfm_readinto); |
| 110 | + |
| 111 | +//| samplerate: int |
| 112 | +//| """The approximate sample rate in Hz used by flux_readinto.""" |
| 113 | +//| |
| 114 | + |
| 115 | +STATIC const mp_rom_map_elem_t floppyio_module_globals_table[] = { |
| 116 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_floppyio) }, |
| 117 | + { MP_ROM_QSTR(MP_QSTR_flux_readinto), MP_ROM_PTR(&floppyio_flux_readinto_obj) }, |
| 118 | + { MP_ROM_QSTR(MP_QSTR_mfm_readinto), MP_ROM_PTR(&floppyio_mfm_readinto_obj) }, |
| 119 | + { MP_ROM_QSTR(MP_QSTR_samplerate), MP_ROM_INT(FLOPPYIO_SAMPLERATE) }, |
| 120 | +}; |
| 121 | +STATIC MP_DEFINE_CONST_DICT(floppyio_module_globals, floppyio_module_globals_table); |
| 122 | + |
| 123 | +const mp_obj_module_t floppyio_module = { |
| 124 | + .base = {&mp_type_module }, |
| 125 | + .globals = (mp_obj_dict_t *)&floppyio_module_globals, |
| 126 | +}; |
| 127 | + |
| 128 | +MP_REGISTER_MODULE(MP_QSTR_floppyio, floppyio_module, CIRCUITPY_FLOPPYIO); |
0 commit comments