Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update APIs #134

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/drv2605/example/main/drv2605_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extern "C" void app_main(void) {
// we're using an ERM motor, so select an ERM library (1-5).
// drv2605.select_library(1, ec);
// we're using an LRA motor, so select an LRA library (6).
drv2605.select_library(6, ec);
drv2605.select_library(espp::Drv2605::Library::LRA, ec);
if (ec) {
logger.error("select library failed: {}", ec.message());
}
Expand Down
132 changes: 130 additions & 2 deletions components/drv2605/include/drv2605.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ class Drv2605 {
LRA ///< Linear Resonant Actuator
};

/**
* @brief The library of waveforms to use.
* @note The DRV2605 has 7 different libraries of waveforms. The first
* library is empty, and the next 5 are ERM (eccentric rotating mass)
* libraries. The last library is an LRA (linear resonant actuator)
* library.
*/
enum class Library {
EMPTY = 0,
ERM_0 = 1,
ERM_1 = 2,
ERM_2 = 3,
ERM_3 = 4,
ERM_4 = 5,
LRA = 6,
};

/**
* @brief Configuration structure for the DRV2605
*/
Expand Down Expand Up @@ -173,9 +190,12 @@ class Drv2605 {
* @param lib Library to use, 0=Empty, 1-5 are ERM, 6 is LRA
* @param ec Error code to set if there is an error.
*/
void select_library(uint8_t lib, std::error_code &ec) {
void select_library(Library lib, std::error_code &ec) {
logger_.info("Selecting library {}", lib);
write_one_((uint8_t)Register::LIBRARY, lib, ec);
if (motor_type_ == MotorType::LRA && lib != Library::LRA) {
logger_.warn("LRA motor selected, but library {} is not an LRA library", lib);
}
write_one_((uint8_t)Register::LIBRARY, (uint8_t)lib, ec);
}

protected:
Expand Down Expand Up @@ -298,3 +318,111 @@ class Drv2605 {
espp::Logger logger_;
};
} // namespace espp

// for easy printing of the enums with the libfmt library:
template <>
struct fmt::formatter<espp::Drv2605::Mode> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }

template <typename FormatContext>
auto format(espp::Drv2605::Mode m, FormatContext &ctx) {
switch (m) {
case espp::Drv2605::Mode::INTTRIG:
return fmt::format_to(ctx.out(), "INTTRIG");
case espp::Drv2605::Mode::EXTTRIGEDGE:
return fmt::format_to(ctx.out(), "EXTTRIGEDGE");
case espp::Drv2605::Mode::EXTTRIGLVL:
return fmt::format_to(ctx.out(), "EXTTRIGLVL");
case espp::Drv2605::Mode::PWMANALOG:
return fmt::format_to(ctx.out(), "PWMANALOG");
case espp::Drv2605::Mode::AUDIOVIBE:
return fmt::format_to(ctx.out(), "AUDIOVIBE");
case espp::Drv2605::Mode::REALTIME:
return fmt::format_to(ctx.out(), "REALTIME");
case espp::Drv2605::Mode::DIAGNOS:
return fmt::format_to(ctx.out(), "DIAGNOS");
case espp::Drv2605::Mode::AUTOCAL:
return fmt::format_to(ctx.out(), "AUTOCAL");
default:
return fmt::format_to(ctx.out(), "UNKNOWN");
}
}
};

template <>
struct fmt::formatter<espp::Drv2605::Waveform> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }

template <typename FormatContext>
auto format(espp::Drv2605::Waveform w, FormatContext &ctx) {
switch (w) {
case espp::Drv2605::Waveform::END:
return fmt::format_to(ctx.out(), "END");
case espp::Drv2605::Waveform::STRONG_CLICK:
return fmt::format_to(ctx.out(), "STRONG_CLICK");
case espp::Drv2605::Waveform::SHARP_CLICK:
return fmt::format_to(ctx.out(), "SHARP_CLICK");
case espp::Drv2605::Waveform::SOFT_BUMP:
return fmt::format_to(ctx.out(), "SOFT_BUMP");
case espp::Drv2605::Waveform::DOUBLE_CLICK:
return fmt::format_to(ctx.out(), "DOUBLE_CLICK");
case espp::Drv2605::Waveform::TRIPLE_CLICK:
return fmt::format_to(ctx.out(), "TRIPLE_CLICK");
case espp::Drv2605::Waveform::SOFT_FUZZ:
return fmt::format_to(ctx.out(), "SOFT_FUZZ");
case espp::Drv2605::Waveform::STRONG_BUZZ:
return fmt::format_to(ctx.out(), "STRONG_BUZZ");
case espp::Drv2605::Waveform::ALERT_750MS:
return fmt::format_to(ctx.out(), "ALERT_750MS");
case espp::Drv2605::Waveform::ALERT_1000MS:
return fmt::format_to(ctx.out(), "ALERT_1000MS");
case espp::Drv2605::Waveform::BUZZ1:
return fmt::format_to(ctx.out(), "BUZZ1");
case espp::Drv2605::Waveform::BUZZ2:
return fmt::format_to(ctx.out(), "BUZZ2");
case espp::Drv2605::Waveform::BUZZ3:
return fmt::format_to(ctx.out(), "BUZZ3");
case espp::Drv2605::Waveform::BUZZ4:
return fmt::format_to(ctx.out(), "BUZZ4");
case espp::Drv2605::Waveform::BUZZ5:
return fmt::format_to(ctx.out(), "BUZZ5");
case espp::Drv2605::Waveform::PULSING_STRONG_1:
return fmt::format_to(ctx.out(), "PULSING_STRONG_1");
case espp::Drv2605::Waveform::PULSING_STRONG_2:
return fmt::format_to(ctx.out(), "PULSING_STRONG_2");
case espp::Drv2605::Waveform::TRANSITION_CLICK_1:
return fmt::format_to(ctx.out(), "TRANSITION_CLICK_1");
case espp::Drv2605::Waveform::TRANSITION_HUM_1:
return fmt::format_to(ctx.out(), "TRANSITION_HUM_1");
default:
return fmt::format_to(ctx.out(), "UNKNOWN");
}
}
};

template <>
struct fmt::formatter<espp::Drv2605::Library> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }

template <typename FormatContext>
auto format(espp::Drv2605::Library l, FormatContext &ctx) {
switch (l) {
case espp::Drv2605::Library::EMPTY:
return fmt::format_to(ctx.out(), "EMPTY");
case espp::Drv2605::Library::ERM_0:
return fmt::format_to(ctx.out(), "ERM_0");
case espp::Drv2605::Library::ERM_1:
return fmt::format_to(ctx.out(), "ERM_1");
case espp::Drv2605::Library::ERM_2:
return fmt::format_to(ctx.out(), "ERM_2");
case espp::Drv2605::Library::ERM_3:
return fmt::format_to(ctx.out(), "ERM_3");
case espp::Drv2605::Library::ERM_4:
return fmt::format_to(ctx.out(), "ERM_4");
case espp::Drv2605::Library::LRA:
return fmt::format_to(ctx.out(), "LRA");
default:
return fmt::format_to(ctx.out(), "UNKNOWN");
}
}
};
20 changes: 10 additions & 10 deletions components/mcp23x17/example/main/mcp23x17_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ extern "C" void app_main(void) {
return err == ESP_OK;
};
// now make the mcp23x17 which handles GPIO
espp::Mcp23x17 mcp23x17({.port_a_direction_mask = (1 << 0), // input on A0
.port_a_interrupt_mask = (1 << 0), // interrupt on A0
.port_b_direction_mask = (1 << 7), // input on B7
.port_b_interrupt_mask = (1 << 7), // interrupt on B7
espp::Mcp23x17 mcp23x17({.port_0_direction_mask = (1 << 0), // input on A0
.port_0_interrupt_mask = (1 << 0), // interrupt on A0
.port_1_direction_mask = (1 << 7), // input on B7
.port_1_interrupt_mask = (1 << 7), // interrupt on B7
.write = mcp23x17_write,
.read = mcp23x17_read,
.log_level = espp::Logger::Verbosity::WARN});
// set pull up on the input pins
std::error_code ec;
mcp23x17.set_pull_up(espp::Mcp23x17::Port::A, (1 << 0), ec);
mcp23x17.set_pull_up(espp::Mcp23x17::Port::PORT0, (1 << 0), ec);
if (ec) {
fmt::print("set_pull_up failed: {}\n", ec.message());
}
mcp23x17.set_pull_up(espp::Mcp23x17::Port::B, (1 << 7), ec);
mcp23x17.set_pull_up(espp::Mcp23x17::Port::PORT1, (1 << 7), ec);
if (ec) {
fmt::print("set_pull_up failed: {}\n", ec.message());
}
Expand All @@ -74,21 +74,21 @@ extern "C" void app_main(void) {
auto now = std::chrono::high_resolution_clock::now();
auto seconds = std::chrono::duration<float>(now - start).count();
std::error_code ec;
auto a_pins = mcp23x17.get_pins(espp::Mcp23x17::Port::A, ec);
auto a_pins = mcp23x17.get_pins(espp::Mcp23x17::Port::PORT0, ec);
if (ec) {
fmt::print("get_pins failed: {}\n", ec.message());
return false;
}
auto b_pins = mcp23x17.get_pins(espp::Mcp23x17::Port::B, ec);
auto b_pins = mcp23x17.get_pins(espp::Mcp23x17::Port::PORT1, ec);
if (ec) {
fmt::print("get_pins failed: {}\n", ec.message());
return false;
}
bool on = !(a_pins & (1 << 0));
if (on) {
mcp23x17.set_pins(espp::Mcp23x17::Port::B, (1 << 3), ec);
mcp23x17.set_pins(espp::Mcp23x17::Port::PORT1, (1 << 3), ec);
} else {
mcp23x17.set_pins(espp::Mcp23x17::Port::B, 0x00, ec);
mcp23x17.set_pins(espp::Mcp23x17::Port::PORT1, 0x00, ec);
}
if (ec) {
fmt::print("set_pins failed: {}\n", ec.message());
Expand Down
Loading
Loading