1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2016 Paul Sokolovsky
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 "py/mpconfig.h"
28#if MICROPY_PY_MACHINE
29
30#include "py/obj.h"
31#include "py/runtime.h"
32#include "extmod/virtpin.h"
33#include "extmod/machine_pinbase.h"
34
35// PinBase class
36
37// As this is abstract class, its instance is null.
38// But there should be an instance, as the rest of instance code
39// expects that there will be concrete object for inheritance.
40typedef struct _mp_pinbase_t {
41 mp_obj_base_t base;
42} mp_pinbase_t;
43
44STATIC const mp_pinbase_t pinbase_singleton = {
45 .base = { &machine_pinbase_type },
46};
47
48STATIC mp_obj_t pinbase_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
49 (void)type;
50 (void)n_args;
51 (void)n_kw;
52 (void)args;
53 return MP_OBJ_FROM_PTR(&pinbase_singleton);
54}
55
56mp_uint_t pinbase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode);
57mp_uint_t pinbase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
58 (void)errcode;
59 switch (request) {
60 case MP_PIN_READ: {
61 mp_obj_t dest[2];
62 mp_load_method(obj, MP_QSTR_value, dest);
63 return mp_obj_get_int(mp_call_method_n_kw(0, 0, dest));
64 }
65 case MP_PIN_WRITE: {
66 mp_obj_t dest[3];
67 mp_load_method(obj, MP_QSTR_value, dest);
68 dest[2] = (arg == 0 ? mp_const_false : mp_const_true);
69 mp_call_method_n_kw(1, 0, dest);
70 return 0;
71 }
72 }
73 return -1;
74}
75
76STATIC const mp_pin_p_t pinbase_pin_p = {
77 .ioctl = pinbase_ioctl,
78};
79
80const mp_obj_type_t machine_pinbase_type = {
81 { &mp_type_type },
82 .name = MP_QSTR_PinBase,
83 .make_new = pinbase_make_new,
84 .protocol = &pinbase_pin_p,
85};
86
87#endif // MICROPY_PY_MACHINE
88