Allow digitalWrite/Read to take an object as an argument (calling .write and .read on it)

This commit is contained in:
Gordon Williams 2017-10-23 10:53:20 +01:00
parent afec9617cb
commit 1ccea9ee4a
3 changed files with 41 additions and 14 deletions

View File

@ -23,6 +23,7 @@
Ensure Ctrl-C doesn't redraw the input line if it's already empty
Added String.replace(regex, function) (fix #1256)
With E.setFlags({pretokenise:1}), ensure stack traces are not tokenised (fix #1258)
Allow digitalWrite/Read to take an object as an argument (calling .write and .read on it)
1v94 : Allow Espruino boards to reset straight out of the DFU Bootloader
Improvements in handling errors in code running in IRQs

View File

@ -253,11 +253,10 @@ as an array of bits where the last array element is the least significant bit.
In this case, pin values are set least significant bit first (from the right-hand side
of the array of pins). This means you can use the same pin multiple times, for
example `digitalWrite([A1,A1,A0,A0],0b0101)` would pulse A0 followed by A1.
*/
/**
* Set the output of a GPIO.
*/
If the pin argument is an object with a `write` method, the `write` method will
be called with the value passed through.
*/
void jswrap_io_digitalWrite(
JsVar *pinVar, //!< A pin or pins.
JsVarInt value //!< The value of the output.
@ -274,9 +273,16 @@ void jswrap_io_digitalWrite(
jsvUnLock(pinNamePtr);
value = value>>1; // next bit down
}
}
// Handle the case where it is a single pin.
else {
} else if (jsvIsObject(pinVar)) {
JsVar *w = jspGetNamedField(pinVar, "write", false);
if (jsvIsFunction(w)) {
JsVar *v = jsvNewFromInteger(value);
jsvUnLock(jspeFunctionCall(w,0,pinVar,false,1,&v));
jsvUnLock(v);
} else jsExceptionHere(JSET_ERROR, "Invalid pin!");
jsvUnLock(w);
} else {
// Handle the case where it is a single pin.
Pin pin = jshGetPinFromVar(pinVar);
jshPinOutput(pin, value != 0);
}
@ -298,11 +304,10 @@ Get the digital value of the given pin.
If the pin argument is an array of pins (eg. `[A2,A1,A0]`) the value returned will be an number where
the last array element is the least significant bit, for example if `A0=A1=1` and `A2=0`, `digitalRead([A2,A1,A0]) == 0b011`
*/
/**
* Read the value of a GPIO pin.
*/
If the pin argument is an object with a `read` method, the `read` method will be called and the integer value it returns
passed back.
*/
JsVarInt jswrap_io_digitalRead(JsVar *pinVar) {
// Hadnle the case where it is an array of pins.
if (jsvIsArray(pinVar)) {
@ -320,9 +325,16 @@ JsVarInt jswrap_io_digitalRead(JsVar *pinVar) {
jsvObjectIteratorFree(&it);
if (pins==0) return 0; // return undefined if array empty
return value;
}
// Handle the case where it is a single pin.
else {
} else if (jsvIsObject(pinVar)) {
JsVarInt v = 0;
JsVar *r = jspGetNamedField(pinVar, "read", false);
if (jsvIsFunction(r)) {
v = jsvGetIntegerAndUnLock(jspeFunctionCall(r,0,pinVar,false,0,0));
} else jsExceptionHere(JSET_ERROR, "Invalid pin!");
jsvUnLock(r);
return v;
} else {
// Handle the case where it is a single pin.
Pin pin = jshGetPinFromVar(pinVar);
return jshPinInput(pin);
}

View File

@ -0,0 +1,14 @@
var r = "";
var v = 0;
var pin = {
something : "foo",
read : function() { r+="r"+this.something; v=!v; return v; },
write : function(v) { r+="w"+this.something+v },
};
r += digitalRead(pin);
r += digitalRead(pin);
digitalWrite(pin,1);
digitalWrite(pin,0);
result = r=="rfoo1rfoo0wfoo1wfoo0";