Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #9 from forno/inclusive-refactor
Browse files Browse the repository at this point in the history
Update v1.2.0 with inclusive refactor
  • Loading branch information
forno authored Nov 3, 2016
2 parents 50bced8 + 0047678 commit 3d38e8f
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 207 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
11 changes: 9 additions & 2 deletions include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#define LIBICS3_ICS3_CORE_H_

#include<vector>
#include<array>
#include<string>
#include<memory>
#include<termios.h>

namespace ics {
class Core {
public:
using value = uint8_t;
using Container = std::vector<value>;
using IDContainerTx = std::array<value, 4>;
using IDContainerRx = std::array<value, 5>;
explicit Core(const std::string&, speed_t); // touch by only libics3
~Core() noexcept;
Core(const Core&) = delete;
Expand All @@ -17,9 +22,11 @@ namespace ics {
Core& operator=(Core&&) noexcept;

static std::shared_ptr<Core> getCore(const std::string&, speed_t);
void communicate(const std::vector<uint8_t>&, std::vector<uint8_t>&);
void communicateID(const std::vector<uint8_t>&, std::vector<uint8_t>&);
void communicate(const Container&, Container&);
void communicateID(const IDContainerTx&, IDContainerRx&);
private:
void closeThis() noexcept;

static termios getTermios() noexcept;

int fd;
Expand Down
72 changes: 37 additions & 35 deletions include/ics3/angle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,47 @@ namespace ics {
class Angle {
friend ICS3; // for touch setRaw
public:
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 double PI {3.141592653589793};
using rawType = uint16_t;
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 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 uint16_t getRaw() const noexcept;
void setRaw(uint16_t);
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 uint16_t castToRaw(double, double) noexcept;
static constexpr uint16_t checkInvalidAngle(uint16_t);
constexpr explicit Angle(type, type);
static constexpr rawType castToRaw(type, type) noexcept;
static constexpr rawType checkInvalidAngle(rawType);

uint16_t rawData;
const double rawCalibration;
rawType rawData;
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};
}

Expand All @@ -55,41 +57,41 @@ 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) {
setRaw(castToRaw(angle, rawCalibration)); // throw std::out_of_range
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;
}

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
}

constexpr Angle::Angle(double calibration, double angle)
: rawData {checkInvalidAngle(castToRaw(angle, calibration))}, // throw std::out_of_range
constexpr Angle::Angle(type calibration, type angle)
: 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<uint16_t>(angle * calibration + MID);
constexpr Angle::rawType Angle::castToRaw(type calibration, type angle) noexcept {
return static_cast<rawType>((calibration * angle) + MID);
}

constexpr uint16_t Angle::checkInvalidAngle(uint16_t raw) {
constexpr Angle::rawType Angle::checkInvalidAngle(rawType raw) {
return checkInvalidRange(raw, MIN, MAX);
}
}
Expand Down
19 changes: 10 additions & 9 deletions include/ics3/baudrate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,37 @@
namespace ics {
class Baudrate {
public:
static constexpr const Baudrate RATE115200() noexcept;
using type = uint8_t;
static constexpr 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;
};

constexpr const Baudrate Baudrate::RATE115200() noexcept {
constexpr Baudrate Baudrate::RATE115200() noexcept {
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();
}

constexpr speed_t Baudrate::getSpeed() const noexcept {
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}
{}
Expand Down
2 changes: 1 addition & 1 deletion include/ics3/check_invalid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ics {
template<typename T>
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;
Expand Down
Loading

0 comments on commit 3d38e8f

Please sign in to comment.