From 41d50525833a6f081b5bbd97516ddc0239d1e2b2 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 15 Dec 2017 11:54:53 +0000 Subject: [PATCH 1/2] Run sensor "change handler" also with the initial value --- libs/core/input.ts | 3 ++- libs/core/linux.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index 05f39e89..20b1dd67 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -13,6 +13,7 @@ namespace sensors.internal { control.runInBackground(() => { let prev = query() + changeHandler(prev, prev) while (true) { loops.pause(periodMs) let curr = query() @@ -183,7 +184,7 @@ namespace sensors.internal { Low = 3, } - export class ThresholdDetector { + export class ThresholdDetector { public id: number; private min: number; private max: number; diff --git a/libs/core/linux.cpp b/libs/core/linux.cpp index 5e7bcfa4..391f7373 100644 --- a/libs/core/linux.cpp +++ b/libs/core/linux.cpp @@ -397,6 +397,10 @@ static void runPoller(Thread *thr) { // note that this is run without the user mutex held - it should not modify any state! TValue prev = pxt::runAction0(query); + startUser(); + pxt::runAction2(thr->act, prev, prev); + stopUser(); + while (true) { sleep_core_us(us); if (paniced) From c7a3f5bbd0360ea694bfc5087e128b40349baa76 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 15 Dec 2017 11:55:20 +0000 Subject: [PATCH 2/2] Delay user code by 100ms to get sensor reading (fixes #90) --- libs/ev3/pxt.json | 3 ++- libs/ev3/startup.ts | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 libs/ev3/startup.ts diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index 61752d25..5a3c7634 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -3,7 +3,8 @@ "description": "The EV3 library", "files": [ "README.md", - "ns.ts" + "ns.ts", + "startup.ts" ], "dependencies": { "base": "file:../base", diff --git a/libs/ev3/startup.ts b/libs/ev3/startup.ts new file mode 100644 index 00000000..3dcce976 --- /dev/null +++ b/libs/ev3/startup.ts @@ -0,0 +1,4 @@ +// This is the last thing executed before user code + +// We pause for 100ms to give time to read sensor values, so they work in on_start block +loops.pause(100)