MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / xs_i2c_write

Function xs_i2c_write

modules/pins/i2c/i2c.c:122–185  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120}
121
122void xs_i2c_write(xsMachine *the)
123{
124 modI2C i2c;
125 uint8_t argc = xsmcArgc, i;
126 unsigned char err;
127 uint8_t stop = true;
128 unsigned int len = 0;
129 unsigned char buffer[40];
130
131 xsmcVars(1);
132
133 for (i = 0; i < argc; i++) {
134 xsType t = xsmcTypeOf(xsArg(i));
135 if ((xsBooleanType == t) && ((argc - 1) == i)) {
136 stop = xsmcToBoolean(xsArg(i));
137 continue;
138 }
139 if ((xsNumberType == t) || (xsIntegerType == t)) {
140 if ((len + 1) > sizeof(buffer))
141 xsUnknownError("40 byte write limit");
142 buffer[len++] = (unsigned char)xsmcToInteger(xsArg(i));
143 continue;
144 }
145 if (xsStringType == t) {
146 char *s = xsmcToString(xsArg(i));
147 int l = c_strlen(s);
148 if ((len + l) > sizeof(buffer))
149 xsUnknownError("40 byte write limit");
150 c_memcpy(buffer + len, s, l);
151 len += l;
152 continue;
153 }
154 if (xsmcIsInstanceOf(xsArg(i), xsArrayBufferPrototype)) {
155 int l = xsmcGetArrayBufferLength(xsArg(i));
156 if ((len + l) > sizeof(buffer))
157 xsUnknownError("40 byte write limit");
158 c_memmove(buffer + len, xsmcToArrayBuffer(xsArg(i)), l);
159 len += l;
160 continue;
161 }
162
163 { // assume some kind of array (Array, Uint8Array, etc) (@@ use .buffer if present)
164 int l;
165 uint8_t j;
166
167 xsmcGet(xsVar(0), xsArg(i), xsID_length);
168 l = xsmcToInteger(xsVar(0));
169 if ((len + l) > sizeof(buffer))
170 xsUnknownError("40 byte write limit");
171 for (j = 0; j < l; j++) {
172 xsmcGetIndex(xsVar(0), xsArg(i), j);
173 buffer[len++] = xsmcToInteger(xsVar(0));
174 }
175 }
176 }
177
178 i2c = xsmcGetHostChunk(xsThis);
179 err = modI2CWrite(&i2c->state, buffer, len, stop);

Callers

nothing calls this directly

Calls 1

modI2CWriteFunction · 0.50

Tested by

no test coverage detected