From da3c20d2511b65695e469743db3600cb2de430e4 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Mon, 8 Jan 2024 17:03:41 +0000 Subject: [PATCH] Added Pin.pulse/pwm/analog as aliases of digitalPulse,analogWrite,analogRead --- ChangeLog | 1 + src/jswrap_pin.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++-- src/jswrap_pin.h | 3 +++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 37145d95fa..ad9eb7344f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -46,6 +46,7 @@ Added console.debug/info/warn/error as aliases of console.log nRF5x: Fix INVALID_PARAM error when connecting to other BLE device when maxInterval or NRF.setLowPowerConnection specified Bangle.js2: Add 'Test' option to factory reset menu, and Bangle.showTestScreen() + Added Pin.pulse/pwm/analog as aliases of digitalPulse,analogWrite,analogRead 2v19 : Fix Object.values/entries for numeric keys after 2v18 regression (fix #2375) nRF52: for SD>5 use static buffers for advertising and scan response data (#2367) diff --git a/src/jswrap_pin.c b/src/jswrap_pin.c index b406090f48..c1e8544a53 100644 --- a/src/jswrap_pin.c +++ b/src/jswrap_pin.c @@ -18,7 +18,7 @@ #include "jstimer.h" /* We use sortorder ensure sort position for Pin class *and at least one non-static member function* - is before that of 'Number', so that jswGetBasicObjectName/jswFindBuiltInFunction/jswGetSymbolListForObjectProto + is before that of 'Number', so that jswGetBasicObjectName/jswFindBuiltInFunction/jswGetSymbolListForObjectProto check first (Pin is also Numeric) */ @@ -164,7 +164,7 @@ void jswrap_pin_writeAtTime(JsVar *parent, bool value, JsVarFloat time) { Return the current mode of the given pin. See `pinMode` for more information. */ JsVar *jswrap_pin_getMode(JsVar *parent) { - return jswrap_io_getPinMode(jshGetPinFromVar(parent)); + return jswrap_io_getPinMode(jshGetPinFromVar(parent)); } /*JSON{ @@ -205,6 +205,68 @@ bool jswrap_pin_toggle(JsVar *parent) { return on; } +/*JSON{ + "type" : "method", + "class" : "Pin", + "name" : "pulse", + "generate" : "jswrap_pin_pulse", + "params" : [ + ["value","bool","Whether to pulse high (true) or low (false)"], + ["time","JsVar","A time in milliseconds, or an array of times (in which case a square wave will be output starting with a pulse of 'value')"] + ] +} +(Added in 2v20) Pulse the pin with the value for the given time in milliseconds. + +``` +LED.pulse(1, 100); // pulse LED on for 100ms +LED.pulse(1, [100,1000,100]); // pulse LED on for 100ms, off for 1s, on for 100ms +``` + +This is identical to `digitalPulse`. + */ +void jswrap_pin_pulse(JsVar *parent, bool value, JsVar *times) { + jswrap_io_digitalPulse(jshGetPinFromVar(parent), value, times); +} + +/*JSON{ + "type" : "method", + "class" : "Pin", + "name" : "analog", + "generate" : "jswrap_pin_analog", + "return" : ["float","The analog Value of the Pin between 0 and 1"] +} +(Added in 2v20) Get the analogue value of the given pin. See `analogRead` for more information. + */ +JsVarFloat jswrap_pin_analog(JsVar *parent) { + return jshPinAnalog(jshGetPinFromVar(parent)); +} + +/*JSON{ + "type" : "method", + "class" : "Pin", + "name" : "pwm", + "generate" : "jswrap_pin_pwm", + "params" : [ + ["value","float","A value between 0 and 1"], + ["options","JsVar",["An object containing options for analog output - see below"]] + ] +} +(Added in 2v20) Set the analog Value of a pin. It will be output using PWM. + +See `analogWrite` for more information. + +Objects can contain: + +* `freq` - pulse frequency in Hz, e.g. ```analogWrite(A0,0.5,{ freq : 10 });``` - + specifying a frequency will force PWM output, even if the pin has a DAC +* `soft` - boolean, If true software PWM is used if hardware is not available. +* `forceSoft` - boolean, If true software PWM is used even if hardware PWM or a + DAC is available + + */ +void jswrap_pin_pwm(JsVar *parent, JsVarFloat value, JsVar *options) { + jswrap_io_analogWrite(jshGetPinFromVar(parent), value, options); +} /*JSON{ "type" : "method", diff --git a/src/jswrap_pin.h b/src/jswrap_pin.h index 67d29adc4b..dfcec2375a 100644 --- a/src/jswrap_pin.h +++ b/src/jswrap_pin.h @@ -26,6 +26,9 @@ void jswrap_pin_writeAtTime(JsVar *parent, bool value, JsVarFloat time); JsVar *jswrap_pin_getMode(JsVar *parent); void jswrap_pin_mode(JsVar *parent, JsVar *mode); bool jswrap_pin_toggle(JsVar *parent); +void jswrap_pin_pulse(JsVar *parent, bool value, JsVar *times); +JsVarFloat jswrap_pin_analog(JsVar *parent); +void jswrap_pin_pwm(JsVar *parent, JsVarFloat value, JsVar *options); JsVar *jswrap_pin_getInfo(JsVar *parent); #endif // JSWRAP_PIN_H_