Skip to content

Commit 4dfb39b

Browse files
committed
🎉 Initial code dump
1 parent cf4339a commit 4dfb39b

26 files changed

Lines changed: 2138 additions & 39 deletions

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ libhal_test_and_make_library(
1919
LIBRARY_NAME libhal-sensor
2020

2121
SOURCES
22-
src/sensor.cpp
22+
src/imu/icm20948.cpp
23+
src/imu/mpu6050.cpp
24+
src/temperature/tmp102.cpp
25+
src/multi/mpl3115a2.cpp
2326

2427
TEST_SOURCES
25-
tests/sensor.test.cpp
28+
tests/imu/mpu6050.test.cpp
29+
tests/imu/mpu6050.test.cpp
30+
tests/temperature/tmp102.test.cpp
2631
tests/main.test.cpp
2732
)

demos/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ project(demos LANGUAGES CXX)
1818

1919
libhal_build_demos(
2020
DEMOS
21-
sensor
21+
mpu6050
22+
mpl3115a2
23+
icm20948
24+
tmp102
2225

2326
INCLUDES
2427
.
2528

2629
PACKAGES
2730
libhal-sensor
31+
libhal-soft
2832

2933
LINK_LIBRARIES
3034
libhal::sensor
35+
libhal::soft
3136
)

demos/applications/icm20948.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2024 Khalil Estell
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <cmath>
16+
17+
#include <libhal-sensor/imu/icm20948.hpp>
18+
#include <libhal-util/serial.hpp>
19+
#include <libhal-util/steady_clock.hpp>
20+
21+
#include <resource_list.hpp>
22+
23+
float compute_heading(float x, float y, float offset = 0.0)
24+
{
25+
float angle = 360 - (atan2(y, x) * (180.0 / std::numbers::pi));
26+
angle += offset; // Apply offset
27+
if (angle < 0) {
28+
angle += 360;
29+
} else if (angle >= 360) {
30+
angle -= 360;
31+
}
32+
return angle;
33+
}
34+
35+
void application(resource_list& p_map)
36+
{
37+
using namespace std::chrono_literals;
38+
using namespace hal::literals;
39+
40+
auto& clock = *p_map.clock.value();
41+
auto& console = *p_map.console.value();
42+
auto& i2c = *p_map.i2c.value();
43+
44+
hal::print(console, "icm Application Starting...\n\n");
45+
hal::delay(clock, 200ms);
46+
hal::sensor::icm20948 icm_device(i2c);
47+
48+
hal::delay(clock, 200ms);
49+
icm_device.init_mag();
50+
hal::delay(clock, 100ms);
51+
52+
icm_device.auto_offsets();
53+
54+
while (true) {
55+
auto accel = icm_device.read_acceleration();
56+
hal::delay(clock, 10ms);
57+
58+
auto gyro = icm_device.read_gyroscope();
59+
hal::delay(clock, 10ms);
60+
61+
auto temp = icm_device.read_temperature();
62+
hal::delay(clock, 10ms);
63+
64+
auto mag = icm_device.read_magnetometer();
65+
hal::delay(clock, 10ms);
66+
67+
hal::print(console, "\n\n================Reading IMU================\n");
68+
69+
hal::print<128>(console,
70+
"\n\nG-Accel Values: x = %fg, y = %fg, z = %fg",
71+
accel.x,
72+
accel.y,
73+
accel.z);
74+
75+
hal::print<128>(console,
76+
"\n\nGyro Values: x = %f, y = %f, z = %f",
77+
gyro.x,
78+
gyro.y,
79+
gyro.z);
80+
81+
hal::print<128>(console, "\n\nCurrent Temperature: %f°C", temp.temp);
82+
83+
hal::print<128>(console,
84+
"\n\nMagnetometer Values: x = %f, y = %f, z = %f",
85+
mag.x,
86+
mag.y,
87+
mag.z);
88+
89+
float heading = compute_heading(mag.x, mag.y, 0.0);
90+
hal::print<128>(console, "\n\nHeading: %f°", heading);
91+
hal::print(console, "\n\n===========================================\n");
92+
}
93+
}

demos/applications/mpl3115a2.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2024 Khalil Estell
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <libhal-sensor/multi/mpl3115a2.hpp>
16+
#include <libhal-util/serial.hpp>
17+
#include <libhal-util/steady_clock.hpp>
18+
19+
#include <resource_list.hpp>
20+
21+
void application(resource_list& p_map)
22+
{
23+
using namespace std::chrono_literals;
24+
using namespace hal::literals;
25+
26+
auto& clock = *p_map.clock.value();
27+
auto& console = *p_map.console.value();
28+
auto& i2c = *p_map.i2c.value();
29+
30+
hal::print(console, "MPL3115A2 Demo Application Starting...\n");
31+
hal::sensor::mpl3115a2 mpl_device(i2c);
32+
33+
int8_t alt_offset = 0;
34+
mpl_device.set_altitude_offset(alt_offset);
35+
36+
// Set sea level pressure to 30 Hg
37+
float slp = 101325; // Default is 101325 Pa
38+
mpl_device.set_sea_pressure(slp);
39+
40+
while (true) {
41+
hal::delay(clock, 500ms);
42+
43+
auto temperature = mpl_device.read_temperature().temperature;
44+
hal::print<42>(console, "Measured temperature = %f °C\n", temperature);
45+
46+
auto pressure = mpl_device.read_pressure().pressure;
47+
hal::print<42>(console, "Measured pressure = %f Pa\n", pressure);
48+
49+
auto altitude = mpl_device.read_altitude().altitude;
50+
hal::print<42>(console, "Measured altitude = %f m\n\n", altitude);
51+
}
52+
}

demos/applications/mpu6050.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2024 Khalil Estell
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <libhal-sensor/imu/mpu6050.hpp>
16+
#include <libhal-util/serial.hpp>
17+
#include <libhal-util/steady_clock.hpp>
18+
19+
#include <resource_list.hpp>
20+
21+
void application(resource_list& p_map)
22+
{
23+
using namespace std::chrono_literals;
24+
using namespace hal::literals;
25+
26+
auto& clock = *p_map.clock.value();
27+
auto& console = *p_map.console.value();
28+
auto& i2c = *p_map.i2c.value();
29+
30+
hal::print(console, "MPU6050 Application Starting...\n");
31+
hal::sensor::mpu6050 mpu(i2c, 0x68);
32+
33+
while (true) {
34+
hal::print(
35+
console,
36+
"Setting acceleration max scale to 2g (2 earth gravities)... \n");
37+
38+
mpu.configure_full_scale(hal::sensor::mpu6050::max_acceleration::g2);
39+
40+
hal::print(console, "Reading acceleration... \n");
41+
auto acceleration = mpu.read();
42+
43+
hal::print<64>(console,
44+
"Scale: 2g \t x = %fg, y = %fg, z = %fg \n",
45+
acceleration.x,
46+
acceleration.y,
47+
acceleration.z);
48+
49+
hal::delay(clock, 500ms);
50+
51+
hal::print(
52+
console,
53+
"Setting acceleration max scale to 4g (4 earth gravities)... \n");
54+
55+
mpu.configure_full_scale(hal::sensor::mpu6050::max_acceleration::g4);
56+
acceleration = mpu.read();
57+
hal::print<64>(console,
58+
"Scale: 4g \t x = %fg, y = %fg, z = %fg \n\n",
59+
acceleration.x,
60+
acceleration.y,
61+
acceleration.z);
62+
}
63+
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "resource_list.hpp"
16+
#include <libhal-sensor/temperature/tmp102.hpp>
1517
#include <libhal-util/serial.hpp>
1618
#include <libhal-util/steady_clock.hpp>
1719

18-
#include "../resource_list.hpp"
20+
#include <resource_list.hpp>
1921

2022
void application(resource_list& p_map)
2123
{
@@ -24,13 +26,13 @@ void application(resource_list& p_map)
2426

2527
auto& clock = *p_map.clock.value();
2628
auto& console = *p_map.console.value();
27-
auto& led = *p_map.status_led.value();
29+
auto& i2c = *p_map.i2c.value();
2830

29-
hal::print(console, "Demo Application Starting...\n\n");
31+
hal::print(console, "[tmp102] Application Starting...\n\n");
32+
hal::tmp::tmp102 tmp102(i2c);
3033

3134
while (true) {
32-
hal::print(console, "Hello, world\n");
33-
led.level(!led.level()); // Toggle LED
3435
hal::delay(clock, 500ms);
36+
hal::print<32>(console, "Measured temperature = %f °C\n", tmp102.read());
3537
}
3638
}

demos/conanfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ def requirements(self):
2222
bootstrap = self.python_requires["libhal-bootstrap"]
2323
bootstrap.module.add_demo_requirements(self)
2424
self.requires("libhal-sensor/[^1.0.0 || latest]")
25+
self.requires("libhal-soft/[^5.2.0]")

demos/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ resource_list resources{};
2323

2424
[[noreturn]] void terminate_handler() noexcept
2525
{
26-
27-
if (not resources.status_led && not resources.console) {
26+
if (not resources.status_led && not resources.status_led) {
2827
// spin here until debugger is connected
2928
while (true) {
3029
continue;

demos/platforms/lpc4078.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#include <libhal-armcortex/system_control.hpp>
1818
#include <libhal-lpc40/clock.hpp>
1919
#include <libhal-lpc40/constants.hpp>
20+
#include <libhal-lpc40/i2c.hpp>
2021
#include <libhal-lpc40/output_pin.hpp>
2122
#include <libhal-lpc40/uart.hpp>
2223
#include <libhal-util/as_bytes.hpp>
2324

24-
#include "../resource_list.hpp"
25+
#include <resource_list.hpp>
2526

2627
resource_list initialize_platform()
2728
{
@@ -43,11 +44,13 @@ resource_list initialize_platform()
4344
});
4445

4546
static hal::lpc40::output_pin led(1, 10);
47+
static hal::lpc40::i2c i2c(2);
4648

4749
return {
4850
.reset = []() { hal::cortex_m::reset(); },
4951
.console = &uart0,
5052
.clock = &counter,
5153
.status_led = &led,
54+
.i2c = &i2c,
5255
};
5356
}

demos/platforms/micromod.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ resource_list initialize_platform()
2727
.console = &hal::micromod::v1::console(hal::buffer<128>),
2828
.clock = &hal::micromod::v1::uptime_clock(),
2929
.status_led = &hal::micromod::v1::led(),
30+
// TODO(): Add i2c back when mod-stm32f1-v4 supports i2c
31+
// .i2c = &i2c,
3032
};
3133
}

0 commit comments

Comments
 (0)