Skip to content

Commit

Permalink
Use 'speed' instead of 'notch'
Browse files Browse the repository at this point in the history
  • Loading branch information
higaski committed Oct 24, 2023
1 parent b9b5b2b commit 965984c
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 71 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.30.0
- Use 'speed' instead of 'notch'
- Update to ZTL 0.18.0

## 0.29.1
- Don't receive packets <=3 bytes

Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()
FetchContent_Declare(
CMakeModules
GIT_REPOSITORY https://github.com/ZIMO-Elektronik/CMakeModules
GIT_TAG v0.1.2
GIT_TAG v0.2.1
SOURCE_DIR ${CMAKE_BINARY_DIR}/CMakeModules)
FetchContent_MakeAvailable(CMakeModules)

Expand Down Expand Up @@ -78,7 +78,7 @@ if(NOT TARGET static_math)
endif()

if(NOT TARGET ZTL::ZTL)
cpmaddpackage("gh:ZIMO-Elektronik/ZTL@0.17.0")
cpmaddpackage("gh:ZIMO-Elektronik/ZTL@0.18.0")
endif()

target_link_libraries(DCC INTERFACE static_math ZTL::ZTL)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ private:
// Set direction (1 forward, -1 backward)
void direction(uint32_t addr, int32_t dir);

// Set notch (0-255)
void notch(uint32_t addr, int32_t notch);
// Set speed (0-255)
void speed(uint32_t addr, int32_t speed);

// Emergency stop
void emergencyStop(uint32_t addr);
Expand Down
4 changes: 2 additions & 2 deletions examples/repl/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void Decoder::direction(uint32_t addr, int32_t dir) {
<< (dir == 1 ? "forward" : "backward") << PROMPTENDL;
}

void Decoder::notch(uint32_t addr, int32_t notch) {
cli::Cli::cout() << "Address " << addr << ": set speed " << notch
void Decoder::speed(uint32_t addr, int32_t speed) {
cli::Cli::cout() << "Address " << addr << ": set speed " << speed
<< PROMPTENDL;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/repl/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct Decoder : dcc::rx::CrtpBase<Decoder> {
// Set direction (1 forward, -1 backward)
void direction(uint32_t addr, int32_t dir);

// Set notch (0-255)
void notch(uint32_t addr, int32_t notch);
// Set speed (0-255)
void speed(uint32_t addr, int32_t speed);

// Emergency stop
void emergencyStop(uint32_t addr);
Expand Down
30 changes: 15 additions & 15 deletions include/dcc/rx/crtp_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,12 @@ struct CrtpBase : bidi::CrtpBase<T> {
case 0b0011'1111u: {
auto const dir{chunk[1uz] & ztl::make_mask(7u) ? 1 : -1};
// Halt
if (!(chunk[1uz] & 0b0111'1111u)) directionNotch(addr, dir, 0);
if (!(chunk[1uz] & 0b0111'1111u)) directionSpeed(addr, dir, 0);
// Emergency stop
else if (!(chunk[1uz] & 0b0111'1110u)) impl().emergencyStop(addr);
else {
auto const notch{scale_notch<126>((chunk[1uz] & 0b0111'1111) - 1)};
directionNotch(addr, dir, notch);
auto const speed{scale_speed<126>((chunk[1uz] & 0b0111'1111) - 1)};
directionSpeed(addr, dir, speed);
}
break;
}
Expand Down Expand Up @@ -428,30 +428,30 @@ struct CrtpBase : bidi::CrtpBase<T> {
/// \param chunk Raw data
void speedAndDirection(uint32_t addr, std::span<uint8_t const> chunk) {
auto const dir{chunk[0uz] & ztl::make_mask(5u) ? 1 : -1};
int32_t notch{};
int32_t speed{};

// Halt
if (!(chunk[0uz] & 0b0000'1111u)) notch = 0;
if (!(chunk[0uz] & 0b0000'1111u)) speed = 0;
// Emergency stop
else if (!(chunk[0uz] & 0b0000'1110u)) return impl().emergencyStop(addr);
else notch = (chunk[0uz] & 0b0000'1111) - 1;
else speed = (chunk[0uz] & 0b0000'1111) - 1;

// 14 speed steps and F0
if (_f0_exception) {
notch = scale_notch<14>(notch);
speed = scale_speed<14>(speed);
auto const mask{ztl::make_mask(0u)};
auto const state{chunk[0uz] & ztl::make_mask(4u) ? ztl::make_mask(0u)
: 0u};
impl().function(addr, mask, state);
}
// 28 speed steps
else {
notch <<= 1u;
if (notch && !(chunk[0uz] & ztl::make_mask(4u))) --notch;
notch = scale_notch<28>(notch);
speed <<= 1u;
if (speed && !(chunk[0uz] & ztl::make_mask(4u))) --speed;
speed = scale_speed<28>(speed);
}

directionNotch(addr, dir, notch);
directionSpeed(addr, dir, speed);
}

/// Execute function group
Expand Down Expand Up @@ -750,17 +750,17 @@ struct CrtpBase : bidi::CrtpBase<T> {
}
}

/// Set direction and notch
/// Set direction and speed
///
/// \param addr Address
/// \param dir Direction
/// \param notch Notch
void directionNotch(uint32_t addr, int32_t dir, int32_t notch) {
/// \param speed Speed
void directionSpeed(uint32_t addr, int32_t dir, int32_t speed) {
auto const reverse{addr == _addrs.primary
? impl().readCv(29u - 1u) & ztl::make_mask(0u)
: impl().readCv(19u - 1u) & ztl::make_mask(7u)};
impl().direction(addr, reverse ? dir * -1 : dir);
impl().notch(addr, notch);
impl().speed(addr, speed);
}

/// Update quality of service (roughly every second)
Expand Down
4 changes: 2 additions & 2 deletions include/dcc/rx/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ concept Decoder = Readable<T> && Writable<T> &&
requires(T t,
uint32_t addr,
int32_t dir,
int32_t notch,
int32_t speed,
uint32_t mask,
uint32_t state,
bool service_mode,
std::span<uint8_t const> chunk) {
{ t.direction(addr, dir) } -> std::same_as<void>;
{ t.notch(addr, notch) } -> std::same_as<void>;
{ t.speed(addr, speed) } -> std::same_as<void>;
{ t.emergencyStop(addr) } -> std::same_as<void>;
{ t.function(addr, mask, state) } -> std::same_as<void>;
{ t.serviceModeHook(service_mode) } -> std::same_as<void>;
Expand Down
16 changes: 8 additions & 8 deletions include/dcc/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ constexpr auto data2uint32(uint8_t const* data) {
data[2uz] << 8u | data[3uz] << 0u);
}

/// Scale notch from 14, 28 or 126 steps to 255
/// Scale speed from 14, 28 or 126 steps to 255
///
/// \tparam Scale Scaling
/// \param notch Notch
/// \raturn Scaled notch
/// \param speed Speed
/// \raturn Scaled speed
template<int32_t Scale>
constexpr int32_t scale_notch(int32_t notch)
constexpr int32_t scale_speed(int32_t speed)
requires(Scale == 14 || Scale == 28 || Scale == 126)
{
// Zero stays zero
if (!notch) return 0;
if (!speed) return 0;
// One shares the same internal minimum across all scales
constexpr int32_t max{255}, min{ztl::lerp<int32_t>(1, 0, 126, 0, max)};
if (notch == 1) return min;
if (speed == 1) return min;
if constexpr (Scale == 14 || Scale == 28)
return ztl::lerp<int32_t>(notch, 1, Scale, min, max);
return ztl::lerp<int32_t>(speed, 1, Scale, min, max);
else if constexpr (Scale == 126)
return ztl::lerp<int32_t>(notch, 0, Scale, 0, max);
return ztl::lerp<int32_t>(speed, 0, Scale, 0, max);
}

} // namespace dcc
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include(GoogleTest)
file(GLOB_RECURSE SRC *.cpp)
add_executable(DCCTests ${SRC})

sanatize("address,undefined")
sanitize("address,undefined")

target_common_warnings(DCCTests PRIVATE)

Expand Down
6 changes: 3 additions & 3 deletions tests/rx/advanced_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TEST_F(RxTest, _126_speed_steps_fwd) {
Expectation read_cv{
EXPECT_CALL(_mock, readCv(_)).WillOnce(Return(_cvs[29uz - 1uz]))};
Expectation dir{EXPECT_CALL(_mock, direction(3u, 1))};
Expectation notch{EXPECT_CALL(_mock, notch(3u, _))};
Expectation speed{EXPECT_CALL(_mock, speed(3u, _))};
Receive(dcc::make_advanced_operations_speed_packet(3u, 1, 10u));
Execute();
}
Expand All @@ -16,7 +16,7 @@ TEST_F(RxTest, _126_speed_steps_bwd) {
Expectation read_cv{
EXPECT_CALL(_mock, readCv(_)).WillOnce(Return(_cvs[29uz - 1uz]))};
Expectation dir{EXPECT_CALL(_mock, direction(3u, -1))};
Expectation notch{EXPECT_CALL(_mock, notch(3u, _))};
Expectation speed{EXPECT_CALL(_mock, speed(3u, _))};
Receive(dcc::make_advanced_operations_speed_packet(3u, -1, 10u));
Execute();
}
Expand All @@ -26,7 +26,7 @@ TEST_F(RxTest, _126_speed_steps_bwd) {
Expectation read_cv{
EXPECT_CALL(_mock, readCv(_)).WillOnce(Return(_cvs[29uz - 1uz]))};
Expectation dir{EXPECT_CALL(_mock, direction(3u, -1))};
Expectation notch{EXPECT_CALL(_mock, notch(3u, _))};
Expectation speed{EXPECT_CALL(_mock, speed(3u, _))};
Receive(dcc::make_advanced_operations_speed_packet(3u, 0, 10u));
Execute();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rx/rx_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

struct RxMock : dcc::rx::CrtpBase<RxMock> {
MOCK_METHOD(void, direction, (uint32_t, int32_t));
MOCK_METHOD(void, notch, (uint32_t, int32_t));
MOCK_METHOD(void, speed, (uint32_t, int32_t));
MOCK_METHOD(void, emergencyStop, (uint32_t));
MOCK_METHOD(void, function, (uint32_t, uint32_t, uint32_t));
MOCK_METHOD(void, serviceModeHook, (bool));
Expand Down
33 changes: 0 additions & 33 deletions tests/rx/scale_notch.cpp

This file was deleted.

33 changes: 33 additions & 0 deletions tests/rx/scale_speed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <gtest/gtest.h>
#include <dcc/dcc.hpp>

using dcc::scale_speed;

TEST(receive_scale_speed, zero) {
EXPECT_EQ(scale_speed<14>(0), 0);
EXPECT_EQ(scale_speed<28>(0), 0);
EXPECT_EQ(scale_speed<126>(0), 0);
}

TEST(receive_scale_speed, one) {
EXPECT_EQ(scale_speed<14>(1), scale_speed<126>(1));
EXPECT_EQ(scale_speed<28>(1), scale_speed<126>(1));
}

TEST(receive_scale_speed, two) {
EXPECT_EQ(scale_speed<14>(2), 21);
EXPECT_EQ(scale_speed<28>(2), 11);
EXPECT_EQ(scale_speed<126>(2), 4);
}

TEST(receive_scale_speed, three) {
EXPECT_EQ(scale_speed<14>(3), 40);
EXPECT_EQ(scale_speed<28>(3), 20);
EXPECT_EQ(scale_speed<126>(3), 6);
}

TEST(receive_scale_speed, max) {
EXPECT_EQ(scale_speed<14>(14), 255);
EXPECT_EQ(scale_speed<28>(28), 255);
EXPECT_EQ(scale_speed<126>(126), 255);
}

0 comments on commit 965984c

Please sign in to comment.