diff --git a/lib/A4988_Stepper/README.adoc b/lib/A4988_Stepper/README.adoc new file mode 100755 index 000000000..0cac353f3 --- /dev/null +++ b/lib/A4988_Stepper/README.adoc @@ -0,0 +1,19 @@ +Stepper Library for Tasmota + +This Class allows you to control bipolar stepper motors. To use it you will need an A4988-StepperDriverCircuit, connected at least with 2 GPIO's (direction and step) and of cause a stepper motor. + +== License == + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/lib/A4988_Stepper/keywords.txt b/lib/A4988_Stepper/keywords.txt new file mode 100755 index 000000000..c83465c8b --- /dev/null +++ b/lib/A4988_Stepper/keywords.txt @@ -0,0 +1,24 @@ +####################################### +# Syntax Coloring Map For Test +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +A4988_Stepper KEYWORD1 A4988_Stepper + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +doMove KEYWORD2 +doRotate KEYWORD2 +setRPM KEYWORD2 +setSPR KEYWORD2 +setMIS KEYWORD2 +version KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/lib/A4988_Stepper/library.properties b/lib/A4988_Stepper/library.properties new file mode 100755 index 000000000..2e6b38bf9 --- /dev/null +++ b/lib/A4988_Stepper/library.properties @@ -0,0 +1,9 @@ +name=A4988_Stepper +version=0.0.1 +author=Tim Leuschner +maintainer=Tim Leuschner +sentence=Allows Tasmota to control stepper motors, connected to A4988-StepperDriverCircuit. +paragraph=This library allows you to control bipolar stepper motors, controlled by A4988-stepperDriverCircuit. +category=Device Control +url= +architectures=* diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp new file mode 100644 index 000000000..cbd72390a --- /dev/null +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -0,0 +1,155 @@ +/* + This library is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Drives a bipolar motor, controlled by A4988 stepper driver circuit + */ +// +#include "Arduino.h" +#include "A4988_Stepper.h" +A4988_Stepper::A4988_Stepper( int m_spr + , int m_rpm + , short m_mis + , short m_dir_pin + , short m_stp_pin + , short m_ena_pin + , short m_ms1_pin + , short m_ms2_pin + , short m_ms3_pin ) { + last_time = 0; // time stamp in us of the last step taken + motor_SPR = m_spr; // StepsPerRevolution + motor_RPM = m_rpm; // RoundsPerMinute + motor_MIS = m_mis; // Microsteps w/o effect if MS1-MS3 not connected - then full steps anyway + motor_dir_pin = m_dir_pin; + motor_stp_pin = m_stp_pin; + motor_ena_pin = m_ena_pin; + motor_ms1_pin = m_ms1_pin; + motor_ms2_pin = m_ms2_pin; + motor_ms3_pin = m_ms3_pin; + + adjustDelay(); + adjustPins(); + adjustMicrosteps(); +} + +void A4988_Stepper::adjustPins(void) { + // setup the pins on the microcontroller: + pinMode(motor_dir_pin, OUTPUT); + pinMode(motor_stp_pin, OUTPUT); + if (motor_ena_pin <99) { + pinMode(motor_ena_pin, OUTPUT); + digitalWrite(motor_ena_pin, HIGH); + } + + if ((motor_ms1_pin<99)&&(motor_ms2_pin<99)&&(motor_ms3_pin<99)) { + pinMode(motor_ms1_pin, OUTPUT); + pinMode(motor_ms2_pin, OUTPUT); + pinMode(motor_ms3_pin, OUTPUT); + } +} + +void A4988_Stepper::adjustMicrosteps() { + if ((motor_ms1_pin<99)&&(motor_ms2_pin<99)&&(motor_ms3_pin<99)) { + unsigned short i = 0; + while (i < 5){ + if (motor_MIS & (1<0?LOW:HIGH); + enable(); + while (steps_togo > 0) { + delay(0); // don't get watchdoged in loop + unsigned long now = micros(); + // move if delay has passed: + if (now - last_time >= motor_delay) { + digitalWrite(motor_stp_pin, lastStepWasHigh?LOW:HIGH); + lastStepWasHigh = !lastStepWasHigh; + // remeber step-time + last_time = now; + if (!lastStepWasHigh) steps_togo--; // same here - only HIGH moves, if pulled LOW step is completed... + } + } + disable(); +} + +void A4988_Stepper::doRotate(long howManyDegrees) +{ long lSteps = 0; + lSteps = motor_SPR*motor_MIS*howManyDegrees/360; + doMove(lSteps); +} + +void A4988_Stepper::doTurn(float howManyTimes) +{ long lSteps = 0; + lSteps = howManyTimes*motor_SPR; + doMove(lSteps); +} + +int A4988_Stepper::version(void) +{ + return 1; +} diff --git a/lib/A4988_Stepper/src/A4988_Stepper.h b/lib/A4988_Stepper/src/A4988_Stepper.h new file mode 100644 index 000000000..a907adfb1 --- /dev/null +++ b/lib/A4988_Stepper/src/A4988_Stepper.h @@ -0,0 +1,73 @@ +/* + This library is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +#ifndef A4988_Stepper_h +#define A4988_Stepper_h + +class A4988_Stepper { + public: + // constructor: + A4988_Stepper( int motor_spr + , int motor_rpm + , short motor_mis + , short motor_dir_pin + , short motor_stp_pin + , short motor_ena_pin + , short motor_ms1_pin + , short motor_ms2_pin + , short motor_ms3_pin + ); + + void setRPM (int whatRPM ); + int getRPM (void ); + + void setMIS (short OneToSixteen); + short getMIS (void ); + + void setSPR (int howMany ); + int getSPR (void ); + + void doMove (long steps_to_move); + void doRotate(long degrs_to_turn); + void doTurn (float howManyTimes); + + void enable (void ); + void disable (void ); + + int version (void ); + const unsigned short MIS_TABLE[5] = {0b000,0b001,0b010,0b011,0b111}; + + private: + void adjustDelay(void); + void adjustPins(void); + void adjustMicrosteps(void); + unsigned long motor_delay; // delay between steps, in ms + int motor_SPR; // Steps Per Revolution + int motor_RPM; // Rounds Per Minute + short motor_MIS; // Micro Steps + + // motor pins: + short motor_dir_pin; + short motor_stp_pin; + short motor_ena_pin; + short motor_ms1_pin; + short motor_ms2_pin; + short motor_ms3_pin; + + unsigned long last_time; // timestamp of last pincycle of last step +}; + +#endif diff --git a/lib/TasmotaSerial-2.3.3/README.md b/lib/TasmotaSerial-2.3.4/README.md similarity index 100% rename from lib/TasmotaSerial-2.3.3/README.md rename to lib/TasmotaSerial-2.3.4/README.md diff --git a/lib/TasmotaSerial-2.3.3/examples/swsertest/swsertest.ino b/lib/TasmotaSerial-2.3.4/examples/swsertest/swsertest.ino similarity index 100% rename from lib/TasmotaSerial-2.3.3/examples/swsertest/swsertest.ino rename to lib/TasmotaSerial-2.3.4/examples/swsertest/swsertest.ino diff --git a/lib/TasmotaSerial-2.3.3/keywords.txt b/lib/TasmotaSerial-2.3.4/keywords.txt similarity index 100% rename from lib/TasmotaSerial-2.3.3/keywords.txt rename to lib/TasmotaSerial-2.3.4/keywords.txt diff --git a/lib/TasmotaSerial-2.3.3/library.json b/lib/TasmotaSerial-2.3.4/library.json similarity index 94% rename from lib/TasmotaSerial-2.3.3/library.json rename to lib/TasmotaSerial-2.3.4/library.json index 46a55b7dc..271deceb0 100644 --- a/lib/TasmotaSerial-2.3.3/library.json +++ b/lib/TasmotaSerial-2.3.4/library.json @@ -1,6 +1,6 @@ { "name": "TasmotaSerial", - "version": "2.3.3", + "version": "2.3.4", "keywords": [ "serial", "io", "TasmotaSerial" ], diff --git a/lib/TasmotaSerial-2.3.3/library.properties b/lib/TasmotaSerial-2.3.4/library.properties similarity index 94% rename from lib/TasmotaSerial-2.3.3/library.properties rename to lib/TasmotaSerial-2.3.4/library.properties index c99402e68..4dccb28ad 100644 --- a/lib/TasmotaSerial-2.3.3/library.properties +++ b/lib/TasmotaSerial-2.3.4/library.properties @@ -1,5 +1,5 @@ name=TasmotaSerial -version=2.3.3 +version=2.3.4 author=Theo Arends maintainer=Theo Arends sentence=Implementation of software serial with hardware serial fallback for ESP8266. diff --git a/lib/TasmotaSerial-2.3.3/src/TasmotaSerial.cpp b/lib/TasmotaSerial-2.3.4/src/TasmotaSerial.cpp similarity index 100% rename from lib/TasmotaSerial-2.3.3/src/TasmotaSerial.cpp rename to lib/TasmotaSerial-2.3.4/src/TasmotaSerial.cpp diff --git a/lib/TasmotaSerial-2.3.3/src/TasmotaSerial.h b/lib/TasmotaSerial-2.3.4/src/TasmotaSerial.h similarity index 100% rename from lib/TasmotaSerial-2.3.3/src/TasmotaSerial.h rename to lib/TasmotaSerial-2.3.4/src/TasmotaSerial.h diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 35b51bd1f..0aba9f63d 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -456,6 +456,18 @@ #define D_JSON_ZIGBEEZNPSENT "ZigbeeZNPSent" #define D_JSON_ZIGBEEZCLRECEIVED "ZigbeeZCLReceived" #define D_JSON_ZIGBEEZCLSENT "ZigbeeZCLSent" + + // Commands xdrv_25_A4988_Stepper.ino + #ifdef USE_A4988_Stepper + #define D_CMND_MOTOR "MOTOR" + #define D_JSON_MOTOR_MOVE "doMove" + #define D_JSON_MOTOR_ROTATE "doRotate" + #define D_JSON_MOTOR_TURN "doTurn" + #define D_JSON_MOTOR_SPR "setSPR" + #define D_JSON_MOTOR_RPM "setRPM" + #define D_JSON_MOTOR_MIS "setMIS" + #endif + /********************************************************************************************/ #define D_ASTERISK_PWD "****" diff --git a/sonoff/language/bg-BG.h b/sonoff/language/bg-BG.h index b3c9fdc52..a6d05efc5 100644 --- a/sonoff/language/bg-BG.h +++ b/sonoff/language/bg-BG.h @@ -597,6 +597,13 @@ #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" + // Units #define D_UNIT_AMPERE "A" #define D_UNIT_CENTIMETER "cm" diff --git a/sonoff/language/cs-CZ.h b/sonoff/language/cs-CZ.h index 2f5ce2b9c..264faf797 100644 --- a/sonoff/language/cs-CZ.h +++ b/sonoff/language/cs-CZ.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/de-DE.h b/sonoff/language/de-DE.h index b6bb4f205..d3f280ded 100644 --- a/sonoff/language/de-DE.h +++ b/sonoff/language/de-DE.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/el-GR.h b/sonoff/language/el-GR.h index 7c9f5d0fc..8f056dcdf 100644 --- a/sonoff/language/el-GR.h +++ b/sonoff/language/el-GR.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/en-GB.h b/sonoff/language/en-GB.h index b69a3a622..4cd665634 100644 --- a/sonoff/language/en-GB.h +++ b/sonoff/language/en-GB.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/es-ES.h b/sonoff/language/es-ES.h index d3218349a..0300d6f4d 100644 --- a/sonoff/language/es-ES.h +++ b/sonoff/language/es-ES.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/fr-FR.h b/sonoff/language/fr-FR.h index f4e66e6f3..28eb6a8a6 100644 --- a/sonoff/language/fr-FR.h +++ b/sonoff/language/fr-FR.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/he-HE.h b/sonoff/language/he-HE.h index 285496b1f..504614de4 100644 --- a/sonoff/language/he-HE.h +++ b/sonoff/language/he-HE.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/hu-HU.h b/sonoff/language/hu-HU.h index b9b787227..529bb8604 100644 --- a/sonoff/language/hu-HU.h +++ b/sonoff/language/hu-HU.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/it-IT.h b/sonoff/language/it-IT.h index d76460815..dc3d6c0dd 100644 --- a/sonoff/language/it-IT.h +++ b/sonoff/language/it-IT.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/ko-KO.h b/sonoff/language/ko-KO.h index e4488e72a..98cae588b 100644 --- a/sonoff/language/ko-KO.h +++ b/sonoff/language/ko-KO.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/nl-NL.h b/sonoff/language/nl-NL.h index 6c720d687..b0009f3e0 100644 --- a/sonoff/language/nl-NL.h +++ b/sonoff/language/nl-NL.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pl-PL.h b/sonoff/language/pl-PL.h index ea4038b52..b2d465288 100644 --- a/sonoff/language/pl-PL.h +++ b/sonoff/language/pl-PL.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pt-BR.h b/sonoff/language/pt-BR.h index 15ec61b9c..2025a665b 100644 --- a/sonoff/language/pt-BR.h +++ b/sonoff/language/pt-BR.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pt-PT.h b/sonoff/language/pt-PT.h index 8dc4b04ae..9313cf287 100644 --- a/sonoff/language/pt-PT.h +++ b/sonoff/language/pt-PT.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/ru-RU.h b/sonoff/language/ru-RU.h index d4eff4456..743e2bf9e 100644 --- a/sonoff/language/ru-RU.h +++ b/sonoff/language/ru-RU.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "А" diff --git a/sonoff/language/sk-SK.h b/sonoff/language/sk-SK.h index 2c880618c..9d99c63ee 100644 --- a/sonoff/language/sk-SK.h +++ b/sonoff/language/sk-SK.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/sv-SE.h b/sonoff/language/sv-SE.h index 10d209e95..33ebf9f3a 100644 --- a/sonoff/language/sv-SE.h +++ b/sonoff/language/sv-SE.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/tr-TR.h b/sonoff/language/tr-TR.h index 402baa968..ab9c9e11e 100755 --- a/sonoff/language/tr-TR.h +++ b/sonoff/language/tr-TR.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/uk-UK.h b/sonoff/language/uk-UK.h index ca4c09fdd..72c1c6144 100644 --- a/sonoff/language/uk-UK.h +++ b/sonoff/language/uk-UK.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "А" diff --git a/sonoff/language/zh-CN.h b/sonoff/language/zh-CN.h index 690ba880b..d0b6b7a5e 100644 --- a/sonoff/language/zh-CN.h +++ b/sonoff/language/zh-CN.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "安" diff --git a/sonoff/language/zh-TW.h b/sonoff/language/zh-TW.h index 9acef9180..6febe1a6b 100644 --- a/sonoff/language/zh-TW.h +++ b/sonoff/language/zh-TW.h @@ -596,6 +596,12 @@ #define D_SENSOR_IBEACON_RX "iBeacon RX" #define D_SENSOR_RDM6300_RX "RDM6300 RX" #define D_SENSOR_CC1101_CS "CC1101 CS" +#define D_SENSOR_A4988_DIR "A4988 DIR" +#define D_SENSOR_A4988_STP "A4988 STP" +#define D_SENSOR_A4988_ENA "A4988 ENA" +#define D_SENSOR_A4988_MS1 "A4988 MS1" +#define D_SENSOR_A4988_MS2 "A4988 MS2" +#define D_SENSOR_A4988_MS3 "A4988 MS3" // Units #define D_UNIT_AMPERE "安" diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index 3b4ac0e68..df8ecc06f 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -528,6 +528,7 @@ #define USE_SM16716 // Add support for SM16716 RGB LED controller (+0k7 code) //#define USE_HRE // Add support for Badger HR-E Water Meter (+1k4 code) +//#define USE_A4988_Stepper // Add support for A4988 stepper-motor-driver-circuit (+10k5 code) /*********************************************************************************************\ * Debug features diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h index 88f29ff5c..bf4f57c3e 100644 --- a/sonoff/sonoff_post.h +++ b/sonoff/sonoff_post.h @@ -258,6 +258,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code) #undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code) #undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code) +#undef USE_A4988_Stepper // Disable support for A4988_Stepper #undef DEBUG_THEO // Disable debug code #undef USE_DEBUG_DRIVER // Disable debug code #endif // FIRMWARE_CLASSIC @@ -387,6 +388,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code) #undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code) #undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code) +#undef USE_A4988_Stepper // Disable support for A4988_Stepper #undef DEBUG_THEO // Disable debug code #undef USE_DEBUG_DRIVER // Disable debug code @@ -481,6 +483,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code) #undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code) #undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code) +#undef USE_A4988_Stepper // Disable support for A4988_Stepper #undef DEBUG_THEO // Disable debug code #undef USE_DEBUG_DRIVER // Disable debug code #endif // FIRMWARE_BASIC @@ -562,6 +565,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_RF_SENSOR // Disable support for RF sensor receiver (434MHz or 868MHz) (+0k8 code) #undef USE_SM16716 // Disable support for SM16716 RGB LED controller (+0k7 code) #undef USE_HRE // Disable support for Badger HR-E Water Meter (+1k4 code) +#undef USE_A4988_Stepper // Disable support for A4988_Stepper #undef DEBUG_THEO // Disable debug code #undef USE_DEBUG_DRIVER // Disable debug code #endif // FIRMWARE_MINIMAL diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index cdfe75e23..aa0dfbe9a 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -194,6 +194,12 @@ enum UserSelectablePins { GPIO_RDM6300_RX, // RDM6300 RX GPIO_IBEACON_TX, // HM17 IBEACON TX GPIO_IBEACON_RX, // HM17 IBEACON RX + GPIO_A4988_DIR, // A4988 direction pin + GPIO_A4988_STP, // A4988 step pin + GPIO_A4988_ENA, // A4988 enabled pin + GPIO_A4988_MS1, // A4988 microstep pin1 + GPIO_A4988_MS2, // A4988 microstep pin2 + GPIO_A4988_MS3, // A4988 microstep pin3 GPIO_SENSOR_END }; // Programmer selectable GPIO functionality @@ -267,6 +273,7 @@ const char kSensorNames[] PROGMEM = D_SENSOR_ZIGBEE_TXD "|" D_SENSOR_ZIGBEE_RXD "|" D_SENSOR_RDM6300_RX "|" D_SENSOR_IBEACON_TX "|" D_SENSOR_IBEACON_RX "|" + D_SENSOR_A4988_DIR "|" D_SENSOR_A4988_STP "|" D_SENSOR_A4988_ENA "|" D_SENSOR_A4988_MS1 "|" D_SENSOR_A4988_MS2 "|" D_SENSOR_A4988_MS3 "|" ; // User selectable ADC0 functionality @@ -689,6 +696,15 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_HRE_CLOCK, GPIO_HRE_DATA, #endif +#ifdef USE_A4988_Stepper + GPIO_A4988_DIR, // A4988 direction pin + GPIO_A4988_STP, // A4988 step pin + // folowing are not mandatory + GPIO_A4988_ENA, // A4988 enabled pin + GPIO_A4988_MS1, // A4988 microstep pin1 + GPIO_A4988_MS2, // A4988 microstep pin2 + GPIO_A4988_MS3, // A4988 microstep pin3 +#endif }; const uint8_t kModuleNiceList[] PROGMEM = { diff --git a/sonoff/support_features.ino b/sonoff/support_features.ino index 491dbf5af..9f36d3349 100644 --- a/sonoff/support_features.ino +++ b/sonoff/support_features.ino @@ -441,11 +441,12 @@ void GetFeatures(void) #ifdef USE_SML_M feature5 |= 0x00000008; // xsns_53_sml.ino #endif - #ifdef USE_INA226 feature5 |= 0x00000010; // xsns_54_ina226.ino #endif -// feature5 |= 0x00000020; +#ifdef USE_A4988_Stepper + feature5 |= 0x00000020; // xdrv_25_A4988.ino +#endif // feature5 |= 0x00000040; // feature5 |= 0x00000080; diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index b5af691e1..89edd5981 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -696,7 +696,7 @@ void CmndMaxEnergyStart(void) void EnergyDrvInit(void) { energy_flg = ENERGY_NONE; - XnrgCall(FUNC_PRE_INIT); + XnrgCall(FUNC_PRE_INIT); // Find first energy driver } void EnergySnsInit(void) @@ -901,14 +901,14 @@ bool Xdrv03(uint8_t function) case FUNC_EVERY_250_MSECOND: XnrgCall(FUNC_EVERY_250_MSECOND); break; + case FUNC_SERIAL: + result = XnrgCall(FUNC_SERIAL); + break; #ifdef USE_ENERGY_MARGIN_DETECTION case FUNC_SET_POWER: Energy.power_steady_counter = 2; break; #endif // USE_ENERGY_MARGIN_DETECTION - case FUNC_SERIAL: - result = XnrgCall(FUNC_SERIAL); - break; case FUNC_COMMAND: result = DecodeCommand(kEnergyCommands, EnergyCommand); break; @@ -923,9 +923,6 @@ bool Xsns03(uint8_t function) if (energy_flg) { switch (function) { - case FUNC_INIT: - EnergySnsInit(); - break; case FUNC_EVERY_SECOND: #ifdef USE_ENERGY_MARGIN_DETECTION EnergyMarginCheck(); @@ -943,6 +940,9 @@ bool Xsns03(uint8_t function) case FUNC_SAVE_BEFORE_RESTART: EnergySaveState(); break; + case FUNC_INIT: + EnergySnsInit(); + break; } } return result; diff --git a/sonoff/xdrv_16_tuyamcu.ino b/sonoff/xdrv_16_tuyamcu.ino index 48e97334a..6d9b26367 100644 --- a/sonoff/xdrv_16_tuyamcu.ino +++ b/sonoff/xdrv_16_tuyamcu.ino @@ -21,7 +21,7 @@ #ifdef USE_TUYA_MCU #define XDRV_16 16 -#define XNRG_08 8 +#define XNRG_16 16 // Needs to be the last XNRG_xx #ifndef TUYA_DIMMER_ID #define TUYA_DIMMER_ID 0 @@ -372,6 +372,7 @@ void TuyaPacketProcess(void) } else if (Tuya.buffer[5] == 8) { // Long value packet + bool tuya_energy_enabled = (XNRG_16 == energy_flg); if (fnId == TUYA_MCU_FUNC_DIMMER) { AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), Tuya.buffer[13]); Tuya.new_dim = changeUIntScale((uint8_t) Tuya.buffer[13], 0, Settings.param[P_TUYA_DIMMER_MAX], 0, 100); @@ -384,13 +385,13 @@ void TuyaPacketProcess(void) } #ifdef USE_ENERGY_SENSOR - else if (fnId == TUYA_MCU_FUNC_VOLTAGE) { + else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_VOLTAGE) { Energy.voltage = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 10; AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Voltage=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13])); - } else if (fnId == TUYA_MCU_FUNC_CURRENT) { + } else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_CURRENT) { Energy.current = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 1000; AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Current=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13])); - } else if (fnId == TUYA_MCU_FUNC_POWER) { + } else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_POWER) { Energy.active_power = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 10; AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Active_Power=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13])); @@ -601,22 +602,20 @@ void TuyaSetWifiLed(void) * Energy Interface \*********************************************************************************************/ -int Xnrg08(uint8_t function) +bool Xnrg16(uint8_t function) { - int result = 0; + bool result = false; if (TUYA_DIMMER == my_module_type) { if (FUNC_PRE_INIT == function) { - if (!energy_flg) { - if (TuyaGetDpId(TUYA_MCU_FUNC_POWER) != 0) { - if (TuyaGetDpId(TUYA_MCU_FUNC_CURRENT) == 0) { - Energy.current_available = false; - } - if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) { - Energy.voltage_available = false; - } - energy_flg = XNRG_08; + if (TuyaGetDpId(TUYA_MCU_FUNC_POWER) != 0) { + if (TuyaGetDpId(TUYA_MCU_FUNC_CURRENT) == 0) { + Energy.current_available = false; } + if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) { + Energy.voltage_available = false; + } + energy_flg = XNRG_16; } } } diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino new file mode 100644 index 000000000..94dbd1bf7 --- /dev/null +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -0,0 +1,172 @@ + +/* + xdrv_25_A4988_Stepper.ino - A4988-StepMotorDriverCircuit- support for Sonoff-Tasmota + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifdef USE_A4988_Stepper +#include +#define XDRV_25 25 + +enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON, A4988_MOVE, A4988_ROTATE, A4988_TURN}; + +short A4988_dir_pin = pin[GPIO_MAX]; +short A4988_stp_pin = pin[GPIO_MAX]; +short A4988_ms1_pin = pin[GPIO_MAX]; +short A4988_ms2_pin = pin[GPIO_MAX]; +short A4988_ms3_pin = pin[GPIO_MAX]; +short A4988_ena_pin = pin[GPIO_MAX]; +int A4988_spr = 0; +float A4988_rpm = 0; +short A4988_mis = 0; + +A4988_Stepper* myA4988 = nullptr; + +void A4988Init(void) +{ + A4988_dir_pin = pin[GPIO_A4988_DIR]; + A4988_stp_pin = pin[GPIO_A4988_STP]; + A4988_ena_pin = pin[GPIO_A4988_ENA]; + A4988_ms1_pin = pin[GPIO_A4988_MS1]; + A4988_ms2_pin = pin[GPIO_A4988_MS2]; + A4988_ms3_pin = pin[GPIO_A4988_MS3]; + A4988_spr = 200; + A4988_rpm = 30; + A4988_mis = 1; + + myA4988 = new A4988_Stepper( A4988_spr + , A4988_rpm + , A4988_mis + , A4988_dir_pin + , A4988_stp_pin + , A4988_ena_pin + , A4988_ms1_pin + , A4988_ms2_pin + , A4988_ms3_pin ); +} + +const char kA4988Commands[] PROGMEM = "|" + "MOTOR"; + +void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR}; + +uint32_t MOTORCmndJson(void) +{ + // MOTOR {"doMove":200} + // MOTOR {"doRotate":360} + // MOTOR {"doTurn":1.0} + uint32_t returnValue =A4988_NO_JSON_COMMAND; + + char parm_uc[12]; + char dataBufUc[XdrvMailbox.data_len]; + UpperCase(dataBufUc, XdrvMailbox.data); + RemoveSpace(dataBufUc); + if (strlen(dataBufUc) < 8) { returnValue =A4988_INVALID_JSON; } + + DynamicJsonBuffer jsonBuf; + JsonObject &json = jsonBuf.parseObject(dataBufUc); + if (json.success()) { + UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_SPR)); + if (json.containsKey(parm_uc)){ + int howManySteps =strtoul(json[parm_uc],nullptr,10); + myA4988->setSPR(howManySteps); + returnValue = A4988_NO_ERROR; + } + UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_RPM)); + if (json.containsKey(parm_uc)){ + int howManyRounds =strtoul(json[parm_uc],nullptr,10); + myA4988->setRPM(howManyRounds); + returnValue = A4988_NO_ERROR; + } + UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_MIS)); + if (json.containsKey(parm_uc)){ + short oneToSixteen =strtoul(json[parm_uc],nullptr,10); + myA4988->setMIS(oneToSixteen); + returnValue = A4988_NO_ERROR; + } + UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_MOVE)); + if (json.containsKey(parm_uc)){ + long stepsPlease = strtoul(json[parm_uc],nullptr,10); + myA4988->doMove(stepsPlease); + returnValue = A4988_MOVE; + } + UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_ROTATE)); + if (json.containsKey(parm_uc)){ + long degrsPlease = strtoul(json[parm_uc],nullptr,10); + myA4988->doRotate(degrsPlease); + returnValue = A4988_ROTATE; + } + UpperCase_P(parm_uc, PSTR(D_JSON_MOTOR_TURN)); + if (json.containsKey(parm_uc)){ + float turnsPlease = strtod(json[parm_uc],nullptr); + myA4988->doTurn(turnsPlease); + returnValue = A4988_TURN; + } + } else returnValue =A4988_INVALID_JSON; + return returnValue; +} + +void CmndMOTOR(void){ + uint32_t error; + if (XdrvMailbox.data_len) { + if (strstr(XdrvMailbox.data, "}") == nullptr) { + error = A4988_NO_JSON_COMMAND; + } else { + error = MOTORCmndJson(); + } + } + A4988CmndResponse(error); +} + +void A4988CmndResponse(uint32_t error){ + switch (error) { + case A4988_NO_JSON_COMMAND: + ResponseCmndChar(PSTR("No command!")); + break; + case A4988_MOVE: + ResponseCmndChar(PSTR("Stepping!")); + break; + case A4988_ROTATE: + ResponseCmndChar(PSTR("Rotating!")); + break; + case A4988_TURN: + ResponseCmndChar(PSTR("Turning!")); + break; + default: // A4988_NO_ERROR + ResponseCmndDone(); + } + +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ +bool Xdrv25(uint8_t function) +{ + bool result = false; + if ((pin[GPIO_A4988_DIR] < 99) && (pin[GPIO_A4988_STP] < 99)) { + switch (function) { + case FUNC_INIT: + A4988Init(); + break; + case FUNC_COMMAND: + result = DecodeCommand(kA4988Commands, A4988Command); + break; + } + } + return result; +} + +#endif diff --git a/sonoff/xnrg_01_hlw8012.ino b/sonoff/xnrg_01_hlw8012.ino index befa9c305..7bf039999 100644 --- a/sonoff/xnrg_01_hlw8012.ino +++ b/sonoff/xnrg_01_hlw8012.ino @@ -249,34 +249,32 @@ void HlwSnsInit(void) void HlwDrvInit(void) { - if (!energy_flg) { - Hlw.model_type = 0; // HLW8012 - if (pin[GPIO_HJL_CF] < 99) { - pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF]; - pin[GPIO_HJL_CF] = 99; - Hlw.model_type = 1; // HJL-01/BL0937 + Hlw.model_type = 0; // HLW8012 + if (pin[GPIO_HJL_CF] < 99) { + pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF]; + pin[GPIO_HJL_CF] = 99; + Hlw.model_type = 1; // HJL-01/BL0937 + } + + if (pin[GPIO_HLW_CF] < 99) { // HLW8012 or HJL-01 based device Power monitor + + Hlw.ui_flag = true; // Voltage on high + if (pin[GPIO_NRG_SEL_INV] < 99) { + pin[GPIO_NRG_SEL] = pin[GPIO_NRG_SEL_INV]; + pin[GPIO_NRG_SEL_INV] = 99; + Hlw.ui_flag = false; // Voltage on low } - if (pin[GPIO_HLW_CF] < 99) { // HLW8012 or HJL-01 based device Power monitor - - Hlw.ui_flag = true; // Voltage on high - if (pin[GPIO_NRG_SEL_INV] < 99) { - pin[GPIO_NRG_SEL] = pin[GPIO_NRG_SEL_INV]; - pin[GPIO_NRG_SEL_INV] = 99; - Hlw.ui_flag = false; // Voltage on low + if (pin[GPIO_NRG_CF1] < 99) { // Voltage and/or Current monitor + if (99 == pin[GPIO_NRG_SEL]) { // Voltage and/or Current selector + Energy.current_available = false; // Assume Voltage } - - if (pin[GPIO_NRG_CF1] < 99) { // Voltage and/or Current monitor - if (99 == pin[GPIO_NRG_SEL]) { // Voltage and/or Current selector - Energy.current_available = false; // Assume Voltage - } - } else { - Energy.current_available = false; - Energy.voltage_available = false; - } - - energy_flg = XNRG_01; + } else { + Energy.current_available = false; + Energy.voltage_available = false; } + + energy_flg = XNRG_01; } } @@ -311,28 +309,26 @@ bool HlwCommand(void) * Interface \*********************************************************************************************/ -int Xnrg01(uint8_t function) +bool Xnrg01(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - HlwDrvInit(); - } - else if (XNRG_01 == energy_flg) { - switch (function) { - case FUNC_INIT: - HlwSnsInit(); - break; - case FUNC_ENERGY_EVERY_SECOND: - HlwEverySecond(); - break; - case FUNC_EVERY_200_MSECOND: - HlwEvery200ms(); - break; - case FUNC_COMMAND: - result = HlwCommand(); - break; - } + switch (function) { + case FUNC_EVERY_200_MSECOND: + HlwEvery200ms(); + break; + case FUNC_ENERGY_EVERY_SECOND: + HlwEverySecond(); + break; + case FUNC_COMMAND: + result = HlwCommand(); + break; + case FUNC_INIT: + HlwSnsInit(); + break; + case FUNC_PRE_INIT: + HlwDrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_02_cse7766.ino b/sonoff/xnrg_02_cse7766.ino index f5e1a606c..a8ab38916 100644 --- a/sonoff/xnrg_02_cse7766.ino +++ b/sonoff/xnrg_02_cse7766.ino @@ -145,7 +145,7 @@ bool CseSerialInput(void) Energy.data_valid = 0; CseReceived(); Cse.received = false; - return 1; + return true; } else { AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: " D_CHECKSUM_FAILURE)); do { // Sync buffer with data (issue #1907 and #3425) @@ -167,7 +167,7 @@ bool CseSerialInput(void) serial_in_buffer[serial_in_byte_counter++] = serial_in_byte; } serial_in_byte = 0; // Discard - return 0; + return false; } /********************************************************************************************/ @@ -208,16 +208,14 @@ void CseEverySecond(void) void CseDrvInit(void) { - if (!energy_flg) { - if ((3 == pin[GPIO_CSE7766_RX]) && (1 == pin[GPIO_CSE7766_TX])) { // As it uses 8E1 currently only hardware serial is supported - baudrate = 4800; - serial_config = SERIAL_8E1; - if (0 == Settings.param[P_CSE7766_INVALID_POWER]) { - Settings.param[P_CSE7766_INVALID_POWER] = CSE_MAX_INVALID_POWER; // SetOption39 1..255 - } - Cse.power_invalid = Settings.param[P_CSE7766_INVALID_POWER]; - energy_flg = XNRG_02; + if ((3 == pin[GPIO_CSE7766_RX]) && (1 == pin[GPIO_CSE7766_TX])) { // As it uses 8E1 currently only hardware serial is supported + baudrate = 4800; + serial_config = SERIAL_8E1; + if (0 == Settings.param[P_CSE7766_INVALID_POWER]) { + Settings.param[P_CSE7766_INVALID_POWER] = CSE_MAX_INVALID_POWER; // SetOption39 1..255 } + Cse.power_invalid = Settings.param[P_CSE7766_INVALID_POWER]; + energy_flg = XNRG_02; } } @@ -249,25 +247,23 @@ bool CseCommand(void) * Interface \*********************************************************************************************/ -int Xnrg02(uint8_t function) +bool Xnrg02(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - CseDrvInit(); - } - else if (XNRG_02 == energy_flg) { - switch (function) { - case FUNC_ENERGY_EVERY_SECOND: - CseEverySecond(); - break; - case FUNC_COMMAND: - result = CseCommand(); - break; - case FUNC_SERIAL: - result = CseSerialInput(); - break; - } + switch (function) { + case FUNC_SERIAL: + result = CseSerialInput(); + break; + case FUNC_ENERGY_EVERY_SECOND: + CseEverySecond(); + break; + case FUNC_COMMAND: + result = CseCommand(); + break; + case FUNC_PRE_INIT: + CseDrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_03_pzem004t.ino b/sonoff/xnrg_03_pzem004t.ino index b31f29582..1fc6e5e5e 100644 --- a/sonoff/xnrg_03_pzem004t.ino +++ b/sonoff/xnrg_03_pzem004t.ino @@ -211,10 +211,8 @@ void PzemSnsInit(void) void PzemDrvInit(void) { - if (!energy_flg) { - if ((pin[GPIO_PZEM004_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { // Any device with a Pzem004T - energy_flg = XNRG_03; - } + if ((pin[GPIO_PZEM004_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { // Any device with a Pzem004T + energy_flg = XNRG_03; } } @@ -222,22 +220,20 @@ void PzemDrvInit(void) * Interface \*********************************************************************************************/ -int Xnrg03(uint8_t function) +bool Xnrg03(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - PzemDrvInit(); - } - else if (XNRG_03 == energy_flg) { - switch (function) { - case FUNC_INIT: - PzemSnsInit(); - break; - case FUNC_EVERY_200_MSECOND: - if (PzemSerial) { PzemEvery200ms(); } - break; - } + switch (function) { + case FUNC_EVERY_200_MSECOND: + if (PzemSerial) { PzemEvery200ms(); } + break; + case FUNC_INIT: + PzemSnsInit(); + break; + case FUNC_PRE_INIT: + PzemDrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_04_mcp39f501.ino b/sonoff/xnrg_04_mcp39f501.ino index 5cdb4a50b..01b915954 100644 --- a/sonoff/xnrg_04_mcp39f501.ino +++ b/sonoff/xnrg_04_mcp39f501.ino @@ -583,17 +583,15 @@ void McpSnsInit(void) void McpDrvInit(void) { - if (!energy_flg) { - if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) { - if (pin[GPIO_MCP39F5_RST] < 99) { - pinMode(pin[GPIO_MCP39F5_RST], OUTPUT); - digitalWrite(pin[GPIO_MCP39F5_RST], 0); // MCP disable - Reset Delta Sigma ADC's - } - mcp_calibrate = 0; - mcp_timeout = 2; // Initial wait - mcp_init = 2; // Initial setup steps - energy_flg = XNRG_04; + if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) { + if (pin[GPIO_MCP39F5_RST] < 99) { + pinMode(pin[GPIO_MCP39F5_RST], OUTPUT); + digitalWrite(pin[GPIO_MCP39F5_RST], 0); // MCP disable - Reset Delta Sigma ADC's } + mcp_calibrate = 0; + mcp_timeout = 2; // Initial wait + mcp_init = 2; // Initial setup steps + energy_flg = XNRG_04; } } @@ -651,28 +649,26 @@ bool McpCommand(void) * Interface \*********************************************************************************************/ -int Xnrg04(uint8_t function) +bool Xnrg04(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - McpDrvInit(); - } - else if (XNRG_04 == energy_flg) { - switch (function) { - case FUNC_LOOP: - if (McpSerial) { McpSerialInput(); } - break; - case FUNC_INIT: - McpSnsInit(); - break; - case FUNC_ENERGY_EVERY_SECOND: - if (McpSerial) { McpEverySecond(); } - break; - case FUNC_COMMAND: - result = McpCommand(); - break; - } + switch (function) { + case FUNC_LOOP: + if (McpSerial) { McpSerialInput(); } + break; + case FUNC_ENERGY_EVERY_SECOND: + if (McpSerial) { McpEverySecond(); } + break; + case FUNC_COMMAND: + result = McpCommand(); + break; + case FUNC_INIT: + McpSnsInit(); + break; + case FUNC_PRE_INIT: + McpDrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_05_pzem_ac.ino b/sonoff/xnrg_05_pzem_ac.ino index 55814435a..0a0258787 100644 --- a/sonoff/xnrg_05_pzem_ac.ino +++ b/sonoff/xnrg_05_pzem_ac.ino @@ -109,10 +109,8 @@ void PzemAcSnsInit(void) void PzemAcDrvInit(void) { - if (!energy_flg) { - if ((pin[GPIO_PZEM016_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { - energy_flg = XNRG_05; - } + if ((pin[GPIO_PZEM016_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { + energy_flg = XNRG_05; } } @@ -120,22 +118,20 @@ void PzemAcDrvInit(void) * Interface \*********************************************************************************************/ -int Xnrg05(uint8_t function) +bool Xnrg05(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - PzemAcDrvInit(); - } - else if (XNRG_05 == energy_flg) { - switch (function) { - case FUNC_INIT: - PzemAcSnsInit(); - break; - case FUNC_ENERGY_EVERY_SECOND: - if (uptime > 4) { PzemAcEverySecond(); } // Fix start up issue #5875 - break; - } + switch (function) { + case FUNC_ENERGY_EVERY_SECOND: + if (uptime > 4) { PzemAcEverySecond(); } // Fix start up issue #5875 + break; + case FUNC_INIT: + PzemAcSnsInit(); + break; + case FUNC_PRE_INIT: + PzemAcDrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_06_pzem_dc.ino b/sonoff/xnrg_06_pzem_dc.ino index bba38dd3d..21c256e75 100644 --- a/sonoff/xnrg_06_pzem_dc.ino +++ b/sonoff/xnrg_06_pzem_dc.ino @@ -88,10 +88,8 @@ void PzemDcSnsInit(void) void PzemDcDrvInit(void) { - if (!energy_flg) { - if ((pin[GPIO_PZEM017_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { - energy_flg = XNRG_06; - } + if ((pin[GPIO_PZEM017_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { + energy_flg = XNRG_06; } } @@ -99,22 +97,20 @@ void PzemDcDrvInit(void) * Interface \*********************************************************************************************/ -int Xnrg06(uint8_t function) +bool Xnrg06(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - PzemDcDrvInit(); - } - else if (XNRG_06 == energy_flg) { - switch (function) { - case FUNC_INIT: - PzemDcSnsInit(); - break; - case FUNC_ENERGY_EVERY_SECOND: - if (uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875 - break; - } + switch (function) { + case FUNC_ENERGY_EVERY_SECOND: + if (uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875 + break; + case FUNC_INIT: + PzemDcSnsInit(); + break; + case FUNC_PRE_INIT: + PzemDcDrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_07_ade7953.ino b/sonoff/xnrg_07_ade7953.ino index de865f0e5..dcb958f63 100644 --- a/sonoff/xnrg_07_ade7953.ino +++ b/sonoff/xnrg_07_ade7953.ino @@ -169,19 +169,17 @@ void Ade7953EnergyEverySecond() void Ade7953DrvInit(void) { - if (!energy_flg) { - if (i2c_flg && (pin[GPIO_ADE7953_IRQ] < 99)) { // Irq on GPIO16 is not supported... - delay(100); // Need 100mS to init ADE7953 - if (I2cDevice(ADE7953_ADDR)) { - if (HLW_PREF_PULSE == Settings.energy_power_calibration) { - Settings.energy_power_calibration = ADE7953_PREF; - Settings.energy_voltage_calibration = ADE7953_UREF; - Settings.energy_current_calibration = ADE7953_IREF; - } - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR); - Ade7953.init_step = 2; - energy_flg = XNRG_07; - } + if (i2c_flg && (pin[GPIO_ADE7953_IRQ] < 99)) { // Irq on GPIO16 is not supported... + delay(100); // Need 100mS to init ADE7953 + if (I2cDevice(ADE7953_ADDR)) { + if (HLW_PREF_PULSE == Settings.energy_power_calibration) { + Settings.energy_power_calibration = ADE7953_PREF; + Settings.energy_voltage_calibration = ADE7953_UREF; + Settings.energy_current_calibration = ADE7953_IREF; + } + AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR); + Ade7953.init_step = 2; + energy_flg = XNRG_07; } } } @@ -234,22 +232,20 @@ bool Ade7953Command(void) * Interface \*********************************************************************************************/ -int Xnrg07(uint8_t function) +bool Xnrg07(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - Ade7953DrvInit(); - } - else if (XNRG_07 == energy_flg) { - switch (function) { - case FUNC_ENERGY_EVERY_SECOND: - Ade7953EnergyEverySecond(); - break; - case FUNC_COMMAND: - result = Ade7953Command(); - break; - } + switch (function) { + case FUNC_ENERGY_EVERY_SECOND: + Ade7953EnergyEverySecond(); + break; + case FUNC_COMMAND: + result = Ade7953Command(); + break; + case FUNC_PRE_INIT: + Ade7953DrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_09_sdm120.ino b/sonoff/xnrg_08_sdm120.ino similarity index 91% rename from sonoff/xnrg_09_sdm120.ino rename to sonoff/xnrg_08_sdm120.ino index 07d422578..3182ddfc1 100644 --- a/sonoff/xnrg_09_sdm120.ino +++ b/sonoff/xnrg_08_sdm120.ino @@ -1,5 +1,5 @@ /* - xnrg_09_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota + xnrg_08_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota Copyright (C) 2019 Gennaro Tortone and Theo Arends @@ -25,7 +25,7 @@ * Based on: https://github.com/reaper7/SDM_Energy_Meter \*********************************************************************************************/ -#define XNRG_09 9 +#define XNRG_08 8 // can be user defined in my_user_config.h #ifndef SDM120_SPEED @@ -189,10 +189,8 @@ void Sdm120SnsInit(void) void Sdm120DrvInit(void) { - if (!energy_flg) { - if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) { - energy_flg = XNRG_09; - } + if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) { + energy_flg = XNRG_08; } } @@ -245,33 +243,31 @@ void Sdm220Show(bool json) * Interface \*********************************************************************************************/ -int Xnrg09(uint8_t function) +bool Xnrg08(uint8_t function) { - int result = 0; + bool result = false; - if (FUNC_PRE_INIT == function) { - Sdm120DrvInit(); - } - else if (XNRG_09 == energy_flg) { - switch (function) { - case FUNC_INIT: - Sdm120SnsInit(); - break; - case FUNC_EVERY_250_MSECOND: - if (uptime > 4) { SDM120Every250ms(); } - break; - case FUNC_ENERGY_RESET: - Sdm220Reset(); - break; - case FUNC_JSON_APPEND: - Sdm220Show(1); - break; + switch (function) { + case FUNC_EVERY_250_MSECOND: + if (uptime > 4) { SDM120Every250ms(); } + break; + case FUNC_JSON_APPEND: + Sdm220Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Sdm220Show(0); - break; + case FUNC_WEB_SENSOR: + Sdm220Show(0); + break; #endif // USE_WEBSERVER - } + case FUNC_ENERGY_RESET: + Sdm220Reset(); + break; + case FUNC_INIT: + Sdm120SnsInit(); + break; + case FUNC_PRE_INIT: + Sdm120DrvInit(); + break; } return result; } diff --git a/sonoff/xnrg_interface.ino b/sonoff/xnrg_interface.ino index 22582f6c7..3e36b334b 100644 --- a/sonoff/xnrg_interface.ino +++ b/sonoff/xnrg_interface.ino @@ -20,9 +20,9 @@ #ifdef USE_ENERGY_SENSOR #ifdef XFUNC_PTR_IN_ROM -int (* const xnrg_func_ptr[])(uint8_t) PROGMEM = { // Energy driver Function Pointers +bool (* const xnrg_func_ptr[])(uint8_t) PROGMEM = { // Energy driver Function Pointers #else -int (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers +bool (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers #endif #ifdef XNRG_01 @@ -92,20 +92,23 @@ int (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers const uint8_t xnrg_present = sizeof(xnrg_func_ptr) / sizeof(xnrg_func_ptr[0]); // Number of drivers found -int XnrgCall(uint8_t Function) +uint8_t xnrg_active = 0; + +bool XnrgCall(uint8_t function) { - int result = 0; - - for (uint32_t x = 0; x < xnrg_present; x++) { - result = xnrg_func_ptr[x](Function); - - if (result && ((FUNC_SERIAL == Function) || - (FUNC_COMMAND == Function) - )) { - break; + if (FUNC_PRE_INIT == function) { + for (uint32_t x = 0; x < xnrg_present; x++) { + xnrg_func_ptr[x](function); + if (energy_flg) { + xnrg_active = x; + return true; // Stop further driver investigation + } } } - return result; + else if (energy_flg) { + return xnrg_func_ptr[xnrg_active](function); + } + return false; } #endif // USE_ENERGY_SENSOR diff --git a/tools/decode-status.py b/tools/decode-status.py index cc511e5d9..c14628bf3 100755 --- a/tools/decode-status.py +++ b/tools/decode-status.py @@ -167,7 +167,7 @@ a_features = [[ "USE_MAX31865","USE_CHIRP","USE_SOLAX_X1","USE_PAJ7620" ],[ "USE_BUZZER","USE_RDM6300","USE_IBEACON","USE_SML_M", - "USE_INA226","","","", + "USE_INA226","USE_A4988_Stepper","","", "","","","", "","","","", "","","","",