Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#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;
|
||||
};
|
||||
@@ -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": "*"
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user