-
-
-
First off thanks for the pattern launcher @crazysaem , the title of this thread is very fitting!
I may have some detection improvement, based on some testing in the web ide. At the very least it reduces the code a bit.const CIRCLE_RADIUS = 25; const CIRCLE_RADIUS_2 = Math.pow(CIRCLE_RADIUS, 2); let positions = []; const getPattern = positions => { let circles = [ {x: 25, y: 25, i: 0}, {x: 87, y: 25, i: 1}, {x: 150, y: 25, i: 2}, {x: 25, y: 87, i: 3}, {x: 87, y: 87, i: 4}, {x: 150, y: 87, i: 5}, {x: 25, y: 150, i: 6}, {x: 87, y: 150, i: 7}, {x: 150, y: 150, i: 8} ]; return positions.reduce((pattern, p, i, arr) => { const idx = circles.findIndex(c => { const dx = p.x > c.x ? p.x - c.x : c.x - p.x; if (dx > CIRCLE_RADIUS) { return false; } const dy = p.y > c.y ? p.y - c.y : c.y - p.y; if (dy > CIRCLE_RADIUS) { return false; } if (dx + dy <= CIRCLE_RADIUS) { return true; } return dx * dx + dy * dy <= CIRCLE_RADIUS_2; }); if (idx >= 0) { pattern += circles[idx].i; circles.splice(idx, 1); } if (circles.length === 0) { arr.splice(1); } return pattern; }, ''); }; const dragHandler = position => { positions.push(position); if (position.b === 0 || positions.length >= 200) { console.log('pattern', getPattern(positions)); positions = []; } }; Bangle.on("drag", dragHandler);
-
Here is a semi complete map poly of a older project of mine.
'use strict'; class Map { constructor (keyVal) { this.clear(); if (Array.isArray(keyVal)) { keyVal.forEach(kv => this._keys.push(kv[0]) && this._values.push(kv[1])); } } get size () { return this._keys.length; } has (key) { return this._keys.includes(key); } get (key) { return this._values[this._keys.indexOf(key)]; } set (key, value) { let i = this._keys.indexOf(key); if (i !== -1) { this._values[i] = value; } else { this._keys.push(key); this._values.push(value); } } delete (key) { let i = this._keys.indexOf(key); if (i === -1) { return false; } this._keys.splice(i, 1); this._values.splice(i, 1); } keys () { return this._keys; } values () { return this._values; } clear () { this._keys = []; this._values = []; } entries () { return this._iterator(); } _iterator () { let i = 0; return { next: () => i < this._keys.length ? {value: [this._keys[i], this._values[i++]], done: false} : {done: true} }; } forEach (callback, scope = null) { this._keys.forEach(key => callback.call(scope, this.get(key), key, this)); } }
Amazingly close to what @Gordon already had :D
-
Well sorry.. its a quick hack. Its a function to run promise chains in sequence with optional mapper functions for the resolves. And the resulting resolve returns the changed map (key: original promise, value: resolve result of that promise) so you can reference results by promises you did feed the function.
I guess it doesnt help that code with 120 chars per line (pretty common standard) is getting wrapped in posts here.
--- Little warning ---
The code has bugs.. it was a first draft of something i needed at work the next day.. so dont use it for production :D -
I guess you are right @Gordon in terms of performance impact. But RAM is still an issue i guess because its very precious :)
I have a sweet little example for the Map vs. Object point @opichals made before.
Should be self-explanatory.'use strict'; let sequence = (...map) => (ok, nok) => { map = new Map(map.map(e => Array.isArray(e) ? e : [e])); ok(Array.from(map.keys()).reduce((all, prm) => { return all.then(res => prm.then(data => res.set(prm, (map.get(prm) || (() => data))(data))).catch(nok)); }, Promise.resolve(map))); }; let add = num => num + 1; let mul = num => num * 2; let addPromise = num => Promise.resolve(num); let mulPromise = num => Promise.resolve(num); let adding = addPromise(10); let multing = mulPromise(20); let map = [adding,[multing, mul]]; new Promise(sequence(...map)).then(result => { console.log('result', {addResult: result.get(adding), mulResult: result.get(multing)}); }).catch(console.log.bind(null, 'error'));
-
-
-
-
-
-
I miss destructuring and rest + spread in the list. Or are they implemented already and i missed it?
Thanks for giving the community this much influence it is really appreciated!
-
-
-
Try to replace
this.w(0xFF, 0x01); this.w(0x00, 0x00); this.w(0xFF, 0x00); this.w(0x09, 0x00); this.w(0x10, 0x00); this.w(0x11, 0x00); this.w(0x24, 0x01); this.w(0x25, 0xFF); this.w(0x75, 0x00); this.w(0xFF, 0x01); this.w(0x4E, 0x2C); this.w(0x48, 0x00); this.w(0x30, 0x20); this.w(0xFF, 0x00); this.w(0x30, 0x09); this.w(0x54, 0x00); this.w(0x31, 0x04); this.w(0x32, 0x03); this.w(0x40, 0x83); this.w(0x46, 0x25); this.w(0x60, 0x00); this.w(0x27, 0x00); this.w(0x50, 0x06); this.w(0x51, 0x00); this.w(0x52, 0x96); this.w(0x56, 0x08); this.w(0x57, 0x30); this.w(0x61, 0x00); this.w(0x62, 0x00); this.w(0x64, 0x00); this.w(0x65, 0x00); this.w(0x66, 0xA0); this.w(0xFF, 0x01); this.w(0x22, 0x32); this.w(0x47, 0x14); this.w(0x49, 0xFF); this.w(0x4A, 0x00); this.w(0xFF, 0x00); this.w(0x7A, 0x0A); this.w(0x7B, 0x00); this.w(0x78, 0x21); this.w(0xFF, 0x01); this.w(0x23, 0x34); this.w(0x42, 0x00); this.w(0x44, 0xFF); this.w(0x45, 0x26); this.w(0x46, 0x05); this.w(0x40, 0x40); this.w(0x0E, 0x06); this.w(0x20, 0x1A); this.w(0x43, 0x40); this.w(0xFF, 0x00); this.w(0x34, 0x03); this.w(0x35, 0x44); this.w(0xFF, 0x01); this.w(0x31, 0x04); this.w(0x4B, 0x09); this.w(0x4C, 0x05); this.w(0x4D, 0x04); this.w(0xFF, 0x00); this.w(0x44, 0x00); this.w(0x45, 0x20); this.w(0x47, 0x08); this.w(0x48, 0x28); this.w(0x67, 0x00); this.w(0x70, 0x04); this.w(0x71, 0x01); this.w(0x72, 0xFE); this.w(0x76, 0x00); this.w(0x77, 0x00); this.w(0xFF, 0x01); this.w(0x0D, 0x01); this.w(0xFF, 0x00); this.w(0x80, 0x01); this.w(0x01, 0xF8); this.w(0xFF, 0x01); this.w(0x8E, 0x01); this.w(0x00, 0x01); this.w(0xFF, 0x00); this.w(0x80, 0x00);
With
this.i2c.writeTo(this.ad, new Uint8Array([ 0xFF, 0x01, 0x00, 0x00, 0xFF, 0x00, 0x09, 0x00, 0x10, 0x00, 0x11, 0x00, 0x24, 0x01, 0x25, 0xFF, 0x75, 0x00, 0xFF, 0x01, 0x4E, 0x2C, 0x48, 0x00, 0x30, 0x20, 0xFF, 0x00, 0x30, 0x09, 0x54, 0x00, 0x31, 0x04, 0x32, 0x03, 0x40, 0x83, 0x46, 0x25, 0x60, 0x00, 0x27, 0x00, 0x50, 0x06, 0x51, 0x00, 0x52, 0x96, 0x56, 0x08, 0x57, 0x30, 0x61, 0x00, 0x62, 0x00, 0x64, 0x00, 0x65, 0x00, 0x66, 0xA0, 0xFF, 0x01, 0x22, 0x32, 0x47, 0x14, 0x49, 0xFF, 0x4A, 0x00, 0xFF, 0x00, 0x7A, 0x0A, 0x7B, 0x00, 0x78, 0x21, 0xFF, 0x01, 0x23, 0x34, 0x42, 0x00, 0x44, 0xFF, 0x45, 0x26, 0x46, 0x05, 0x40, 0x40, 0x0E, 0x06, 0x20, 0x1A, 0x43, 0x40, 0xFF, 0x00, 0x34, 0x03, 0x35, 0x44, 0xFF, 0x01, 0x31, 0x04, 0x4B, 0x09, 0x4C, 0x05, 0x4D, 0x04, 0xFF, 0x00, 0x44, 0x00, 0x45, 0x20, 0x47, 0x08, 0x48, 0x28, 0x67, 0x00, 0x70, 0x04, 0x71, 0x01, 0x72, 0xFE, 0x76, 0x00, 0x77, 0x00, 0xFF, 0x01, 0x0D, 0x01, 0xFF, 0x00, 0x80, 0x01, 0x01, 0xF8, 0xFF, 0x01, 0x8E, 0x01, 0x00, 0x01, 0xFF, 0x00, 0x80, 0x00 ]));
Or even
this.i2c.writeTo(this.ad, atob("/wEAAP8ACQAQABEAJAEl/3UA/wFOLEgAMCD/ADAJVAAxBDIDQINGJWAAJwBQBlEAUpZWCFcwYQBiAGQAZQBmoP8BIjJHFEn/SgD/AHoKewB4If8BIzRCAET/RSZGBUBADgYgGkNA/wA0AzVE/wExBEsJTAVNBP8ARABFIEcISChnAHAEcQFy/nYAdwD/AQ0B/wCAAQH4/wGOAQAB/wCAAA=="));
-
-
-
-
-
-
-
I finally had some time to look into the VCNL4200 code. According to the datasheet i found the module should not work.
You are writing to the command register at 0x80 but i would expect it to be at 0x00. Apart from that and what gordon mentioned about the usability aspect it looks good to me.I got my VL53L0X today so i could help work on that if you like.
-
-
Yep you are right.. haven't looked into the code. All ideas that come to mind now are dirty hacks.
Like overwriting Bangle.on to watch for touch event listseners and only if no pattern is matched call the other listeners. But i am not sure if this is even allowed/possible with the API.