Last active
April 10, 2024 20:54
-
-
Save kammce/fbc8467e349a52f797b3de3346977730 to your computer and use it in GitHub Desktop.
Quick driver for the ld2410c human presence board
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. | |
// Datasheet used: https://naylampmechatronics.com/img/cms/001080/Protocolo_comunicacion_serial_LD2410C.pdf | |
// I threw this into the libhal-lpc40/demos just for speed. You can copy this file into that project and go from there. | |
#include <array> | |
#include <libhal-armcortex/dwt_counter.hpp> | |
#include <libhal-lpc40/clock.hpp> | |
#include <libhal-lpc40/constants.hpp> | |
#include <libhal-lpc40/uart.hpp> | |
#include <libhal-util/serial.hpp> | |
#include <libhal-util/steady_clock.hpp> | |
#include <libhal-util/streams.hpp> | |
#include <libhal-util/timeout.hpp> | |
#include <libhal/serial.hpp> | |
void application() | |
{ | |
hal::cortex_m::dwt_counter counter( | |
hal::lpc40::get_frequency(hal::lpc40::peripheral::cpu)); | |
std::array<hal::byte, 32> uart0_receive_buffer{}; | |
hal::lpc40::uart uart0(0, uart0_receive_buffer); | |
std::array<hal::byte, 512> ld2410c_receive_buffer{}; | |
hal::lpc40::uart uart3(3, | |
ld2410c_receive_buffer, | |
hal::serial::settings{ | |
.baud_rate = 256'000, | |
}); | |
static constexpr std::array<hal::byte, 4> start_sequence{ | |
0xF4, 0xF3, 0xF2, 0xF1 | |
}; | |
static constexpr std::array<hal::byte, 4> end_sequence{ | |
0xF8, 0xF7, 0xF6, 0xF5 | |
}; | |
std::array<hal::byte, 2> length_buffer{}; | |
std::array<hal::byte, 32> payload_buffer{}; | |
hal::stream_find find_start(start_sequence); | |
hal::stream_skip skip_last_start_char(1); | |
hal::stream_fill fill_length(length_buffer); | |
hal::stream_fill_upto fill_payload(end_sequence, payload_buffer); | |
hal::print(uart0, "Starting ld2410c application!\n"); | |
while (true) { | |
using namespace std::chrono_literals; | |
using namespace std::string_view_literals; | |
std::array<hal::byte, 64> read_buffer{}; | |
auto received = uart3.read(read_buffer).data; | |
// pipe received data into stream flow | |
received | find_start | skip_last_start_char | fill_length | fill_payload; | |
#if 0 | |
if (not received.empty()) { | |
hal::print(uart0, "[ "); | |
for (const auto& my_byte : received) { | |
hal::print<8>(uart0, "0x%02X, ", my_byte); | |
} | |
hal::print(uart0, "]"); | |
} | |
#endif | |
if (hal::finished(fill_payload)) { | |
unsigned length = length_buffer[1] << 8 | length_buffer[0]; | |
hal::byte data_type = fill_payload.span()[0]; | |
hal::byte head = fill_payload.span()[1]; | |
hal::byte target_status = fill_payload.span()[2]; | |
hal::byte target_movement_cm = fill_payload.span()[3]; | |
hal::byte exercise_target_energy_value = fill_payload.span()[4]; | |
hal::byte distance_to_stationary_target_cm = fill_payload.span()[5]; | |
hal::byte stationary_target_energy_value = fill_payload.span()[6]; | |
payload_buffer.fill(0); | |
length_buffer.fill(0); | |
hal::print<64>(uart0, | |
"L: %04u T: 0x%02X H: 0x%02X S: 0x%02X ", | |
length, | |
data_type, | |
head, | |
target_status); | |
hal::print<64>(uart0, | |
"stev: 0x%02X etev: 0x%02X ", | |
stationary_target_energy_value, | |
exercise_target_energy_value); | |
hal::print<64>(uart0, | |
"stationary_cm: 0x%02X (%04d) ", | |
distance_to_stationary_target_cm, | |
distance_to_stationary_target_cm); | |
hal::print<64>(uart0, | |
"movement_cm: 0x%02X (%04d)\n", | |
target_movement_cm, | |
target_movement_cm); | |
// Reset the streams | |
find_start = hal::stream_find(start_sequence); | |
skip_last_start_char = hal::stream_skip(1); | |
fill_length = hal::stream_fill(length_buffer); | |
fill_payload = hal::stream_fill_upto(end_sequence, payload_buffer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment