From 46ad5166e1b02472f5ff26614b8154501d3569c0 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 19 Feb 2020 21:55:20 -0800 Subject: [PATCH 1/2] Add Wemos DC mottor shild (v1.0.0) support --- I2CDEVICES.md | 1 + tasmota/my_user_config.h | 3 + tasmota/support_features.ino | 4 +- tasmota/tasmota_post.h | 3 + tasmota/xdrv_34_wemos_motor_v1.ino | 188 +++++++++++++++++++++++++++++ 5 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 tasmota/xdrv_34_wemos_motor_v1.ino diff --git a/I2CDEVICES.md b/I2CDEVICES.md index c7b42108f..57e906af7 100644 --- a/I2CDEVICES.md +++ b/I2CDEVICES.md @@ -64,4 +64,5 @@ Index | Define | Driver | Device | Address(es) | Description 41 | USE_DHT12 | xsns_58 | DHT12 | 0x5C | Temperature and humidity sensor 42 | USE_DS1624 | xsns_59 | DS1621 | 0x48 - 0x4F | Temperature sensor 42 | USE_DS1624 | xsns_59 | DS1624 | 0x48 - 0x4F | Temperature sensor + 43 | USE_WEMOS_MOTOR_V1 | xdrv_34 | | 0x30 | WEMOS motor shield v1.0.0 (6612FNG) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 67ab1cf1c..af3295928 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -501,6 +501,9 @@ #define MTX_ADDRESS7 0x00 // [DisplayAddress7] I2C address of seventh 8x8 matrix module #define MTX_ADDRESS8 0x00 // [DisplayAddress8] I2C address of eigth 8x8 matrix module // #define USE_DISPLAY_SH1106 // [DisplayModel 7] [I2cDriver6] Enable SH1106 Oled 128x64 display (I2C addresses 0x3C and 0x3D) + #define USE_WEMOS_MOTOR_V1 + #define USE_WEMOS_MOTOR_V1_ADDR 0x30 + #define USE_WEMOS_MOTOR_V1_FREQ 1000 #endif // USE_I2C // -- SPI sensors --------------------------------- diff --git a/tasmota/support_features.ino b/tasmota/support_features.ino index 2dc25b8e5..224ef86f6 100644 --- a/tasmota/support_features.ino +++ b/tasmota/support_features.ino @@ -513,8 +513,10 @@ void GetFeatures(void) #ifdef USE_LE01MR feature5 |= 0x08000000; // xnrg_13_fif_le01mr.ino #endif +#ifdef USE_WEMOS_MOTOR_V1 + feature5 |= 0x10000000; // xdrv_34_wemos_motor_v1.ino +#endif -// feature5 |= 0x10000000; // feature5 |= 0x20000000; // feature5 |= 0x40000000; // feature5 |= 0x80000000; diff --git a/tasmota/tasmota_post.h b/tasmota/tasmota_post.h index 615ff8919..33e9c200b 100644 --- a/tasmota/tasmota_post.h +++ b/tasmota/tasmota_post.h @@ -221,6 +221,9 @@ extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack //#define USE_ARDUINO_SLAVE // Add support for Arduino Uno/Pro Mini via serial interface including flashing (+2k3 code, 44 mem) #undef DEBUG_THEO // Disable debug code #undef USE_DEBUG_DRIVER // Disable debug code +#define USE_WEMOS_MOTOR_V1 + #define USE_WEMOS_MOTOR_V1_ADDR 0x30 + #define USE_WEMOS_MOTOR_V1_FREQ 1000 #endif // FIRMWARE_SENSORS /*********************************************************************************************\ diff --git a/tasmota/xdrv_34_wemos_motor_v1.ino b/tasmota/xdrv_34_wemos_motor_v1.ino new file mode 100644 index 000000000..fbf03d66d --- /dev/null +++ b/tasmota/xdrv_34_wemos_motor_v1.ino @@ -0,0 +1,188 @@ +/* + xdrv_34_wemos_motor_v1.ino - Support for I2C WEMOS motor shield (6612FNG) + + Copyright (C) 2020 Andre Thomas 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_I2C +#ifdef USE_WEMOS_MOTOR_V1 +/*********************************************************************************************\ + * WEMOS_MOTOR_V1 - DC motor driver shield (6612FNG) v 1.0.0 + * + * I2C Address: 0x30 + * Command format: + * driver43 ,,{,} + * command: + * RESET - reset a motor shield + * SETMOTOR - seter motor state + * motor: + * 0 - motor A + * 1 - motor B + * direction: + * 0 - short break + * 1 - CCW + * 2 - CW + * 3 - stop + * 4 - standby + * duty (optional): + * 0 - 100% (100% by default) +\*********************************************************************************************/ + +#define XDRV_34 34 +#define XI2C_43 43 // See I2CDEVICES.md + +#define MOTOR_A 0 +#define MOTOR_B 1 + +#define SHORT_BRAKE 0 +#define CCW 1 +#define CW 2 +#define STOP 3 +#define STANDBY 4 + +bool wemos_driver_detected = false; + +uint8_t _motor; + +void Wemos_Driver_Detect(void) +{ + if (!I2cActive(USE_WEMOS_MOTOR_V1_ADDR)) + { + I2cSetActiveFound(USE_WEMOS_MOTOR_V1_ADDR, "WEMOS_MOTOR_V1"); + wemos_driver_detected = true; + Motor_Reset(); + return; + } +} + +void Motor_Reset(void) +{ + Wire.begin(); + Motor_SetFreq(USE_WEMOS_MOTOR_V1_FREQ); + Response_P(PSTR("{\"WEMOS_MOTOR_V1\":{\"RESET\":\"OK\"}}")); +} + +void Motor_SetFreq(uint32_t freq) +{ + Wire.beginTransmission(USE_WEMOS_MOTOR_V1_ADDR); + Wire.write(((byte)(freq >> 16)) & (byte)0x0f); + Wire.write((byte)(freq >> 16)); + Wire.write((byte)(freq >> 8)); + Wire.write((byte)freq); + Wire.endTransmission(); // stop transmitting + delay(100); +} + +void Motor_SetMotor(uint8_t motor, uint8_t dir, float pwm_val) +{ + uint16_t _pwm_val; + Wire.beginTransmission(USE_WEMOS_MOTOR_V1_ADDR); + Wire.write(motor | (byte)0x10); + Wire.write(dir); + + _pwm_val = uint16_t(pwm_val * 100); + + if (_pwm_val > 10000) + _pwm_val = 10000; + + Wire.write((byte)(_pwm_val >> 8)); + Wire.write((byte)_pwm_val); + Wire.endTransmission(); + + delay(100); + + Response_P(PSTR("{\"WEMOS_MOTOR_V1\":{\"SETMOTOR\":\"OK\"}}")); +} + +bool Motor_Command(void) +{ + uint8_t args_count = 0; + if (XdrvMailbox.data_len > 0) + { + args_count = 1; + } + else + { + return false; + } + + for (uint32_t idx = 0; idx < XdrvMailbox.data_len; idx++) + { + if (' ' == XdrvMailbox.data[idx]) + { + XdrvMailbox.data[idx] = ','; + } + if (',' == XdrvMailbox.data[idx]) + { + args_count++; + } + } + UpperCase(XdrvMailbox.data, XdrvMailbox.data); + + char *command = strtok(XdrvMailbox.data, ","); + + if (strcmp(command, "RESET") == 0) + { + Motor_Reset(); + return true; + } + + if (strcmp(command, "SETMOTOR") == 0) + { + if (args_count >= 3) + { + + int motor = atoi(strtok(NULL, ",")); + int dir = atoi(strtok(NULL, ",")); + int duty = 100; + + if (args_count == 4) + { + duty = atoi(strtok(NULL, ",")); + } + + Motor_SetMotor(motor, dir, duty); + + return true; + } + } + return false; +} + +bool Xdrv34(uint8_t function) +{ + if (!I2cEnabled(XI2C_43)) + { + return false; + } + bool result = false; + if (FUNC_INIT == function) + { + Wemos_Driver_Detect(); + result = wemos_driver_detected; + } + else if (wemos_driver_detected) + { + if (FUNC_COMMAND_DRIVER == function && XI2C_43 == XdrvMailbox.index) + { + result = Motor_Command(); + } + } + return result; +} + +#endif // USE_WEMOS_MOTOR_V1 +#endif // USE_IC2 From 5f1dac2119de6676f8cf410d9c40ffe3ed2042c4 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 22 Feb 2020 11:01:47 +0100 Subject: [PATCH 2/2] Update my_user_config.h --- tasmota/my_user_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 15f8e3a3f..8bf359605 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -502,7 +502,7 @@ #define MTX_ADDRESS7 0x00 // [DisplayAddress7] I2C address of seventh 8x8 matrix module #define MTX_ADDRESS8 0x00 // [DisplayAddress8] I2C address of eigth 8x8 matrix module // #define USE_DISPLAY_SH1106 // [DisplayModel 7] [I2cDriver6] Enable SH1106 Oled 128x64 display (I2C addresses 0x3C and 0x3D) - #define USE_WEMOS_MOTOR_V1 +// #define USE_WEMOS_MOTOR_V1 #define USE_WEMOS_MOTOR_V1_ADDR 0x30 #define USE_WEMOS_MOTOR_V1_FREQ 1000 #endif // USE_I2C