diff --git a/Maixpy_demo.ipynb b/Maixpy_demo.ipynb new file mode 100644 index 0000000..342d850 --- /dev/null +++ b/Maixpy_demo.ipynb @@ -0,0 +1,126 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mConnecting to --port=/dev/ttyUSB1 --baud=115200 \u001b[0m\n", + "\u001b[34mReady.\n", + "\u001b[0m" + ] + } + ], + "source": [ + "%serialconnect" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the ov2640_init\n", + "[MAIXPY]LCD:init\n" + ] + } + ], + "source": [ + "\n", + "import machine\n", + "c = machine.ov2640()\n", + "c.init()\n", + "l = machine.st7789()\n", + "l.init()\n", + "i = bytearray(320*240*2)\n", + "\n", + "c.get_image(i)\n", + "l.draw_picture_default(i)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "." + ] + } + ], + "source": [ + "for j in range(50):\n", + " c.get_image(i)\n", + " l.draw_picture_default(i)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[leftinbuffer] ['\\x04\\x04>']\n", + "GC: total: 1040384, used: 413728, free: 626656\n", + " No. of 1-blocks: 48, 2-blocks: 10, max blk sz: 12657, max free sz: 9014\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Traceback (most recent call last):\n", + " File \"\", line 4, in \n", + "MemoryError: memory allocation failed, allocating 405300 bytes\n" + ] + } + ], + "source": [ + "for j in range(30):\n", + " c.get_image(i)\n", + " for x in range(100,120):\n", + " i[x*320*2 + 100:x*320*2 + 200] = b'0x99'*100\n", + " l.draw_picture_default(i)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "MicroPython - USB", + "language": "micropython", + "name": "micropython" + }, + "language_info": { + "codemirror_mode": "python", + "file_extension": ".py", + "mimetype": "text/python", + "name": "micropython" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/README.md b/README.md index 79ab8db..1f97a11 100644 --- a/README.md +++ b/README.md @@ -132,13 +132,14 @@ Other known projects that have implemented a Jupyter Micropython kernel are: * https://github.com/TDAbboud/mpkernel * https://github.com/takluyver/ubit_kernel * https://github.com/jneines/nodemcu_kernel +* https://github.com/zsquareplusc/mpy-repl-tool In my defence, this is not an effect of not-invented-here syndrome; I did not discover most of these other projects until I had mostly written this one. I do think that for robustness it is important to expose the full processes -of making connections and But for my purposes, this is more robust and contains debugging (of the -serial connections) capability. +of making connections. For my purposes this is more robust and contains debugging (of the +serial connections) capability through its %lsmagic functions. Other known projects to have made Jupyter-like or secondary interfaces to Micropython: * https://github.com/nickzoic/mpy-webpad diff --git a/jupyter_micropython_kernel/deviceconnector.py b/jupyter_micropython_kernel/deviceconnector.py index 42b7147..803a5a7 100644 --- a/jupyter_micropython_kernel/deviceconnector.py +++ b/jupyter_micropython_kernel/deviceconnector.py @@ -423,9 +423,9 @@ class DeviceConnector: if bbinary: chunksize = 30 - sswrite(b"import sys, os; O7=sys.stdout.write\r\n") - sswrite(b"import ubinascii; O8 = ubinascii.b2a_base64\r\n") - sswrite("O=open({}, 'rb')\r\n".format(repr(sourcefilename)).encode()) + sswrite(b"import sys,os;O7=sys.stdout.write\r\n") + sswrite(b"import ubinascii;O8=ubinascii.b2a_base64\r\n") + sswrite("O=open({},'rb')\r\n".format(repr(sourcefilename)).encode()) sswrite(b"O9=bytearray(%d)\r\n" % chunksize) sswrite("O4=os.stat({})[6]\r\n".format(repr(sourcefilename)).encode()) sswrite(b"print(O4)\r\n") @@ -438,8 +438,8 @@ class DeviceConnector: return None sswrite(b"O7(O8(O.read(O4%%%d)))\r\n" % chunksize) # get sub-block - sswrite(b"while O.readinto(O9): O7(O8(O9))\r\n") - sswrite(b"O.close(); del O, O7, O8, O9, O4\r\n") + sswrite(b"while O.readinto(O9): O7(O8(O9))\r\n") + sswrite(b"O.close(); del O,O7,O8,O9,O4\r\n") sswrite(b'\r\x04') chunks = self.receivestream(bseekokay=True, bfetchfilecapture_nchunks=nbytes//chunksize+1) rres = [ ] diff --git a/jupyter_micropython_kernel/kernel.py b/jupyter_micropython_kernel/kernel.py index def9dfe..8fc79a5 100644 --- a/jupyter_micropython_kernel/kernel.py +++ b/jupyter_micropython_kernel/kernel.py @@ -267,7 +267,6 @@ class MicroPythonKernel(Kernel): self.sres("%lsmagic\n list magic commands\n\n") self.sres(re.sub("usage: ", "", ap_mpycross.format_usage())) self.sres(" cross-compile a .py file to a .mpy file\n\n") - self.sres("%readbytes\n does serial.read_all()\n\n") self.sres(re.sub("usage: ", "", ap_readbytes.format_usage())) self.sres(" does serial.read_all()\n\n") self.sres("%rebootdevice\n reboots device\n\n") @@ -374,7 +373,9 @@ class MicroPythonKernel(Kernel): if apargs.print: self.sres(fetchedcontents.decode() if type(fetchedcontents)==bytes else fetchedcontents, clear_output=True) if (apargs.destinationfilename or not apargs.print) and fetchedcontents: - fout = open(apargs.destinationfilename or apargs.sourcefilename, "wb" if apargs.binary else "w") + dstfile = apargs.destinationfilename or os.path.basename(apargs.sourcefilename) + self.sres("Saving file to {}".format(repr(dstfile))) + fout = open(dstfile, "wb" if apargs.binary else "w") fout.write(fetchedcontents) fout.close() else: diff --git a/micropythondemo.ipynb b/micropythondemo.ipynb index 8a4bbfa..b98306e 100644 --- a/micropythondemo.ipynb +++ b/micropythondemo.ipynb @@ -306,7 +306,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Connect through wifi to 192.168.0.142\r\n" + "Connect through wifi to 192.168.0.142\n" ] } ], @@ -491,7 +491,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "['__init__', 'remove_reader', 'wait', '__qualname__', 'add_writer', 'remove_writer', 'add_reader', '__module__', 'q', 'poller', 'objmap']\r\n" + "['__init__', 'remove_reader', 'wait', '__qualname__', 'add_writer', 'remove_writer', 'add_reader', '__module__', 'q', 'poller', 'objmap']\n" ] } ], @@ -511,7 +511,7 @@ ], "metadata": { "kernelspec": { - "display_name": "MicroPython - /dev/ttyUSB0", + "display_name": "MicroPython - USB", "language": "micropython", "name": "micropython" },