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.

Committer:
infinnovation
Date:
Wed Apr 27 21:20:06 2016 +0000
Revision:
18:04bb73e9e912
Parent:
15:0eb1e7e4152d
Update library version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Colin Hogben 13:d8a89b43fe5b 1 /*
Colin Hogben 13:d8a89b43fe5b 2 * The MIT License (MIT)
Colin Hogben 13:d8a89b43fe5b 3 *
Colin Hogben 13:d8a89b43fe5b 4 * Copyright (c) 2016 Colin Hogben
Colin Hogben 13:d8a89b43fe5b 5 *
Colin Hogben 13:d8a89b43fe5b 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
Colin Hogben 13:d8a89b43fe5b 7 * of this software and associated documentation files (the "Software"), to deal
Colin Hogben 13:d8a89b43fe5b 8 * in the Software without restriction, including without limitation the rights
Colin Hogben 13:d8a89b43fe5b 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Colin Hogben 13:d8a89b43fe5b 10 * copies of the Software, and to permit persons to whom the Software is
Colin Hogben 13:d8a89b43fe5b 11 * furnished to do so, subject to the following conditions:
Colin Hogben 13:d8a89b43fe5b 12 *
Colin Hogben 13:d8a89b43fe5b 13 * The above copyright notice and this permission notice shall be included in
Colin Hogben 13:d8a89b43fe5b 14 * all copies or substantial portions of the Software.
Colin Hogben 13:d8a89b43fe5b 15 *
Colin Hogben 13:d8a89b43fe5b 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Colin Hogben 13:d8a89b43fe5b 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Colin Hogben 13:d8a89b43fe5b 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Colin Hogben 13:d8a89b43fe5b 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Colin Hogben 13:d8a89b43fe5b 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Colin Hogben 13:d8a89b43fe5b 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Colin Hogben 13:d8a89b43fe5b 22 * THE SOFTWARE.
Colin Hogben 13:d8a89b43fe5b 23 */
Colin Hogben 13:d8a89b43fe5b 24
Colin Hogben 13:d8a89b43fe5b 25 /**
Colin Hogben 13:d8a89b43fe5b 26 * A read-eval-print-loop for micropython on the mbed platform.
Colin Hogben 13:d8a89b43fe5b 27 *
Colin Hogben 13:d8a89b43fe5b 28 * The USB serial link is used.
Colin Hogben 13:d8a89b43fe5b 29 */
Colin Hogben 6:54f3d4af42cc 30 #include "mbed.h"
Colin Hogben 8:11e2b1ca356a 31 extern "C" {
Colin Hogben 8:11e2b1ca356a 32 #include "py/runtime.h"
Colin Hogben 9:195af29eef76 33 #include "py/mphal.h"
Colin Hogben 8:11e2b1ca356a 34 #include "lib/utils/pyexec.h"
Colin Hogben 8:11e2b1ca356a 35 }
Colin Hogben 6:54f3d4af42cc 36
Colin Hogben 9:195af29eef76 37 // Serial communication to host PC via USB
Colin Hogben 9:195af29eef76 38 Serial pc(USBTX, USBRX);
Colin Hogben 9:195af29eef76 39
Colin Hogben 9:195af29eef76 40 // Implement the micropython HAL I/O functions
Colin Hogben 9:195af29eef76 41 extern "C" void mp_hal_stdout_tx_chr(int c);
Colin Hogben 15:0eb1e7e4152d 42 void mp_hal_stdout_tx_chr(int c) {
Colin Hogben 15:0eb1e7e4152d 43 pc.putc(c);
Colin Hogben 9:195af29eef76 44 }
Colin Hogben 9:195af29eef76 45 extern "C" int mp_hal_stdin_rx_chr(void);
Colin Hogben 15:0eb1e7e4152d 46 int mp_hal_stdin_rx_chr(void) {
Colin Hogben 15:0eb1e7e4152d 47 int c = pc.getc();
Colin Hogben 15:0eb1e7e4152d 48 return c;
Colin Hogben 9:195af29eef76 49 }
Colin Hogben 9:195af29eef76 50
Colin Hogben 13:d8a89b43fe5b 51 // Now the main program - run the REPL.
Colin Hogben 15:0eb1e7e4152d 52 int main() {
Colin Hogben 15:0eb1e7e4152d 53 mp_init();
Colin Hogben 15:0eb1e7e4152d 54 pyexec_friendly_repl();
Colin Hogben 15:0eb1e7e4152d 55 mp_deinit();
Colin Hogben 15:0eb1e7e4152d 56 return 0;
Colin Hogben 6:54f3d4af42cc 57 }