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

Function xs_FT6202_read

modules/drivers/ft6206/modFt6206.c:168–269  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

166}
167
168void xs_FT6202_read(xsMachine *the)
169{
170 ft6206 ft = xsmcGetHostData(xsThis);
171 uint8_t data[32], err;
172 uint8_t count, maxID = 0, i;
173
174 xsmcVars(2);
175
176 data[0] = FT6206_REG_NUMTOUCHES;
177 err = modI2CWrite(&ft->i2c, data, 1, false);
178 if (err) xsUnknownError("write FT6206_REG_NUMTOUCHES ");
179 modI2CRead(&ft->i2c, data, 1, true);
180 count = data[0] & 0x0F;
181
182 xsmcGet(xsVar(0), xsArg(0), xsID_length);
183 maxID = xsmcToInteger(xsVar(0)) - 1;
184
185 xsmcSetInteger(xsVar(0), 0);
186 for (i = 0; i <= maxID; i++) {
187 xsmcGetIndex(xsVar(1), xsArg(0), i);
188 xsmcSet(xsVar(1), xsID_state, xsVar(0));
189 }
190
191 if ((1 != count) && (2 != count))
192 return;
193
194 data[0] = 3; // x registers followed by y registers, etc.
195 modI2CWrite(&ft->i2c, data, 1, false);
196 modI2CRead(&ft->i2c, data, count * 6, true);
197
198 for (i = 0; i < count; i++) {
199 uint8_t id = data[(i * 6) + 2] >> 4;
200 uint8_t event = data[(i * 6) + 0] >> 6, state = 0;
201 int16_t x = ((data[(i * 6) + 0] & 0x0F) << 8) | data[(i * 6) + 1];
202 int16_t y = ((data[(i * 6) + 2] & 0x0F) << 8) | data[(i * 6) + 3];
203
204 if (id > maxID)
205 continue;
206
207 if (0 == event) // down
208 state = 1;
209 else if (2 == event) // contact
210 state = 2;
211 else if (1 == event) // lift (never happens?)
212 state = 3;
213 else
214 continue;
215
216 // reflect
217 if (MODDEF_FT6206_FLIPX)
218 x = MODDEF_FT6206_WIDTH - x;
219
220 if (MODDEF_FT6206_FLIPY)
221 y = MODDEF_FT6206_HEIGHT - y;
222
223 // scale
224 if (MODDEF_FT6206_FITX && (240 != MODDEF_FT6206_WIDTH))
225 x = (x * MODDEF_FT6206_WIDTH) / 240;

Callers

nothing calls this directly

Calls 2

modI2CWriteFunction · 0.50
modI2CReadFunction · 0.50

Tested by

no test coverage detected