Skip to content

Commit

Permalink
Feat/ads7138 delayed init (#113)
Browse files Browse the repository at this point in the history
* feat(ads7138): add initialize function
* Store Config in Ads7138 class
* Add initialize() function which calls protected init(config_)
* Update Config to have auto_init bool (default=true)
* Only call initialize() in constructor if config.auto_init is true

* doc: rebuild
  • Loading branch information
finger563 authored Oct 18, 2023
1 parent 32042a2 commit f37dc95
Show file tree
Hide file tree
Showing 75 changed files with 228 additions and 156 deletions.
20 changes: 18 additions & 2 deletions components/ads7138/include/ads7138.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class Ads7138 {
bool statistics_enabled = true; ///< Enable statistics collection (min, max, recent)
write_fn write; ///< Function to write to the ADC
read_fn read; ///< Function to read from the ADC
bool auto_init = true; ///< Automatically initialize the ADC on construction. If false,
///< initialize() must be called before any other functions.
espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN}; ///< Verbosity for the logger.
};

Expand All @@ -174,7 +176,7 @@ class Ads7138 {
* @param config Configuration structure.
*/
Ads7138(const Config &config)
: mode_(config.mode), avdd_mv_(config.avdd_volts * 1000.0f) // Convert to mV
: config_(config), mode_(config.mode), avdd_mv_(config.avdd_volts * 1000.0f) // Convert to mV
,
data_format_(config.oversampling_ratio == OversamplingRatio::NONE ? DataFormat::RAW
: DataFormat::AVERAGED),
Expand All @@ -183,9 +185,21 @@ class Ads7138 {
oversampling_ratio_(config.oversampling_ratio), address_(config.device_address),
write_(config.write), read_(config.read),
logger_({.tag = "Ads7138", .level = config.log_level}) {
init(config);
// initialize the ADC
if (config.auto_init) {
initialize();
}
}

/**
* @brief Initialize the ADC
* This function uses the configuration structure passed to the
* constructor to configure the ADC.
* @note This function must be called before any other functions as it
* configures the ADC pins and sets the mode.
*/
void initialize() { init(config_); }

/**
* @brief Communicate with the ADC to get the analog value for the channel
* and return it.
Expand Down Expand Up @@ -1313,6 +1327,8 @@ class Ads7138 {
write_(address_, data_with_header, total_len);
}

Config config_;

Mode mode_;
float avdd_mv_;
DataFormat data_format_;
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/adc_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADC Types</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -158,7 +158,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/adc/include/adc_types.hpp">components/adc/include/adc_types.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/adc/include/adc_types.hpp">components/adc/include/adc_types.hpp</a></p></li>
</ul>
</section>
</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads1x15.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS1x15 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -159,7 +159,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
20 changes: 18 additions & 2 deletions docs/adc/ads7138.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS7138 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -164,7 +164,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down Expand Up @@ -675,6 +675,16 @@ <h4>ADS7138 Example<a class="headerlink" href="#classespp_1_1_ads7138_1ads7138_e
</dl>
</dd></dl>

<dl class="cpp function">
<dt class="sig sig-object cpp" id="_CPPv4N4espp7Ads713810initializeEv">
<span id="_CPPv3N4espp7Ads713810initializeEv"></span><span id="_CPPv2N4espp7Ads713810initializeEv"></span><span id="espp::Ads7138::initialize"></span><span class="target" id="classespp_1_1_ads7138_1a7e01761680cb201c0e62f96b3dd2aaaf"></span><span class="k"><span class="pre">inline</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">initialize</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv4N4espp7Ads713810initializeEv" title="Permalink to this definition"></a><br /></dt>
<dd><p>Initialize the ADC This function uses the configuration structure passed to the constructor to configure the ADC. </p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function must be called before any other functions as it configures the ADC pins and sets the mode. </p>
</div>
</dd></dl>

<dl class="cpp function">
<dt class="sig sig-object cpp" id="_CPPv4N4espp7Ads71386get_mvE7Channel">
<span id="_CPPv3N4espp7Ads71386get_mvE7Channel"></span><span id="_CPPv2N4espp7Ads71386get_mvE7Channel"></span><span id="espp::Ads7138::get_mv__Channel"></span><span class="target" id="classespp_1_1_ads7138_1a4a8b217b4e0ff3ffa2ba8709670472b4"></span><span class="k"><span class="pre">inline</span></span><span class="w"> </span><span class="kt"><span class="pre">float</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">get_mv</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#_CPPv4N4espp7Ads71387ChannelE" title="espp::Ads7138::Channel"><span class="n"><span class="pre">Channel</span></span></a><span class="w"> </span><span class="n sig-param"><span class="pre">channel</span></span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv4N4espp7Ads71386get_mvE7Channel" title="Permalink to this definition"></a><br /></dt>
Expand Down Expand Up @@ -1053,6 +1063,12 @@ <h4>ADS7138 Example<a class="headerlink" href="#classespp_1_1_ads7138_1ads7138_e
<dd><p>Function to read from the ADC. </p>
</dd></dl>

<dl class="cpp var">
<dt class="sig sig-object cpp" id="_CPPv4N4espp7Ads71386Config9auto_initE">
<span id="_CPPv3N4espp7Ads71386Config9auto_initE"></span><span id="_CPPv2N4espp7Ads71386Config9auto_initE"></span><span id="espp::Ads7138::Config::auto_init__b"></span><span class="target" id="structespp_1_1_ads7138_1_1_config_1a89cceac61ab51388da1b3374bed9d74f"></span><span class="kt"><span class="pre">bool</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">auto_init</span></span></span><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="k"><span class="pre">true</span></span><a class="headerlink" href="#_CPPv4N4espp7Ads71386Config9auto_initE" title="Permalink to this definition"></a><br /></dt>
<dd><p>Automatically initialize the ADC on construction. If false, <a class="reference internal" href="#classespp_1_1_ads7138_1a7e01761680cb201c0e62f96b3dd2aaaf"><span class="std std-ref">initialize()</span></a> must be called before any other functions. </p>
</dd></dl>

<dl class="cpp var">
<dt class="sig sig-object cpp" id="_CPPv4N4espp7Ads71386Config9log_levelE">
<span id="_CPPv3N4espp7Ads71386Config9log_levelE"></span><span id="_CPPv2N4espp7Ads71386Config9log_levelE"></span><span id="espp::Ads7138::Config::log_level__espp::Logger::Verbosity"></span><span class="target" id="structespp_1_1_ads7138_1_1_config_1a0eaf1e965da1dde8c44e2601089ce84e"></span><span class="n"><span class="pre">espp</span></span><span class="p"><span class="pre">::</span></span><a class="reference internal" href="../logger.html#_CPPv4N4espp6LoggerE" title="espp::Logger"><span class="n"><span class="pre">Logger</span></span></a><span class="p"><span class="pre">::</span></span><a class="reference internal" href="../logger.html#_CPPv4N4espp6Logger9VerbosityE" title="espp::Logger::Verbosity"><span class="n"><span class="pre">Verbosity</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">log_level</span></span></span><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="p"><span class="pre">{</span></span><span class="n"><span class="pre">espp</span></span><span class="p"><span class="pre">::</span></span><a class="reference internal" href="../logger.html#_CPPv4N4espp6LoggerE" title="espp::Logger"><span class="n"><span class="pre">Logger</span></span></a><span class="p"><span class="pre">::</span></span><a class="reference internal" href="../logger.html#_CPPv4N4espp6Logger9VerbosityE" title="espp::Logger::Verbosity"><span class="n"><span class="pre">Verbosity</span></span></a><span class="p"><span class="pre">::</span></span><a class="reference internal" href="../logger.html#_CPPv4N4espp6Logger9Verbosity4WARNE" title="espp::Logger::Verbosity::WARN"><span class="n"><span class="pre">WARN</span></span></a><span class="p"><span class="pre">}</span></span><a class="headerlink" href="#_CPPv4N4espp7Ads71386Config9log_levelE" title="Permalink to this definition"></a><br /></dt>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/continuous_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Continuous ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -164,7 +164,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>ADC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/oneshot_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Oneshot ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -163,7 +163,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/bldc/bldc_driver.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Driver</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -156,7 +156,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
8 changes: 4 additions & 4 deletions docs/bldc/bldc_motor.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Motor</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down Expand Up @@ -172,7 +172,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down Expand Up @@ -565,13 +565,13 @@ <h4>Example Usage<a class="headerlink" href="#classespp_1_1_bldc_motor_1bldc_mot
<section id="id1">
<h3>Header File<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/bldc_motor/include/bldc_types.hpp">components/bldc_motor/include/bldc_types.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/bldc_motor/include/bldc_types.hpp">components/bldc_motor/include/bldc_types.hpp</a></p></li>
</ul>
</section>
<section id="id2">
<h3>Header File<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/bldc_motor/include/sensor_direction.hpp">components/bldc_motor/include/sensor_direction.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/bldc_motor/include/sensor_direction.hpp">components/bldc_motor/include/sensor_direction.hpp</a></p></li>
</ul>
</section>
</section>
Expand Down
2 changes: 1 addition & 1 deletion docs/bldc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>BLDC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/button.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Button APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/87d6eb4/docs/en/button.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/43b9241/docs/en/button.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -160,7 +160,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/87d6eb4/components/button/include/button.hpp">components/button/include/button.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/43b9241/components/button/include/button.hpp">components/button/include/button.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
Loading

0 comments on commit f37dc95

Please sign in to comment.