Colin Hogben / micropython

Dependents:   micropython-repl

Files at this revision

API Documentation at this revision

Comitter:
Colin Hogben
Date:
Sun Apr 17 23:08:33 2016 +0100
Parent:
2:c89e95946844
Child:
4:c6b9b315325a
Commit message:
modmbed: add Serial

Changed in this revision

genhdr/qstrdefs.generated.h Show annotated file Show diff for this revision Revisions of this file
modmbed.c Show annotated file Show diff for this revision Revisions of this file
modmbed_i.cpp Show annotated file Show diff for this revision Revisions of this file
modmbed_i.h Show annotated file Show diff for this revision Revisions of this file
mpconfigport.h Show annotated file Show diff for this revision Revisions of this file
qstrdefsport.h Show annotated file Show diff for this revision Revisions of this file
--- a/genhdr/qstrdefs.generated.h	Sat Apr 16 22:43:41 2016 +0100
+++ b/genhdr/qstrdefs.generated.h	Sun Apr 17 23:08:33 2016 +0100
@@ -29,6 +29,10 @@
 QDEF(MP_QSTR_mbed, (const byte*)"\x0b\xb5\x04" "mbed")
 QDEF(MP_QSTR_DigitalOut, (const byte*)"\x11\xcb\x0a" "DigitalOut")
 QDEF(MP_QSTR_write, (const byte*)"\x98\xa8\x05" "write")
+QDEF(MP_QSTR_Serial, (const byte*)"\xe5\xe5\x06" "Serial")
+QDEF(MP_QSTR_putc, (const byte*)"\x97\x7e\x04" "putc")
+QDEF(MP_QSTR_puts, (const byte*)"\x87\x7e\x04" "puts")
+QDEF(MP_QSTR_getc, (const byte*)"\xf0\xa1\x04" "getc")
 QDEF(MP_QSTR___name__, (const byte*)"\xe2\x38\x08" "__name__")
 QDEF(MP_QSTR_umachine, (const byte*)"\x95\x7f\x08" "umachine")
 QDEF(MP_QSTR_mem8, (const byte*)"\x18\xc8\x04" "mem8")
--- a/modmbed.c	Sat Apr 16 22:43:41 2016 +0100
+++ b/modmbed.c	Sun Apr 17 23:08:33 2016 +0100
@@ -86,12 +86,96 @@
 
 #endif // MICROPY_MBED_DIGITALOUT
 
+#if MICROPY_MBED_SERIAL
+/*-----------------------------------------------------------------------
+ *	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);
+
+STATIC const mp_map_elem_t mbed_Serial_locals_dict_table[] = {
+  { MP_OBJ_NEW_QSTR(MP_QSTR_putc), (mp_obj_t)&mbed_Serial_putc_obj },
+  { MP_OBJ_NEW_QSTR(MP_QSTR_puts), (mp_obj_t)&mbed_Serial_puts_obj },
+  { MP_OBJ_NEW_QSTR(MP_QSTR_getc), (mp_obj_t)&mbed_Serial_getc_obj },
+};
+  
+STATIC MP_DEFINE_CONST_DICT(mbed_Serial_locals_dict,
+			    mbed_Serial_locals_dict_table);
+
+STATIC const mp_obj_type_t mbed_Serial_type = {
+  { &mp_type_type },
+  .name = MP_QSTR_Serial,
+  // .print
+  .make_new = mbed_Serial_make_new,
+  .locals_dict = (mp_obj_t)&mbed_Serial_locals_dict,
+};
+
+#endif // MICROPY_MBED_SERIAL
+
 // Module
 STATIC const mp_rom_map_elem_t mp_mbed_module_globals_table[] = {
   { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_mbed) },
 #if MICROPY_MBED_DIGITALOUT
   { MP_ROM_QSTR(MP_QSTR_DigitalOut), MP_ROM_PTR(&mbed_DigitalOut_type) },
 #endif
+#if MICROPY_MBED_SERIAL
+  { MP_ROM_QSTR(MP_QSTR_Serial), MP_ROM_PTR(&mbed_Serial_type) },
+#endif
 };
 
 STATIC MP_DEFINE_CONST_DICT(mp_mbed_module_globals,
--- a/modmbed_i.cpp	Sat Apr 16 22:43:41 2016 +0100
+++ b/modmbed_i.cpp	Sun Apr 17 23:08:33 2016 +0100
@@ -50,4 +50,28 @@
   *dout = value;
 }
 
+void *mbed_Serial__create(int tx, int rx)
+{
+  Serial *serial = new Serial((PinName)tx, (PinName)rx);
+  return (void *)serial;
+}
+
+void mbed_Serial__putc(void *self, int c)
+{
+  Serial *serial = (Serial *)self;
+  serial->putc(c);
+}
+
+void mbed_Serial__puts(void *self, const char *str)
+{
+  Serial *serial = (Serial *)self;
+  serial->puts(str);
+}
+
+int mbed_Serial__getc(void *self)
+{
+  Serial *serial = (Serial *)self;
+  return serial->getc();
+}
+
 #endif
--- a/modmbed_i.h	Sat Apr 16 22:43:41 2016 +0100
+++ b/modmbed_i.h	Sun Apr 17 23:08:33 2016 +0100
@@ -24,3 +24,8 @@
 
 extern void *mbed_DigitalOut__create(int pin);
 extern void mbed_DigitalOut__write(void *self, int value);
+
+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);
--- a/mpconfigport.h	Sat Apr 16 22:43:41 2016 +0100
+++ b/mpconfigport.h	Sun Apr 17 23:08:33 2016 +0100
@@ -76,6 +76,7 @@
 #define MICROPY_PY_MBED             1
 // Select which mbed features wanted
 #define MICROPY_MBED_DIGITALOUT     1
+#define MICROPY_MBED_SERIAL         1
 
 #define MICROPY_MODULE_FROZEN       (0)
 #define MICROPY_CPYTHON_COMPAT      (0)
--- a/qstrdefsport.h	Sat Apr 16 22:43:41 2016 +0100
+++ b/qstrdefsport.h	Sun Apr 17 23:08:33 2016 +0100
@@ -36,4 +36,9 @@
 #if MICROPY_PY_MBED
 Q(mbed)
 Q(DigitalOut)
+Q(write)
+Q(Serial)
+Q(putc)
+Q(puts)
+Q(getc)
 #endif