Skip to content

Commit

Permalink
✨ Add input pin inverter
Browse files Browse the repository at this point in the history
  • Loading branch information
MaliaLabor authored and kammce committed Nov 5, 2023
1 parent 07b3a2a commit f42e9e2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
13 changes: 13 additions & 0 deletions include/libhal-soft/inverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
#pragma once

#include <libhal/input_pin.hpp>
#include <libhal/output_pin.hpp>

namespace hal::soft {
Expand All @@ -28,4 +29,16 @@ class output_pin_inverter : public hal::output_pin

hal::output_pin* m_output_pin;
};

class input_pin_inverter : public hal::input_pin
{
public:
input_pin_inverter(hal::input_pin& p_input_pin);

private:
status driver_configure(const settings& p_settings) override;
result<level_t> driver_level() override;

hal::input_pin* m_input_pin;
};
} // namespace hal::soft
19 changes: 18 additions & 1 deletion src/inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ output_pin_inverter::output_pin_inverter(hal::output_pin& p_output_pin)
{
}

status output_pin_inverter::driver_configure(const settings& p_settings)
input_pin_inverter::input_pin_inverter(hal::input_pin& p_input_pin)
: m_input_pin(&p_input_pin)
{
}

status output_pin_inverter::driver_configure(
const hal::output_pin::settings& p_settings)
{
return m_output_pin->configure(p_settings);
};
Expand All @@ -35,4 +41,15 @@ result<hal::output_pin::level_t> output_pin_inverter::driver_level()
return level;
};

status input_pin_inverter::driver_configure(
const hal::input_pin::settings& p_settings)
{
return m_input_pin->configure(p_settings);
};
result<hal::input_pin::level_t> input_pin_inverter::driver_level()
{
auto level = HAL_CHECK(m_input_pin->level());
level.state = !level.state;
return level;
};
} // namespace hal::soft
65 changes: 65 additions & 0 deletions tests/inverter.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <libhal-soft/inverter.hpp>

#include <libhal-mock/input_pin.hpp>
#include <libhal-mock/output_pin.hpp>

#include <boost/ut.hpp>
Expand Down Expand Up @@ -99,4 +100,68 @@ void output_pin_iverter_test()
};
};
}

void input_pin_iverter_test()
{
using namespace boost::ut;

"hal::input_pin_inverter::configure"_test = []() {
"configure() with default settings"_test = []() {
// Setup
hal::mock::input_pin mock_input_pin;
hal::input_pin::settings expected_settings;
auto inverted_input_pin = input_pin_inverter(mock_input_pin);

// Exercise
auto result = inverted_input_pin.configure(expected_settings);
auto result_settings =
std::get<0>(mock_input_pin.spy_configure.call_history().at(0));

// Verify
expect(bool{ result });
expect(pin_resistor::pull_up == result_settings.resistor);
};

"configure() with set values for settings"_test = []() {
// Setup
hal::mock::input_pin mock_input_pin;
hal::input_pin::settings expected_settings;
expected_settings.resistor = pin_resistor::pull_down;
auto inverted_input_pin = input_pin_inverter(mock_input_pin);

// Exercise
auto result = inverted_input_pin.configure(expected_settings);
auto result_settings =
std::get<0>(mock_input_pin.spy_configure.call_history().at(0));

// Verify
expect(bool{ result });
expect(pin_resistor::pull_down == result_settings.resistor);
};
};

"hal::input_pin_inverter::level"_test = []() {
"level()"_test = []() {
// Setup
hal::mock::input_pin mock_input_pin;
auto inverted_input_pin = input_pin_inverter(mock_input_pin);
std::deque inputs{
input_pin::level_t{ .state = true },
input_pin::level_t{ .state = false },
};
std::queue queue(inputs);
mock_input_pin.set(queue);

// Exercise
auto result1 = inverted_input_pin.level();
auto result2 = inverted_input_pin.level();

// Verify
expect(bool{ result1 });
expect(bool{ result2 });
expect(that % false == result1.value().state);
expect(that % true == result2.value().state);
};
};
}
} // namespace hal::soft
4 changes: 4 additions & 0 deletions tests/main.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace hal::soft {
extern void adc_mux_test();
extern void minimum_speed_test();
extern void rc_servo_test();
extern void output_pin_iverter_test();
extern void input_pin_iverter_test();

extern void inert_accelerometer_test();
extern void inert_adc_test();
Expand All @@ -38,6 +40,8 @@ int main()
hal::soft::adc_mux_test();
hal::soft::minimum_speed_test();
hal::soft::rc_servo_test();
hal::soft::output_pin_iverter_test();
hal::soft::input_pin_iverter_test();

hal::soft::inert_accelerometer_test();
hal::soft::inert_adc_test();
Expand Down

0 comments on commit f42e9e2

Please sign in to comment.