From b5a53b41ada10c9c5389f67b5bd60e3f7c7fa96c Mon Sep 17 00:00:00 2001 From: Shashank Agarwal <48916902+Witty-Wizard@users.noreply.github.com> Date: Thu, 10 Oct 2024 23:41:35 +0530 Subject: [PATCH] Enhancement: Fixed a Bug - handled the case where value is greater than 255 - added doc strings for documentation --- src/HBridge.cpp | 13 +++---------- src/HBridge.h | 45 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/HBridge.cpp b/src/HBridge.cpp index dcf7eeb..fb343b5 100644 --- a/src/HBridge.cpp +++ b/src/HBridge.cpp @@ -12,15 +12,8 @@ void HBridge::begin() void HBridge::write(int16_t value) { - value = value % 256; - if (value >= 0) - { - digitalWrite(_dir_pin, LOW); - } - else - { - digitalWrite(_dir_pin, HIGH); - value = 255 + value; - } + value = constrain(value, -255, 255); + digitalWrite(_dir_pin, (value > 0) ? LOW : HIGH); + value = (value > 0) ? value : 255 + value; analogWrite(_pin, value); } \ No newline at end of file diff --git a/src/HBridge.h b/src/HBridge.h index ddeb029..4d970b1 100644 --- a/src/HBridge.h +++ b/src/HBridge.h @@ -1,6 +1,6 @@ /** * @file HBridge.h - * @brief Header file for the HBridge class. + * @brief Header file for the HBridge class, which controls an H-Bridge motor driver. */ #pragma once @@ -9,13 +9,42 @@ #include "DriveMaster.h" -class HBridge : public DriveMaster{ - private: - +/** + * @class HBridge + * @brief A class to control an H-Bridge motor driver. + * + * The HBridge class provides methods to initialize and control the speed and direction + * of a motor using an H-Bridge. It inherits from the DriveMaster base class, which + * provides the control pins. + */ +class HBridge : public DriveMaster { public: - explicit HBridge(int pin, int dir_pin); - void begin() override; - void write(int16_t value) override; + /** + * @brief Constructs an HBridge object with specified control pins. + * + * @param pin The PWM pin used for controlling motor speed (inherited from DriveMaster). + * @param dir_pin The direction control pin (inherited from DriveMaster). + */ + explicit HBridge(int pin, int dir_pin); + + /** + * @brief Initializes the H-Bridge driver. + * + * Sets up the necessary pin modes for motor control. + */ + void begin() override; + + /** + * @brief Writes a speed value to the motor. + * + * The value controls both the speed and direction of the motor. + * Positive values set forward direction, while negative values set reverse direction. + * The value is constrained between -255 and 255. + * + * @param value The speed value to write, where 0 is stopped, 255 is full forward, + * and -255 is full reverse. + */ + void write(int16_t value) override; }; -#endif \ No newline at end of file +#endif // HBRIDGE_H