summaryrefslogtreecommitdiff
path: root/examples/embedding/main.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-10-06 11:14:58 +1100
committerDamien George <damien@micropython.org>2023-01-20 22:28:50 +1100
commit4f3780a156e1ba3f6981ab3a90be655a718f88f5 (patch)
tree8a65be84c5d2189bdbd9ae79e7d8e846ad36b054 /examples/embedding/main.c
parenta8a1ad13916aa74fca30ad23061a5552935bf097 (diff)
examples/embedding: Rework example to use ports/embed.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'examples/embedding/main.c')
-rw-r--r--examples/embedding/main.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/embedding/main.c b/examples/embedding/main.c
new file mode 100644
index 000000000..ee673fc93
--- /dev/null
+++ b/examples/embedding/main.c
@@ -0,0 +1,44 @@
+/* This file is part of the MicroPython project, http://micropython.org/
+ * The MIT License (MIT)
+ * Copyright (c) 2022-2023 Damien P. George
+ */
+
+#include "port/micropython_embed.h"
+
+// This is example 1 script, which will be compiled and executed.
+static const char *example_1 =
+ "print('hello world!', list(x + 1 for x in range(10)), end='eol\\n')";
+
+// This is example 2 script, which will be compiled and executed.
+static const char *example_2 =
+ "for i in range(10):\n"
+ " print('iter {:08}'.format(i))\n"
+ "\n"
+ "try:\n"
+ " 1//0\n"
+ "except Exception as er:\n"
+ " print('caught exception', repr(er))\n"
+ "\n"
+ "import gc\n"
+ "print('run GC collect')\n"
+ "gc.collect()\n"
+ "\n"
+ "print('finish')\n"
+ ;
+
+// This array is the MicroPython GC heap.
+static char heap[8 * 1024];
+
+int main() {
+ // Initialise MicroPython.
+ mp_embed_init(&heap[0], sizeof(heap));
+
+ // Run the example scripts (they will be compiled first).
+ mp_embed_exec_str(example_1);
+ mp_embed_exec_str(example_2);
+
+ // Deinitialise MicroPython.
+ mp_embed_deinit();
+
+ return 0;
+}