From 5eac2dd939f05f45e270677c589e25c6d31da99a Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sat, 7 Sep 2019 14:03:07 +0200 Subject: [PATCH 01/12] Support Stepper-motors connected to A4988-stepper-driver-circuit --- lib/A4988_Stepper/README.adoc | 19 ++ lib/A4988_Stepper/keywords.txt | 24 ++ lib/A4988_Stepper/library.properties | 9 + lib/A4988_Stepper/src/A4988_Stepper.cpp | 174 +++++++++++++ lib/A4988_Stepper/src/A4988_Stepper.h | 73 ++++++ sonoff/i18n.h | 8 + sonoff/language/bg-BG.h | 7 + sonoff/language/cs-CZ.h | 6 + sonoff/language/de-DE.h | 6 + sonoff/language/el-GR.h | 6 + sonoff/language/en-GB.h | 6 + sonoff/language/es-ES.h | 6 + sonoff/language/fr-FR.h | 6 + sonoff/language/he-HE.h | 6 + sonoff/language/hu-HU.h | 6 + sonoff/language/it-IT.h | 6 + sonoff/language/ko-KO.h | 6 + sonoff/language/nl-NL.h | 6 + sonoff/language/pl-PL.h | 6 + sonoff/language/pt-BR.h | 6 + sonoff/language/pt-PT.h | 6 + sonoff/language/ru-RU.h | 6 + sonoff/language/sk-SK.h | 6 + sonoff/language/sv-SE.h | 6 + sonoff/language/tr-TR.h | 6 + sonoff/language/uk-UK.h | 6 + sonoff/language/zh-CN.h | 6 + sonoff/language/zh-TW.h | 6 + sonoff/my_user_config.h | 1 + sonoff/sonoff_post.h | 4 + sonoff/sonoff_template.h | 16 ++ sonoff/support_features.ino | 4 +- sonoff/xdrv_25_A4988_Stepper.ino | 320 ++++++++++++++++++++++++ tools/decode-status.py | 2 +- 34 files changed, 785 insertions(+), 2 deletions(-) create mode 100755 lib/A4988_Stepper/README.adoc create mode 100755 lib/A4988_Stepper/keywords.txt create mode 100755 lib/A4988_Stepper/library.properties create mode 100644 lib/A4988_Stepper/src/A4988_Stepper.cpp create mode 100644 lib/A4988_Stepper/src/A4988_Stepper.h create mode 100644 sonoff/xdrv_25_A4988_Stepper.ino 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..c799eacc6 --- /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 +setMIC 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..92aabea30 --- /dev/null +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -0,0 +1,174 @@ +/* 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 + * + * Drives a bipolar motor, controlled by A4988 stepper driver circuit + */ + +#include "Arduino.h" +#include "A4988_Stepper.h" +#include +A4988_Stepper::A4988_Stepper( int m_spr + , int m_rpm + , short m_mic + , 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_MIC = m_mic; // 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)) { + switch (motor_MIC){ + case 1: + digitalWrite(motor_ms1_pin, LOW); + digitalWrite(motor_ms2_pin, LOW); + digitalWrite(motor_ms3_pin, LOW); + break; + case 2: + digitalWrite(motor_ms1_pin, HIGH); + digitalWrite(motor_ms2_pin, LOW); + digitalWrite(motor_ms3_pin, LOW); + break; + digitalWrite(motor_ms1_pin, LOW); + digitalWrite(motor_ms2_pin, HIGH); + digitalWrite(motor_ms3_pin, LOW); + case 4: + digitalWrite(motor_ms1_pin, HIGH); + digitalWrite(motor_ms2_pin, HIGH); + digitalWrite(motor_ms3_pin, LOW); + break; + case 8: + digitalWrite(motor_ms1_pin, LOW); + digitalWrite(motor_ms2_pin, LOW); + digitalWrite(motor_ms3_pin, HIGH); + break; + case 16: + digitalWrite(motor_ms1_pin, HIGH); + digitalWrite(motor_ms2_pin, HIGH); + digitalWrite(motor_ms3_pin, HIGH); + break; + } + } else { + motor_MIC = 1; + } +} + + void A4988_Stepper::adjustDelay(void) { + motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIC; + } + +void A4988_Stepper::setMIC(short oneToSixteen) { + motor_MIC = oneToSixteen; + adjustMicrosteps(); + } + + short A4988_Stepper::getMIC(void) { + return motor_MIC; + } + + void A4988_Stepper::setRPM(int howManyRounds) { + motor_RPM = howManyRounds; + adjustDelay(); + } + + int A4988_Stepper::getRPM(void) { + return motor_RPM; + } + +void A4988_Stepper::setSPR(int howManySteps){ + motor_SPR = howManySteps; + adjustDelay(); +} + +int A4988_Stepper::getSPR(void) { + return motor_SPR; +} + +void A4988_Stepper::enable(){ + if (motor_ena_pin < 99) {digitalWrite(motor_ena_pin, LOW);} +} + +void A4988_Stepper::disable(){ + if (motor_ena_pin < 99) {digitalWrite(motor_ena_pin, HIGH);} +} + +void A4988_Stepper::doMove(long howManySteps) +{ + long steps_togo = abs(howManySteps); // how many steps to take + bool lastStepWasHigh = false; + digitalWrite(motor_dir_pin, howManySteps>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 if last signal was HIGH we can pull low after 50ms as only HIGH actually moves the stepper + last_time = lastStepWasHigh?now-50: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_MIC*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..0bf9d3f81 --- /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 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 + * + * Drives a bipolar motor, controlled by A4988 stepper driver circuit + */ + +#ifndef A4988_Stepper_h +#define A4988_Stepper_h + +class A4988_Stepper { + public: + // constructor: + A4988_Stepper( int motor_spr + , int motor_rpm + , short motor_mic + , 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 setMIC (short OneToSixteen); + short getMIC (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 ); + + 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_MIC; // 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/sonoff/i18n.h b/sonoff/i18n.h index 35b51bd1f..9434496f1 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -456,6 +456,14 @@ #define D_JSON_ZIGBEEZNPSENT "ZigbeeZNPSent" #define D_JSON_ZIGBEEZCLRECEIVED "ZigbeeZCLReceived" #define D_JSON_ZIGBEEZCLSENT "ZigbeeZCLSent" + + // Commands xdrv_98_A4988.ino + #ifdef USE_A4988_Stepper + #define D_CMND_MOTOR "MOTOR" + #define D_JSON_MOTOR_COMMAND "Command" + #define D_JSON_MOTOR_VALUE "Value" + #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 8bbdb8b35..a65f5cd90 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 c62c9fe32..94e385c12 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 a18adc7b7..cad9c64bb 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -527,6 +527,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-Motors-Driver-circuit /*********************************************************************************************\ * Debug features diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h index 271325da1..dc36815ae 100644 --- a/sonoff/sonoff_post.h +++ b/sonoff/sonoff_post.h @@ -257,6 +257,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 @@ -386,6 +387,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 @@ -480,6 +482,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 @@ -561,6 +564,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 8265e4cb9..d788473ef 100644 --- a/sonoff/support_features.ino +++ b/sonoff/support_features.ino @@ -438,7 +438,9 @@ void GetFeatures(void) #ifdef USE_IBEACON feature5 |= 0x00000004; // xsns_52_ibeacon.ino #endif -// feature5 |= 0x00000008; +#ifdef USE_A4988_Stepper + feature5 |= 0x00000008; // xdrv_98_A4988.ino +#endif // feature5 |= 0x00000010; // feature5 |= 0x00000020; diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino new file mode 100644 index 000000000..ddbe0a1fa --- /dev/null +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -0,0 +1,320 @@ + +/* + xsns_22_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota + + Copyright (C) 2019 Nuno Ferreira and Theo Arends + + 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 +/*********************************************************************************************\ + * Stepper mötör on driver A4988 + * + * - https://www.dfrobot.com/wiki/index.php/Weather-proof_Ultrasonic_Sensor_SKU_:_SEN0207 +\*********************************************************************************************/ + +#define XDRV_98 98 + +enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON}; + +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_mic = 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_mic = 1; + + myA4988 = new A4988_Stepper( A4988_spr + , A4988_rpm + , A4988_mic + , A4988_dir_pin + , A4988_stp_pin + , A4988_ena_pin + , A4988_ms1_pin + , A4988_ms2_pin + , A4988_ms3_pin ); + A4988_spr = myA4988->getSPR(); + A4988_rpm = myA4988->getRPM(); + A4988_mic = myA4988->getMIC(); + if ((A4988_ms1_pin < 99)&&(A4988_ms2_pin < 99)&&(A4988_ms3_pin < 99)&&(A4988_ena_pin<99)) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MS1[%d] MS2[%d] MS3[%d]"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin,A4988_ms1_pin,A4988_ms2_pin,A4988_ms3_pin); + } else { + if ((A4988_ena_pin<99)) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); + } else { + AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] motor permanently on, MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); + } + } +} + +const char kA4988Commands[] PROGMEM = "MOTOR|" + "doMove|doRotate|doTurn|setSPR|setRPM|setMIC|getSPR|getRPM|getMIC|doVader"; + +void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR, &CmndDoMove, &CmndDoRotate, &CmndDoTurn, &CmndSetSPR, &CmndSetRPM, &CmndSetMic , &CmndGetSPR, &CmndGetRPM, &CmndGetMIC, &CmndDoVader }; + + +uint32_t MOTORCmndJson(void) +{ + // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(3) + 40 = 96 + // MOTOR { "command": "doMove", "value": 200 } + // MOTOR { "command": "doRotate", "value": 200 } + // MOTOR { "command": "doTurn", "value": 200 } + + char dataBufUc[XdrvMailbox.data_len]; + UpperCase(dataBufUc, XdrvMailbox.data); + RemoveSpace(dataBufUc); + if (strlen(dataBufUc) < 8) { + return A4988_INVALID_JSON; + } + + StaticJsonBuffer<40> jsonBuf; + JsonObject &root = jsonBuf.parseObject(dataBufUc); + if (!root.success()) { + return A4988_INVALID_JSON; + } + + char parm_uc[10]; + const char *command = root[PSTR(parm_uc, PSTR(D_JSON_MOTOR_COMMAND))]; + const char *commandval = root[PSTR(parm_uc, PSTR(D_JSON_MOTOR_VALUE))]; + + if (!(command && commandval)) { + return A4988_INVALID_JSON; + } + + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MOTOR: command: '%s', value: '%s'"),command, commandval); + + switch (command) + { case "doMove": { + long stepsPlease = 1; + stepsPlease = strtoul(commandval,nullptr,10); + myA4988->doMove(stepsPlease); break; + } + case "doRotate" : { + long degrsPlease = 1; + degrsPlease = strtoul(commandval,nullptr,10); + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease); + myA4988->doRotate(degrsPlease); break; + } + case "doTurn" : { + float turnsPlease = 0; + turnsPlease = strtod(commandval,nullptr); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease); + myA4988->doTurn(turnsPlease); break; + } + default: + ResponseCmndChar(D_JSON_PROTOCOL_NOT_SUPPORTED); + } + + return A4988_NO_ERROR; +} + +void CmndMOTOR(void){ + if (XdrvMailbox.data_len) { + if (strstr(XdrvMailbox.data, "{") == nullptr) { + error = A4988_NO_JSON_COMMAND; + } else { + error = MOTORCmndJson(); + } + } + A4988CmndResponse(error); +} + +void A4988CmndResponse(uint32_t error){ + +} + +void CmndGetSPR(void) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: SPR = %d steps"), myA4988->getSPR()); +} + +void CmndGetRPM(void) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: RPM = %d rounds"), myA4988->getRPM()); +} + +void CmndGetMIC(void) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: MIC = %d steps"), myA4988->getMIC()); +} + +void CmndDoMove(void) +{ + if (XdrvMailbox.data_len > 0) { + long stepsPlease = 1; + stepsPlease = strtoul(XdrvMailbox.data,nullptr,10); + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving %d steps"), stepsPlease); + myA4988->doMove(stepsPlease); + } else { + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving 25 steps")); + myA4988->doMove(25); + } + ResponseCmndDone(); +} + +void CmndDoRotate(void) +{ + if (XdrvMailbox.data_len > 0) { + long degrsPlease = 1; + degrsPlease = strtoul(XdrvMailbox.data,nullptr,10); + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease); + myA4988->doRotate(degrsPlease); + } else { + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving 45 degrs")); + myA4988->doRotate(45); + } + ResponseCmndDone(); +} + +void CmndDoTurn(void) +{ + if (XdrvMailbox.data_len > 0) { + float turnsPlease = 0; + turnsPlease = strtod(XdrvMailbox.data,nullptr); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease); + myA4988->doTurn(turnsPlease); + } else { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning 0.25 times")); + myA4988->doRotate(0.25); + } + ResponseCmndDone(); +} + +void CmndSetRPM(void) +{ + short rpmPlease = 60; + if (XdrvMailbox.data_len > 0) { + rpmPlease = strtoul(XdrvMailbox.data,nullptr,10); + } + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: RPM set to %d"), rpmPlease); + myA4988->setRPM(rpmPlease); + ResponseCmndDone(); +} + +void CmndSetSPR(void) +{ + int sprPlease = 200; + if (XdrvMailbox.data_len > 0) { + sprPlease = strtoul(XdrvMailbox.data,nullptr,10); + } + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: SPR set to %d"), sprPlease); + myA4988->setSPR(sprPlease); + ResponseCmndDone(); +} + +void CmndSetMic(void) +{ + if ((pin[GPIO_A4988_MS1] < 99) && (pin[GPIO_A4988_MS2] < 99) && (pin[GPIO_A4988_MS3] < 99)) { + short micPlease = 1; + if (XdrvMailbox.data_len > 0) { + micPlease = strtoul(XdrvMailbox.data,nullptr,10); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps set to %d"), micPlease); + myA4988->setMIC(micPlease); + } + } else { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps constant = 1. You'll have to define GPIO's for MS1-MS3 and connect them to A4988 or hardwire A4988 itself")); + } + ResponseCmndDone(); +} + +void CmndDoVader(void){ + myA4988->enable(); + myA4988->setRPM(25); myA4988->doMove( 30); delay(5); + myA4988->setRPM(25); myA4988->doMove(-30); delay(5); + myA4988->setRPM(25); myA4988->doMove( 30); delay(5); + myA4988->setRPM(20); myA4988->doMove(-25); delay(5); + myA4988->setRPM(30); myA4988->doMove( 20); delay(5); + myA4988->setRPM(25); myA4988->doMove( 50); delay(5); + myA4988->setRPM(20); myA4988->doMove(-25); delay(5); + myA4988->setRPM(30); myA4988->doMove( 20); delay(5); + myA4988->setRPM(25); myA4988->doMove(-50); delay(5);// + myA4988->setRPM(38); myA4988->doMove( 31); delay(5); + myA4988->setRPM(38); myA4988->doMove(-31); delay(5); + myA4988->setRPM(38); myA4988->doMove( 31); delay(5); + myA4988->setRPM(38); myA4988->doMove(-31); delay(5); + myA4988->setRPM(41); myA4988->doMove( 21); delay(5); + myA4988->setRPM(30); myA4988->doMove(-21); delay(5); + myA4988->setRPM(25); myA4988->doMove( 30); delay(5); + myA4988->setRPM(20); myA4988->doMove(-25); delay(5); + myA4988->setRPM(30); myA4988->doMove( 20); delay(5); + myA4988->setRPM(25); myA4988->doMove(-50); delay(5);// + myA4988->setRPM(50); myA4988->doMove( 40); delay(5); + myA4988->setRPM(25); myA4988->doMove(-20); delay(5); + myA4988->setRPM(25); myA4988->doMove( 15); delay(5); + myA4988->setRPM(50); myA4988->doMove(-40); delay(5); + myA4988->setRPM(48); myA4988->doMove( 35); delay(5); + myA4988->setRPM(45); myA4988->doMove(-25); delay(5); + myA4988->setRPM(41); myA4988->doMove( 25); delay(5); + myA4988->setRPM(38); myA4988->doMove(-26); delay(5); + myA4988->setRPM(41); myA4988->doMove( 25); delay(5); + myA4988->setRPM(25); myA4988->doMove( 25); delay(5); + myA4988->setRPM(41); myA4988->doMove(-35); delay(5); + myA4988->setRPM(38); myA4988->doMove( 25); delay(5); + myA4988->setRPM(36); myA4988->doMove(-25); delay(5); + myA4988->setRPM(33); myA4988->doMove( 25); delay(5); + myA4988->setRPM(30); myA4988->doMove(-27); delay(5); + myA4988->setRPM(33); myA4988->doMove( 25); delay(5); + myA4988->setRPM(33); myA4988->doMove( 25); delay(5); + myA4988->setRPM(38); myA4988->doMove(-30); delay(5); + myA4988->setRPM(18); myA4988->doMove( 10); delay(5); + myA4988->setRPM(18); myA4988->doMove(-10); delay(5); + myA4988->setRPM(25); myA4988->doMove( 20); delay(5); + myA4988->setRPM(20); myA4988->doMove(-25); delay(5); + myA4988->setRPM(30); myA4988->doMove( 20); delay(5); + myA4988->setRPM(25); myA4988->doMove(-50); delay(5); + myA4988->setRPM(20); myA4988->doMove( 25); delay(5); + myA4988->setRPM(30); myA4988->doMove(-20); delay(5); + myA4988->setRPM(25); myA4988->doMove( 50); delay(5); + myA4988->disable(); + AddLog_P2(LOG_LEVEL_INFO, PSTR("Stepper: may the force be with you!")); + ResponseCmndDone(); +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ +bool Xdrv98(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/tools/decode-status.py b/tools/decode-status.py index 973c8655c..f7f0fa126 100755 --- a/tools/decode-status.py +++ b/tools/decode-status.py @@ -160,7 +160,7 @@ a_features = [[ "USE_ADE7953","USE_SPS30","USE_VL53L0X","USE_MLX90614", "USE_MAX31865","USE_CHIRP","USE_SOLAX_X1","USE_PAJ7620" ],[ - "USE_BUZZER","USE_RDM6300","USE_IBEACON","", + "USE_BUZZER","USE_RDM6300","USE_IBEACON","USE_A4988_Stepper", "","","","", "","","","", "","","","", From 8350be64a07b8489076ac2aa4e2977261bb12d7a Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sat, 7 Sep 2019 14:09:44 +0200 Subject: [PATCH 02/12] . --- lib/A4988_Stepper/keywords.txt | 2 +- lib/A4988_Stepper/src/A4988_Stepper.cpp | 12 ++++++------ lib/A4988_Stepper/src/A4988_Stepper.h | 4 ++-- sonoff/xdrv_25_A4988_Stepper.ino | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/A4988_Stepper/keywords.txt b/lib/A4988_Stepper/keywords.txt index c799eacc6..c83465c8b 100755 --- a/lib/A4988_Stepper/keywords.txt +++ b/lib/A4988_Stepper/keywords.txt @@ -16,7 +16,7 @@ doMove KEYWORD2 doRotate KEYWORD2 setRPM KEYWORD2 setSPR KEYWORD2 -setMIC KEYWORD2 +setMIS KEYWORD2 version KEYWORD2 ####################################### diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp index 92aabea30..ad83d3e87 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.cpp +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -20,7 +20,7 @@ #include A4988_Stepper::A4988_Stepper( int m_spr , int m_rpm - , short m_mic + , short m_mis , short m_dir_pin , short m_stp_pin , short m_ena_pin @@ -30,7 +30,7 @@ A4988_Stepper::A4988_Stepper( int m_spr last_time = 0; // time stamp in us of the last step taken motor_SPR = m_spr; // StepsPerRevolution motor_RPM = m_rpm; // RoundsPerMinute - motor_MIC = m_mic; // Microsteps w/o effect if MS1-MS3 not connected - then full steps anyway + 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; @@ -61,7 +61,7 @@ void A4988_Stepper::adjustPins(void) { void A4988_Stepper::adjustMicrosteps() { if ((motor_ms1_pin<99)&&(motor_ms2_pin<99)&&(motor_ms3_pin<99)) { - switch (motor_MIC){ + switch (motor_MIS){ case 1: digitalWrite(motor_ms1_pin, LOW); digitalWrite(motor_ms2_pin, LOW); @@ -100,12 +100,12 @@ void A4988_Stepper::adjustMicrosteps() { motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIC; } -void A4988_Stepper::setMIC(short oneToSixteen) { +void A4988_Stepper::setMIS(short oneToSixteen) { motor_MIC = oneToSixteen; adjustMicrosteps(); } - short A4988_Stepper::getMIC(void) { + short A4988_Stepper::getMIS(void) { return motor_MIC; } @@ -158,7 +158,7 @@ void A4988_Stepper::doMove(long howManySteps) void A4988_Stepper::doRotate(long howManyDegrees) { long lSteps = 0; - lSteps = motor_SPR*motor_MIC*howManyDegrees/360; + lSteps = motor_SPR*motor_MIS*howManyDegrees/360; doMove(lSteps); } diff --git a/lib/A4988_Stepper/src/A4988_Stepper.h b/lib/A4988_Stepper/src/A4988_Stepper.h index 0bf9d3f81..b3aa27c35 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.h +++ b/lib/A4988_Stepper/src/A4988_Stepper.h @@ -35,8 +35,8 @@ class A4988_Stepper { void setRPM (int whatRPM ); int getRPM (void ); - void setMIC (short OneToSixteen); - short getMIC (void ); + void setMIS (short OneToSixteen); + short getMIS (void ); void setSPR (int howMany ); int getSPR (void ); diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino index ddbe0a1fa..e1b6ebb75 100644 --- a/sonoff/xdrv_25_A4988_Stepper.ino +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -52,11 +52,11 @@ void A4988Init(void) A4988_ms3_pin = pin[GPIO_A4988_MS3]; A4988_spr = 200; A4988_rpm = 30; - A4988_mic = 1; + A4988_mis = 1; myA4988 = new A4988_Stepper( A4988_spr , A4988_rpm - , A4988_mic + , A4988_mis , A4988_dir_pin , A4988_stp_pin , A4988_ena_pin @@ -65,7 +65,7 @@ void A4988Init(void) , A4988_ms3_pin ); A4988_spr = myA4988->getSPR(); A4988_rpm = myA4988->getRPM(); - A4988_mic = myA4988->getMIC(); + A4988_mis = myA4988->getMIS(); if ((A4988_ms1_pin < 99)&&(A4988_ms2_pin < 99)&&(A4988_ms3_pin < 99)&&(A4988_ena_pin<99)) { AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MS1[%d] MS2[%d] MS3[%d]"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin,A4988_ms1_pin,A4988_ms2_pin,A4988_ms3_pin); } else { @@ -229,7 +229,7 @@ void CmndSetSPR(void) ResponseCmndDone(); } -void CmndSetMic(void) +void CmndSetMIS(void) { if ((pin[GPIO_A4988_MS1] < 99) && (pin[GPIO_A4988_MS2] < 99) && (pin[GPIO_A4988_MS3] < 99)) { short micPlease = 1; From 549fd7d84c250fcc7d09af0825f8b4414aa2760c Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sat, 7 Sep 2019 14:11:17 +0200 Subject: [PATCH 03/12] . --- lib/A4988_Stepper/src/A4988_Stepper.cpp | 4 ++-- lib/A4988_Stepper/src/A4988_Stepper.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp index ad83d3e87..16f9b1608 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.cpp +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -101,12 +101,12 @@ void A4988_Stepper::adjustMicrosteps() { } void A4988_Stepper::setMIS(short oneToSixteen) { - motor_MIC = oneToSixteen; + motor_MIS = oneToSixteen; adjustMicrosteps(); } short A4988_Stepper::getMIS(void) { - return motor_MIC; + return motor_MIS; } void A4988_Stepper::setRPM(int howManyRounds) { diff --git a/lib/A4988_Stepper/src/A4988_Stepper.h b/lib/A4988_Stepper/src/A4988_Stepper.h index b3aa27c35..b0e16ed31 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.h +++ b/lib/A4988_Stepper/src/A4988_Stepper.h @@ -23,7 +23,7 @@ class A4988_Stepper { // constructor: A4988_Stepper( int motor_spr , int motor_rpm - , short motor_mic + , short motor_mis , short motor_dir_pin , short motor_stp_pin , short motor_ena_pin From 179d74ab0894db17ed87ed8569089f87ca0f395c Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sat, 7 Sep 2019 20:38:06 +0200 Subject: [PATCH 04/12] it compiles... --- lib/A4988_Stepper/src/A4988_Stepper.cpp | 4 +- lib/A4988_Stepper/src/A4988_Stepper.h | 2 +- sonoff/i18n.h | 3 + sonoff/xdrv_25_A4988_Stepper.ino | 100 ++++++++++-------------- 4 files changed, 49 insertions(+), 60 deletions(-) diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp index 16f9b1608..52e8e8860 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.cpp +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -92,12 +92,12 @@ void A4988_Stepper::adjustMicrosteps() { break; } } else { - motor_MIC = 1; + motor_MIS = 1; } } void A4988_Stepper::adjustDelay(void) { - motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIC; + motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIS; } void A4988_Stepper::setMIS(short oneToSixteen) { diff --git a/lib/A4988_Stepper/src/A4988_Stepper.h b/lib/A4988_Stepper/src/A4988_Stepper.h index b0e16ed31..9aac76859 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.h +++ b/lib/A4988_Stepper/src/A4988_Stepper.h @@ -57,7 +57,7 @@ class A4988_Stepper { unsigned long motor_delay; // delay between steps, in ms int motor_SPR; // Steps Per Revolution int motor_RPM; // Rounds Per Minute - short motor_MIC; // Micro Steps + short motor_MIS; // Micro Steps // motor pins: short motor_dir_pin; diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 9434496f1..6b6f219f9 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -462,6 +462,9 @@ #define D_CMND_MOTOR "MOTOR" #define D_JSON_MOTOR_COMMAND "Command" #define D_JSON_MOTOR_VALUE "Value" + #define D_JSON_MOTOR_MOVE "doMove" + #define D_JSON_MOTOR_ROTATE "doRotate" + #define D_JSON_MOTOR_TURN "doTurn" #endif /********************************************************************************************/ diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino index e1b6ebb75..0a2782d1a 100644 --- a/sonoff/xdrv_25_A4988_Stepper.ino +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -38,7 +38,7 @@ short A4988_ms3_pin = pin[GPIO_MAX]; short A4988_ena_pin = pin[GPIO_MAX]; int A4988_spr = 0; float A4988_rpm = 0; -short A4988_mic = 0; +short A4988_mis = 0; A4988_Stepper* myA4988 = nullptr; @@ -67,12 +67,12 @@ void A4988Init(void) A4988_rpm = myA4988->getRPM(); A4988_mis = myA4988->getMIS(); if ((A4988_ms1_pin < 99)&&(A4988_ms2_pin < 99)&&(A4988_ms3_pin < 99)&&(A4988_ena_pin<99)) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MS1[%d] MS2[%d] MS3[%d]"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin,A4988_ms1_pin,A4988_ms2_pin,A4988_ms3_pin); + AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIS). Pins: Dir[%d] Stp[%d] Ena[%d] MS1[%d] MS2[%d] MS3[%d]"),A4988_spr, A4988_rpm, A4988_mis, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin,A4988_ms1_pin,A4988_ms2_pin,A4988_ms3_pin); } else { if ((A4988_ena_pin<99)) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] Ena[%d] MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); + AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIS). Pins: Dir[%d] Stp[%d] Ena[%d] MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mis, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); } else { - AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIC). Pins: Dir[%d] Stp[%d] motor permanently on, MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mic, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); + AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIS). Pins: Dir[%d] Stp[%d] motor permanently on, MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mis, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); } } } @@ -80,65 +80,44 @@ void A4988Init(void) const char kA4988Commands[] PROGMEM = "MOTOR|" "doMove|doRotate|doTurn|setSPR|setRPM|setMIC|getSPR|getRPM|getMIC|doVader"; -void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR, &CmndDoMove, &CmndDoRotate, &CmndDoTurn, &CmndSetSPR, &CmndSetRPM, &CmndSetMic , &CmndGetSPR, &CmndGetRPM, &CmndGetMIC, &CmndDoVader }; +void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR, &CmndDoMove, &CmndDoRotate, &CmndDoTurn, &CmndSetSPR, &CmndSetRPM, &CmndSetMIS , &CmndGetSPR, &CmndGetRPM, &CmndGetMIS, &CmndDoVader }; uint32_t MOTORCmndJson(void) { - // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(3) + 40 = 96 - // MOTOR { "command": "doMove", "value": 200 } - // MOTOR { "command": "doRotate", "value": 200 } - // MOTOR { "command": "doTurn", "value": 200 } - + // MOTOR {"Command":"doMove","Value":200} + // MOTOR {"Command":"doRotate","Value":360} + // MOTOR {"Command":"doTurn","Value":1.0} char dataBufUc[XdrvMailbox.data_len]; UpperCase(dataBufUc, XdrvMailbox.data); RemoveSpace(dataBufUc); - if (strlen(dataBufUc) < 8) { - return A4988_INVALID_JSON; - } + if (strlen(dataBufUc) < 8) { return A4988_INVALID_JSON; } - StaticJsonBuffer<40> jsonBuf; - JsonObject &root = jsonBuf.parseObject(dataBufUc); - if (!root.success()) { - return A4988_INVALID_JSON; - } + DynamicJsonBuffer jsonBuf; + JsonObject &json = jsonBuf.parseObject(dataBufUc); + if (!json.success()) { return A4988_INVALID_JSON; } - char parm_uc[10]; - const char *command = root[PSTR(parm_uc, PSTR(D_JSON_MOTOR_COMMAND))]; - const char *commandval = root[PSTR(parm_uc, PSTR(D_JSON_MOTOR_VALUE))]; - - if (!(command && commandval)) { - return A4988_INVALID_JSON; - } - - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MOTOR: command: '%s', value: '%s'"),command, commandval); - - switch (command) - { case "doMove": { - long stepsPlease = 1; - stepsPlease = strtoul(commandval,nullptr,10); - myA4988->doMove(stepsPlease); break; - } - case "doRotate" : { - long degrsPlease = 1; - degrsPlease = strtoul(commandval,nullptr,10); - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease); - myA4988->doRotate(degrsPlease); break; - } - case "doTurn" : { - float turnsPlease = 0; - turnsPlease = strtod(commandval,nullptr); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease); - myA4988->doTurn(turnsPlease); break; - } - default: - ResponseCmndChar(D_JSON_PROTOCOL_NOT_SUPPORTED); - } - - return A4988_NO_ERROR; + if (json.containsKey(D_JSON_MOTOR_MOVE )){ + long stepsPlease = 50; + stepsPlease = strtoul(json[D_JSON_MOTOR_MOVE],nullptr,10); + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving %d steps"), stepsPlease); + myA4988->doMove(stepsPlease); + } else if (json.containsKey(D_JSON_MOTOR_ROTATE )){ + long degrsPlease = 45; + degrsPlease = strtoul(json[D_JSON_MOTOR_ROTATE],nullptr,10); + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease); + myA4988->doRotate(degrsPlease); + } else if (json.containsKey(D_JSON_MOTOR_TURN )){ + float turnsPlease = 0.25; + turnsPlease = strtod(json[D_JSON_MOTOR_TURN],nullptr); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease); + myA4988->doTurn(turnsPlease); + } else return A4988_NO_JSON_COMMAND; + return A4988_NO_ERROR; } void CmndMOTOR(void){ + uint32_t error; if (XdrvMailbox.data_len) { if (strstr(XdrvMailbox.data, "{") == nullptr) { error = A4988_NO_JSON_COMMAND; @@ -150,6 +129,13 @@ void CmndMOTOR(void){ } void A4988CmndResponse(uint32_t error){ + switch (error) { + case A4988_NO_JSON_COMMAND: + ResponseCmndChar(D_JSON_INVALID_JSON); + break; + default: // A4988_NO_ERROR + ResponseCmndDone(); + } } @@ -161,8 +147,8 @@ void CmndGetRPM(void) { AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: RPM = %d rounds"), myA4988->getRPM()); } -void CmndGetMIC(void) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: MIC = %d steps"), myA4988->getMIC()); +void CmndGetMIS(void) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: MIS = %d steps"), myA4988->getMIS()); } void CmndDoMove(void) @@ -232,11 +218,11 @@ void CmndSetSPR(void) void CmndSetMIS(void) { if ((pin[GPIO_A4988_MS1] < 99) && (pin[GPIO_A4988_MS2] < 99) && (pin[GPIO_A4988_MS3] < 99)) { - short micPlease = 1; + short misPlease = 1; if (XdrvMailbox.data_len > 0) { - micPlease = strtoul(XdrvMailbox.data,nullptr,10); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps set to %d"), micPlease); - myA4988->setMIC(micPlease); + misPlease = strtoul(XdrvMailbox.data,nullptr,10); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps set to %d"), misPlease); + myA4988->setMIS(misPlease); } } else { AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps constant = 1. You'll have to define GPIO's for MS1-MS3 and connect them to A4988 or hardwire A4988 itself")); From e5162629046f7471bb46e8ea0b704623f99efebb Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sat, 7 Sep 2019 21:37:01 +0200 Subject: [PATCH 05/12] ..to be tested --- lib/A4988_Stepper/src/A4988_Stepper.cpp | 3 +- platformio.ini | 2 +- sonoff/i18n.h | 5 +- sonoff/my_user_config.h | 2 +- sonoff/support_features.ino | 2 +- sonoff/xdrv_25_A4988_Stepper.ino | 185 ++---------------------- 6 files changed, 21 insertions(+), 178 deletions(-) diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp index 52e8e8860..d44375396 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.cpp +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -17,7 +17,6 @@ #include "Arduino.h" #include "A4988_Stepper.h" -#include A4988_Stepper::A4988_Stepper( int m_spr , int m_rpm , short m_mis @@ -27,7 +26,7 @@ A4988_Stepper::A4988_Stepper( int m_spr , short m_ms1_pin , short m_ms2_pin , short m_ms3_pin ) { - last_time = 0; // time stamp in us of the last step taken + 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 diff --git a/platformio.ini b/platformio.ini index d19018416..f4d99c7ef 100755 --- a/platformio.ini +++ b/platformio.ini @@ -25,7 +25,7 @@ build_dir = .pioenvs ;default_envs = sonoff-BR ;default_envs = sonoff-CN ;default_envs = sonoff-CZ -;default_envs = sonoff-DE +default_envs = sonoff-DE ;default_envs = sonoff-ES ;default_envs = sonoff-FR ;default_envs = sonoff-GR diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 6b6f219f9..70132f06a 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -457,7 +457,7 @@ #define D_JSON_ZIGBEEZCLRECEIVED "ZigbeeZCLReceived" #define D_JSON_ZIGBEEZCLSENT "ZigbeeZCLSent" - // Commands xdrv_98_A4988.ino + // Commands xdrv_25_A4988_Stepper.ino #ifdef USE_A4988_Stepper #define D_CMND_MOTOR "MOTOR" #define D_JSON_MOTOR_COMMAND "Command" @@ -465,6 +465,9 @@ #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 /********************************************************************************************/ diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index cad9c64bb..1d63e842c 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -527,7 +527,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-Motors-Driver-circuit +#define USE_A4988_Stepper // Add support for A4988 Stepper-Motors-Driver-circuit (+12k7 code) /*********************************************************************************************\ * Debug features diff --git a/sonoff/support_features.ino b/sonoff/support_features.ino index d788473ef..94019ce3d 100644 --- a/sonoff/support_features.ino +++ b/sonoff/support_features.ino @@ -439,7 +439,7 @@ void GetFeatures(void) feature5 |= 0x00000004; // xsns_52_ibeacon.ino #endif #ifdef USE_A4988_Stepper - feature5 |= 0x00000008; // xdrv_98_A4988.ino + feature5 |= 0x00000008; // xdrv_25_A4988.ino #endif // feature5 |= 0x00000010; diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino index 0a2782d1a..e1d6903bf 100644 --- a/sonoff/xdrv_25_A4988_Stepper.ino +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -20,13 +20,7 @@ #ifdef USE_A4988_Stepper #include -/*********************************************************************************************\ - * Stepper mötör on driver A4988 - * - * - https://www.dfrobot.com/wiki/index.php/Weather-proof_Ultrasonic_Sensor_SKU_:_SEN0207 -\*********************************************************************************************/ - -#define XDRV_98 98 +#define XDRV_25 25 enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON}; @@ -63,25 +57,12 @@ void A4988Init(void) , A4988_ms1_pin , A4988_ms2_pin , A4988_ms3_pin ); - A4988_spr = myA4988->getSPR(); - A4988_rpm = myA4988->getRPM(); - A4988_mis = myA4988->getMIS(); - if ((A4988_ms1_pin < 99)&&(A4988_ms2_pin < 99)&&(A4988_ms3_pin < 99)&&(A4988_ena_pin<99)) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIS). Pins: Dir[%d] Stp[%d] Ena[%d] MS1[%d] MS2[%d] MS3[%d]"),A4988_spr, A4988_rpm, A4988_mis, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin,A4988_ms1_pin,A4988_ms2_pin,A4988_ms3_pin); - } else { - if ((A4988_ena_pin<99)) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIS). Pins: Dir[%d] Stp[%d] Ena[%d] MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mis, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); - } else { - AddLog_P2(LOG_LEVEL_INFO, PSTR("STP: A4988-Driver initialized (%dSPR, %dRPM,%dMIS). Pins: Dir[%d] Stp[%d] motor permanently on, MicroStepping hardwired"),A4988_spr, A4988_rpm, A4988_mis, A4988_dir_pin,A4988_stp_pin,A4988_ena_pin); - } - } } const char kA4988Commands[] PROGMEM = "MOTOR|" - "doMove|doRotate|doTurn|setSPR|setRPM|setMIC|getSPR|getRPM|getMIC|doVader"; - -void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR, &CmndDoMove, &CmndDoRotate, &CmndDoTurn, &CmndSetSPR, &CmndSetRPM, &CmndSetMIS , &CmndGetSPR, &CmndGetRPM, &CmndGetMIS, &CmndDoVader }; + "doMove|doRotate|doTurn|setSPR|setRPM|setMIS"; +void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR}; uint32_t MOTORCmndJson(void) { @@ -96,22 +77,27 @@ uint32_t MOTORCmndJson(void) DynamicJsonBuffer jsonBuf; JsonObject &json = jsonBuf.parseObject(dataBufUc); if (!json.success()) { return A4988_INVALID_JSON; } - if (json.containsKey(D_JSON_MOTOR_MOVE )){ long stepsPlease = 50; stepsPlease = strtoul(json[D_JSON_MOTOR_MOVE],nullptr,10); - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving %d steps"), stepsPlease); myA4988->doMove(stepsPlease); } else if (json.containsKey(D_JSON_MOTOR_ROTATE )){ long degrsPlease = 45; degrsPlease = strtoul(json[D_JSON_MOTOR_ROTATE],nullptr,10); - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease); myA4988->doRotate(degrsPlease); } else if (json.containsKey(D_JSON_MOTOR_TURN )){ float turnsPlease = 0.25; turnsPlease = strtod(json[D_JSON_MOTOR_TURN],nullptr); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease); myA4988->doTurn(turnsPlease); + } else if (json.containsKey(D_JSON_MOTOR_SPR )){ + int howManySteps =strtoul(json[D_JSON_MOTOR_SPR],nullptr,10); + myA4988->setSPR(howManySteps); + } else if (json.containsKey(D_JSON_MOTOR_RPM )){ + int howManyRounds =strtoul(json[D_JSON_MOTOR_RPM],nullptr,10); + myA4988->setRPM(howManyRounds); + } else if (json.containsKey(D_JSON_MOTOR_MIS )){ + short oneToSixteen =strtoul(json[D_JSON_MOTOR_MIS],nullptr,10); + myA4988->setMIS(oneToSixteen); } else return A4988_NO_JSON_COMMAND; return A4988_NO_ERROR; } @@ -139,157 +125,12 @@ void A4988CmndResponse(uint32_t error){ } -void CmndGetSPR(void) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: SPR = %d steps"), myA4988->getSPR()); -} - -void CmndGetRPM(void) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: RPM = %d rounds"), myA4988->getRPM()); -} - -void CmndGetMIS(void) { - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: MIS = %d steps"), myA4988->getMIS()); -} - -void CmndDoMove(void) -{ - if (XdrvMailbox.data_len > 0) { - long stepsPlease = 1; - stepsPlease = strtoul(XdrvMailbox.data,nullptr,10); - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving %d steps"), stepsPlease); - myA4988->doMove(stepsPlease); - } else { - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving 25 steps")); - myA4988->doMove(25); - } - ResponseCmndDone(); -} - -void CmndDoRotate(void) -{ - if (XdrvMailbox.data_len > 0) { - long degrsPlease = 1; - degrsPlease = strtoul(XdrvMailbox.data,nullptr,10); - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Rotating %d degrs"), degrsPlease); - myA4988->doRotate(degrsPlease); - } else { - AddLog_P2(LOG_LEVEL_INFO, PSTR("A4988: Moving 45 degrs")); - myA4988->doRotate(45); - } - ResponseCmndDone(); -} - -void CmndDoTurn(void) -{ - if (XdrvMailbox.data_len > 0) { - float turnsPlease = 0; - turnsPlease = strtod(XdrvMailbox.data,nullptr); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning %d times"), turnsPlease); - myA4988->doTurn(turnsPlease); - } else { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Turning 0.25 times")); - myA4988->doRotate(0.25); - } - ResponseCmndDone(); -} - -void CmndSetRPM(void) -{ - short rpmPlease = 60; - if (XdrvMailbox.data_len > 0) { - rpmPlease = strtoul(XdrvMailbox.data,nullptr,10); - } - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: RPM set to %d"), rpmPlease); - myA4988->setRPM(rpmPlease); - ResponseCmndDone(); -} - -void CmndSetSPR(void) -{ - int sprPlease = 200; - if (XdrvMailbox.data_len > 0) { - sprPlease = strtoul(XdrvMailbox.data,nullptr,10); - } - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: SPR set to %d"), sprPlease); - myA4988->setSPR(sprPlease); - ResponseCmndDone(); -} - -void CmndSetMIS(void) -{ - if ((pin[GPIO_A4988_MS1] < 99) && (pin[GPIO_A4988_MS2] < 99) && (pin[GPIO_A4988_MS3] < 99)) { - short misPlease = 1; - if (XdrvMailbox.data_len > 0) { - misPlease = strtoul(XdrvMailbox.data,nullptr,10); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps set to %d"), misPlease); - myA4988->setMIS(misPlease); - } - } else { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("A4988: Microsteps constant = 1. You'll have to define GPIO's for MS1-MS3 and connect them to A4988 or hardwire A4988 itself")); - } - ResponseCmndDone(); -} - -void CmndDoVader(void){ - myA4988->enable(); - myA4988->setRPM(25); myA4988->doMove( 30); delay(5); - myA4988->setRPM(25); myA4988->doMove(-30); delay(5); - myA4988->setRPM(25); myA4988->doMove( 30); delay(5); - myA4988->setRPM(20); myA4988->doMove(-25); delay(5); - myA4988->setRPM(30); myA4988->doMove( 20); delay(5); - myA4988->setRPM(25); myA4988->doMove( 50); delay(5); - myA4988->setRPM(20); myA4988->doMove(-25); delay(5); - myA4988->setRPM(30); myA4988->doMove( 20); delay(5); - myA4988->setRPM(25); myA4988->doMove(-50); delay(5);// - myA4988->setRPM(38); myA4988->doMove( 31); delay(5); - myA4988->setRPM(38); myA4988->doMove(-31); delay(5); - myA4988->setRPM(38); myA4988->doMove( 31); delay(5); - myA4988->setRPM(38); myA4988->doMove(-31); delay(5); - myA4988->setRPM(41); myA4988->doMove( 21); delay(5); - myA4988->setRPM(30); myA4988->doMove(-21); delay(5); - myA4988->setRPM(25); myA4988->doMove( 30); delay(5); - myA4988->setRPM(20); myA4988->doMove(-25); delay(5); - myA4988->setRPM(30); myA4988->doMove( 20); delay(5); - myA4988->setRPM(25); myA4988->doMove(-50); delay(5);// - myA4988->setRPM(50); myA4988->doMove( 40); delay(5); - myA4988->setRPM(25); myA4988->doMove(-20); delay(5); - myA4988->setRPM(25); myA4988->doMove( 15); delay(5); - myA4988->setRPM(50); myA4988->doMove(-40); delay(5); - myA4988->setRPM(48); myA4988->doMove( 35); delay(5); - myA4988->setRPM(45); myA4988->doMove(-25); delay(5); - myA4988->setRPM(41); myA4988->doMove( 25); delay(5); - myA4988->setRPM(38); myA4988->doMove(-26); delay(5); - myA4988->setRPM(41); myA4988->doMove( 25); delay(5); - myA4988->setRPM(25); myA4988->doMove( 25); delay(5); - myA4988->setRPM(41); myA4988->doMove(-35); delay(5); - myA4988->setRPM(38); myA4988->doMove( 25); delay(5); - myA4988->setRPM(36); myA4988->doMove(-25); delay(5); - myA4988->setRPM(33); myA4988->doMove( 25); delay(5); - myA4988->setRPM(30); myA4988->doMove(-27); delay(5); - myA4988->setRPM(33); myA4988->doMove( 25); delay(5); - myA4988->setRPM(33); myA4988->doMove( 25); delay(5); - myA4988->setRPM(38); myA4988->doMove(-30); delay(5); - myA4988->setRPM(18); myA4988->doMove( 10); delay(5); - myA4988->setRPM(18); myA4988->doMove(-10); delay(5); - myA4988->setRPM(25); myA4988->doMove( 20); delay(5); - myA4988->setRPM(20); myA4988->doMove(-25); delay(5); - myA4988->setRPM(30); myA4988->doMove( 20); delay(5); - myA4988->setRPM(25); myA4988->doMove(-50); delay(5); - myA4988->setRPM(20); myA4988->doMove( 25); delay(5); - myA4988->setRPM(30); myA4988->doMove(-20); delay(5); - myA4988->setRPM(25); myA4988->doMove( 50); delay(5); - myA4988->disable(); - AddLog_P2(LOG_LEVEL_INFO, PSTR("Stepper: may the force be with you!")); - ResponseCmndDone(); -} - /*********************************************************************************************\ * Interface \*********************************************************************************************/ -bool Xdrv98(uint8_t function) +bool Xdrv25(uint8_t function) { bool result = false; - if ((pin[GPIO_A4988_DIR] < 99) && (pin[GPIO_A4988_STP] < 99)) { switch (function) { case FUNC_INIT: From 4e1480d0f0ca5c541b52736089cf33abde7c2449 Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sun, 8 Sep 2019 03:28:19 +0200 Subject: [PATCH 06/12] works fine... --- lib/A4988_Stepper/src/A4988_Stepper.cpp | 52 ++++++------ lib/A4988_Stepper/src/A4988_Stepper.h | 31 ++++--- platformio.ini | 3 +- sonoff/i18n.h | 2 - sonoff/my_user_config.h | 14 ++-- sonoff/xdrv_25_A4988_Stepper.ino | 105 ++++++++++++++++-------- 6 files changed, 119 insertions(+), 88 deletions(-) diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp index d44375396..5d723195d 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.cpp +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -1,31 +1,31 @@ -/* 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 - * - * Drives a bipolar motor, controlled by A4988 stepper driver circuit +/* + 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 ) { + , 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 @@ -96,7 +96,7 @@ void A4988_Stepper::adjustMicrosteps() { } void A4988_Stepper::adjustDelay(void) { - motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIS; + motor_delay = 60L * 1000L * 1000L / motor_SPR / motor_RPM / motor_MIS/2; } void A4988_Stepper::setMIS(short oneToSixteen) { @@ -147,8 +147,8 @@ void A4988_Stepper::doMove(long howManySteps) if (now - last_time >= motor_delay) { digitalWrite(motor_stp_pin, lastStepWasHigh?LOW:HIGH); lastStepWasHigh = !lastStepWasHigh; - // remeber step-time if last signal was HIGH we can pull low after 50ms as only HIGH actually moves the stepper - last_time = lastStepWasHigh?now-50:now; + // remeber step-time + last_time = now; if (!lastStepWasHigh) steps_togo--; // same here - only HIGH moves, if pulled LOW step is completed... } } diff --git a/lib/A4988_Stepper/src/A4988_Stepper.h b/lib/A4988_Stepper/src/A4988_Stepper.h index 9aac76859..c03b63c1f 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.h +++ b/lib/A4988_Stepper/src/A4988_Stepper.h @@ -1,19 +1,18 @@ -/* 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 - * - * Drives a bipolar motor, controlled by A4988 stepper driver circuit - */ +/* + 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 diff --git a/platformio.ini b/platformio.ini index f4d99c7ef..50c652c19 100755 --- a/platformio.ini +++ b/platformio.ini @@ -218,7 +218,8 @@ upload_speed = 115200 upload_resetmethod = nodemcu ; *** Upload Serial reset method for Wemos and NodeMCU -upload_port = COM5 +; upload_port = COM5 +upload_port = /dev/cu.wchusbserial1410 extra_scripts = pio/strip-floats.py ; *** Upload file to OTA server using SCP diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 70132f06a..0aba9f63d 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -460,8 +460,6 @@ // Commands xdrv_25_A4988_Stepper.ino #ifdef USE_A4988_Stepper #define D_CMND_MOTOR "MOTOR" - #define D_JSON_MOTOR_COMMAND "Command" - #define D_JSON_MOTOR_VALUE "Value" #define D_JSON_MOTOR_MOVE "doMove" #define D_JSON_MOTOR_ROTATE "doRotate" #define D_JSON_MOTOR_TURN "doTurn" diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index 1d63e842c..7d9c54246 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -57,13 +57,13 @@ #define BOOT_LOOP_OFFSET 1 // [SetOption36] Number of boot loops before starting restoring defaults (0 = disable, 1..200 = boot loops offset) // -- Wifi ---------------------------------------- -#define WIFI_IP_ADDRESS "0.0.0.0" // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address -#define WIFI_GATEWAY "192.168.1.1" // [IpAddress2] If not using DHCP set Gateway IP address -#define WIFI_SUBNETMASK "255.255.255.0" // [IpAddress3] If not using DHCP set Network mask -#define WIFI_DNS "192.168.1.1" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY) +#define WIFI_IP_ADDRESS "172.17." // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address +#define WIFI_GATEWAY "172.17.0.1" // [IpAddress2] If not using DHCP set Gateway IP address +#define WIFI_SUBNETMASK "255.255.192.0" // [IpAddress3] If not using DHCP set Network mask +#define WIFI_DNS "172.17.0.1" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY) -#define STA_SSID1 "" // [Ssid1] Wifi SSID -#define STA_PASS1 "" // [Password1] Wifi password +#define STA_SSID1 "Tim" // [Ssid1] Wifi SSID +#define STA_PASS1 "qwer1234!" // [Password1] Wifi password #define STA_SSID2 "" // [Ssid2] Optional alternate AP Wifi SSID #define STA_PASS2 "" // [Password2] Optional alternate AP Wifi password #define WIFI_CONFIG_TOOL WIFI_RETRY // [WifiConfig] Default tool if wifi fails to connect @@ -527,7 +527,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-Motors-Driver-circuit (+12k7 code) +#define USE_A4988_Stepper // Add support for A4988 Stepper-Motors-Driver-circuit (+10k4 code) /*********************************************************************************************\ * Debug features diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino index e1d6903bf..8f1c5ef17 100644 --- a/sonoff/xdrv_25_A4988_Stepper.ino +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -1,8 +1,6 @@ /* - xsns_22_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota - - Copyright (C) 2019 Nuno Ferreira and Theo Arends + 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 @@ -22,7 +20,7 @@ #include #define XDRV_25 25 -enum A4988Errors { A4988_NO_ERROR, A4988_NO_JSON_COMMAND, A4988_INVALID_JSON}; +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]; @@ -59,53 +57,79 @@ void A4988Init(void) , A4988_ms3_pin ); } -const char kA4988Commands[] PROGMEM = "MOTOR|" - "doMove|doRotate|doTurn|setSPR|setRPM|setMIS"; +const char kA4988Commands[] PROGMEM = "|" + "MOTOR"; void (* const A4988Command[])(void) PROGMEM = { &CmndMOTOR}; uint32_t MOTORCmndJson(void) { - // MOTOR {"Command":"doMove","Value":200} - // MOTOR {"Command":"doRotate","Value":360} - // MOTOR {"Command":"doTurn","Value":1.0} + // 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) { return A4988_INVALID_JSON; } + if (strlen(dataBufUc) < 8) { returnValue =A4988_INVALID_JSON; } DynamicJsonBuffer jsonBuf; JsonObject &json = jsonBuf.parseObject(dataBufUc); - if (!json.success()) { return A4988_INVALID_JSON; } - if (json.containsKey(D_JSON_MOTOR_MOVE )){ - long stepsPlease = 50; - stepsPlease = strtoul(json[D_JSON_MOTOR_MOVE],nullptr,10); - myA4988->doMove(stepsPlease); - } else if (json.containsKey(D_JSON_MOTOR_ROTATE )){ - long degrsPlease = 45; - degrsPlease = strtoul(json[D_JSON_MOTOR_ROTATE],nullptr,10); - myA4988->doRotate(degrsPlease); - } else if (json.containsKey(D_JSON_MOTOR_TURN )){ - float turnsPlease = 0.25; - turnsPlease = strtod(json[D_JSON_MOTOR_TURN],nullptr); - myA4988->doTurn(turnsPlease); - } else if (json.containsKey(D_JSON_MOTOR_SPR )){ - int howManySteps =strtoul(json[D_JSON_MOTOR_SPR],nullptr,10); - myA4988->setSPR(howManySteps); - } else if (json.containsKey(D_JSON_MOTOR_RPM )){ - int howManyRounds =strtoul(json[D_JSON_MOTOR_RPM],nullptr,10); - myA4988->setRPM(howManyRounds); - } else if (json.containsKey(D_JSON_MOTOR_MIS )){ - short oneToSixteen =strtoul(json[D_JSON_MOTOR_MIS],nullptr,10); - myA4988->setMIS(oneToSixteen); - } else return A4988_NO_JSON_COMMAND; - return A4988_NO_ERROR; + if (json.success()) { + while (json.count()>0) { + 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; + json.remove(parm_uc); + } + 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; + json.remove(parm_uc); + } + 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; + json.remove(parm_uc); + } + 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; + json.remove(parm_uc); + } + 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; + json.remove(parm_uc); + } + 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; + json.remove(parm_uc); + } + } + } else returnValue =A4988_INVALID_JSON; + return returnValue; } void CmndMOTOR(void){ uint32_t error; if (XdrvMailbox.data_len) { - if (strstr(XdrvMailbox.data, "{") == nullptr) { + if (strstr(XdrvMailbox.data, "}") == nullptr) { error = A4988_NO_JSON_COMMAND; } else { error = MOTORCmndJson(); @@ -117,7 +141,16 @@ void CmndMOTOR(void){ void A4988CmndResponse(uint32_t error){ switch (error) { case A4988_NO_JSON_COMMAND: - ResponseCmndChar(D_JSON_INVALID_JSON); + ResponseCmndChar(PSTR("Kein Commando!")); + 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(); From e0a17cda55f91176a5545e404812d3adaaa58f33 Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sun, 8 Sep 2019 04:01:02 +0200 Subject: [PATCH 07/12] this compiles... --- sonoff/xdrv_25_A4988_Stepper.ino | 8 -------- 1 file changed, 8 deletions(-) diff --git a/sonoff/xdrv_25_A4988_Stepper.ino b/sonoff/xdrv_25_A4988_Stepper.ino index 8f1c5ef17..9ab3d0a95 100644 --- a/sonoff/xdrv_25_A4988_Stepper.ino +++ b/sonoff/xdrv_25_A4988_Stepper.ino @@ -78,50 +78,42 @@ uint32_t MOTORCmndJson(void) DynamicJsonBuffer jsonBuf; JsonObject &json = jsonBuf.parseObject(dataBufUc); if (json.success()) { - while (json.count()>0) { 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; - json.remove(parm_uc); } 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; - json.remove(parm_uc); } 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; - json.remove(parm_uc); } 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; - json.remove(parm_uc); } 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; - json.remove(parm_uc); } 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; - json.remove(parm_uc); } - } } else returnValue =A4988_INVALID_JSON; return returnValue; } From 4533039c9f0622e2ef1fa1005d1646dea49a043b Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sun, 8 Sep 2019 04:02:47 +0200 Subject: [PATCH 08/12] removed my user-settings --- sonoff/my_user_config.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index 7d9c54246..d2a2e1753 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -57,13 +57,13 @@ #define BOOT_LOOP_OFFSET 1 // [SetOption36] Number of boot loops before starting restoring defaults (0 = disable, 1..200 = boot loops offset) // -- Wifi ---------------------------------------- -#define WIFI_IP_ADDRESS "172.17." // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address -#define WIFI_GATEWAY "172.17.0.1" // [IpAddress2] If not using DHCP set Gateway IP address -#define WIFI_SUBNETMASK "255.255.192.0" // [IpAddress3] If not using DHCP set Network mask -#define WIFI_DNS "172.17.0.1" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY) +#define WIFI_IP_ADDRESS "" // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address +#define WIFI_GATEWAY "" // [IpAddress2] If not using DHCP set Gateway IP address +#define WIFI_SUBNETMASK "" // [IpAddress3] If not using DHCP set Network mask +#define WIFI_DNS "" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY) -#define STA_SSID1 "Tim" // [Ssid1] Wifi SSID -#define STA_PASS1 "qwer1234!" // [Password1] Wifi password +#define STA_SSID1 "" // [Ssid1] Wifi SSID +#define STA_PASS1 "" // [Password1] Wifi password #define STA_SSID2 "" // [Ssid2] Optional alternate AP Wifi SSID #define STA_PASS2 "" // [Password2] Optional alternate AP Wifi password #define WIFI_CONFIG_TOOL WIFI_RETRY // [WifiConfig] Default tool if wifi fails to connect From 75abfc5fd9e3534632676d4a2388660c09710762 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 8 Sep 2019 16:57:56 +0200 Subject: [PATCH 09/12] Refactor energy driver detection Refactor energy driver detection and function call --- sonoff/xdrv_03_energy.ino | 14 ++-- sonoff/xdrv_16_tuyamcu.ino | 29 ++++--- sonoff/xnrg_01_hlw8012.ino | 84 +++++++++---------- sonoff/xnrg_02_cse7766.ino | 52 ++++++------ sonoff/xnrg_03_pzem004t.ino | 32 ++++--- sonoff/xnrg_04_mcp39f501.ino | 56 ++++++------- sonoff/xnrg_05_pzem_ac.ino | 32 ++++--- sonoff/xnrg_06_pzem_dc.ino | 32 ++++--- sonoff/xnrg_07_ade7953.ino | 50 +++++------ ...{xnrg_09_sdm120.ino => xnrg_08_sdm120.ino} | 54 ++++++------ sonoff/xnrg_interface.ino | 29 ++++--- 11 files changed, 217 insertions(+), 247 deletions(-) rename sonoff/{xnrg_09_sdm120.ino => xnrg_08_sdm120.ino} (91%) 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/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..be1d4d824 100644 --- a/sonoff/xnrg_interface.ino +++ b/sonoff/xnrg_interface.ino @@ -19,10 +19,12 @@ #ifdef USE_ENERGY_SENSOR +uint8_t xnrg_active_driver_number = 0; + #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 +94,21 @@ 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) +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_driver_number = x; + break; // Stop further driver investigation + } } } - return result; + else if (energy_flg) { + return xnrg_func_ptr[xnrg_active_driver_number](function); + } + return false; } #endif // USE_ENERGY_SENSOR From 612654ffcaca838ded9579fcc6be6c25abe6550b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 8 Sep 2019 17:21:26 +0200 Subject: [PATCH 10/12] Refactor energy driver selection Refactor energy driver selection --- sonoff/xnrg_interface.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sonoff/xnrg_interface.ino b/sonoff/xnrg_interface.ino index be1d4d824..3e36b334b 100644 --- a/sonoff/xnrg_interface.ino +++ b/sonoff/xnrg_interface.ino @@ -19,8 +19,6 @@ #ifdef USE_ENERGY_SENSOR -uint8_t xnrg_active_driver_number = 0; - #ifdef XFUNC_PTR_IN_ROM bool (* const xnrg_func_ptr[])(uint8_t) PROGMEM = { // Energy driver Function Pointers #else @@ -94,19 +92,21 @@ bool (* 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 +uint8_t xnrg_active = 0; + bool XnrgCall(uint8_t function) { if (FUNC_PRE_INIT == function) { for (uint32_t x = 0; x < xnrg_present; x++) { xnrg_func_ptr[x](function); if (energy_flg) { - xnrg_active_driver_number = x; - break; // Stop further driver investigation + xnrg_active = x; + return true; // Stop further driver investigation } } } else if (energy_flg) { - return xnrg_func_ptr[xnrg_active_driver_number](function); + return xnrg_func_ptr[xnrg_active](function); } return false; } From fafb837120305b58855a0be79e8f5bc1a3b2d3d2 Mon Sep 17 00:00:00 2001 From: Tim Leuschner Date: Sun, 8 Sep 2019 17:30:17 +0200 Subject: [PATCH 11/12] removed my changes to platformio&my_user_config, removed the unnessesary digitalwrites in adjustMicrosteps() --- lib/A4988_Stepper/src/A4988_Stepper.cpp | 42 +++++++------------------ lib/A4988_Stepper/src/A4988_Stepper.h | 1 + platformio.ini | 5 ++- sonoff/my_user_config.h | 11 ++++--- sonoff/xdrv_25_A4988_Stepper.ino | 2 +- 5 files changed, 22 insertions(+), 39 deletions(-) diff --git a/lib/A4988_Stepper/src/A4988_Stepper.cpp b/lib/A4988_Stepper/src/A4988_Stepper.cpp index 5d723195d..cbd72390a 100644 --- a/lib/A4988_Stepper/src/A4988_Stepper.cpp +++ b/lib/A4988_Stepper/src/A4988_Stepper.cpp @@ -14,7 +14,7 @@ Drives a bipolar motor, controlled by A4988 stepper driver circuit */ - +// #include "Arduino.h" #include "A4988_Stepper.h" A4988_Stepper::A4988_Stepper( int m_spr @@ -60,35 +60,16 @@ void A4988_Stepper::adjustPins(void) { void A4988_Stepper::adjustMicrosteps() { if ((motor_ms1_pin<99)&&(motor_ms2_pin<99)&&(motor_ms3_pin<99)) { - switch (motor_MIS){ - case 1: - digitalWrite(motor_ms1_pin, LOW); - digitalWrite(motor_ms2_pin, LOW); - digitalWrite(motor_ms3_pin, LOW); - break; - case 2: - digitalWrite(motor_ms1_pin, HIGH); - digitalWrite(motor_ms2_pin, LOW); - digitalWrite(motor_ms3_pin, LOW); - break; - digitalWrite(motor_ms1_pin, LOW); - digitalWrite(motor_ms2_pin, HIGH); - digitalWrite(motor_ms3_pin, LOW); - case 4: - digitalWrite(motor_ms1_pin, HIGH); - digitalWrite(motor_ms2_pin, HIGH); - digitalWrite(motor_ms3_pin, LOW); - break; - case 8: - digitalWrite(motor_ms1_pin, LOW); - digitalWrite(motor_ms2_pin, LOW); - digitalWrite(motor_ms3_pin, HIGH); - break; - case 16: - digitalWrite(motor_ms1_pin, HIGH); - digitalWrite(motor_ms2_pin, HIGH); - digitalWrite(motor_ms3_pin, HIGH); - break; + unsigned short i = 0; + while (i < 5){ + if (motor_MIS & (1< Date: Sun, 8 Sep 2019 17:38:13 +0200 Subject: [PATCH 12/12] Reduce TasmotaSerial iRam usage Reduce TasmotaSerial iRam usage for core stage and Pre-2.6 (#6373) --- lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/README.md | 0 .../examples/swsertest/swsertest.ino | 0 .../keywords.txt | 0 .../library.json | 2 +- .../library.properties | 2 +- .../src/TasmotaSerial.cpp | 7 +++++++ .../src/TasmotaSerial.h | 0 7 files changed, 9 insertions(+), 2 deletions(-) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/README.md (100%) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/examples/swsertest/swsertest.ino (100%) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/keywords.txt (100%) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/library.json (94%) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/library.properties (94%) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/src/TasmotaSerial.cpp (96%) rename lib/{TasmotaSerial-2.3.3 => TasmotaSerial-2.3.4}/src/TasmotaSerial.h (100%) 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 96% rename from lib/TasmotaSerial-2.3.3/src/TasmotaSerial.cpp rename to lib/TasmotaSerial-2.3.4/src/TasmotaSerial.cpp index 88151a8f1..472425336 100644 --- a/lib/TasmotaSerial-2.3.3/src/TasmotaSerial.cpp +++ b/lib/TasmotaSerial-2.3.4/src/TasmotaSerial.cpp @@ -27,6 +27,9 @@ extern "C" { #include +// for STAGE and pre-2.6, we can have a single wrapper using attachInterruptArg() +void ICACHE_RAM_ATTR callRxRead(void *self) { ((TasmotaSerial*)self)->rxRead(); }; + // As the Arduino attachInterrupt has no parameter, lists of objects // and callbacks corresponding to each possible GPIO pins have to be defined TasmotaSerial *tms_obj_list[16]; @@ -105,7 +108,11 @@ TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fal m_bit_time = ESP.getCpuFreqMHz() * 1000000 / TM_SERIAL_BAUDRATE; pinMode(m_rx_pin, INPUT); tms_obj_list[m_rx_pin] = this; +#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_2) || defined(ARDUINO_ESP8266_RELEASE_2_5_2) attachInterrupt(m_rx_pin, ISRList[m_rx_pin], (m_nwmode) ? CHANGE : FALLING); +#else + attachInterruptArg(m_rx_pin, callRxRead, this, (m_nwmode) ? CHANGE : FALLING); +#endif } if (m_tx_pin > -1) { pinMode(m_tx_pin, OUTPUT); 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