@@ -13,49 +13,64 @@ extern "C" void app_main(void) {
13
13
static espp::Logger logger ({.tag = " drv2605 example" , .level = espp::Logger::Verbosity::INFO});
14
14
// This example shows using the i2c haptic motor driver (drv2605)
15
15
{
16
- logger.info (" Running i2c adc example !" );
16
+ logger.info (" Starting !" );
17
17
// ! [drv2605 example]
18
18
// make the I2C that we'll use to communicate
19
19
espp::I2c i2c ({
20
20
.port = I2C_NUM_1,
21
21
.sda_io_num = (gpio_num_t )CONFIG_EXAMPLE_I2C_SDA_GPIO,
22
22
.scl_io_num = (gpio_num_t )CONFIG_EXAMPLE_I2C_SCL_GPIO,
23
23
});
24
+
25
+ using Driver = espp::Drv2605;
26
+
24
27
// make the actual drv2605 class
25
- espp::Drv2605 drv2605 (espp::Drv2605 ::Config{
26
- .device_address = espp::Drv2605 ::DEFAULT_ADDRESS,
28
+ Driver drv2605 (Driver ::Config{
29
+ .device_address = Driver ::DEFAULT_ADDRESS,
27
30
.write = std::bind (&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2,
28
31
std::placeholders::_3),
29
32
.read_register =
30
33
std::bind (&espp::I2c::read_at_register, &i2c, std::placeholders::_1,
31
34
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4),
32
- .motor_type = espp::Drv2605::MotorType::LRA});
35
+ .motor_type = Driver::MotorType::LRA,
36
+ .log_level = espp::Logger::Verbosity::INFO,
37
+ });
33
38
std::error_code ec;
34
39
// we're using an ERM motor, so select an ERM library (1-5).
35
40
// drv2605.select_library(1, ec);
36
41
// we're using an LRA motor, so select an LRA library (6).
37
- drv2605.select_library (espp::Drv2605 ::Library::LRA, ec);
42
+ drv2605.select_library (Driver ::Library::LRA, ec);
38
43
if (ec) {
39
44
logger.error (" select library failed: {}" , ec.message ());
40
45
}
46
+
47
+ // do the calibration for the LRA motor
48
+ Driver::LraCalibrationSettings lra_calibration_settings{};
49
+ lra_calibration_settings.rated_voltage = 255 ;
50
+ lra_calibration_settings.overdrive_clamp = 255 ;
51
+ lra_calibration_settings.drive_time = Driver::lra_freq_to_drive_time (200 .0f );
52
+ Driver::CalibratedData calibrated_data;
53
+ if (!drv2605.calibrate (lra_calibration_settings, calibrated_data, ec)) {
54
+ logger.error (" calibration failed: {}" , ec.message ());
55
+ return ;
56
+ }
57
+ logger.info (" calibration complete: {}" , calibrated_data);
58
+
41
59
// make the task which will cycle through all the waveforms
42
60
auto task_fn = [&drv2605](std::mutex &m, std::condition_variable &cv) {
43
- static auto start = std::chrono::high_resolution_clock::now ();
44
- auto now = std::chrono::high_resolution_clock::now ();
45
- auto elapsed = std::chrono::duration<float >(now - start).count ();
46
61
static uint8_t waveform = 0 ;
47
62
std::error_code ec;
48
63
drv2605.stop (ec);
49
64
if (ec) {
50
65
logger.error (" stop failed: {}" , ec.message ());
51
66
return false ;
52
67
}
53
- drv2605.set_waveform (0 , (espp::Drv2605 ::Waveform)waveform, ec);
68
+ drv2605.set_waveform (0 , (Driver ::Waveform)waveform, ec);
54
69
if (ec) {
55
70
logger.error (" set waveform failed: {}" , ec.message ());
56
71
return false ;
57
72
}
58
- drv2605.set_waveform (1 , espp::Drv2605 ::Waveform::END, ec);
73
+ drv2605.set_waveform (1 , Driver ::Waveform::END, ec);
59
74
if (ec) {
60
75
logger.error (" set waveform failed: {}" , ec.message ());
61
76
return false ;
@@ -66,10 +81,10 @@ extern "C" void app_main(void) {
66
81
return false ;
67
82
}
68
83
waveform++;
69
- if (waveform > (uint8_t )espp::Drv2605 ::Waveform::MAX) {
84
+ if (waveform > (uint8_t )Driver ::Waveform::MAX) {
70
85
waveform = 0 ;
71
86
}
72
- logger.info (" {:.3f}, {} " , elapsed , waveform);
87
+ logger.info (" {} " , waveform);
73
88
// NOTE: sleeping in this way allows the sleep to exit early when the
74
89
// task is being stopped / destroyed
75
90
{
0 commit comments