Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Outputの抽象クラスを実装 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Jul 20, 2022
1 parent ec1d426 commit 9a5ec40
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
9 changes: 8 additions & 1 deletion include/ssr/AnalogOut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

#include <Arduino.h>
#include "ssr/Types.hpp"
#include "ssr/Output.hpp"

// このライブラリが使う名前空間
namespace ssr {

// アナログ出力ピンを扱う
class AnalogOut {
class AnalogOut : public Output<uint16_t> {
private:
// ピンに出力した値
uint16_t _value;
Expand Down Expand Up @@ -42,6 +43,12 @@ class AnalogOut {
* @return 最後に出力した値
*/
uint16_t getValue();

/**
* 値を出力する
* @param uint16_t value 出力する値
*/
void write(uint16_t value) override;
};

}
Expand Down
9 changes: 8 additions & 1 deletion include/ssr/DigitalOut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

#include <Arduino.h>
#include "ssr/Types.hpp"
#include "ssr/Output.hpp"

// このライブラリが使う名前空間
namespace ssr {

// デジタル出力ピンを扱う
class DigitalOut {
class DigitalOut : Output<bool> {
private:
// 設定した値
bool _value;
Expand Down Expand Up @@ -43,6 +44,12 @@ class DigitalOut {
*/
void setValue(bool value);

/**
* 値を出力する
* @param bool value 出力する値
*/
void write(bool value) override;

// 最後に設定した値と逆の値を書き込む
void toggle();
};
Expand Down
9 changes: 8 additions & 1 deletion include/ssr/MotorDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#include <Arduino.h>
#include "ssr/AnalogOut.hpp"
#include "ssr/DigitalOut.hpp"
#include "ssr/Output.hpp"

// このライブラリが使う名前空間
namespace ssr {

// モータードライバを使う MD10C R3(CYTRON TECHNOLOGY)対応
class MotorDriver {
class MotorDriver : public Output<int16_t> {
public:
// PWMピン
AnalogOut pwm;
Expand Down Expand Up @@ -39,6 +40,12 @@ class MotorDriver {
* @param int16_t 出力するパワー。範囲は-255~255
*/
void setPower(int16_t power);

/**
* パワーを出力する
* @param int16_t value 出力するパワー。範囲は-255~255
*/
void write(int16_t value) override;
};

}
Expand Down
24 changes: 24 additions & 0 deletions include/ssr/Output.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#ifndef SSR_OUTPUT_HPP
#define SSR_OUTPUT_HPP

// このライブラリが使う名前空間
namespace ssr {

/**
* 出力するもの全般の抽象型
* @tparam ArgT 出力する値の型
*/
template<typename ArgT> class Output {
public:
/**
* 値を出力する
* @param ArgT value 出力する値
*/
virtual void write(ArgT value) = 0;
};

} /* namespace ssr */

#endif /* SSR_OUTPUT_HPP */
4 changes: 4 additions & 0 deletions src/ssr/AnalogOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ void ssr::AnalogOut::setValue(uint16_t value) {

uint16_t ssr::AnalogOut::getValue() {
return _value;
}

void ssr::AnalogOut::write(uint16_t value) {
setValue(value);
}
4 changes: 4 additions & 0 deletions src/ssr/DigitalOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void ssr::DigitalOut::setValue(bool value) {
digitalWrite(pin, _value);
}

void ssr::DigitalOut::write(bool value) {
setValue(value);
}

void ssr::DigitalOut::toggle() {
setValue(!_value);
}
4 changes: 4 additions & 0 deletions src/ssr/MotorDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ void ssr::MotorDriver::setPower(int16_t power) {
dir.setValue(power >= 0);
pwm.setValue(abs(power));
}

void ssr::MotorDriver::write(int16_t value) {
setPower(value);
}

0 comments on commit 9a5ec40

Please sign in to comment.