Skip to content

Commit 125476d

Browse files
committed
Add microbit_obj_get_pin_name function to reduce need for MicroBit.h.
1 parent c801604 commit 125476d

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

inc/microbit/microbitobj.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
#ifndef __MICROPY_INCLUDED_MICROBIT_MICROBITOBJ_H__
2727
#define __MICROPY_INCLUDED_MICROBIT_MICROBITOBJ_H__
2828

29-
#include "MicroBit.h"
30-
3129
extern "C" {
3230

3331
#include "py/obj.h"
3432

35-
MicroBitPin *microbit_obj_get_pin(mp_obj_t o);
36-
33+
class MicroBitPin *microbit_obj_get_pin(mp_obj_t o);
34+
PinName microbit_obj_get_pin_name(mp_obj_t o);
3735

3836
extern volatile bool compass_up_to_date;
3937
extern volatile bool compass_updating;

inc/microbit/microbitpin.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
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+
#ifndef __MICROPY_INCLUDED_MICROBIT_MICROBITPIN_H__
27+
#define __MICROPY_INCLUDED_MICROBIT_MICROBITPIN_H__
28+
29+
#ifndef MICROBIT_PIN_P0
30+
31+
#define MICROBIT_PIN_P0 (P0_3)
32+
#define MICROBIT_PIN_P1 (P0_2)
33+
#define MICROBIT_PIN_P13 (P0_23)
34+
#define MICROBIT_PIN_P14 (P0_22)
35+
#define MICROBIT_PIN_P15 (P0_21)
36+
37+
#endif
38+
39+
#endif // __MICROPY_INCLUDED_MICROBIT_MICROBITPIN_H__

source/microbit/microbitpin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,8 @@ MicroBitPin *microbit_obj_get_pin(mp_obj_t o) {
214214
}
215215
}
216216

217+
PinName microbit_obj_get_pin_name(mp_obj_t o) {
218+
return microbit_obj_get_pin(o)->name;
219+
}
220+
217221
}

source/microbit/microbitspi.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "spi_api.h"
28-
#include "MicroBit.h"
29-
3027
extern "C" {
3128

29+
#include "spi_api.h"
3230
#include "py/runtime.h"
3331
#include "modmicrobit.h"
32+
#include "microbitpin.h"
3433
#include "microbitobj.h"
3534

3635
typedef struct _microbit_spi_obj_t {
@@ -61,13 +60,13 @@ STATIC mp_obj_t microbit_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp
6160
PinName p_mosi = MICROBIT_PIN_P15;
6261
PinName p_miso = MICROBIT_PIN_P14;
6362
if (args.sclk.u_obj != mp_const_none) {
64-
p_sclk = microbit_obj_get_pin(args.sclk.u_obj)->name;
63+
p_sclk = microbit_obj_get_pin_name(args.sclk.u_obj);
6564
}
6665
if (args.mosi.u_obj != mp_const_none) {
67-
p_mosi = microbit_obj_get_pin(args.mosi.u_obj)->name;
66+
p_mosi = microbit_obj_get_pin_name(args.mosi.u_obj);
6867
}
6968
if (args.miso.u_obj != mp_const_none) {
70-
p_miso = microbit_obj_get_pin(args.miso.u_obj)->name;
69+
p_miso = microbit_obj_get_pin_name(args.miso.u_obj);
7170
}
7271

7372
// initialise the SPI

source/microbit/microbituart.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <errno.h>
28-
29-
#include "MicroBit.h"
30-
3127
extern "C" {
3228

29+
#include <errno.h>
30+
#include "serial_api.h"
3331
#include "py/runtime.h"
3432
#include "py/stream.h"
3533
#include "py/mphal.h"
3634
#include "modmicrobit.h"
35+
#include "microbitpin.h"
3736
#include "microbitobj.h"
3837

3938
// There is only one UART peripheral and it's already used by stdio (and
@@ -71,8 +70,8 @@ STATIC mp_obj_t microbit_uart_init(mp_uint_t n_args, const mp_obj_t *pos_args, m
7170
if (args[4].u_obj != mp_const_none) {
7271
mp_obj_t *pins;
7372
mp_obj_get_array_fixed_n(args[4].u_obj, 2, &pins);
74-
p_tx = microbit_obj_get_pin(pins[0])->name;
75-
p_rx = microbit_obj_get_pin(pins[1])->name;
73+
p_tx = microbit_obj_get_pin_name(pins[0]);
74+
p_rx = microbit_obj_get_pin_name(pins[1]);
7675
}
7776

7877
// initialise the uart

source/microbit/modneopixel.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <stdio.h>
28-
#include "MicroBit.h"
29-
3027
extern "C" {
3128

29+
#include <stdio.h>
30+
#include "gpio_api.h"
3231
#include "py/runtime0.h"
3332
#include "py/runtime.h"
3433
#include "lib/neopixel.h"
@@ -45,7 +44,7 @@ STATIC mp_obj_t neopixel_make_new(const mp_obj_type_t *type_in, mp_uint_t n_args
4544
(void)type_in;
4645
mp_arg_check_num(n_args, n_kw, 2, 2, false);
4746

48-
PinName pin = microbit_obj_get_pin(args[0])->name;
47+
PinName pin = microbit_obj_get_pin_name(args[0]);
4948
mp_int_t num_pixels = mp_obj_get_int(args[1]);
5049

5150
if (num_pixels <= 0) {

0 commit comments

Comments
 (0)
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