From 11c5231f2dd95979e92ec45b8fdad301398c9a58 Mon Sep 17 00:00:00 2001 From: ArthurZheng <869705086@qq.com> Date: Thu, 20 Jun 2024 23:13:55 +0800 Subject: [PATCH] Fixed the issue of analog pin parsing error on control boards using firmata protocol. --- src/devices/arduinoEsp8266/arduinoEsp8266.js | 2 ++ src/devices/arduinoLeonardo/arduinoLeonardo.js | 2 ++ src/devices/arduinoMega2560/arduinoMega2560.js | 2 ++ src/devices/arduinoUno/arduinoUno.js | 2 ++ src/devices/common/arduino-peripheral.js | 4 ++-- 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/devices/arduinoEsp8266/arduinoEsp8266.js b/src/devices/arduinoEsp8266/arduinoEsp8266.js index 3e190ebd1b0..1957a8425c8 100644 --- a/src/devices/arduinoEsp8266/arduinoEsp8266.js +++ b/src/devices/arduinoEsp8266/arduinoEsp8266.js @@ -548,6 +548,8 @@ class OpenBlockArduinoEsp8266Device { // Create a new Arduino esp8266 peripheral instance this._peripheral = new ArduinoEsp8266(this.runtime, this.DEVICE_ID, originalDeviceId); + + this._peripheral.numDigitalPins = 17; } /** diff --git a/src/devices/arduinoLeonardo/arduinoLeonardo.js b/src/devices/arduinoLeonardo/arduinoLeonardo.js index 9c809660c7a..1d52663a7df 100644 --- a/src/devices/arduinoLeonardo/arduinoLeonardo.js +++ b/src/devices/arduinoLeonardo/arduinoLeonardo.js @@ -474,6 +474,8 @@ class OpenBlockArduinoLeonardoDevice { // Create a new Arduino leonardo peripheral instance this._peripheral = new ArduinoLeonardo(this.runtime, this.DEVICE_ID, originalDeviceId); + + this._peripheral.numDigitalPins = 14; } /** diff --git a/src/devices/arduinoMega2560/arduinoMega2560.js b/src/devices/arduinoMega2560/arduinoMega2560.js index bce282025f6..deea756e989 100644 --- a/src/devices/arduinoMega2560/arduinoMega2560.js +++ b/src/devices/arduinoMega2560/arduinoMega2560.js @@ -841,6 +841,8 @@ class OpenBlockArduinoMega2560Device { // Create a new Arduino mega 2560 peripheral instance this._peripheral = new ArduinoMega2560(this.runtime, this.DEVICE_ID, originalDeviceId); + + this._peripheral.numDigitalPins = 54; } /** diff --git a/src/devices/arduinoUno/arduinoUno.js b/src/devices/arduinoUno/arduinoUno.js index 1f13e30e158..157a59550f0 100644 --- a/src/devices/arduinoUno/arduinoUno.js +++ b/src/devices/arduinoUno/arduinoUno.js @@ -469,6 +469,8 @@ class OpenBlockArduinoUnoDevice { // Create a new Arduino uno peripheral instance this._peripheral = new ArduinoUno(this.runtime, this.DEVICE_ID, originalDeviceId); + + this._peripheral.numDigitalPins = 14; } /** diff --git a/src/devices/common/arduino-peripheral.js b/src/devices/common/arduino-peripheral.js index adc1ad91cef..9472de069f5 100644 --- a/src/devices/common/arduino-peripheral.js +++ b/src/devices/common/arduino-peripheral.js @@ -444,7 +444,7 @@ class ArduinoPeripheral{ */ parsePin (pin) { if (pin.charAt(0) === 'A') { - return parseInt(pin.slice(1), 10) + 14; + return parseInt(pin.slice(1), 10) + this.numDigitalPins; } return parseInt(pin, 10); } @@ -538,7 +538,7 @@ class ArduinoPeripheral{ if (this.isReady()) { pin = this.parsePin(pin); // Shifting to analog pin number. - pin = pin - 14; + pin = pin - this.numDigitalPins; this._firmata.pinMode(pin, this._firmata.MODES.ANALOG); return new Promise(resolve => { this._firmata.analogRead(pin, value => {