Interactive MicroPython read-eval-print-loop See also the micropython library and its README file.

Dependencies:   mbed micropython

This program needs the micropython library - see there for Getting Started information.

See the mbed MicroPython wiki for more information.

main.cpp

Committer:
Colin Hogben
Date:
2016-04-16
Revision:
9:195af29eef76
Parent:
8:11e2b1ca356a
Child:
13:d8a89b43fe5b

File content as of revision 9:195af29eef76:

#include "mbed.h"
extern "C" {
#include "py/runtime.h"
#include "py/mphal.h"
#include "lib/utils/pyexec.h"
}

// Serial communication to host PC via USB
Serial pc(USBTX, USBRX);

// Implement the micropython HAL I/O functions
extern "C" void mp_hal_stdout_tx_chr(int c);
void mp_hal_stdout_tx_chr(int c)
{
  pc.putc(c);
}
extern "C" int mp_hal_stdin_rx_chr(void);
int mp_hal_stdin_rx_chr(void)
{
  int c = pc.getc();
  //printf("getc=%02x\n", c);
  return c;
}

void hexdump(const void *ptr, int len)
{
  int line;
  for (line=0; line < len; line += 16) {
    int word;
    for (word=0; word < 16 && line+word < len; word+=4) {
      int b;
      for (b=0; b < 4 && line+word+b < len; b++) {
	pc.printf(" %02x", ((const char *)ptr)[line+word+b]);
      }
      pc.printf(" ");
    }
    pc.printf("\r\n");
  }
}

int main()
{
  mp_init();
  // hexdump(&MP_STATE_VM(dict_main), 64);
  pyexec_friendly_repl();
  mp_deinit();
  return 0;
}