|
| 1 | +<!DOCTYPE HTML > |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<script type = "text/javascript"> |
| 6 | + |
| 7 | +/////////////////////////////////////////////////// |
| 8 | +// copied from FirmataConstants.h |
| 9 | +// message command bytes (128-255/0x80-0xFF) |
| 10 | + |
| 11 | +const DIGITAL_MESSAGE = 0x90; // send data for a digital port (collection of 8 pins) |
| 12 | +const ANALOG_MESSAGE = 0xE0; // send data for an analog pin (or PWM) |
| 13 | +const REPORT_ANALOG = 0xC0; // enable analog input by pin # |
| 14 | +const REPORT_DIGITAL = 0xD0; // enable digital input by port pair |
| 15 | +// |
| 16 | +const SET_PIN_MODE = 0xF4; // set a pin to INPUT/OUTPUT/PWM/etc |
| 17 | +const SET_DIGITAL_PIN_VALUE = 0xF5; // set value of an individual digital pin |
| 18 | +// |
| 19 | +const REPORT_VERSION = 0xF9; // report protocol version |
| 20 | +const SYSTEM_RESET = 0xFF; // reset from MIDI |
| 21 | +// |
| 22 | +const START_SYSEX = 0xF0; // start a MIDI Sysex message |
| 23 | +const END_SYSEX = 0xF7; // end a MIDI Sysex message |
| 24 | + |
| 25 | +// extended command set using sysex (0-127/0x00-0x7F) |
| 26 | +/* 0x00-0x0F reserved for user-defined commands */ |
| 27 | + |
| 28 | +const SERIAL_DATA = 0x60; // communicate with serial devices, including other boards |
| 29 | +const ENCODER_DATA = 0x61; // reply with encoders current positions |
| 30 | +const SERVO_CONFIG = 0x70; // set max angle, minPulse, maxPulse, freq |
| 31 | +const STRING_DATA = 0x71; // a string message with 14-bits per char |
| 32 | +const STEPPER_DATA = 0x72; // control a stepper motor |
| 33 | +const ONEWIRE_DATA = 0x73; // send an OneWire read/write/reset/select/skip/search request |
| 34 | +const SHIFT_DATA = 0x75; // a bitstream to/from a shift register |
| 35 | +const I2C_REQUEST = 0x76; // send an I2C read/write request |
| 36 | +const I2C_REPLY = 0x77; // a reply to an I2C read request |
| 37 | +const I2C_CONFIG = 0x78; // config I2C settings such as delay times and power pins |
| 38 | +const REPORT_FIRMWARE = 0x79; // report name and version of the firmware |
| 39 | +const EXTENDED_ANALOG = 0x6F; // analog write (PWM, Servo, etc) to any pin |
| 40 | +const PIN_STATE_QUERY = 0x6D; // ask for a pin's current mode and value |
| 41 | +const PIN_STATE_RESPONSE = 0x6E; // reply with pin's current mode and value |
| 42 | +const CAPABILITY_QUERY = 0x6B; // ask for supported modes and resolution of all pins |
| 43 | +const CAPABILITY_RESPONSE = 0x6C; // reply with supported modes and resolution |
| 44 | +const ANALOG_MAPPING_QUERY = 0x69; // ask for mapping of analog to pin numbers |
| 45 | +const ANALOG_MAPPING_RESPONSE = 0x6A; // reply with mapping info |
| 46 | +const SAMPLING_INTERVAL = 0x7A; // set the poll rate of the main loop |
| 47 | +const SCHEDULER_DATA = 0x7B; // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler |
| 48 | +const SYSEX_NON_REALTIME = 0x7E; // MIDI Reserved for non-realtime messages |
| 49 | +const SYSEX_REALTIME = 0x7F; // MIDI Reserved for realtime messages |
| 50 | + |
| 51 | +// pin modes |
| 52 | +const PIN_MODE_INPUT = 0x00; // same as INPUT defined in Arduino.h |
| 53 | +const PIN_MODE_OUTPUT = 0x01; // same as OUTPUT defined in Arduino.h |
| 54 | +const PIN_MODE_ANALOG = 0x02; // analog pin in analogInput mode |
| 55 | +const PIN_MODE_PWM = 0x03; // digital pin in PWM output mode |
| 56 | +const PIN_MODE_SERVO = 0x04; // digital pin in Servo output mode |
| 57 | +const PIN_MODE_SHIFT = 0x05; // shiftIn/shiftOut mode |
| 58 | +const PIN_MODE_I2C = 0x06; // pin included in I2C setup |
| 59 | +const PIN_MODE_ONEWIRE = 0x07; // pin configured for 1-wire |
| 60 | +const PIN_MODE_STEPPER = 0x08; // pin configured for stepper motor |
| 61 | +const PIN_MODE_ENCODER = 0x09; // pin configured for rotary encoders |
| 62 | +const PIN_MODE_SERIAL = 0x0A; // pin configured for serial communication |
| 63 | +const PIN_MODE_PULLUP = 0x0B; // enable internal pull-up resistor for pin |
| 64 | +const PIN_MODE_IGNORE = 0x7F; // pin configured to be ignored by digitalWrite and capabilityResponse |
| 65 | + |
| 66 | +const TOTAL_PIN_MODES = 13; |
| 67 | +/////////////////////////////////////////////////// |
| 68 | + |
| 69 | +var ws = undefined; |
| 70 | +var gpios = 0; |
| 71 | +var port = 0; //?? |
| 72 | + |
| 73 | +function start() |
| 74 | +{ |
| 75 | + console.log("Firmata-WebSocket demo running"); |
| 76 | + |
| 77 | + if (!("WebSocket" in window)) |
| 78 | + { |
| 79 | + alert("WebSocket NOT supported by your Browser!"); |
| 80 | + return; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function notConnected() |
| 85 | +{ |
| 86 | + console.log("not connected"); |
| 87 | +} |
| 88 | + |
| 89 | +function digitalWrite(n, v) |
| 90 | +{ |
| 91 | + if (ws) |
| 92 | + { |
| 93 | + b = new Uint8Array(3); |
| 94 | + msg = 1 << n; |
| 95 | + if (v) |
| 96 | + gpios |= msg; |
| 97 | + else |
| 98 | + gpios &= ~msg; |
| 99 | + b[0] = DIGITAL_MESSAGE + port; |
| 100 | + b[1] = gpios & 0x7f; |
| 101 | + b[2] = (gpios >> 7) & 0x7f; |
| 102 | + ws.send(b); |
| 103 | + console.log("digitalWrite(" + n + ', ' + v + ') - gpio=0x' + gpios.toString(16)); |
| 104 | + } |
| 105 | + else |
| 106 | + notConnected(); |
| 107 | +} |
| 108 | + |
| 109 | +function digitalRead(n) |
| 110 | +{ |
| 111 | + if (ws) |
| 112 | + { |
| 113 | + b = new Uint8Array(2); |
| 114 | + b[0] = REPORT_DIGITAL + port; |
| 115 | + b[1] = 1; // enable |
| 116 | + ws.send(b); |
| 117 | + console.log("ask for reporting digital"); |
| 118 | + } |
| 119 | + else |
| 120 | + notConnected(); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +function connect() |
| 125 | +{ |
| 126 | + console.log('connect: ' + document.getElementById('hostname').value); |
| 127 | + ws = new WebSocket(document.getElementById('hostname').value); |
| 128 | + if (!ws) |
| 129 | + { |
| 130 | + alert("Cannot open WebSocket " + document.getElementById('hostname').value); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + ws.onopen = function() |
| 135 | + { |
| 136 | + console.log('connected to server\n'); |
| 137 | + // firmata-websocket makes a difference between command and text, if that matters |
| 138 | + ws.send("hello from ws"); |
| 139 | + }; |
| 140 | + |
| 141 | + ws.onmessage = function(evt) |
| 142 | + { |
| 143 | + var len = evt.data.length; |
| 144 | + console.log('type=' + typeof(evt.data)); |
| 145 | + if (evt.data instanceof ArrayBuffer) |
| 146 | + { |
| 147 | + console.log('receive binary event, len=' + evt.data.length + '\n'); |
| 148 | + for (var i = 0; i < evt.data.length; i++) |
| 149 | + console.log(' 0x' + evt.data[i].toString(16)); |
| 150 | + console.log('\n'); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + console.log('receive text event len=' + evt.data.length + ': "' + evt.data + '"\n'); |
| 155 | + } |
| 156 | + //ws.send(evt.data); |
| 157 | + }; |
| 158 | + |
| 159 | + ws.onclose = function() |
| 160 | + { |
| 161 | + console.log('ws closed\n'); |
| 162 | + ws = undefined; |
| 163 | + }; |
| 164 | + |
| 165 | + ws.onerror = function() |
| 166 | + { |
| 167 | + alert('No connection to ' + document.getElementById('hostname').value); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +function disconnect() |
| 172 | +{ |
| 173 | + console.log('disconnect: ' + document.getElementById('hostname').value); |
| 174 | + ws = undefined; |
| 175 | +} |
| 176 | + |
| 177 | +</script> |
| 178 | +</head> |
| 179 | + |
| 180 | +<body onload="start()"> |
| 181 | + |
| 182 | +<textarea id="hostname" rows="1" cols="64">ws://firmata.local:3031/this-text-is/parsable</textarea> |
| 183 | +<br> |
| 184 | +<button onclick="connect()">Connect</button> |
| 185 | +<button onclick="disconnect()">Disconnect</button> |
| 186 | +<br> |
| 187 | +<button onclick="digitalWrite(0,0)">digitalWrite(0,0)</button> |
| 188 | +<button onclick="digitalWrite(0,1)">digitalWrite(0,1)</button> |
| 189 | +<br> |
| 190 | +<button onclick="digitalWrite(1,0)">digitalWrite(1,0)</button> |
| 191 | +<button onclick="digitalWrite(1,1)">digitalWrite(1,1)</button> |
| 192 | +<br> |
| 193 | +<button onclick="digitalWrite(2,0)">digitalWrite(2,0)</button> |
| 194 | +<button onclick="digitalWrite(2,1)">digitalWrite(2,1)</button> |
| 195 | +<button id="dr2" onclick="digitalRead(2)">digitalRead(2)</button> |
| 196 | +<br> |
| 197 | +<button onclick="digitalWrite(3,0)">digitalWrite(3,0)</button> |
| 198 | +<button onclick="digitalWrite(3,1)">digitalWrite(3,1)</button> |
| 199 | +<br> |
| 200 | +<button onclick="digitalWrite(4,0)">digitalWrite(4,0)</button> |
| 201 | +<button onclick="digitalWrite(4,1)">digitalWrite(4,1)</button> |
| 202 | + |
| 203 | +</body> |
| 204 | +</html> |
0 commit comments