Skip to content

Commit 3fecbb2

Browse files
committed
extmod/machine_pinbase: Implementation of PinBase class.
Allows to translate C-level pin API to Python-level pin API. In other words, allows to implement a pin class and Python which will be usable for efficient C-coded algorithms, like bitbanging SPI/I2C, time_pulse, etc.
1 parent 2b6dcdd commit 3fecbb2

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

extmod/machine_pinbase.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
34+
// PinBase class
35+
36+
// As this is abstract class, its instance is null.
37+
// But there should be an instance, as the rest of instance code
38+
// expects that there will be concrete object for inheritance.
39+
typedef struct _mp_pinbase_t {
40+
mp_obj_base_t base;
41+
} mp_pinbase_t;
42+
43+
STATIC const mp_obj_type_t pinbase_type;
44+
45+
STATIC mp_pinbase_t pinbase_singleton = {
46+
.base = { &pinbase_type },
47+
};
48+
49+
STATIC 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) {
50+
(void)type;
51+
(void)n_args;
52+
(void)n_kw;
53+
(void)args;
54+
return MP_ROM_PTR(&pinbase_singleton);
55+
}
56+
57+
mp_uint_t pinbase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode);
58+
mp_uint_t pinbase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
59+
(void)errcode;
60+
switch (request) {
61+
case MP_PIN_READ: {
62+
mp_obj_t dest[2];
63+
mp_load_method(obj, MP_QSTR_value, dest);
64+
return mp_obj_get_int(mp_call_method_n_kw(0, 0, dest));
65+
}
66+
case MP_PIN_WRITE: {
67+
mp_obj_t dest[3];
68+
mp_load_method(obj, MP_QSTR_value, dest);
69+
dest[2] = (arg == 0 ? mp_const_false : mp_const_true);
70+
mp_call_method_n_kw(1, 0, dest);
71+
return 0;
72+
}
73+
}
74+
return -1;
75+
}
76+
77+
STATIC const mp_pin_p_t pinbase_pin_p = {
78+
.ioctl = pinbase_ioctl,
79+
};
80+
81+
const mp_obj_type_t machine_pinbase_type = {
82+
{ &mp_type_type },
83+
.name = MP_QSTR_PinBase,
84+
.make_new = pinbase_make_new,
85+
.protocol = &pinbase_pin_p,
86+
};
87+
88+
#endif // MICROPY_PY_MACHINE

extmod/machine_pinbase.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
28+
#ifndef __MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H__
29+
#define __MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H__
30+
31+
#include "py/obj.h"
32+
33+
extern const mp_obj_type_t machine_pinbase_type;
34+
35+
#endif // __MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H__

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ PY_O_BASENAME = \
201201
../extmod/modubinascii.o \
202202
../extmod/virtpin.o \
203203
../extmod/machine_mem.o \
204+
../extmod/machine_pinbase.o \
204205
../extmod/machine_pulse.o \
205206
../extmod/machine_i2c.o \
206207
../extmod/modussl.o \

0 commit comments

Comments
 (0)