2021-10-05 01:19:24 +03:00
|
|
|
function ledTest() {
|
|
|
|
forever(() => {
|
|
|
|
const max = 0x40
|
|
|
|
const step = 1
|
|
|
|
for (let ch = 0; ch < 3; ++ch) {
|
|
|
|
for (let i = 0; i < max; i += step) {
|
|
|
|
jacdac.setLedChannel(ch, i << 8)
|
|
|
|
pause(10)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < max; i += step) {
|
|
|
|
jacdac.setLedChannel(ch, (max - i) << 8)
|
|
|
|
pause(10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:30:50 +03:00
|
|
|
function jdpackTest() {
|
2021-09-10 02:46:42 +03:00
|
|
|
function testOne(
|
|
|
|
fmt: string,
|
|
|
|
data0: any[],
|
|
|
|
options?: {
|
|
|
|
expectedPayload?: string
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
const expectedPayload = options ? options.expectedPayload : undefined
|
2020-12-09 13:30:50 +03:00
|
|
|
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-09-10 02:46:42 +03:00
|
|
|
const bufHex = buf.toHex()
|
2021-06-19 03:20:03 +03:00
|
|
|
console.log(
|
2021-09-10 02:46:42 +03:00
|
|
|
`${JSON.stringify(data0)}->${fmt}->${bufHex}->${JSON.stringify(
|
2021-06-19 03:20:03 +03:00
|
|
|
data1
|
|
|
|
)}`
|
|
|
|
)
|
2021-09-10 02:46:42 +03:00
|
|
|
if (expectedPayload !== undefined && expectedPayload !== bufHex)
|
|
|
|
throw `jdpack test error: payload ${bufHex}, exected ${expectedPayload}`
|
|
|
|
|
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"]])
|
2021-09-10 02:46:42 +03:00
|
|
|
testOne(
|
|
|
|
"b[8] u32 u8 s",
|
|
|
|
[hex`a1b2c3d4e5f6a7b8`, 0x12345678, 0x42, "barbaz"],
|
|
|
|
{ expectedPayload: "a1b2c3d4e5f6a7b8785634124262617262617a" }
|
|
|
|
)
|
2022-04-19 08:22:07 +03:00
|
|
|
testOne("r: u16", [[[0], [1]]])
|
|
|
|
testOne("u16 r: u16", [1, [[2], [3]]])
|
2020-12-09 13:30:50 +03:00
|
|
|
}
|
2021-02-10 05:56:59 +03:00
|
|
|
|
2020-12-08 15:09:10 +03:00
|
|
|
// pins.A9.digitalWrite(false)
|
2021-02-06 03:53:55 +03:00
|
|
|
|
2021-06-23 02:35:14 +03:00
|
|
|
jacdac.logPriority = ConsolePriority.Log
|
2021-04-21 17:09:39 +03:00
|
|
|
jacdac.roleManagerServer.start()
|
2021-04-08 11:10:21 +03:00
|
|
|
jacdac.protoTestServer.start()
|
2022-04-27 17:17:18 +03:00
|
|
|
|
|
|
|
jacdac.createSimpleSensorServer(0x16c810b8, "u22.10", () => 66, {
|
|
|
|
instanceName: "humi",
|
|
|
|
readingError: () => 1
|
|
|
|
}).start()
|
|
|
|
jacdac.createSimpleSensorServer(0x14ad1a5d, "u0.16", () => {
|
|
|
|
console.log(`read sound level`)
|
|
|
|
return 0.5 + Math.random() / 20
|
|
|
|
}, {
|
|
|
|
instanceName: "slvl",
|
|
|
|
enabled: false,
|
|
|
|
streamingInterval: 50
|
|
|
|
}).start()
|
|
|
|
|
2020-12-08 15:09:10 +03:00
|
|
|
jacdac.start()
|
2021-04-21 17:09:39 +03:00
|
|
|
jacdac.loggerServer.log("test started")
|
2021-02-11 14:40:22 +03:00
|
|
|
|
|
|
|
jdpackTest()
|
2021-02-06 03:53:55 +03:00
|
|
|
|
2021-02-10 05:56:59 +03:00
|
|
|
function addClient(cls: number, name: string) {
|
2021-02-06 03:53:55 +03:00
|
|
|
console.log(`client: ${name} (${cls})`)
|
2021-02-10 05:56:59 +03:00
|
|
|
new jacdac.Client(cls, name).start()
|
2021-02-06 03:53:55 +03:00
|
|
|
}
|
2022-04-25 20:15:52 +03:00
|
|
|
addClient(jacdac.SRV_PROTO_TEST, "prototest?dev=self")
|
2021-02-10 05:56:59 +03:00
|
|
|
addClient(0x1f140409, "left_leg/acc1")
|
|
|
|
addClient(0x1473a263, "btn1")
|
|
|
|
addClient(0x1421bac7, "small/temp")
|
|
|
|
addClient(0x169c9dc6, "big/eco2")
|
2021-05-04 16:28:27 +03:00
|
|
|
addClient(0x16c810b8, "big/hum")
|
2021-02-10 05:56:59 +03:00
|
|
|
addClient(0x1421bac7, "big/temp")
|
2021-05-04 16:28:27 +03:00
|
|
|
addClient(0x16c810b8, "xsmall/hum")
|
2021-02-10 05:56:59 +03:00
|
|
|
addClient(0x1421bac7, "xsmall/temp")
|
2022-04-25 20:15:52 +03:00
|
|
|
addClient(0x1f37c56a, "screen1?dev=self&srvo=1")
|
|
|
|
addClient(0x1f37c56a, "screen2?dev=self&rows=4&columns=8")
|
2022-04-28 07:27:05 +03:00
|
|
|
addClient(0x1609d4f0, "led stick?variant=Stick&num_pixels=8")
|
|
|
|
addClient(0x1609d4f0, "led matrix?variant=Matrix&num_pixels=12&num_columns=4")
|
|
|
|
addClient(0x1609d4f0, "led fixed strip?variant=Strip&num_pixels=64")
|
2021-02-06 03:53:55 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 22:30:00 +03:00
|
|
|
const humiditySmallHum = new modules.HumidityClient("small/hum")
|
|
|
|
humiditySmallHum.onHumidityChangedBy(1, () => {
|
2021-09-15 23:10:41 +03:00
|
|
|
console.log(`humidity changed start (this is a log message)`)
|
|
|
|
pause(3000)
|
|
|
|
console.log(`humidity changed end (this is a log message)`)
|
2021-09-15 22:30:00 +03:00
|
|
|
})
|
2021-03-11 17:39:53 +03:00
|
|
|
forever(() => {
|
2021-09-15 22:30:00 +03:00
|
|
|
const h = humiditySmallHum.humidity()
|
2021-06-19 03:20:03 +03:00
|
|
|
// console.debug(`debug message`)
|
2021-09-15 22:30:00 +03:00
|
|
|
console.log(`humidity: ${h} (this is a log message)`)
|
|
|
|
pause(3000)
|
2021-03-11 17:39:53 +03:00
|
|
|
})
|