From c20ee14d857e77900ffeb149da128f1cfd14b332 Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Thu, 9 Feb 2023 11:39:47 +0100 Subject: [PATCH] Initial commit --- include/motor.h | 36 ++++++++++++++++++++++++ library.json | 26 +++++++++++++++++ src/motor.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 include/motor.h create mode 100644 library.json create mode 100644 src/motor.cpp diff --git a/include/motor.h b/include/motor.h new file mode 100644 index 0000000..76a24fa --- /dev/null +++ b/include/motor.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include "shiftregister.h" + +class Motor { + public: + enum Direction : byte{ + None, + Left, + Right + }; + + Motor(ShiftRegister* shiftRegister, uint8_t pwmPin, uint8_t dirPinA, uint8_t dirPinB); + Motor(uint8_t pwmPin, uint8_t dirPinA, uint8_t dirPinB); + + void stop(); + virtual bool start(); + + void setDirection(Direction direction) { this->direction = direction; }; + void setSpeed(uint8_t speed) { this->speed = speed; } + + protected: + Direction direction = Direction::None; + void configureMotor(); + + private: + ShiftRegister* shiftRegister = nullptr; + + + uint8_t pwmPin; + uint8_t dirPinA; + uint8_t dirPinB; + uint8_t speed = 0; +}; \ No newline at end of file diff --git a/library.json b/library.json new file mode 100644 index 0000000..c55c405 --- /dev/null +++ b/library.json @@ -0,0 +1,26 @@ +{ + "name": "Motor", + "version": "1.0.0", + "description": "Control Motor with simple Class.", + "keywords": "motor, direction, engine", + "repository": + { + "type": "git", + "url": "https://github.com/username/hello-world.git" + }, + "authors": + [ + { + "name": "Alexander Klein", + "email": "alex@kleiax.de", + "url": "https://www.kleiax.de/contact" + } + ], + "license": "MIT", + "homepage": "https://www.kleiax.de/", + "dependencies": { + "https://git.kleiax.de/PlatformIO-Libs/Shiftregister.git#v1.0": "v1.0" + }, + "frameworks": "*", + "platforms": "*" + } \ No newline at end of file diff --git a/src/motor.cpp b/src/motor.cpp new file mode 100644 index 0000000..e8ce833 --- /dev/null +++ b/src/motor.cpp @@ -0,0 +1,75 @@ +#include "motor.h" + +Motor::Motor(ShiftRegister* shiftRegister, uint8_t pwmPin, + uint8_t dirPinA, uint8_t dirPinB) { + this->shiftRegister = shiftRegister; + this->pwmPin = pwmPin; + this->dirPinA = dirPinA; + this->dirPinB = dirPinB; + + pinMode(this->pwmPin, OUTPUT); + this->stop(); +} + +Motor::Motor(uint8_t pwmPin, uint8_t dirPinA, uint8_t dirPinB) { + this->pwmPin = pwmPin; + this->dirPinA = dirPinA; + this->dirPinB = dirPinB; + + pinMode(this->dirPinA, OUTPUT); + pinMode(this->dirPinB, OUTPUT); + pinMode(this->pwmPin, OUTPUT); + this->stop(); +} + + +void Motor::stop() { + if (this->shiftRegister) { + this->shiftRegister->writePin(this->dirPinA, LOW); + this->shiftRegister->writePin(this->dirPinB, LOW); + } else { + digitalWrite(this->dirPinA, LOW); + digitalWrite(this->dirPinB, LOW); + } + analogWrite(this->pwmPin, LOW); +} + +bool Motor::start() { + this->configureMotor(); + return true; +} + +void Motor::configureMotor() { + uint8_t pinA, pinB; + + switch (this->direction) { + case Direction::Left : + pinA = HIGH; + pinB = LOW; + break; + + case Direction::Right : + pinA = LOW; + pinB = HIGH; + break; + + case Direction::None : + pinA = LOW; + pinB = LOW; + break; + + default: + pinA = LOW; + pinB = LOW; + break; + } + + if (this->shiftRegister) { + this->shiftRegister->writePin(this->dirPinA, pinA); + this->shiftRegister->writePin(this->dirPinB, pinB); + } else { + digitalWrite(this->dirPinA, pinA); + digitalWrite(this->dirPinB, pinB); + } + analogWrite(this->pwmPin, this->speed); +}