From 74844db93822863d8029b1a572be984771d776f7 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 14:18:30 +0900 Subject: [PATCH 01/30] Update argument order of castToRaw for uniform --- include/ics3/angle.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/ics3/angle.hpp b/include/ics3/angle.hpp index 13aa425..c9f0707 100644 --- a/include/ics3/angle.hpp +++ b/include/ics3/angle.hpp @@ -64,7 +64,7 @@ namespace ics { } inline void Angle::set(double angle) { - setRaw(castToRaw(angle, rawCalibration)); // throw std::out_of_range + setRaw(castToRaw(rawCalibration, angle)); // throw std::out_of_range } inline Angle& Angle::operator=(double angle) { @@ -81,12 +81,12 @@ namespace ics { } constexpr Angle::Angle(double calibration, double angle) - : rawData {checkInvalidAngle(castToRaw(angle, calibration))}, // throw std::out_of_range + : rawData {checkInvalidAngle(castToRaw(calibration, angle))}, // throw std::out_of_range rawCalibration {calibration} {} - constexpr uint16_t Angle::castToRaw(double angle, double calibration) noexcept { - return static_cast(angle * calibration + MID); + constexpr uint16_t Angle::castToRaw(double calibration, double angle) noexcept { + return static_cast((calibration * angle) + MID); } constexpr uint16_t Angle::checkInvalidAngle(uint16_t raw) { From f32d3783025e9ccd12fe0e92e27f514d9f982060 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 14:37:25 +0900 Subject: [PATCH 02/30] Update type of Baudrate member and add type alias --- include/ics3/baudrate.hpp | 15 ++++++++------- src/test.cpp | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/ics3/baudrate.hpp b/include/ics3/baudrate.hpp index c9e2d41..edd7537 100644 --- a/include/ics3/baudrate.hpp +++ b/include/ics3/baudrate.hpp @@ -6,16 +6,17 @@ namespace ics { class Baudrate { public: + using type = uint8_t; static constexpr const Baudrate RATE115200() noexcept; //static constexpr Baudrate RATE625000() noexcept; //static constexpr Baudrate RATE1250000() noexcept; - constexpr uint16_t get() const noexcept; - constexpr operator uint16_t() const noexcept; + constexpr type get() const noexcept; + constexpr operator type() const noexcept; constexpr speed_t getSpeed() const noexcept; private: - explicit constexpr Baudrate(uint16_t, speed_t) noexcept; + explicit constexpr Baudrate(type, speed_t) noexcept; - const uint16_t romdata; + const type romdata; const speed_t baudrate; }; @@ -23,11 +24,11 @@ namespace ics { return Baudrate {10, B115200}; } - constexpr uint16_t Baudrate::get() const noexcept { + constexpr Baudrate::type Baudrate::get() const noexcept { return romdata; } - constexpr Baudrate::operator uint16_t() const noexcept { + constexpr Baudrate::operator Baudrate::type() const noexcept { return get(); } @@ -35,7 +36,7 @@ namespace ics { return baudrate; } - constexpr Baudrate::Baudrate(uint16_t romdata, speed_t baudrate) noexcept + constexpr Baudrate::Baudrate(type romdata, speed_t baudrate) noexcept : romdata {romdata}, baudrate {baudrate} {} diff --git a/src/test.cpp b/src/test.cpp index 57e6a60..9a3e7bb 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -86,6 +86,7 @@ void testBaudrate() { static_assert(baudrate115200 == 10, "Baudrate: not equal B115200"); static_assert(baudrate115200.get() == 10, "Baudrate: romdata error"); static_assert(baudrate115200.getSpeed() == B115200, "Baudrate: getSpeed method error"); + static_assert(static_cast(baudrate115200) == 10, "cast to Baudrate::type(uint8_t)"); } void testEepParam() { From 08185e49cca1fe02936b4425570290770ee118b8 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 14:39:34 +0900 Subject: [PATCH 03/30] Update argument with const, but want move input --- include/ics3/check_invalid.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ics3/check_invalid.hpp b/include/ics3/check_invalid.hpp index c418b75..8c058e7 100644 --- a/include/ics3/check_invalid.hpp +++ b/include/ics3/check_invalid.hpp @@ -5,7 +5,7 @@ namespace ics { template - constexpr T checkInvalidRange(T input, T min, T max) { + constexpr T checkInvalidRange(T input, const T min, const T max) { return input < min ? throw std::out_of_range {"Too small argument"} : max < input ? throw std::out_of_range {"Too big argument"} : input; From 2f6c171ed04435b8ff7b268073a70f47ff22953c Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:01:00 +0900 Subject: [PATCH 04/30] Update to use const iterator --- include/ics3/eeprom.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/ics3/eeprom.hpp b/include/ics3/eeprom.hpp index 79d513b..11e4c7c 100644 --- a/include/ics3/eeprom.hpp +++ b/include/ics3/eeprom.hpp @@ -13,7 +13,6 @@ namespace ics { public: EepParam get(EepParam) const; void set(const EepParam&) noexcept; - template void write(Iter&&) const; private: @@ -33,7 +32,7 @@ namespace ics { template inline void EepRom::write(Iter&& dest) const { - std::copy(data.begin(), data.end(), dest); + std::copy(data.cbegin(), data.cend(), dest); } inline EepRom::EepRom(const std::array& src) From ccb3bcd6a991db458f48f80f87e87c91e0580a9f Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:06:07 +0900 Subject: [PATCH 05/30] Add type alias --- include/ics3/parameter.hpp | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/include/ics3/parameter.hpp b/include/ics3/parameter.hpp index 7a3d614..9f988cf 100644 --- a/include/ics3/parameter.hpp +++ b/include/ics3/parameter.hpp @@ -6,68 +6,69 @@ namespace ics { class Parameter { public: - static constexpr Parameter stretch(uint8_t = 30); - static constexpr Parameter speed(uint8_t = 127); - static constexpr Parameter current(uint8_t = 63); - static constexpr Parameter temperature(uint8_t = 80); - static constexpr Parameter newParameter(const Parameter&, uint8_t); - - constexpr uint8_t get() const noexcept; - constexpr operator uint8_t() const noexcept; - void set(uint8_t); - Parameter& operator=(uint8_t); - constexpr uint8_t getSubcommand() const noexcept; + using type = uint8_t; + static constexpr Parameter stretch(type = 30); + static constexpr Parameter speed(type = 127); + static constexpr Parameter current(type = 63); + static constexpr Parameter temperature(type = 80); + static constexpr Parameter newParameter(const Parameter&, type); + + constexpr type get() const noexcept; + constexpr operator type() const noexcept; + void set(type); + Parameter& operator=(type); + constexpr type getSubcommand() const noexcept; private: - explicit constexpr Parameter(uint8_t, uint8_t, uint8_t, uint8_t); + explicit constexpr Parameter(type, type, type, type); - const uint8_t sc; - const uint8_t min; - const uint8_t max; - uint8_t data; + const type sc; + const type min; + const type max; + type data; }; - constexpr Parameter Parameter::stretch(uint8_t data) { + constexpr Parameter Parameter::stretch(type data) { return Parameter {0x01, 1, 127, data}; } - constexpr Parameter Parameter::speed(uint8_t data) { + constexpr Parameter Parameter::speed(type data) { return Parameter {0x02, 1, 127, data}; } - constexpr Parameter Parameter::current(uint8_t data) { + constexpr Parameter Parameter::current(type data) { return Parameter {0x03, 0, 63, data}; } - constexpr Parameter Parameter::temperature(uint8_t data) { + constexpr Parameter Parameter::temperature(type data) { return Parameter {0x04, 1, 127, data}; } - constexpr Parameter Parameter::newParameter(const Parameter& base, uint8_t data) { + constexpr Parameter Parameter::newParameter(const Parameter& base, type data) { return Parameter {base.sc, base.min, base.max, data}; } - constexpr uint8_t Parameter::get() const noexcept { + constexpr Parameter::type Parameter::get() const noexcept { return data; } - constexpr Parameter::operator uint8_t() const noexcept { + constexpr Parameter::operator type() const noexcept { return get(); } - inline void Parameter::set(uint8_t input) { + inline void Parameter::set(type input) { data = checkInvalidRange(input, min, max); } - inline Parameter& Parameter::operator=(uint8_t rhs) { + inline Parameter& Parameter::operator=(type rhs) { set(rhs); return *this; } - constexpr uint8_t Parameter::getSubcommand() const noexcept { + constexpr Parameter::type Parameter::getSubcommand() const noexcept { return sc; } - constexpr Parameter::Parameter(uint8_t sc, uint8_t min, uint8_t max, uint8_t defaultData) + constexpr Parameter::Parameter(type sc, type min, type max, type defaultData) : sc {sc}, min {min}, max {max}, From 7249a7deef44480b476b5efc213c351916c4f9be Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:19:37 +0900 Subject: [PATCH 06/30] Add alias of raw data --- include/ics3/angle.hpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/include/ics3/angle.hpp b/include/ics3/angle.hpp index c9f0707..d9f636c 100644 --- a/include/ics3/angle.hpp +++ b/include/ics3/angle.hpp @@ -8,13 +8,14 @@ namespace ics { class Angle { friend ICS3; // for touch setRaw public: + using rawType = uint16_t; static constexpr Angle newDegree(double = 0.0); static constexpr Angle newRadian(double = 0.0); static constexpr Angle newSameUnit(const Angle&, double = 0.0); static constexpr Angle newCalibration(double, double = 0.0); - static constexpr uint16_t MIN {3500}; - static constexpr uint16_t MID {7500}; - static constexpr uint16_t MAX {11500}; + static constexpr rawType MIN {3500}; + static constexpr rawType MID {7500}; + static constexpr rawType MAX {11500}; static constexpr double PI {3.141592653589793}; Angle(const Angle&) noexcept = default; @@ -23,14 +24,14 @@ namespace ics { constexpr operator double() const noexcept; void set(double); Angle& operator=(double); - constexpr uint16_t getRaw() const noexcept; - void setRaw(uint16_t); + constexpr rawType getRaw() const noexcept; + void setRaw(rawType); private: constexpr explicit Angle(double, double); - static constexpr uint16_t castToRaw(double, double) noexcept; - static constexpr uint16_t checkInvalidAngle(uint16_t); + static constexpr rawType castToRaw(double, double) noexcept; + static constexpr rawType checkInvalidAngle(rawType); - uint16_t rawData; + rawType rawData; const double rawCalibration; }; @@ -72,11 +73,11 @@ namespace ics { return *this; } - constexpr uint16_t Angle::getRaw() const noexcept { + constexpr Angle::rawType Angle::getRaw() const noexcept { return rawData; } - inline void Angle::setRaw(uint16_t raw) { + inline void Angle::setRaw(rawType raw) { rawData = checkInvalidAngle(raw); // throw std::out_of_range } @@ -85,11 +86,11 @@ namespace ics { rawCalibration {calibration} {} - constexpr uint16_t Angle::castToRaw(double calibration, double angle) noexcept { - return static_cast((calibration * angle) + MID); + constexpr Angle::rawType Angle::castToRaw(double calibration, double angle) noexcept { + return static_cast((calibration * angle) + MID); } - constexpr uint16_t Angle::checkInvalidAngle(uint16_t raw) { + constexpr Angle::rawType Angle::checkInvalidAngle(rawType raw) { return checkInvalidRange(raw, MIN, MAX); } } From 05c16dfc648e20cc4e3e0feb5a7e14816f28ce82 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:22:20 +0900 Subject: [PATCH 07/30] Add alias of interface type --- include/ics3/angle.hpp | 45 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/include/ics3/angle.hpp b/include/ics3/angle.hpp index d9f636c..fe660cf 100644 --- a/include/ics3/angle.hpp +++ b/include/ics3/angle.hpp @@ -9,45 +9,46 @@ namespace ics { friend ICS3; // for touch setRaw public: using rawType = uint16_t; - static constexpr Angle newDegree(double = 0.0); - static constexpr Angle newRadian(double = 0.0); - static constexpr Angle newSameUnit(const Angle&, double = 0.0); - static constexpr Angle newCalibration(double, double = 0.0); + using type = double; + static constexpr Angle newDegree(type = 0.0); + static constexpr Angle newRadian(type = 0.0); + static constexpr Angle newSameUnit(const Angle&, type = 0.0); + static constexpr Angle newCalibration(type, type = 0.0); static constexpr rawType MIN {3500}; static constexpr rawType MID {7500}; static constexpr rawType MAX {11500}; - static constexpr double PI {3.141592653589793}; + static constexpr type PI {3.141592653589793}; Angle(const Angle&) noexcept = default; Angle& operator=(const Angle&) noexcept; - constexpr double get() const noexcept; - constexpr operator double() const noexcept; - void set(double); - Angle& operator=(double); + constexpr type get() const noexcept; + constexpr operator type() const noexcept; + void set(type); + Angle& operator=(type); constexpr rawType getRaw() const noexcept; void setRaw(rawType); private: - constexpr explicit Angle(double, double); - static constexpr rawType castToRaw(double, double) noexcept; + constexpr explicit Angle(type, type); + static constexpr rawType castToRaw(type, type) noexcept; static constexpr rawType checkInvalidAngle(rawType); rawType rawData; - const double rawCalibration; + const type rawCalibration; }; - constexpr Angle Angle::newDegree(double angle) { + constexpr Angle Angle::newDegree(type angle) { return Angle {800.0 / 27.0, angle}; } - constexpr Angle Angle::newRadian(double angle) { + constexpr Angle Angle::newRadian(type angle) { return Angle {16000.0 / 3.0 / PI, angle}; } - constexpr Angle Angle::newSameUnit(const Angle& unit, double angle) { + constexpr Angle Angle::newSameUnit(const Angle& unit, type angle) { return Angle {unit.rawCalibration, angle}; } - constexpr Angle Angle::newCalibration(double calibration, double angle) { + constexpr Angle Angle::newCalibration(type calibration, type angle) { return Angle {calibration, angle}; } @@ -56,19 +57,19 @@ namespace ics { return *this; } - constexpr double Angle::get() const noexcept { + constexpr Angle::type Angle::get() const noexcept { return (rawData - MID) / rawCalibration; } - constexpr Angle::operator double() const noexcept { + constexpr Angle::operator type() const noexcept { return get(); } - inline void Angle::set(double angle) { + inline void Angle::set(type angle) { setRaw(castToRaw(rawCalibration, angle)); // throw std::out_of_range } - inline Angle& Angle::operator=(double angle) { + inline Angle& Angle::operator=(type angle) { set(angle); return *this; } @@ -81,12 +82,12 @@ namespace ics { rawData = checkInvalidAngle(raw); // throw std::out_of_range } - constexpr Angle::Angle(double calibration, double angle) + constexpr Angle::Angle(type calibration, type angle) : rawData {checkInvalidAngle(castToRaw(calibration, angle))}, // throw std::out_of_range rawCalibration {calibration} {} - constexpr Angle::rawType Angle::castToRaw(double calibration, double angle) noexcept { + constexpr Angle::rawType Angle::castToRaw(type calibration, type angle) noexcept { return static_cast((calibration * angle) + MID); } From f5ead26e45c7093b851aea32b865ae5fd30395ce Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:39:02 +0900 Subject: [PATCH 08/30] Add alias of inclusive type --- include/ics3/eepparam.hpp | 151 +++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 75 deletions(-) diff --git a/include/ics3/eepparam.hpp b/include/ics3/eepparam.hpp index 576cb10..cecf016 100644 --- a/include/ics3/eepparam.hpp +++ b/include/ics3/eepparam.hpp @@ -9,7 +9,8 @@ namespace ics { class EepParam { public: - enum Flag : uint16_t { + using type = uint16_t; + enum Flag : type { REVERSE = 0x01, FREE = 0x02, PWMINH = 0x08, @@ -17,152 +18,152 @@ namespace ics { SLAVE = 0x80 }; static constexpr int byteSize {4}; - static constexpr uint16_t mask {0xF}; - - static constexpr EepParam stretch(uint16_t = 60); - static constexpr EepParam speed(uint16_t = 127); - static constexpr EepParam punch(uint16_t = 1); - static constexpr EepParam deadBand(uint16_t = 2); - static constexpr EepParam dumping(uint16_t = 40); - static constexpr EepParam selfTimer(uint16_t = 250); - static constexpr EepParam flag(uint16_t = 0x8C); - static constexpr EepParam pulseMax(uint16_t = 11500); - static constexpr EepParam pulseMin(uint16_t = 3500); - static constexpr EepParam baudrate(uint16_t = 10); - static constexpr EepParam temperature(uint16_t = 80); - static constexpr EepParam current(uint16_t = 63); - static constexpr EepParam response(uint16_t = 0); - static constexpr EepParam userOffset(uint16_t = 0); - static constexpr EepParam id(uint16_t = 0); - static constexpr EepParam strech1(uint16_t = 60); - static constexpr EepParam strech2(uint16_t = 60); - static constexpr EepParam strech3(uint16_t = 60); - static constexpr EepParam newEepParam(const EepParam&, uint16_t); - - constexpr uint16_t get() const noexcept; - constexpr operator uint16_t() const noexcept; - void set(uint16_t); - EepParam& operator=(uint16_t); + static constexpr type mask {0xF}; + + static constexpr EepParam stretch(type = 60); + static constexpr EepParam speed(type = 127); + static constexpr EepParam punch(type = 1); + static constexpr EepParam deadBand(type = 2); + static constexpr EepParam dumping(type = 40); + static constexpr EepParam selfTimer(type = 250); + static constexpr EepParam flag(type = 0x8C); + static constexpr EepParam pulseMax(type = 11500); + static constexpr EepParam pulseMin(type = 3500); + static constexpr EepParam baudrate(type = 10); + static constexpr EepParam temperature(type = 80); + static constexpr EepParam current(type = 63); + static constexpr EepParam response(type = 0); + static constexpr EepParam userOffset(type = 0); + static constexpr EepParam id(type = 0); + static constexpr EepParam strech1(type = 60); + static constexpr EepParam strech2(type = 60); + static constexpr EepParam strech3(type = 60); + static constexpr EepParam newEepParam(const EepParam&, type); + + constexpr type get() const noexcept; + constexpr operator type() const noexcept; + void set(type); + EepParam& operator=(type); void write(std::array&) const noexcept; void read(const std::array&); private: constexpr explicit EepParam( size_t, size_t, - uint16_t, - uint16_t, - uint16_t (*)(uint16_t, uint16_t, uint16_t), - uint16_t + type, + type, + type (*)(type, type, type), + type ); - static constexpr uint16_t checkInvalidRange(uint16_t, uint16_t, uint16_t); - static constexpr uint16_t checkInvalidEvenRange(uint16_t, uint16_t, uint16_t); - static constexpr uint16_t checkInvalidFlag(uint16_t, uint16_t, uint16_t); - static constexpr uint16_t checkInvalidBaudrate(uint16_t, uint16_t, uint16_t); - static constexpr uint16_t checkInvalidOffset(uint16_t, uint16_t, uint16_t); + static constexpr type checkInvalidRange(type, type, type); + static constexpr type checkInvalidEvenRange(type, type, type); + static constexpr type checkInvalidFlag(type, type, type); + static constexpr type checkInvalidBaudrate(type, type, type); + static constexpr type checkInvalidOffset(type, type, type); const size_t offset; const size_t length; - const uint16_t min; - const uint16_t max; - uint16_t (*const setFunc)(uint16_t, uint16_t, uint16_t); - uint16_t data; + const type min; + const type max; + type (*const setFunc)(type, type, type); + type data; }; - constexpr EepParam EepParam::stretch(uint16_t data) { + constexpr EepParam EepParam::stretch(type data) { return EepParam {2, 2, 2, 254, &EepParam::checkInvalidEvenRange, data}; } - constexpr EepParam EepParam::speed(uint16_t data) { + constexpr EepParam EepParam::speed(type data) { return EepParam {4, 2, 1, 127, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::punch(uint16_t data) { + constexpr EepParam EepParam::punch(type data) { return EepParam {6, 2, 0, 10, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::deadBand(uint16_t data) { + constexpr EepParam EepParam::deadBand(type data) { return EepParam {8, 2, 0, 5, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::dumping(uint16_t data) { + constexpr EepParam EepParam::dumping(type data) { return EepParam {10, 2, 1, 255, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::selfTimer(uint16_t data) { + constexpr EepParam EepParam::selfTimer(type data) { return EepParam {12, 2, 10, 255, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::flag(uint16_t data) { + constexpr EepParam EepParam::flag(type data) { return EepParam {14, 2, 0, 255, &EepParam::checkInvalidFlag, data}; } - constexpr EepParam EepParam::pulseMax(uint16_t data) { + constexpr EepParam EepParam::pulseMax(type data) { return EepParam {16, 4, 3500, 11500, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::pulseMin(uint16_t data) { + constexpr EepParam EepParam::pulseMin(type data) { return EepParam {20, 4, 3500, 11500, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::baudrate(uint16_t data) { + constexpr EepParam EepParam::baudrate(type data) { return EepParam {26, 2, 0, 10, &EepParam::checkInvalidBaudrate, data}; } - constexpr EepParam EepParam::temperature(uint16_t data) { + constexpr EepParam EepParam::temperature(type data) { return EepParam {28, 2, 1, 127, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::current(uint16_t data) { + constexpr EepParam EepParam::current(type data) { return EepParam {30, 2, 1, 63, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::response(uint16_t data) { + constexpr EepParam EepParam::response(type data) { return EepParam {50, 2, 1, 5, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::userOffset(uint16_t data) { + constexpr EepParam EepParam::userOffset(type data) { return EepParam {52, 2, 0x81, 127, &EepParam::checkInvalidOffset, data}; // 0x81 is -127 on uint8_t type } - constexpr EepParam EepParam::id(uint16_t data) { + constexpr EepParam EepParam::id(type data) { return EepParam {56, 2, 0, 31, &EepParam::checkInvalidRange, data}; } - constexpr EepParam EepParam::strech1(uint16_t data) { + constexpr EepParam EepParam::strech1(type data) { return EepParam {58, 2, 2, 254, &EepParam::checkInvalidEvenRange, data}; } - constexpr EepParam EepParam::strech2(uint16_t data) { + constexpr EepParam EepParam::strech2(type data) { return EepParam {60, 2, 2, 254, &EepParam::checkInvalidEvenRange, data}; } - constexpr EepParam EepParam::strech3(uint16_t data) { + constexpr EepParam EepParam::strech3(type data) { return EepParam {62, 2, 2, 254, &EepParam::checkInvalidEvenRange, data}; } - constexpr EepParam EepParam::newEepParam(const EepParam& type, uint16_t data) { - return EepParam {type.offset, type.length, type.min, type.max, type.setFunc, data}; + constexpr EepParam EepParam::newEepParam(const EepParam& paramType, type data) { + return EepParam {paramType.offset, paramType.length, paramType.min, paramType.max, paramType.setFunc, data}; } - constexpr uint16_t EepParam::get() const noexcept { + constexpr EepParam::type EepParam::get() const noexcept { return data; } - constexpr EepParam::operator uint16_t() const noexcept { + constexpr EepParam::operator type() const noexcept { return get(); } - inline void EepParam::set(uint16_t input) { + inline void EepParam::set(type input) { data = (*setFunc)(input, min, max); // throw std::invalid_argument, std::out_of_range } - inline EepParam& EepParam::operator=(uint16_t input) { + inline EepParam& EepParam::operator=(type input) { set(input); return *this; } inline void EepParam::write(std::array& dest) const noexcept { - uint16_t nowData {data}; + type nowData {data}; for (size_t i {offset + length - 1}; i >= offset; --i) { dest[i] = nowData & mask; nowData >>= byteSize; @@ -170,7 +171,7 @@ namespace ics { } inline void EepParam::read(const std::array& src) { - uint16_t result {0}; + type result {0}; const size_t loopend = offset + length; for (size_t i {offset}; i < loopend; ++i) { result <<= byteSize; @@ -181,10 +182,10 @@ namespace ics { constexpr EepParam::EepParam( size_t offset, size_t length, - uint16_t min, - uint16_t max, - uint16_t (*setFunc)(uint16_t, uint16_t, uint16_t), - uint16_t data + type min, + type max, + type (*setFunc)(type, type, type), + type data ) : offset(offset), length(length), @@ -194,30 +195,30 @@ namespace ics { data(setFunc(data, min, max)) // throw std::invalid_argument, std::out_of_range {} - constexpr uint16_t EepParam::checkInvalidRange(uint16_t input, uint16_t min, uint16_t max) { + constexpr EepParam::type EepParam::checkInvalidRange(type input, type min, type max) { return ics::checkInvalidRange(input, min, max); } - constexpr uint16_t EepParam::checkInvalidEvenRange(uint16_t input, uint16_t min, uint16_t max) { + constexpr EepParam::type EepParam::checkInvalidEvenRange(type input, type min, type max) { return input % 2 ? throw std::out_of_range {"Must even value"} : checkInvalidRange(input, min, max); // throw std::out_of_range } - constexpr uint16_t EepParam::checkInvalidFlag(uint16_t input, uint16_t, uint16_t) { + constexpr EepParam::type EepParam::checkInvalidFlag(type input, type, type) { return !(input & 0x04) ? throw std::invalid_argument {"Eepparam(flag): Must up bits 0x04"} : ~(input | ~0x60) ? throw std::invalid_argument {"Eepparam(flag): Must down bits 0x60"} : input; } - constexpr uint16_t EepParam::checkInvalidBaudrate(uint16_t input, uint16_t, uint16_t) { + constexpr EepParam::type EepParam::checkInvalidBaudrate(type input, type, type) { return input == Baudrate::RATE115200().get() ? input : //input == Baudrate::RATE625000().get() ? input : //input == Baudrate::RATE1250000().get() ? input : throw std::invalid_argument {"baudrate not exist"}; } - constexpr uint16_t EepParam::checkInvalidOffset(uint16_t input, uint16_t min, uint16_t max) { + constexpr EepParam::type EepParam::checkInvalidOffset(type input, type min, type max) { return input < max ? input : // this min < 0; min value is 0 min < input ? input : // this min < 0; input must is bigger than min. throw std::out_of_range {"Eeprom(offset): range over"}; // min < input < max is failed From 700ea6e9e212abc0b21d07ab079c51d0d060c716 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:48:33 +0900 Subject: [PATCH 09/30] Add alias of size_t --- include/ics3/eepparam.hpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/include/ics3/eepparam.hpp b/include/ics3/eepparam.hpp index cecf016..9b8ef94 100644 --- a/include/ics3/eepparam.hpp +++ b/include/ics3/eepparam.hpp @@ -10,6 +10,7 @@ namespace ics { class EepParam { public: using type = uint16_t; + using size_type = std::size_t; enum Flag : type { REVERSE = 0x01, FREE = 0x02, @@ -48,8 +49,8 @@ namespace ics { void read(const std::array&); private: constexpr explicit EepParam( - size_t, - size_t, + size_type, + size_type, type, type, type (*)(type, type, type), @@ -61,8 +62,8 @@ namespace ics { static constexpr type checkInvalidBaudrate(type, type, type); static constexpr type checkInvalidOffset(type, type, type); - const size_t offset; - const size_t length; + const size_type offset; + const size_type length; const type min; const type max; type (*const setFunc)(type, type, type); @@ -164,7 +165,7 @@ namespace ics { inline void EepParam::write(std::array& dest) const noexcept { type nowData {data}; - for (size_t i {offset + length - 1}; i >= offset; --i) { + for (size_type i {offset + length - 1}; i >= offset; --i) { dest[i] = nowData & mask; nowData >>= byteSize; } @@ -172,16 +173,16 @@ namespace ics { inline void EepParam::read(const std::array& src) { type result {0}; - const size_t loopend = offset + length; - for (size_t i {offset}; i < loopend; ++i) { + const size_type loopend = offset + length; + for (size_type i {offset}; i < loopend; ++i) { result <<= byteSize; result |= src[i] & mask; } set(result); // throw std::invalid_argument, std::out_of_range } constexpr EepParam::EepParam( - size_t offset, - size_t length, + size_type offset, + size_type length, type min, type max, type (*setFunc)(type, type, type), From c8b272a67e26157f284aa5c3eec591dd6b302efc Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 15:59:05 +0900 Subject: [PATCH 10/30] Add alias of size_type and omit magic number --- include/ics3/eeprom.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/ics3/eeprom.hpp b/include/ics3/eeprom.hpp index 11e4c7c..d7bb55a 100644 --- a/include/ics3/eeprom.hpp +++ b/include/ics3/eeprom.hpp @@ -11,14 +11,17 @@ namespace ics { class EepRom { friend ICS3; // for ICS3::getRom() public: + using size_type = std::size_t; + static constexpr size_type length {64}; + EepParam get(EepParam) const; void set(const EepParam&) noexcept; template void write(Iter&&) const; private: - explicit EepRom(const std::array&); + explicit EepRom(const std::array&); - std::array data; + std::array data; }; inline ics::EepParam ics::EepRom::get(EepParam type) const { @@ -35,7 +38,7 @@ namespace ics { std::copy(data.cbegin(), data.cend(), dest); } - inline EepRom::EepRom(const std::array& src) + inline EepRom::EepRom(const std::array& src) : data(src) // for Ubuntu14.04 compiler {} } From c66eeae2723951c53b8020e655b2a9dc00ba7612 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:03:09 +0900 Subject: [PATCH 11/30] Update writing format for too many argument --- include/ics3/eepparam.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/ics3/eepparam.hpp b/include/ics3/eepparam.hpp index 9b8ef94..e3023f8 100644 --- a/include/ics3/eepparam.hpp +++ b/include/ics3/eepparam.hpp @@ -54,8 +54,7 @@ namespace ics { type, type, type (*)(type, type, type), - type - ); + type); static constexpr type checkInvalidRange(type, type, type); static constexpr type checkInvalidEvenRange(type, type, type); static constexpr type checkInvalidFlag(type, type, type); @@ -186,8 +185,7 @@ namespace ics { type min, type max, type (*setFunc)(type, type, type), - type data - ) + type data) : offset(offset), length(length), min(min), From 4f975d1003abfe0547de6b490f2d13b266cd0bb5 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:06:22 +0900 Subject: [PATCH 12/30] Add alias of EepRom Container --- include/ics3/eeprom.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/ics3/eeprom.hpp b/include/ics3/eeprom.hpp index d7bb55a..0069bed 100644 --- a/include/ics3/eeprom.hpp +++ b/include/ics3/eeprom.hpp @@ -13,13 +13,14 @@ namespace ics { public: using size_type = std::size_t; static constexpr size_type length {64}; + using Container = std::array; EepParam get(EepParam) const; void set(const EepParam&) noexcept; template void write(Iter&&) const; private: - explicit EepRom(const std::array&); + explicit EepRom(const Container&); std::array data; }; @@ -38,7 +39,7 @@ namespace ics { std::copy(data.cbegin(), data.cend(), dest); } - inline EepRom::EepRom(const std::array& src) + inline EepRom::EepRom(const Container& src) : data(src) // for Ubuntu14.04 compiler {} } From 8994432d8788fe96bb3e126a7bd1749da47accad Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:09:04 +0900 Subject: [PATCH 13/30] Add alias of id data --- include/ics3/id.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/ics3/id.hpp b/include/ics3/id.hpp index f09dffb..f9650a0 100644 --- a/include/ics3/id.hpp +++ b/include/ics3/id.hpp @@ -6,22 +6,23 @@ namespace ics { class ID { public: - constexpr ID(uint8_t); // ID is non explicit constructor because only do check limit - constexpr uint8_t get() const noexcept; - constexpr operator uint8_t() const noexcept; + using type = uint8_t; + constexpr ID(type); // ID is non explicit constructor because only do check limit + constexpr type get() const noexcept; + constexpr operator type() const noexcept; private: - const uint8_t data; + const type data; }; - constexpr ID::ID(uint8_t id) + constexpr ID::ID(type id) : data {id < 32 ? id : throw std::invalid_argument {"invalid ID: must be 0 <= id <= 31"}} {} - constexpr uint8_t ID::get() const noexcept { + constexpr ID::type ID::get() const noexcept { return data; } - constexpr ID::operator uint8_t() const noexcept { + constexpr ID::operator type() const noexcept { return get(); } } From ec4448e1ccc9d6ae6da4c66d2d6248fbefd0b99b Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:23:50 +0900 Subject: [PATCH 14/30] Add alias of core interface container --- include/core.hpp | 5 +++-- src/core.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/core.hpp b/include/core.hpp index 0742b20..2184adb 100644 --- a/include/core.hpp +++ b/include/core.hpp @@ -9,6 +9,7 @@ namespace ics { class Core { public: + using Container = std::vector; explicit Core(const std::string&, speed_t); // touch by only libics3 ~Core() noexcept; Core(const Core&) = delete; @@ -17,8 +18,8 @@ namespace ics { Core& operator=(Core&&) noexcept; static std::shared_ptr getCore(const std::string&, speed_t); - void communicate(const std::vector&, std::vector&); - void communicateID(const std::vector&, std::vector&); + void communicate(const Container&, Container&); + void communicateID(const Container&, Container&); private: static termios getTermios() noexcept; diff --git a/src/core.cpp b/src/core.cpp index 9c89b4c..a83e287 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -62,7 +62,7 @@ std::shared_ptr ics::Core::getCore(const std::string& path, speed_t b return objPtr; } -void ics::Core::communicate(const std::vector& tx, std::vector& rx) { +void ics::Core::communicate(const Container& tx, Container& rx) { write(fd, tx.data(), tx.size()); // send for (auto& receive : rx) read(fd, &receive, 1); // receive // check section @@ -78,7 +78,7 @@ void ics::Core::communicate(const std::vector& tx, std::vector if ((tx[0] & 0x7F) != *receive) throw std::runtime_error {"Receive failed: invalid target data"}; } -void ics::Core::communicateID(const std::vector& tx, std::vector& rx) { +void ics::Core::communicateID(const Container& tx, Container& rx) { write(fd, tx.data(), tx.size()); // send for (auto& receive : rx) read(fd, &receive, 1); // receive // check section From 8f7dea170cba9b7de9a0dde1b7c7c84e71d6108e Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:26:39 +0900 Subject: [PATCH 15/30] Add alias of container value --- include/core.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/core.hpp b/include/core.hpp index 2184adb..5579a04 100644 --- a/include/core.hpp +++ b/include/core.hpp @@ -2,6 +2,7 @@ #define LIBICS3_ICS3_CORE_H_ #include +#include #include #include #include @@ -9,7 +10,8 @@ namespace ics { class Core { public: - using Container = std::vector; + using value = uint8_t; + using Container = std::vector; explicit Core(const std::string&, speed_t); // touch by only libics3 ~Core() noexcept; Core(const Core&) = delete; From bc7e99042ed7d5455c61584ab6ac4df7d6dabe35 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:35:37 +0900 Subject: [PATCH 16/30] Add alias for id interface --- include/core.hpp | 4 +++- src/core.cpp | 2 +- src/ics3.cpp | 9 +++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/include/core.hpp b/include/core.hpp index 5579a04..a7a3e6d 100644 --- a/include/core.hpp +++ b/include/core.hpp @@ -12,6 +12,8 @@ namespace ics { public: using value = uint8_t; using Container = std::vector; + using IDContainerTx = std::array; + using IDContainerRx = std::array; explicit Core(const std::string&, speed_t); // touch by only libics3 ~Core() noexcept; Core(const Core&) = delete; @@ -21,7 +23,7 @@ namespace ics { static std::shared_ptr getCore(const std::string&, speed_t); void communicate(const Container&, Container&); - void communicateID(const Container&, Container&); + void communicateID(const IDContainerTx&, IDContainerRx&); private: static termios getTermios() noexcept; diff --git a/src/core.cpp b/src/core.cpp index a83e287..5d026f2 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -78,7 +78,7 @@ void ics::Core::communicate(const Container& tx, Container& rx) { if ((tx[0] & 0x7F) != *receive) throw std::runtime_error {"Receive failed: invalid target data"}; } -void ics::Core::communicateID(const Container& tx, Container& rx) { +void ics::Core::communicateID(const IDContainerTx& tx, IDContainerRx& rx) { write(fd, tx.data(), tx.size()); // send for (auto& receive : rx) read(fd, &receive, 1); // receive // check section diff --git a/src/ics3.cpp b/src/ics3.cpp index 803a3ad..acedfc2 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -65,14 +65,15 @@ void ics::ICS3::setRom(const ID& id, const EepRom& rom) { } ics::ID ics::ICS3::getID() { - const std::vector tx {0xFF, 0x00, 0x00, 0x00}; - std::vector rx(5); + const Core::IDContainerTx tx {0xFF, 0x00, 0x00, 0x00}; + Core::IDContainerRx rx; core->communicateID(tx, rx); return ID {static_cast(0x1F & rx[4])}; } void ics::ICS3::setID(const ID& id) { - std::vector tx(4, 1), rx(5); - tx[0] = 0xE0 | id.get(); // tx[1] == tx[2] == tx[3] == 1 + auto cmd = static_cast(0xE0 | id.get()); + const Core::IDContainerTx tx {cmd, 1, 1, 1}; + Core::IDContainerRx rx; core->communicateID(tx, rx); } From aab9de5ec9fa4e27b9b00d4a8b1fa13efa4ddda5 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:43:20 +0900 Subject: [PATCH 17/30] Apply Core::Container to ICS3 --- src/ics3.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index acedfc2..9c6a47a 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -5,7 +5,7 @@ #include"ics3/parameter.hpp" #include"ics3/id.hpp" -inline uint16_t getReceiveAngle(const std::vector& rx) noexcept { +static inline uint16_t getReceiveAngle(const ics::Core::Container& rx) noexcept { return static_cast((rx[4] << 7) | rx[5]); } @@ -14,7 +14,7 @@ ics::ICS3::ICS3(const std::string& path, const Baudrate& baudrate) {} ics::Angle ics::ICS3::move(const ID& id, Angle angle) { - static std::vector tx(3), rx(6); // cache for runtime speed + static Core::Container tx(3), rx(6); // cache for runtime speed const uint16_t send {angle.getRaw()}; tx[0] = 0x80 | id.get(); tx[1] = 0x7F & (send >> 7); @@ -25,7 +25,7 @@ ics::Angle ics::ICS3::move(const ID& id, Angle angle) { } ics::Angle ics::ICS3::free(const ID& id, Angle unit) { - static std::vector tx(3), rx(6); // cache for runtime speed + static Core::Container tx(3), rx(6); // cache for runtime speed tx[0] = 0x80 | id.get(); // tx[1] == tx[2] == 0 core->communicate(tx, rx); // throw std::runtime_error unit.rawData = getReceiveAngle(rx); // avoid invalid check. need friend @@ -33,7 +33,7 @@ ics::Angle ics::ICS3::free(const ID& id, Angle unit) { } ics::Parameter ics::ICS3::get(const ID& id, const Parameter& type) { - std::vector tx(2), rx(5); + Core::Container tx(2), rx(5); tx[0] = 0xA0 | id.get(); tx[1] = type.getSubcommand(); core->communicate(tx, rx); // throw std::runtime_error @@ -41,7 +41,7 @@ ics::Parameter ics::ICS3::get(const ID& id, const Parameter& type) { } void ics::ICS3::set(const ID& id, const Parameter& param) { - std::vector tx(3), rx(6); + Core::Container tx(3), rx(6); tx[0] = 0xC0 | id.get(); tx[1] = param.getSubcommand(); tx[2] = param.get(); @@ -49,7 +49,7 @@ void ics::ICS3::set(const ID& id, const Parameter& param) { } ics::EepRom ics::ICS3::getRom(const ID& id) { - std::vector tx(2), rx(68); + Core::Container tx(2), rx(68); tx[0] = 0xA0 | id.get(); // tx[1] == 0 core->communicate(tx, rx); // throw std::runtime_error std::array romData; @@ -58,7 +58,7 @@ ics::EepRom ics::ICS3::getRom(const ID& id) { } void ics::ICS3::setRom(const ID& id, const EepRom& rom) { - std::vector tx(66), rx(68); + Core::Container tx(66), rx(68); tx[0] = 0xC0 | id.get(); // tx[1] == 0 rom.write(tx.begin() + 2); core->communicate(tx, rx); // throw std::runtime_error From 4ad8011da559ef53a96ce45146501692f1cb5f46 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:45:45 +0900 Subject: [PATCH 18/30] Apply ics::Angle::rawType to ICS3 --- src/ics3.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index 9c6a47a..dc22bc8 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -5,8 +5,8 @@ #include"ics3/parameter.hpp" #include"ics3/id.hpp" -static inline uint16_t getReceiveAngle(const ics::Core::Container& rx) noexcept { - return static_cast((rx[4] << 7) | rx[5]); +static inline ics::Angle::rawType getReceiveAngle(const ics::Core::Container& rx) noexcept { + return static_cast((rx[4] << 7) | rx[5]); } ics::ICS3::ICS3(const std::string& path, const Baudrate& baudrate) From b82dd0f1108295723bd99395fd7d9fad5b0f86c9 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:52:08 +0900 Subject: [PATCH 19/30] Use constexpr on array --- src/ics3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index dc22bc8..0f39e0a 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -65,7 +65,7 @@ void ics::ICS3::setRom(const ID& id, const EepRom& rom) { } ics::ID ics::ICS3::getID() { - const Core::IDContainerTx tx {0xFF, 0x00, 0x00, 0x00}; + constexpr Core::IDContainerTx tx {0xFF, 0x00, 0x00, 0x00}; Core::IDContainerRx rx; core->communicateID(tx, rx); return ID {static_cast(0x1F & rx[4])}; From 2b7aaae6e3796b29d613d1c9744a459e003e12c7 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 16:56:06 +0900 Subject: [PATCH 20/30] Add const to local object --- src/ics3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index 0f39e0a..39f5e20 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -72,7 +72,7 @@ ics::ID ics::ICS3::getID() { } void ics::ICS3::setID(const ID& id) { - auto cmd = static_cast(0xE0 | id.get()); + const auto cmd = static_cast(0xE0 | id.get()); const Core::IDContainerTx tx {cmd, 1, 1, 1}; Core::IDContainerRx rx; core->communicateID(tx, rx); From 0d0dfacc5df4b6f2a301f9fa9673066cfc459b5d Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:10:03 +0900 Subject: [PATCH 21/30] Refactor cmd makeing to getCmd --- src/ics3.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index 39f5e20..207696a 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -9,6 +9,10 @@ static inline ics::Angle::rawType getReceiveAngle(const ics::Core::Container& rx return static_cast((rx[4] << 7) | rx[5]); } +static inline ics::Core::value getCmd(const int head, const ics::ID& id) { + return head | id.get(); +} + ics::ICS3::ICS3(const std::string& path, const Baudrate& baudrate) : core {Core::getCore(path, baudrate.getSpeed())} {} @@ -16,7 +20,7 @@ ics::ICS3::ICS3(const std::string& path, const Baudrate& baudrate) ics::Angle ics::ICS3::move(const ID& id, Angle angle) { static Core::Container tx(3), rx(6); // cache for runtime speed const uint16_t send {angle.getRaw()}; - tx[0] = 0x80 | id.get(); + tx[0] = getCmd(0x80, id); tx[1] = 0x7F & (send >> 7); tx[2] = 0x7F & send; core->communicate(tx, rx); // throw std::runtime_error @@ -26,7 +30,7 @@ ics::Angle ics::ICS3::move(const ID& id, Angle angle) { ics::Angle ics::ICS3::free(const ID& id, Angle unit) { static Core::Container tx(3), rx(6); // cache for runtime speed - tx[0] = 0x80 | id.get(); // tx[1] == tx[2] == 0 + tx[0] = getCmd(0x80, id); // tx[1] == tx[2] == 0 core->communicate(tx, rx); // throw std::runtime_error unit.rawData = getReceiveAngle(rx); // avoid invalid check. need friend return unit; @@ -34,7 +38,7 @@ ics::Angle ics::ICS3::free(const ID& id, Angle unit) { ics::Parameter ics::ICS3::get(const ID& id, const Parameter& type) { Core::Container tx(2), rx(5); - tx[0] = 0xA0 | id.get(); + tx[0] = getCmd(0xA0, id); tx[1] = type.getSubcommand(); core->communicate(tx, rx); // throw std::runtime_error return Parameter::newParameter(type, rx[4]); @@ -42,7 +46,7 @@ ics::Parameter ics::ICS3::get(const ID& id, const Parameter& type) { void ics::ICS3::set(const ID& id, const Parameter& param) { Core::Container tx(3), rx(6); - tx[0] = 0xC0 | id.get(); + tx[0] = getCmd(0xC0, id); tx[1] = param.getSubcommand(); tx[2] = param.get(); core->communicate(tx, rx); // throw std::runtime_error @@ -50,7 +54,7 @@ void ics::ICS3::set(const ID& id, const Parameter& param) { ics::EepRom ics::ICS3::getRom(const ID& id) { Core::Container tx(2), rx(68); - tx[0] = 0xA0 | id.get(); // tx[1] == 0 + tx[0] = getCmd(0xA0, id); // tx[1] == 0 core->communicate(tx, rx); // throw std::runtime_error std::array romData; std::copy(rx.cbegin() + 4, rx.cend(), romData.begin()); @@ -59,7 +63,7 @@ ics::EepRom ics::ICS3::getRom(const ID& id) { void ics::ICS3::setRom(const ID& id, const EepRom& rom) { Core::Container tx(66), rx(68); - tx[0] = 0xC0 | id.get(); // tx[1] == 0 + tx[0] = getCmd(0xC0, id); // tx[1] == 0 rom.write(tx.begin() + 2); core->communicate(tx, rx); // throw std::runtime_error } @@ -72,8 +76,7 @@ ics::ID ics::ICS3::getID() { } void ics::ICS3::setID(const ID& id) { - const auto cmd = static_cast(0xE0 | id.get()); - const Core::IDContainerTx tx {cmd, 1, 1, 1}; + const Core::IDContainerTx tx {getCmd(0xE0, id), 1, 1, 1}; Core::IDContainerRx rx; core->communicateID(tx, rx); } From ec36ece34b4cee584605239c45045dca775cb3cc Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:14:58 +0900 Subject: [PATCH 22/30] Update get method with const tx --- src/ics3.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index 207696a..367a1a6 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -37,9 +37,8 @@ ics::Angle ics::ICS3::free(const ID& id, Angle unit) { } ics::Parameter ics::ICS3::get(const ID& id, const Parameter& type) { - Core::Container tx(2), rx(5); - tx[0] = getCmd(0xA0, id); - tx[1] = type.getSubcommand(); + const Core::Container tx {getCmd(0xA0, id), type.getSubcommand()}; + Core::Container rx(5); core->communicate(tx, rx); // throw std::runtime_error return Parameter::newParameter(type, rx[4]); } From 32e9dff26f431663f099d99e6a0e53e58aec912f Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:16:34 +0900 Subject: [PATCH 23/30] Update set method with const tx --- src/ics3.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index 367a1a6..551779f 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -44,10 +44,8 @@ ics::Parameter ics::ICS3::get(const ID& id, const Parameter& type) { } void ics::ICS3::set(const ID& id, const Parameter& param) { - Core::Container tx(3), rx(6); - tx[0] = getCmd(0xC0, id); - tx[1] = param.getSubcommand(); - tx[2] = param.get(); + const Core::Container tx {getCmd(0xC0, id), param.getSubcommand(), param.get()}; + Core::Container rx(6); core->communicate(tx, rx); // throw std::runtime_error } From 7400ec25df848ab28d2ea62b2b698fa65a1533d7 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:20:46 +0900 Subject: [PATCH 24/30] Update getRom method with const rx and alias --- src/ics3.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index 551779f..b6f9d29 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -50,10 +50,10 @@ void ics::ICS3::set(const ID& id, const Parameter& param) { } ics::EepRom ics::ICS3::getRom(const ID& id) { - Core::Container tx(2), rx(68); - tx[0] = getCmd(0xA0, id); // tx[1] == 0 + const Core::Container tx {getCmd(0xA0, id), 0}; + Core::Container rx(68); core->communicate(tx, rx); // throw std::runtime_error - std::array romData; + EepRom::Container romData; std::copy(rx.cbegin() + 4, rx.cend(), romData.begin()); return EepRom {romData}; // need friend } From 1e620a779069a5c0a26f16747f11fba35c7ff6f2 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:28:00 +0900 Subject: [PATCH 25/30] Update type specify with some alias --- src/ics3.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ics3.cpp b/src/ics3.cpp index b6f9d29..fc3a9f7 100644 --- a/src/ics3.cpp +++ b/src/ics3.cpp @@ -6,7 +6,7 @@ #include"ics3/id.hpp" static inline ics::Angle::rawType getReceiveAngle(const ics::Core::Container& rx) noexcept { - return static_cast((rx[4] << 7) | rx[5]); + return (rx[4] << 7) | rx[5]; } static inline ics::Core::value getCmd(const int head, const ics::ID& id) { @@ -69,7 +69,7 @@ ics::ID ics::ICS3::getID() { constexpr Core::IDContainerTx tx {0xFF, 0x00, 0x00, 0x00}; Core::IDContainerRx rx; core->communicateID(tx, rx); - return ID {static_cast(0x1F & rx[4])}; + return ID {static_cast(0x1F & rx[4])}; } void ics::ICS3::setID(const ID& id) { From cb69245f27a5c50e94f142df37b0bc2227c6a63f Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:47:45 +0900 Subject: [PATCH 26/30] Fix core move method with close fd --- include/core.hpp | 2 ++ src/core.cpp | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/core.hpp b/include/core.hpp index a7a3e6d..7a40167 100644 --- a/include/core.hpp +++ b/include/core.hpp @@ -25,6 +25,8 @@ namespace ics { void communicate(const Container&, Container&); void communicateID(const IDContainerTx&, IDContainerRx&); private: + void closeThis() noexcept; + static termios getTermios() noexcept; int fd; diff --git a/src/core.cpp b/src/core.cpp index 5d026f2..f034707 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -33,8 +33,7 @@ ics::Core::Core(const std::string& path, speed_t baudrate) ics::Core::~Core() noexcept { if (fd < 0) return; - tcsetattr(fd, TCSANOW, &oldTio); - close(fd); + closeThis(); } ics::Core::Core(Core&& rhs) noexcept @@ -45,6 +44,7 @@ ics::Core::Core(Core&& rhs) noexcept } ics::Core& ics::Core::operator=(Core&& rhs) noexcept { + closeThis(); fd = rhs.fd; oldTio = rhs.oldTio; rhs.fd = -1; @@ -94,6 +94,11 @@ void ics::Core::communicateID(const IDContainerTx& tx, IDContainerRx& rx) { if ((tx[0] & 0xE0) != (*receive & 0xE0)) throw std::runtime_error {"Receive failed: invalid target data"}; } +void ics::Core::closeThis() noexcept { + tcsetattr(fd, TCSANOW, &oldTio); + close(fd); +} + termios ics::Core::getTermios() noexcept { termios newTio; std::memset(&newTio, 0, sizeof(newTio)); From 21e2cb2ab8e6d020d7d2a4e40c194b601e02c156 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:58:21 +0900 Subject: [PATCH 27/30] Update testcode with some alias --- src/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test.cpp b/src/test.cpp index 9a3e7bb..7f5ad65 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -139,7 +139,7 @@ void testID() { std::cout << e.what() << std::endl; } try { - ics::ID error {static_cast(-1)}; // if constexpr, compile error + ics::ID error {static_cast(-1)}; // if constexpr, compile error assert(false); } catch (const std::invalid_argument& e) { std::cout << e.what() << std::endl; @@ -188,7 +188,7 @@ void testParameter() { void testICS3() { std::cout << std::endl << "ICS3 test section" << std::endl; - constexpr const char* const path = "/dev/ttyUSB0"; + constexpr auto path = "/dev/ttyUSB0"; constexpr auto baudrate = ics::Baudrate::RATE115200(); constexpr ics::ID id {2}; try { @@ -233,7 +233,7 @@ void testIcsParam(ics::ICS3& ics, const ics::ID& id) { std::cout << "ICS3 'get' and 'set' method test section" << std::endl; auto defaultStretch = ics.get(id, ics::Parameter::stretch()); std::cout << "default stretch is " << static_cast(defaultStretch) << std::endl; - constexpr uint8_t writeNumber {29}; + constexpr ics::Parameter::type writeNumber {29}; ics.set(id, ics::Parameter::stretch(writeNumber)); auto newStretch = ics.get(id, defaultStretch); ics.set(id, defaultStretch); // before checking, restore data. @@ -247,7 +247,7 @@ void testIcsEepRom(ics::ICS3& ics, const ics::ID& id) { auto defaultStretch = rom.get(ics::EepParam::stretch()); std::cout << "default stretch is " << defaultStretch << std::endl; auto setRom = rom; - constexpr uint16_t writeNumber {62}; + constexpr ics::EepParam::type writeNumber {62}; setRom.set(ics::EepParam::stretch(writeNumber)); ics.setRom(id, setRom); auto newRom = ics.getRom(id); From bfe108b3fd76b1c982eb0cf224a3e223e27a9ed9 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 17:58:36 +0900 Subject: [PATCH 28/30] Update dump function for runtime speed --- src/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test.cpp b/src/test.cpp index 7f5ad65..e995fa7 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -274,7 +274,7 @@ void testIcsID(ics::ICS3& ics) { } template -void dump(const Iter& begin, const Iter& end) noexcept { +inline void dump(const Iter& begin, const Iter& end) noexcept { while (begin != end) std::cout << static_cast(*begin++) << ", "; std::cout << std::endl; } From 0a3354f972218ec62d2f756a1c00e42b75a6c590 Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 18:07:23 +0900 Subject: [PATCH 29/30] Update specify constexpr const to constexpr --- include/ics3/baudrate.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ics3/baudrate.hpp b/include/ics3/baudrate.hpp index edd7537..839a0f7 100644 --- a/include/ics3/baudrate.hpp +++ b/include/ics3/baudrate.hpp @@ -7,7 +7,7 @@ namespace ics { class Baudrate { public: using type = uint8_t; - static constexpr const Baudrate RATE115200() noexcept; + static constexpr Baudrate RATE115200() noexcept; //static constexpr Baudrate RATE625000() noexcept; //static constexpr Baudrate RATE1250000() noexcept; constexpr type get() const noexcept; @@ -20,7 +20,7 @@ namespace ics { const speed_t baudrate; }; - constexpr const Baudrate Baudrate::RATE115200() noexcept { + constexpr Baudrate Baudrate::RATE115200() noexcept { return Baudrate {10, B115200}; } From 0047678415cca25831c913ec106ed5c23a07827a Mon Sep 17 00:00:00 2001 From: Doi Yusuke Date: Thu, 3 Nov 2016 18:09:51 +0900 Subject: [PATCH 30/30] Upgrade v1.2.0 with inclusive refactor --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f4680c..3be58e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12.1) project(ics3 CXX) SET(PROJECT_VER_MAJOR 1) -SET(PROJECT_VER_MINOR 1) -SET(PROJECT_VER_PATCH 1) +SET(PROJECT_VER_MINOR 2) +SET(PROJECT_VER_PATCH 0) SET(PROJECT_VER "${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}.${PROJECT_VER_PATCH}") SET(PROJECT_APIVER "${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}")