|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2018 Moddable Tech, Inc. |
| 3 | + * |
| 4 | + * This file is part of the Moddable SDK Runtime. |
| 5 | + * |
| 6 | + * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "xsmc.h" |
| 22 | +#ifdef gecko |
| 23 | + #include "xsPlatform.h" |
| 24 | + #include "xsgecko.h" |
| 25 | +#else |
| 26 | + #include "xsesp.h" |
| 27 | +#endif |
| 28 | +#include "mc.xs.h" // for xsID_ values |
| 29 | + |
| 30 | +#include "modSerial.h" |
| 31 | + |
| 32 | +/* |
| 33 | + Serial |
| 34 | +*/ |
| 35 | + |
| 36 | +void xs_serial(xsMachine *the) |
| 37 | +{ |
| 38 | + modSerialConfiguration serial; |
| 39 | + int baudrate, rx, tx = 0; |
| 40 | + |
| 41 | + xsmcVars(1); |
| 42 | + xsmcGet(xsVar(0), xsArg(0), xsID_baudrate); |
| 43 | + baudrate = (xsUndefinedType == xsmcTypeOf(xsVar(0))) ? -1 : xsmcToInteger(xsVar(0)); |
| 44 | + xsmcGet(xsVar(0), xsArg(0), xsID_rx); |
| 45 | + rx = (xsUndefinedType == xsmcTypeOf(xsVar(0))) ? -1 : xsmcToInteger(xsVar(0)); |
| 46 | + xsmcGet(xsVar(0), xsArg(0), xsID_tx); |
| 47 | + tx = xsmcToInteger(xsVar(0)); |
| 48 | + |
| 49 | + serial = xsmcSetHostChunk(xsThis, NULL, sizeof(modSerialConfigurationRecord)); |
| 50 | + serial->baudrate = baudrate; |
| 51 | + serial->rx = rx; |
| 52 | + serial->tx = tx; |
| 53 | + modSerialInit(serial); |
| 54 | +} |
| 55 | + |
| 56 | +void xs_serial_destructor(void *data) |
| 57 | +{ |
| 58 | + if (data) |
| 59 | + modSerialUninit((modSerialConfiguration)data); |
| 60 | +} |
| 61 | + |
| 62 | +void xs_serial_close(xsMachine *the) |
| 63 | +{ |
| 64 | + modSerialConfiguration serial = xsmcGetHostChunk(xsThis); |
| 65 | + if (serial) { |
| 66 | + modSerialUninit(serial); |
| 67 | + xsmcSetHostData(xsThis, NULL); // this clears the host chunk allocated |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void xs_serial_read(xsMachine *the) |
| 72 | +{ |
| 73 | + modSerialConfiguration serial; |
| 74 | + int argc = xsmcArgc; |
| 75 | + unsigned int len = xsmcToInteger(xsArg(0)), i; |
| 76 | + unsigned char err; |
| 77 | + unsigned char buffer[40]; |
| 78 | + |
| 79 | + if (len > sizeof(buffer)) |
| 80 | + xsUnknownError("40 byte read limit"); |
| 81 | + |
| 82 | + serial = xsmcGetHostChunk(xsThis); |
| 83 | + err = modSerialRead(serial, buffer, len); |
| 84 | + if (err) |
| 85 | + return; // undefined returned on read failure |
| 86 | + |
| 87 | + if (argc >= 2) { |
| 88 | + int bufferByteLength; |
| 89 | + xsResult = xsArg(1); |
| 90 | + bufferByteLength = xsGetArrayBufferLength(xsResult); |
| 91 | + if (bufferByteLength < len) |
| 92 | + xsUnknownError("buffer too small"); |
| 93 | + c_memmove(xsmcToArrayBuffer(xsResult), buffer, len); |
| 94 | + |
| 95 | + } |
| 96 | + else { |
| 97 | + xsResult = xsArrayBuffer(buffer, len); |
| 98 | + xsResult = xsNew1(xsGlobal, xsID_Uint8Array, xsResult); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void xs_serial_write(xsMachine *the) |
| 103 | +{ |
| 104 | + modSerialConfiguration serial; |
| 105 | + uint8_t argc = xsmcArgc, i; |
| 106 | + unsigned char err; |
| 107 | + uint8_t stop = true; |
| 108 | + unsigned int len = 0; |
| 109 | + unsigned char buffer[40]; |
| 110 | + |
| 111 | + xsmcVars(1); |
| 112 | + |
| 113 | + for (i = 0; i < argc; i++) { |
| 114 | + xsType t = xsmcTypeOf(xsArg(i)); |
| 115 | + if ((xsBooleanType == t) && ((argc - 1) == i)) { |
| 116 | + stop = xsmcToBoolean(xsArg(i)); |
| 117 | + continue; |
| 118 | + } |
| 119 | + if ((xsNumberType == t) || (xsIntegerType == t)) { |
| 120 | + if ((len + 1) > sizeof(buffer)) |
| 121 | + xsUnknownError("40 byte write limit"); |
| 122 | + buffer[len++] = (unsigned char)xsmcToInteger(xsArg(i)); |
| 123 | + continue; |
| 124 | + } |
| 125 | + if (xsStringType == t) { |
| 126 | + char *s = xsmcToString(xsArg(i)); |
| 127 | + int l = c_strlen(s); |
| 128 | + if ((len + l) > sizeof(buffer)) |
| 129 | + xsUnknownError("40 byte write limit"); |
| 130 | + c_memcpy(buffer + len, s, l); |
| 131 | + len += l; |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + { // assume some kind of array (Array, Uint8Array, etc) (@@ use .buffer if present) |
| 136 | + int l; |
| 137 | + uint8_t j; |
| 138 | + |
| 139 | + xsmcGet(xsVar(0), xsArg(i), xsID_length); |
| 140 | + l = xsmcToInteger(xsVar(0)); |
| 141 | + if ((len + l) > sizeof(buffer)) |
| 142 | + xsUnknownError("40 byte write limit"); |
| 143 | + for (j = 0; j < l; j++) { |
| 144 | + xsmcGet(xsVar(0), xsArg(i), j); |
| 145 | + buffer[len++] = xsmcToInteger(xsVar(0)); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + serial = xsmcGetHostChunk(xsThis); |
| 151 | + err = modSerialWrite(serial, buffer); |
| 152 | + if (err) |
| 153 | + xsUnknownError("write failed"); |
| 154 | +} |
| 155 | + |
0 commit comments