Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 6:3e98ebcedb4c, committed 2016-04-25
- Comitter:
- Colin Hogben
- Date:
- Mon Apr 25 19:55:21 2016 +0100
- Parent:
- 5:1d19b8110e11
- Child:
- 7:379d46fd02c2
- Commit message:
- Split C/C++ code differently
Changed in this revision
--- a/modmbed.c Sun Apr 24 21:57:21 2016 +0100
+++ b/modmbed.c Mon Apr 25 19:55:21 2016 +0100
@@ -34,38 +34,6 @@
//-----------------------------------------------------------------------
// DigitalOut class
//-----------------------------------------------------------------------
-STATIC const mp_obj_type_t mbed_DigitalOut_type;
-
-typedef struct _mbed_DigitalOut_obj_t {
- mp_obj_base_t base;
- void *dout;
-} mbed_DigitalOut_obj_t;
-
-// constructor DigitalOut(pin)
-STATIC mp_obj_t mbed_DigitalOut_make_new(const mp_obj_type_t *type,
- mp_uint_t n_args, mp_uint_t n_kw,
- const mp_obj_t *args)
-{
- (void)type;
- mp_arg_check_num(n_args, n_kw, 1, 1, false);
- int pin = mp_obj_get_int(args[0]);
-
- mbed_DigitalOut_obj_t *o = m_new_obj_with_finaliser(mbed_DigitalOut_obj_t);
- o->base.type = (mp_obj_t)&mbed_DigitalOut_type;
- o->dout = mbed_DigitalOut__create(pin);
-
- return o;
-}
-
-STATIC mp_obj_t mbed_DigitalOut_write(mp_obj_t self_in, mp_obj_t value_in)
-{
- mbed_DigitalOut_obj_t *self = self_in;
- int value = mp_obj_get_int(value_in);
-
- mbed_DigitalOut__write(self->dout, value);
- return mp_const_none;
-}
-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_DigitalOut_write_obj,
mbed_DigitalOut_write);
@@ -76,7 +44,7 @@
STATIC MP_DEFINE_CONST_DICT(mbed_DigitalOut_locals_dict,
mbed_DigitalOut_locals_dict_table);
-STATIC const mp_obj_type_t mbed_DigitalOut_type = {
+const mp_obj_type_t mbed_DigitalOut_type = {
{ &mp_type_type },
.name = MP_QSTR_DigitalOut,
// .print
@@ -90,61 +58,12 @@
/*-----------------------------------------------------------------------
* Serial class
*-----------------------------------------------------------------------*/
-STATIC const mp_obj_type_t mbed_Serial_type;
-
-typedef struct _mbed_Serial_obj_t {
- mp_obj_base_t base;
- void *serial;
-} mbed_Serial_obj_t;
-
-// constructor Serial(pin)
-STATIC mp_obj_t mbed_Serial_make_new(const mp_obj_type_t *type,
- mp_uint_t n_args, mp_uint_t n_kw,
- const mp_obj_t *args)
-{
- (void)type;
- mp_arg_check_num(n_args, n_kw, 2, 2, false);
- int tx = mp_obj_get_int(args[0]);
- int rx = mp_obj_get_int(args[1]);
-
- mbed_Serial_obj_t *o = m_new_obj_with_finaliser(mbed_Serial_obj_t);
- o->base.type = (mp_obj_t)&mbed_Serial_type;
- o->serial = mbed_Serial__create(tx, rx);
-
- return o;
-}
-
-STATIC mp_obj_t mbed_Serial_putc(mp_obj_t self_in, mp_obj_t chr_in)
-{
- mbed_Serial_obj_t *self = self_in;
- int chr = mp_obj_get_int(chr_in);
-
- mbed_Serial__putc(self->serial, chr);
- return mp_const_none;
-}
-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_Serial_putc_obj,
mbed_Serial_putc);
-STATIC mp_obj_t mbed_Serial_puts(mp_obj_t self_in, mp_obj_t str_in)
-{
- mbed_Serial_obj_t *self = self_in;
- const char *str = mp_obj_str_get_str(str_in);
-
- mbed_Serial__puts(self->serial, str);
- return mp_const_none;
-}
-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_Serial_puts_obj,
mbed_Serial_puts);
-STATIC mp_obj_t mbed_Serial_getc(mp_obj_t self_in)
-{
- mbed_Serial_obj_t *self = self_in;
- int chr = mbed_Serial__getc(self->serial);
- return MP_OBJ_NEW_SMALL_INT(chr);
-}
-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_Serial_getc_obj,
mbed_Serial_getc);
@@ -157,7 +76,7 @@
STATIC MP_DEFINE_CONST_DICT(mbed_Serial_locals_dict,
mbed_Serial_locals_dict_table);
-STATIC const mp_obj_type_t mbed_Serial_type = {
+const mp_obj_type_t mbed_Serial_type = {
{ &mp_type_type },
.name = MP_QSTR_Serial,
// .print
--- a/modmbed_i.cpp Sun Apr 24 21:57:21 2016 +0100
+++ b/modmbed_i.cpp Mon Apr 25 19:55:21 2016 +0100
@@ -35,43 +35,85 @@
#include "mbed.h"
extern "C" {
+ #include "py/runtime.h"
#include "modmbed_i.h"
}
-void *mbed_DigitalOut__create(int pin)
+typedef struct _mbed_DigitalOut_obj_t {
+ mp_obj_base_t base;
+ DigitalOut *dout;
+} mbed_DigitalOut_obj_t;
+
+// constructor DigitalOut(pin)
+mp_obj_t mbed_DigitalOut_make_new(const mp_obj_type_t *type,
+ mp_uint_t n_args, mp_uint_t n_kw,
+ const mp_obj_t *args)
{
- DigitalOut *dout = new DigitalOut((PinName)pin);
- return (void *)dout;
+ (void)type;
+ mp_arg_check_num(n_args, n_kw, 1, 1, false);
+ int pin = mp_obj_get_int(args[0]);
+
+ mbed_DigitalOut_obj_t *o = m_new_obj_with_finaliser(mbed_DigitalOut_obj_t);
+ o->base.type = &mbed_DigitalOut_type;
+ o->dout = new DigitalOut((PinName)pin);
+
+ return o;
}
-void mbed_DigitalOut__write(void *self, int value)
+mp_obj_t mbed_DigitalOut_write(mp_obj_t self_in, mp_obj_t value_in)
{
- DigitalOut *dout = (DigitalOut *)self;
- *dout = value;
+ mbed_DigitalOut_obj_t *self = (mbed_DigitalOut_obj_t *)self_in;
+ int value = mp_obj_get_int(value_in);
+
+ self->dout->write(value);
+ return mp_const_none;
}
-void *mbed_Serial__create(int tx, int rx)
+typedef struct _mbed_Serial_obj_t {
+ mp_obj_base_t base;
+ Serial *serial;
+} mbed_Serial_obj_t;
+
+// constructor Serial(pin)
+mp_obj_t mbed_Serial_make_new(const mp_obj_type_t *type,
+ mp_uint_t n_args, mp_uint_t n_kw,
+ const mp_obj_t *args)
{
- Serial *serial = new Serial((PinName)tx, (PinName)rx);
- return (void *)serial;
+ (void)type;
+ mp_arg_check_num(n_args, n_kw, 2, 2, false);
+ int tx = mp_obj_get_int(args[0]);
+ int rx = mp_obj_get_int(args[1]);
+
+ mbed_Serial_obj_t *o = m_new_obj_with_finaliser(mbed_Serial_obj_t);
+ o->base.type = &mbed_Serial_type;
+ o->serial = new Serial((PinName)tx, (PinName)rx);
+
+ return o;
}
-void mbed_Serial__putc(void *self, int c)
+mp_obj_t mbed_Serial_putc(mp_obj_t self_in, mp_obj_t chr_in)
{
- Serial *serial = (Serial *)self;
- serial->putc(c);
+ mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
+ int chr = mp_obj_get_int(chr_in);
+
+ self->serial->putc(chr);
+ return mp_const_none;
}
-void mbed_Serial__puts(void *self, const char *str)
+mp_obj_t mbed_Serial_puts(mp_obj_t self_in, mp_obj_t str_in)
{
- Serial *serial = (Serial *)self;
- serial->puts(str);
+ mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
+ const char *str = mp_obj_str_get_str(str_in);
+
+ self->serial->puts(str);
+ return mp_const_none;
}
-int mbed_Serial__getc(void *self)
+mp_obj_t mbed_Serial_getc(mp_obj_t self_in)
{
- Serial *serial = (Serial *)self;
- return serial->getc();
+ mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
+ int chr = self->serial->getc();
+ return MP_OBJ_NEW_SMALL_INT(chr);
}
#endif
--- a/modmbed_i.h Sun Apr 24 21:57:21 2016 +0100 +++ b/modmbed_i.h Mon Apr 25 19:55:21 2016 +0100 @@ -22,10 +22,14 @@ * THE SOFTWARE. */ -extern void *mbed_DigitalOut__create(int pin); -extern void mbed_DigitalOut__write(void *self, int value); +extern const mp_obj_type_t mbed_DigitalOut_type; +extern mp_obj_t mbed_DigitalOut_make_new(const mp_obj_type_t *, mp_uint_t, mp_uint_t, const mp_obj_t *); +extern mp_obj_t mbed_DigitalOut_write(mp_obj_t self_in, mp_obj_t value_in); -extern void *mbed_Serial__create(int tx, int rx); -extern void mbed_Serial__putc(void *self, int); -extern void mbed_Serial__puts(void *self, const char *); -extern int mbed_Serial__getc(void *self); +extern const mp_obj_type_t mbed_Serial_type; +extern mp_obj_t mbed_Serial_make_new(const mp_obj_type_t *type, + mp_uint_t n_args, mp_uint_t n_kw, + const mp_obj_t *args); +extern mp_obj_t mbed_Serial_putc(mp_obj_t self_in, mp_obj_t chr_in); +extern mp_obj_t mbed_Serial_puts(mp_obj_t self_in, mp_obj_t str_in); +extern mp_obj_t mbed_Serial_getc(mp_obj_t self_in);