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

✨ Add tla2528 demos #10

Merged
merged 11 commits into from
Jan 23, 2025
3 changes: 3 additions & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ project(demos LANGUAGES CXX)
libhal_build_demos(
DEMOS
pca9685
tla2528_adc
tla2528_input_pin
tla2528_output_pin

INCLUDES
.
Expand Down
36 changes: 36 additions & 0 deletions demos/applications/tla2528_adc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <array>

#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>

#include <resource_list.hpp>

using namespace hal::literals;
using namespace std::chrono_literals;

void application(resource_list& p_map)
{
auto& terminal = *p_map.console.value();
auto& i2c = *p_map.i2c.value();
auto& steady_clock = *p_map.clock.value();
auto adc_mux = hal::expander::tla2528(i2c);
std::array<hal::expander::tla2528_adc, 8> adcs{
make_adc(adc_mux, 0), make_adc(adc_mux, 1), make_adc(adc_mux, 2),
make_adc(adc_mux, 3), make_adc(adc_mux, 4), make_adc(adc_mux, 5),
make_adc(adc_mux, 6), make_adc(adc_mux, 7)
};

while (true) {
hal::print(terminal, "\nvalues:\n");
for (int i = 0; i < 8; i++) {
hal::print<64>(terminal, "%d:%f\n", i, adcs[i].read());
}
hal::delay(steady_clock, 500ms);
}
}
37 changes: 37 additions & 0 deletions demos/applications/tla2528_input_pin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <array>

#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>

#include <resource_list.hpp>

using namespace hal::literals;
using namespace std::chrono_literals;

void application(resource_list& p_map)
{
auto& terminal = *p_map.console.value();
auto& i2c = *p_map.i2c.value();
auto& steady_clock = *p_map.clock.value();
auto gpi_expander = hal::expander::tla2528(i2c);
std::array<hal::expander::tla2528_input_pin, 8> gpis{
make_input_pin(gpi_expander, 0), make_input_pin(gpi_expander, 1),
make_input_pin(gpi_expander, 2), make_input_pin(gpi_expander, 3),
make_input_pin(gpi_expander, 4), make_input_pin(gpi_expander, 5),
make_input_pin(gpi_expander, 6), make_input_pin(gpi_expander, 7)
};

while (true) {
hal::print(terminal, "\nvalues:");
for (int i = 0; i < 8; i++) {
hal::print<4>(terminal, "%x", gpis[i].level());
}
hal::delay(steady_clock, 500ms);
}
}
50 changes: 50 additions & 0 deletions demos/applications/tla2528_output_pin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <array>

#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/output_pin.hpp>
#include <libhal/units.hpp>

#include <resource_list.hpp>

using namespace hal::literals;
using namespace std::chrono_literals;

void application(resource_list& p_map)
{
constexpr bool demo_open_drain = false;

auto& terminal = *p_map.console.value();
auto& i2c = *p_map.i2c.value();
auto& steady_clock = *p_map.clock.value();
auto gpo_expander = hal::expander::tla2528(i2c);
constexpr hal::output_pin::settings output_pin_config = {
.resistor = hal::pin_resistor::none, .open_drain = demo_open_drain
};
std::array<hal::expander::tla2528_output_pin, 8> gpos{
make_output_pin(gpo_expander, 0, output_pin_config),
make_output_pin(gpo_expander, 1, output_pin_config),
make_output_pin(gpo_expander, 2, output_pin_config),
make_output_pin(gpo_expander, 3, output_pin_config),
make_output_pin(gpo_expander, 4, output_pin_config),
make_output_pin(gpo_expander, 5, output_pin_config),
make_output_pin(gpo_expander, 6, output_pin_config),
make_output_pin(gpo_expander, 7, output_pin_config)
};

// output counts in binary to go though all out put combinations
hal::byte counter = 0;
hal::print(terminal, "Starting Binary Count\n");
while (true) {
counter++;
for (int i = 0; i < 8; i++) {
gpos[i].level(hal::bit_extract(hal::bit_mask::from(i), counter));
}
hal::print<16>(terminal, "count:%x\n", counter);
hal::delay(steady_clock, 200ms);
}
}
Loading