|
| 1 | +<!doctype html> |
| 2 | +<title>MicroPython WebREPL</title> |
| 3 | +<!-- |
| 4 | + term.js |
| 5 | + Copyright (c) 2012-2013, Christopher Jeffrey (MIT License) |
| 6 | + Copyright (c) 2016, Paul Sokolovsky |
| 7 | +--> |
| 8 | +<style> |
| 9 | + html { |
| 10 | + background: #555; |
| 11 | + } |
| 12 | + |
| 13 | + h1 { |
| 14 | + margin-bottom: 20px; |
| 15 | + font: 20px/1.5 sans-serif; |
| 16 | + } |
| 17 | + |
| 18 | +/* |
| 19 | + .terminal { |
| 20 | + float: left; |
| 21 | + border: #000 solid 5px; |
| 22 | + font-family: "DejaVu Sans Mono", "Liberation Mono", monospace; |
| 23 | + font-size: 11px; |
| 24 | + color: #f0f0f0; |
| 25 | + background: #000; |
| 26 | + } |
| 27 | +
|
| 28 | + .terminal-cursor { |
| 29 | + color: #000; |
| 30 | + background: #f0f0f0; |
| 31 | + } |
| 32 | +*/ |
| 33 | +</style> |
| 34 | +<script src="term.js"></script> |
| 35 | +<html> |
| 36 | +<body> |
| 37 | +<input type="text" id="url" value="ws://192.168.4.1:8080/" /> |
| 38 | +<input type="button" id="button" value="Connect" onclick="button_click()" /> |
| 39 | +<div id="term"> |
| 40 | +</div> |
| 41 | +<br clear="both" /> |
| 42 | +<i>To paste, press Ctrl+A, then Ctrl+V</i> |
| 43 | +</body> |
| 44 | + |
| 45 | +<script> |
| 46 | +; |
| 47 | + |
| 48 | +var term; |
| 49 | +var ws; |
| 50 | +var connected = false; |
| 51 | + |
| 52 | +(function() { |
| 53 | + window.onload = function() { |
| 54 | + term = new Terminal({ |
| 55 | + cols: 80, |
| 56 | + rows: 24, |
| 57 | + useStyle: true, |
| 58 | + screenKeys: true, |
| 59 | + cursorBlink: false |
| 60 | + }); |
| 61 | + term.open(document.getElementById("term")); |
| 62 | + } |
| 63 | +}).call(this); |
| 64 | + |
| 65 | +function button_click() { |
| 66 | + if (connected) { |
| 67 | + ws.close(); |
| 68 | + } else { |
| 69 | + document.getElementById('url').disabled = true; |
| 70 | + document.getElementById('button').value = "Disconnect"; |
| 71 | + connected = true; |
| 72 | + connect(document.getElementById('url').value); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function prepare_for_connect() { |
| 77 | + document.getElementById('url').disabled = false; |
| 78 | + document.getElementById('button').value = "Connect"; |
| 79 | +} |
| 80 | + |
| 81 | +function connect(url) { |
| 82 | + ws = new WebSocket(url); |
| 83 | + ws.onopen = function() { |
| 84 | + term.removeAllListeners('data'); |
| 85 | + term.on('data', function(data) { |
| 86 | + ws.send(data); |
| 87 | + }); |
| 88 | + |
| 89 | + term.on('title', function(title) { |
| 90 | + document.title = title; |
| 91 | + }); |
| 92 | + |
| 93 | + term.write('\x1b[31mWelcome to MicroPython!\x1b[m\r\n'); |
| 94 | + |
| 95 | + ws.onmessage = function(event) { |
| 96 | + term.write(event.data); |
| 97 | + }; |
| 98 | + }; |
| 99 | + |
| 100 | + ws.onclose = function() { |
| 101 | + connected = false; |
| 102 | + if (term) { |
| 103 | + term.write('\x1b[31mDisconnected\x1b[m\r\n'); |
| 104 | + } |
| 105 | + prepare_for_connect(); |
| 106 | + } |
| 107 | +} |
| 108 | +</script> |
| 109 | + |
| 110 | +</html> |
0 commit comments