Skip to content
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ libhal_test_and_make_library(
LIBRARY_NAME libhal-sensor

SOURCES
src/as5600.cpp
Comment thread
MaliaLabor marked this conversation as resolved.
src/imu/icm20948.cpp
src/imu/mpu6050.cpp
src/multi/bmp180.cpp
Expand All @@ -33,5 +34,6 @@ libhal_test_and_make_library(
tests/imu/mpu6050.test.cpp
tests/multi/bmp180.test.cpp
tests/temperature/tmp102.test.cpp
tests/as5600.cpp
tests/main.test.cpp
)
Binary file added datasheets/AS5600.pdf
Binary file not shown.
10 changes: 6 additions & 4 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ project(demos LANGUAGES CXX)
libhal_build_demos(
DEMOS
#bmp180
icm20948
mpl3115a2
mpu6050
tmp102
as5600
#icm20948
Comment thread
MaliaLabor marked this conversation as resolved.
Comment thread
MaliaLabor marked this conversation as resolved.
#mpl3115a2
#mpu6050
#tmp102
#TODO(#37): update drivers and demos to use strong ptr

INCLUDES
.
Expand Down
67 changes: 67 additions & 0 deletions demos/applications/as5600.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2026 Malia Labor and the libhal contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-sensor/as5600.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>

#include <resource_list.hpp>

void application()
{
using namespace std::chrono_literals;
using namespace hal::literals;

auto const clock = resources::clock();
auto const console = resources::console();
auto const i2c = resources::i2c();

hal::print(*console, "AS5600 Application Starting...\n");
hal::sensor::as5600 hall_sensor(i2c);

auto magnet_status = hall_sensor.magnet_status();
if (magnet_status.detected) {
hal::print(*console, "Magnet detected\n");
}
auto const start_angle = hall_sensor.start_angle();
auto const stop_angle = hall_sensor.stop_angle();
auto const angular_range = hall_sensor.angular_range();
auto const power_mode = hall_sensor.power_mode();
auto const hysteresis = hall_sensor.hysteresis();
bool const watchdog_enabled = hall_sensor.watchdog_enabled();

auto const agc = hall_sensor.auto_gain_control();
auto const mag = hall_sensor.magnitude();

hal::print<32>(*console, "Start angle: %.2f \n", start_angle);
hal::print<32>(*console, "Stop angle: %.2f \n", stop_angle);
hal::print<32>(*console, "Max Angle: %.2f \n", angular_range);
hal::print<32>(*console, "Power Mode: %d \n", power_mode);
hal::print<32>(*console, "Hysteresis: %d \n", hysteresis);
hal::print<32>(*console, "WD: %d \n", watchdog_enabled);
hal::print<32>(*console, "AGC: %d \n", agc);
hal::print<32>(*console, "Magnitude: %d \n", mag);

while (true) {
magnet_status = hall_sensor.magnet_status();
if (magnet_status.detected) {
auto raw_angle = hall_sensor.raw_angle();
auto angle = hall_sensor.angle();

hal::print<64>(*console, "Raw Angle: %.2f ", raw_angle);
hal::print<64>(*console, "Angle: %.2f \n", angle);
}
hal::delay(*clock, 500ms);
}
}
91 changes: 39 additions & 52 deletions demos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,62 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-exceptions/control.hpp>
#include <cstdio>

#include <exception>

#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/error.hpp>

#include <resource_list.hpp>

resource_list resources{};
int main()
{
initialize_platform();
application();
std::terminate();
}

[[noreturn]] void terminate_handler() noexcept
// Override global new operator
void* operator new(std::size_t)
{
if (resources.console) {
hal::print(*resources.console.value(), "☠️ APPLICATION TERMINATED ☠️\n\n");
}
std::terminate();
}

if (resources.status_led && resources.clock) {
auto& led = *resources.status_led.value();
auto& clock = *resources.clock.value();
// Override global new[] operator
void* operator new[](std::size_t)
{
std::terminate();
}

while (true) {
using namespace std::chrono_literals;
led.level(false);
hal::delay(clock, 100ms);
led.level(true);
hal::delay(clock, 100ms);
led.level(false);
hal::delay(clock, 100ms);
led.level(true);
hal::delay(clock, 1000ms);
}
}
void* operator new(unsigned int, std::align_val_t)
{
std::terminate();
}

// spin here forever
while (true) {
continue;
}
// Override global delete operator
void operator delete(void*) noexcept
{
}

int main()
// Override global delete[] operator
void operator delete[](void*) noexcept
{
try {
initialize_platform(resources);
} catch (...) {
while (true) {
// halt here and wait for a debugger to connect
continue;
}
}
}

hal::set_terminate(terminate_handler);
// Optional: Override sized delete operators (C++14 and later)
void operator delete(void*, std::size_t) noexcept
{
}

try {
application(resources);
} catch (std::bad_optional_access const& e) {
if (resources.console) {
hal::print(*resources.console.value(),
"A resource required by the application was not available!\n"
"Calling terminate!\n");
}
} // Allow any other exceptions to terminate the application
void operator delete[](void*, std::size_t) noexcept
{
}

// Terminate if the code reaches this point.
std::terminate();
void operator delete[](void*, std::align_val_t) noexcept
{
}

extern "C"
void operator delete(void*, std::align_val_t) noexcept
{
// This gets rid of an issue with libhal-exceptions in Debug mode.
void __assert_func()
{
}
}
Loading
Loading