pxt-jacdac/test.ts

201 строка
6.2 KiB
TypeScript
Исходник Обычный вид История

2020-12-09 13:30:50 +03:00
function jdpackTest() {
function testOne(fmt: string, data0: any[]) {
function checksame(a: any, b: any) {
function fail(msg: string): never {
debugger
throw (`jdpack test error: ${msg} (at ${fmt}; a=${JSON.stringify(a)}; b=${JSON.stringify(b)})`)
}
if (a === b || JSON.stringify(a) == JSON.stringify(b))
return
fail("not the same")
}
const buf = jacdac.jdpack(fmt, data0)
const data1 = jacdac.jdunpack(buf, fmt)
2021-02-11 14:40:22 +03:00
console.log(`${JSON.stringify(data0)}->${fmt}->${buf.toHex()}->${JSON.stringify(data1)}`)
2020-12-09 13:30:50 +03:00
// console.log(fmt, data0, data1, toHex(buf))
checksame(data0, data1)
}
2021-02-11 14:40:22 +03:00
testOne("i8", [-42])
testOne("u16", [42])
2020-12-09 13:30:50 +03:00
testOne("u16 u16 i16", [42, 77, -10])
testOne("u16 z s", [42, "foo", "bar"])
testOne("u32 z s", [42, "foo", "bar"])
testOne("i8 z s", [42, "foo", "bar"])
testOne("u8 z s", [42, "foo12", "bar"])
testOne("u8 r: u8 z", [42, [[17, "xy"], [18, "xx"]]])
//testOne("z b", ["foo12", jacdac.stringToBuffer("bar")])
testOne("u16 r: u16", [42, [[17], [18]]])
testOne("i8 s[9] u16 s[10] u8", [-100, "foo", 1000, "barbaz", 250])
testOne("i8 x[4] s[9] u16 x[2] s[10] x[3] u8", [-100, "foo", 1000, "barbaz", 250])
testOne("u16 u16[]", [42, [17, 18]])
testOne("u16 u16[]", [42, [18]])
testOne("u16 u16[]", [42, []])
testOne("u16 z[]", [42, ["foo", "bar", "bz"]])
}
// pins.A9.digitalWrite(false)
2020-12-17 00:36:59 +03:00
jacdac.consolePriority = ConsolePriority.Log;
2020-12-09 13:30:50 +03:00
jacdac.roleManagerHost.start()
2020-12-08 20:24:03 +03:00
jacdac.protoTestHost.start()
jacdac.start()
2020-12-09 13:30:50 +03:00
jacdac.loggerHost.log("test started")
2021-02-25 14:41:03 +03:00
modules.identify();
2021-02-11 14:40:22 +03:00
jdpackTest()
function addClient(cls: number, name: string) {
console.log(`client: ${name} (${cls})`)
new jacdac.Client(cls, name).start()
}
addClient(0x1f140409, "left_leg/acc1")
addClient(0x1473a263, "btn1")
2021-03-11 17:39:53 +03:00
//addClient(0x16c810b8, "small/hum")
addClient(0x1421bac7, "small/temp")
addClient(0x169c9dc6, "big/eco2")
2021-03-11 17:39:53 +03:00
//addClient(0x16c810b8, "big/hum")
addClient(0x1421bac7, "big/temp")
2021-03-11 17:39:53 +03:00
//addClient(0x16c810b8, "xsmall/hum")
addClient(0x1421bac7, "xsmall/temp")
jacdac._rolemgr.clearRoles()
2021-03-11 17:39:53 +03:00
namespace jacdac {
// Service: Humidity
export const SRV_HUMIDITY = 0x16c810b8
export const enum HumidityReg {
/**
* Read-only %RH u22.10 (uint32_t). The relative humidity in percentage of full water saturation.
*
* ```
* const [humidity] = jdunpack<[number]>(buf, "u22.10")
* ```
*/
Humidity = 0x101,
/**
* Read-only %RH u22.10 (uint32_t). The real humidity is between `humidity - humidity_error` and `humidity + humidity_error`.
*
* ```
* const [humidityError] = jdunpack<[number]>(buf, "u22.10")
* ```
*/
HumidityError = 0x106,
/**
* Constant °C u22.10 (uint32_t). Lowest humidity that can be reported.
*
* ```
* const [minHumidity] = jdunpack<[number]>(buf, "u22.10")
* ```
*/
MinHumidity = 0x104,
/**
* Constant °C u22.10 (uint32_t). Highest humidity that can be reported.
*
* ```
* const [maxHumidity] = jdunpack<[number]>(buf, "u22.10")
* ```
*/
MaxHumidity = 0x105,
}
}
namespace modules {
/**
* A sensor measuring humidity of outside environment.
**/
//% fixedInstances blockGap=8
export class HumidityClient extends jacdac.SimpleSensorClient {
private readonly _humidityError : jacdac.RegisterClient<[number]>;
private readonly _minHumidity : jacdac.RegisterClient<[number]>;
private readonly _maxHumidity : jacdac.RegisterClient<[number]>;
constructor(role: string) {
super(jacdac.SRV_HUMIDITY, role, "u22.10");
this._humidityError = this.addRegister<[number]>(jacdac.HumidityReg.HumidityError, "u22.10");
this._minHumidity = this.addRegister<[number]>(jacdac.HumidityReg.MinHumidity, "u22.10");
this._maxHumidity = this.addRegister<[number]>(jacdac.HumidityReg.MaxHumidity, "u22.10");
}
/**
* The relative humidity in percentage of full water saturation.
*/
//% callInDebugger
//% group="Environment"
//% block="%humidity humidity"
//% blockId=jacdac_humidity_humidity___get
//% weight=100
humidity(): number {
return this.reading();
}
/**
* The real humidity is between `humidity - humidity_error` and `humidity + humidity_error`.
*/
//% callInDebugger
//% group="Environment"
//% weight=99
humidityError(): number {
this.start();
const values = this._humidityError.pauseUntilValues() as any[];
return values[0];
}
/**
* Lowest humidity that can be reported.
*/
//% callInDebugger
//% group="Environment"
//% weight=98
minHumidity(): number {
this.start();
const values = this._minHumidity.pauseUntilValues() as any[];
return values[0];
}
/**
* Highest humidity that can be reported.
*/
//% callInDebugger
//% group="Environment"
//% weight=97
maxHumidity(): number {
this.start();
const values = this._maxHumidity.pauseUntilValues() as any[];
return values[0];
}
/**
* Run code when the humidity changes by the given threshold value.
*/
//% group="Environment"
//% blockId=jacdac_humidity_on_humidity_change
//% block="on %humidity humidity changed by %threshold"
//% weight=96
//% threshold.defl=1
onHumidityChangedBy(threshold: number, handler: () => void): void {
this.onReadingChangedBy(threshold, handler);
}
}
//% fixedInstance whenUsed
export const humidity = new HumidityClient("humidity");
}
modules.humidity.start()
forever(() => {
const h = modules.humidity.humidity()
console.log(`humidity: ${h}`)
2021-03-11 17:47:38 +03:00
pause(100)
2021-03-11 17:39:53 +03:00
})