Skip to content

Commit

Permalink
🎉 Initial code dump
Browse files Browse the repository at this point in the history
  • Loading branch information
kammce committed Aug 13, 2024
1 parent cf4339a commit fbf1fa4
Show file tree
Hide file tree
Showing 26 changed files with 2,134 additions and 35 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ libhal_test_and_make_library(
LIBRARY_NAME libhal-sensor

SOURCES
src/sensor.cpp
src/imu/icm20948.cpp
src/imu/mpu6050.cpp
src/temperature/tmp102.cpp
src/multi/mpl3115a2.cpp

TEST_SOURCES
tests/sensor.test.cpp
tests/imu/mpu6050.test.cpp
tests/imu/mpu6050.test.cpp
tests/temperature/tmp102.test.cpp
tests/main.test.cpp
)
7 changes: 6 additions & 1 deletion demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ project(demos LANGUAGES CXX)

libhal_build_demos(
DEMOS
sensor
mpu6050
mpl3115a2
icm20948
tmp102

INCLUDES
.

PACKAGES
libhal-sensor
libhal-soft

LINK_LIBRARIES
libhal::sensor
libhal::soft
)
93 changes: 93 additions & 0 deletions demos/applications/icm20948.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 Khalil Estell
//
// 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 <cmath>

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

#include <resource_list.hpp>

float compute_heading(float x, float y, float offset = 0.0)
{
float angle = 360 - (atan2(y, x) * (180.0 / std::numbers::pi));
angle += offset; // Apply offset
if (angle < 0) {
angle += 360;
} else if (angle >= 360) {
angle -= 360;
}
return angle;
}

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

auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& i2c = *p_map.i2c.value();

hal::print(console, "icm Application Starting...\n\n");
hal::delay(clock, 200ms);
hal::sensor::icm20948 icm_device(i2c);

hal::delay(clock, 200ms);
icm_device.init_mag();
hal::delay(clock, 100ms);

icm_device.auto_offsets();

while (true) {
auto accel = icm_device.read_acceleration();
hal::delay(clock, 10ms);

auto gyro = icm_device.read_gyroscope();
hal::delay(clock, 10ms);

auto temp = icm_device.read_temperature();
hal::delay(clock, 10ms);

auto mag = icm_device.read_magnetometer();
hal::delay(clock, 10ms);

hal::print(console, "\n\n================Reading IMU================\n");

hal::print<128>(console,
"\n\nG-Accel Values: x = %fg, y = %fg, z = %fg",
accel.x,
accel.y,
accel.z);

hal::print<128>(console,
"\n\nGyro Values: x = %f, y = %f, z = %f",
gyro.x,
gyro.y,
gyro.z);

hal::print<128>(console, "\n\nCurrent Temperature: %f°C", temp.temp);

hal::print<128>(console,
"\n\nMagnetometer Values: x = %f, y = %f, z = %f",
mag.x,
mag.y,
mag.z);

float heading = compute_heading(mag.x, mag.y, 0.0);
hal::print<128>(console, "\n\nHeading: %f°", heading);
hal::print(console, "\n\n===========================================\n");
}
}
52 changes: 52 additions & 0 deletions demos/applications/mpl3115a2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2024 Khalil Estell
//
// 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/multi/mpl3115a2.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>

#include <resource_list.hpp>

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

auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& i2c = *p_map.i2c.value();

hal::print(console, "MPL3115A2 Demo Application Starting...\n");
hal::sensor::mpl3115a2 mpl_device(i2c);

int8_t alt_offset = 0;
mpl_device.set_altitude_offset(alt_offset);

// Set sea level pressure to 30 Hg
float slp = 101325; // Default is 101325 Pa
mpl_device.set_sea_pressure(slp);

while (true) {
hal::delay(clock, 500ms);

auto temperature = mpl_device.read_temperature().temperature;
hal::print<42>(console, "Measured temperature = %f °C\n", temperature);

auto pressure = mpl_device.read_pressure().pressure;
hal::print<42>(console, "Measured pressure = %f Pa\n", pressure);

auto altitude = mpl_device.read_altitude().altitude;
hal::print<42>(console, "Measured altitude = %f m\n\n", altitude);
}
}
63 changes: 63 additions & 0 deletions demos/applications/mpu6050.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 Khalil Estell
//
// 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/imu/mpu6050.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>

#include <resource_list.hpp>

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

auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& i2c = *p_map.i2c.value();

hal::print(console, "MPU6050 Application Starting...\n");
hal::sensor::mpu6050 mpu(i2c, 0x68);

while (true) {
hal::print(
console,
"Setting acceleration max scale to 2g (2 earth gravities)... \n");

mpu.configure_full_scale(hal::sensor::mpu6050::max_acceleration::g2);

hal::print(console, "Reading acceleration... \n");
auto acceleration = mpu.read();

hal::print<64>(console,
"Scale: 2g \t x = %fg, y = %fg, z = %fg \n",
acceleration.x,
acceleration.y,
acceleration.z);

hal::delay(clock, 500ms);

hal::print(
console,
"Setting acceleration max scale to 4g (4 earth gravities)... \n");

mpu.configure_full_scale(hal::sensor::mpu6050::max_acceleration::g4);
acceleration = mpu.read();
hal::print<64>(console,
"Scale: 4g \t x = %fg, y = %fg, z = %fg \n\n",
acceleration.x,
acceleration.y,
acceleration.z);
}
}
12 changes: 7 additions & 5 deletions demos/applications/sensor.cpp → demos/applications/tmp102.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "resource_list.hpp"
#include <libhal-sensor/temperature/tmp102.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>

#include "../resource_list.hpp"
#include <resource_list.hpp>

void application(resource_list& p_map)
{
Expand All @@ -24,13 +26,13 @@ void application(resource_list& p_map)

auto& clock = *p_map.clock.value();
auto& console = *p_map.console.value();
auto& led = *p_map.status_led.value();
auto& i2c = *p_map.i2c.value();

hal::print(console, "Demo Application Starting...\n\n");
hal::print(console, "[tmp102] Application Starting...\n\n");
hal::tmp::tmp102 tmp102(i2c);

while (true) {
hal::print(console, "Hello, world\n");
led.level(!led.level()); // Toggle LED
hal::delay(clock, 500ms);
hal::print<32>(console, "Measured temperature = %f °C\n", tmp102.read());
}
}
1 change: 1 addition & 0 deletions demos/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ def requirements(self):
bootstrap = self.python_requires["libhal-bootstrap"]
bootstrap.module.add_demo_requirements(self)
self.requires("libhal-sensor/[^1.0.0 || latest]")
self.requires("libhal-soft/[^5.2.0]")
3 changes: 1 addition & 2 deletions demos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ resource_list resources{};

[[noreturn]] void terminate_handler() noexcept
{

if (not resources.status_led && not resources.console) {
if (not resources.status_led && not resources.status_led) {
// spin here until debugger is connected
while (true) {
continue;
Expand Down
5 changes: 4 additions & 1 deletion demos/platforms/lpc4078.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
#include <libhal-armcortex/system_control.hpp>
#include <libhal-lpc40/clock.hpp>
#include <libhal-lpc40/constants.hpp>
#include <libhal-lpc40/i2c.hpp>
#include <libhal-lpc40/output_pin.hpp>
#include <libhal-lpc40/uart.hpp>
#include <libhal-util/as_bytes.hpp>

#include "../resource_list.hpp"
#include <resource_list.hpp>

resource_list initialize_platform()
{
Expand All @@ -43,11 +44,13 @@ resource_list initialize_platform()
});

static hal::lpc40::output_pin led(1, 10);
static hal::lpc40::i2c i2c(2);

return {
.reset = []() { hal::cortex_m::reset(); },
.console = &uart0,
.clock = &counter,
.status_led = &led,
.i2c = &i2c,
};
}
2 changes: 2 additions & 0 deletions demos/platforms/micromod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ resource_list initialize_platform()
.console = &hal::micromod::v1::console(hal::buffer<128>),
.clock = &hal::micromod::v1::uptime_clock(),
.status_led = &hal::micromod::v1::led(),
// TODO(): Add i2c back when mod-stm32f1-v4 supports i2c
// .i2c = &i2c,
};
}
16 changes: 7 additions & 9 deletions demos/platforms/stm32f103c8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-arm-mcu/dwt_counter.hpp>
#include <libhal-arm-mcu/startup.hpp>
#include <libhal-arm-mcu/stm32f1/clock.hpp>
#include <libhal-arm-mcu/stm32f1/constants.hpp>
#include <libhal-arm-mcu/stm32f1/output_pin.hpp>
#include <libhal-arm-mcu/stm32f1/uart.hpp>
#include <libhal-arm-mcu/system_control.hpp>
#include <libhal/units.hpp>

#include <libhal-armcortex/dwt_counter.hpp>
#include <libhal-armcortex/startup.hpp>
#include <libhal-armcortex/system_control.hpp>

#include <libhal-stm32f1/clock.hpp>
#include <libhal-stm32f1/constants.hpp>
#include <libhal-stm32f1/output_pin.hpp>
#include <libhal-stm32f1/uart.hpp>

#include <resource_list.hpp>

resource_list initialize_platform()
Expand Down
2 changes: 2 additions & 0 deletions demos/resource_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <optional>

#include <libhal/functional.hpp>
#include <libhal/i2c.hpp>
#include <libhal/output_pin.hpp>
#include <libhal/serial.hpp>
#include <libhal/steady_clock.hpp>
Expand All @@ -27,6 +28,7 @@ struct resource_list
std::optional<hal::serial*> console;
std::optional<hal::steady_clock*> clock;
std::optional<hal::output_pin*> status_led;
std::optional<hal::i2c*> i2c;
// Add more driver interfaces here ...
};

Expand Down
Loading

0 comments on commit fbf1fa4

Please sign in to comment.