pxt-jacdac/test.ts

221 строка
6.5 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
2021-06-19 03:20:03 +03:00
throw `jdpack test error: ${msg} (at ${fmt}; a=${JSON.stringify(
a
)}; b=${JSON.stringify(b)})`
2020-12-09 13:30:50 +03:00
}
2021-06-19 03:20:03 +03:00
if (a === b || JSON.stringify(a) == JSON.stringify(b)) return
2020-12-09 13:30:50 +03:00
fail("not the same")
}
const buf = jacdac.jdpack(fmt, data0)
const data1 = jacdac.jdunpack(buf, fmt)
2021-06-19 03:20:03 +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"])
2021-06-19 03:20:03 +03:00
testOne("u8 r: u8 z", [
42,
[
[17, "xy"],
[18, "xx"],
],
])
2020-12-09 13:30:50 +03:00
//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])
2021-06-19 03:20:03 +03:00
testOne("i8 x[4] s[9] u16 x[2] s[10] x[3] u8", [
-100,
"foo",
1000,
"barbaz",
250,
])
2020-12-09 13:30:50 +03:00
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)
2021-06-19 03:20:03 +03:00
jacdac.logPriority = jacdac.LoggerPriority.Log
2021-04-21 17:09:39 +03:00
jacdac.roleManagerServer.start()
2021-04-08 11:10:21 +03:00
jacdac.protoTestServer.start()
jacdac.start()
2021-04-21 17:09:39 +03:00
jacdac.loggerServer.log("test started")
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-05-04 16:28:27 +03:00
addClient(0x16c810b8, "small/hum")
addClient(0x1421bac7, "small/temp")
addClient(0x169c9dc6, "big/eco2")
2021-05-04 16:28:27 +03:00
addClient(0x16c810b8, "big/hum")
addClient(0x1421bac7, "big/temp")
2021-05-04 16:28:27 +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 {
2021-06-19 03:20:03 +03:00
private readonly _humidityError: jacdac.RegisterClient<[number]>
private readonly _minHumidity: jacdac.RegisterClient<[number]>
private readonly _maxHumidity: jacdac.RegisterClient<[number]>
2021-03-11 17:39:53 +03:00
constructor(role: string) {
2021-06-19 03:20:03 +03:00
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"
)
2021-03-11 17:39:53 +03:00
}
/**
2021-06-19 03:20:03 +03:00
* The relative humidity in percentage of full water saturation.
*/
2021-03-11 17:39:53 +03:00
//% callInDebugger
//% group="Environment"
//% block="%humidity humidity"
//% blockId=jacdac_humidity_humidity___get
//% weight=100
humidity(): number {
2021-06-19 03:20:03 +03:00
return this.reading()
2021-03-11 17:39:53 +03:00
}
/**
2021-06-19 03:20:03 +03:00
* The real humidity is between `humidity - humidity_error` and `humidity + humidity_error`.
*/
2021-03-11 17:39:53 +03:00
//% callInDebugger
//% group="Environment"
//% weight=99
humidityError(): number {
2021-06-19 03:20:03 +03:00
this.start()
const values = this._humidityError.pauseUntilValues() as any[]
return values[0]
2021-03-11 17:39:53 +03:00
}
/**
2021-06-19 03:20:03 +03:00
* Lowest humidity that can be reported.
*/
2021-03-11 17:39:53 +03:00
//% callInDebugger
//% group="Environment"
//% weight=98
minHumidity(): number {
2021-06-19 03:20:03 +03:00
this.start()
const values = this._minHumidity.pauseUntilValues() as any[]
return values[0]
2021-03-11 17:39:53 +03:00
}
/**
2021-06-19 03:20:03 +03:00
* Highest humidity that can be reported.
*/
2021-03-11 17:39:53 +03:00
//% callInDebugger
//% group="Environment"
//% weight=97
maxHumidity(): number {
2021-06-19 03:20:03 +03:00
this.start()
const values = this._maxHumidity.pauseUntilValues() as any[]
return values[0]
2021-03-11 17:39:53 +03:00
}
/**
* Run code when the humidity changes by the given threshold value.
2021-06-19 03:20:03 +03:00
*/
2021-03-11 17:39:53 +03:00
//% 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 {
2021-06-19 03:20:03 +03:00
this.onReadingChangedBy(threshold, handler)
2021-03-11 17:39:53 +03:00
}
}
//% fixedInstance whenUsed
2021-06-19 03:20:03 +03:00
export const humidity = new HumidityClient("humidity")
2021-03-11 17:39:53 +03:00
}
modules.humidity.start()
forever(() => {
const h = modules.humidity.humidity()
2021-06-19 03:20:03 +03:00
// console.debug(`debug message`)
// console.log(`humidity: ${h} (this is a log message)`)
2021-05-18 01:14:21 +03:00
pause(1000)
2021-03-11 17:39:53 +03:00
})