moving neopixel state to pxt
This commit is contained in:
parent
72a621ec8b
commit
e59ae37954
@ -4,54 +4,10 @@ namespace pxsim {
|
|||||||
if (b) {
|
if (b) {
|
||||||
let np = b.neopixelState;
|
let np = b.neopixelState;
|
||||||
if (np) {
|
if (np) {
|
||||||
np.updateBuffer(buffer, pin);
|
let buf = <Uint8Array[]>(<any>buffer).data;
|
||||||
|
np.updateBuffer(buf, pin);
|
||||||
runtime.queueDisplayUpdate();
|
runtime.queueDisplayUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace pxsim {
|
|
||||||
export enum NeoPixelMode {RGB, RGBW};
|
|
||||||
export type RGBW = [number, number, number, number];
|
|
||||||
|
|
||||||
function readNeoPixelBuffer(inBuffer: Uint8Array[], outColors: RGBW[], mode: NeoPixelMode) {
|
|
||||||
let buf = inBuffer;
|
|
||||||
let stride = mode === NeoPixelMode.RGBW ? 4 : 3;
|
|
||||||
let pixelCount = Math.floor(buf.length / stride);
|
|
||||||
for (let i = 0; i < pixelCount; i++) {
|
|
||||||
// NOTE: for whatever reason, NeoPixels pack GRB not RGB
|
|
||||||
let r = buf[i * stride + 1] as any as number
|
|
||||||
let g = buf[i * stride + 0] as any as number
|
|
||||||
let b = buf[i * stride + 2] as any as number
|
|
||||||
let w = 0;
|
|
||||||
if (stride === 4)
|
|
||||||
w = buf[i * stride + 3] as any as number
|
|
||||||
outColors[i] = [r, g, b, w]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export class NeoPixelState {
|
|
||||||
private buffers: {[pin: number]: Uint8Array[]} = {};
|
|
||||||
private colors: {[pin: number]: RGBW[]} = {};
|
|
||||||
private dirty: {[pin: number]: boolean} = {};
|
|
||||||
|
|
||||||
public updateBuffer(buffer: Buffer, pin: DigitalPin) {
|
|
||||||
//update buffers
|
|
||||||
let buf = <Uint8Array[]>(<any>buffer).data;
|
|
||||||
this.buffers[pin] = buf;
|
|
||||||
this.dirty[pin] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getColors(pin: number, mode: NeoPixelMode): RGBW[] {
|
|
||||||
let outColors = this.colors[pin] || (this.colors[pin] = []);
|
|
||||||
if (this.dirty[pin]) {
|
|
||||||
let buf = this.buffers[pin] || (this.buffers[pin] = []);
|
|
||||||
readNeoPixelBuffer(buf, outColors, mode);
|
|
||||||
this.dirty[pin] = false;
|
|
||||||
}
|
|
||||||
return outColors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,43 +3,6 @@
|
|||||||
/// <reference path="../../libs/microbit/shims.d.ts"/>
|
/// <reference path="../../libs/microbit/shims.d.ts"/>
|
||||||
/// <reference path="../../libs/microbit/enums.d.ts"/>
|
/// <reference path="../../libs/microbit/enums.d.ts"/>
|
||||||
|
|
||||||
//TODO move to utils
|
|
||||||
namespace pxsim.visuals {
|
|
||||||
//expects rgb from 0,255, gives h in [0,360], s in [0, 100], l in [0, 100]
|
|
||||||
export function rgbToHsl(rgb: [number, number, number]): [number, number, number] {
|
|
||||||
let [r, g, b] = rgb;
|
|
||||||
let [r$, g$, b$] = [r / 255, g / 255, b / 255];
|
|
||||||
let cMin = Math.min(r$, g$, b$);
|
|
||||||
let cMax = Math.max(r$, g$, b$);
|
|
||||||
let cDelta = cMax - cMin;
|
|
||||||
let h: number, s: number, l: number;
|
|
||||||
let maxAndMin = cMax + cMin;
|
|
||||||
|
|
||||||
//lum
|
|
||||||
l = (maxAndMin / 2) * 100
|
|
||||||
|
|
||||||
if (cDelta === 0)
|
|
||||||
s = h = 0;
|
|
||||||
else {
|
|
||||||
//hue
|
|
||||||
if (cMax === r$)
|
|
||||||
h = 60 * (((g$ - b$) / cDelta) % 6);
|
|
||||||
else if (cMax === g$)
|
|
||||||
h = 60 * (((b$ - r$) / cDelta) + 2);
|
|
||||||
else if (cMax === b$)
|
|
||||||
h = 60 * (((r$ - g$) / cDelta) + 4);
|
|
||||||
|
|
||||||
//sat
|
|
||||||
if (l > 50)
|
|
||||||
s = 100 * (cDelta / (2 - maxAndMin));
|
|
||||||
else
|
|
||||||
s = 100 * (cDelta / maxAndMin);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [Math.floor(h), Math.floor(s), Math.floor(l)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace pxsim.visuals {
|
namespace pxsim.visuals {
|
||||||
const PIXEL_SPACING = PIN_DIST * 3;
|
const PIXEL_SPACING = PIN_DIST * 3;
|
||||||
const PIXEL_RADIUS = PIN_DIST;
|
const PIXEL_RADIUS = PIN_DIST;
|
||||||
@ -115,7 +78,7 @@ namespace pxsim.visuals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public setRgb(rgb: [number, number, number]) {
|
public setRgb(rgb: [number, number, number]) {
|
||||||
let hsl = rgbToHsl(rgb);
|
let hsl = visuals.rgbToHsl(rgb);
|
||||||
let [h, s, l] = hsl;
|
let [h, s, l] = hsl;
|
||||||
// at least 70% luminosity
|
// at least 70% luminosity
|
||||||
l = Math.max(l, 60);
|
l = Math.max(l, 60);
|
||||||
|
Loading…
Reference in New Issue
Block a user