first commit for demo usage

This commit is contained in:
zys 2026-03-20 15:43:39 +08:00
parent beb9a3e4a2
commit 9de7db11f6
6 changed files with 116 additions and 54 deletions

View File

@ -0,0 +1,15 @@
&spi0 {
status = "okay";
potentiometer1: mcp41xxx@0 {
compatible = "microchip,mcp41xxx";
reg = <0>;
res-kohms = <100>;
spi-max-frequency = <1000000>;
};
potentiometer2: tpl0501@0 {
compatible = "ti,tpl0501";
reg = <0>;
res-kohms = <100>;
spi-max-frequency = <1000000>;
};
};

25
boards/rpi_pico.overlay Normal file
View File

@ -0,0 +1,25 @@
/ {
zephyr,user {
in3-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>;
in4-gpios = <&gpio0 8 GPIO_ACTIVE_HIGH>;
enb-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>;
};
};
&uart0 {
status = "okay";
protocal: uart-com-protocal {
compatible = "uart-com-protocal";
status = "okay";
header = <0x7e 0xe7>;
crc16 = "none";
freq{
id = <0x00>;
};
switch{
id = <0x01>;
};
status{
id = <0x02>;
};
};
};

View File

@ -0,0 +1,3 @@
/ {
};

10
include/modbus.hpp Normal file
View File

@ -0,0 +1,10 @@
#ifndef __APP_MODBUS_HPP__
#define __APP_MODBUS_HPP__
namespace zpp {
}
#endif

View File

@ -1,5 +1,4 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_STD_CPP20=y
CONFIG_CPP=y
CONFIG_REQUIRES_FULL_LIBCPP=y
@ -7,8 +6,5 @@ CONFIG_REQUIRES_FULL_LIBCPP=y
CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_PMC_COM=y
CONFIG_HWINFO=y
CONFIG_REBOOT=y
CONFIG_WATCHDOG=y
CONFIG_LOG=y

View File

@ -1,61 +1,74 @@
#include <etl/vector.h>
#include <zephyr/drivers/hwinfo.h>
#include <zephyr/drivers/sensor.h>
#include <led_strip_indicator/led_strip_indicator.hpp>
#include <uart_com/protocal.hpp>
#include <zpp/device.hpp>
#include "watchdog.hpp"
#include "zpp/fmt.hpp"
#include <zephyr/drivers/gpio.h>
#include <zephyr/kernel.h>
namespace {
auto sensor = DEVICE_DT_GET(DT_NODELABEL(godtek));
auto &pmc = ZPP_DRV_GET(uart_com::Protocal, DT_NODELABEL(pm_protocal));
auto &led_strip = ZPP_DRV_GET(ledstrip::Indicator, DT_NODELABEL(indicator));
enum Command { kTemp = 0x00, kGetId, kRunningState };
enum RunningState {};
constexpr auto kDeviceIdSize = 20;
enum StatusId : uint8_t { kRunning, kPause, kError, kStandby };
volatile bool flag{false};
enum Cmd {
kFreq = 0x00,
kSwitch = 0x01,
kStatus = 0x02,
};
#define ZEPHYR_USER_NODE DT_PATH(zephyr_user)
const gpio_dt_spec in3 = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, in3_gpios);
const gpio_dt_spec in4 = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, in4_gpios);
const gpio_dt_spec enb = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, enb_gpios);
volatile uint8_t duty_cycle_event{50};
volatile bool is_enable{false};
class Motor {
public:
Motor() {
gpio_pin_configure_dt(&in3, GPIO_OUTPUT_INACTIVE);
gpio_pin_configure_dt(&in4, GPIO_OUTPUT_INACTIVE);
gpio_pin_configure_dt(&enb, GPIO_OUTPUT_INACTIVE);
}
auto Rotate() -> void {
gpio_pin_set_dt(&in3, 1);
gpio_pin_set_dt(&in4, 0);
};
auto Reverse() {
gpio_pin_set_dt(&in3, 0);
gpio_pin_set_dt(&in4, 1);
};
auto Stop() {
gpio_pin_set_dt(&in3, 0);
gpio_pin_set_dt(&in4, 0);
}
};
Motor motor{};
} // namespace
auto main(void) -> int {
pmc.SetRxCallback(kGetId, [](uart_com::DataType data) -> void {
uint8_t buffer[kDeviceIdSize];
auto size = hwinfo_get_device_id(buffer, sizeof(buffer));
pmc.Send(kGetId, uart_com::DataType(buffer, size));
});
auto &protocal = ZPP_DRV_GET(uart_com::Protocal, DT_NODELABEL(protocal));
protocal.SetRxCallback(kSwitch, [](uart_com::DataType data) {
if (data[0] == 0) {
motor.Stop();
} else {
motor.Rotate();
}
pmc.SetRxCallback(kRunningState, [](uart_com::DataType data) -> void {
auto state = static_cast<StatusId>(data[0]);
pmc.Send(
kRunningState,
uart_com::DataType(reinterpret_cast<uint8_t *>(&state), sizeof(state)));
led_strip.Status(state).on_error([](zpp::error_code code) {});
printk("set switch: %d\n", data[0]);
is_enable = data[0];
});
protocal.SetRxCallback(kFreq, [](uart_com::DataType data) {
printk("set freq: %d\n", data[0]);
uint8_t freq = 0;
if (data[0] == 100) {
freq = 100;
}
freq = data[0];
duty_cycle_event = freq;
});
auto wdt = app::WatchDogConfig{};
sensor_trigger tri{.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_AMBIENT_TEMP};
if (auto r = sensor_trigger_set(
sensor, &tri,
[](const device *dev, const sensor_trigger *trig) { flag = true; });
r != 0) {
return -ENODEV;
};
while (1) {
if (flag) {
sensor_value val{0, 0};
if (sensor_sample_fetch_chan(sensor, SENSOR_CHAN_AMBIENT_TEMP) == 0) {
sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &val);
const uint8_t data[2] = {static_cast<uint8_t>(val.val1),
static_cast<uint8_t>(val.val2)};
pmc.Send("temp", data);
}
flag = false;
if (is_enable) {
printk("duty circle %d\n", duty_cycle_event);
gpio_pin_set_dt(&enb, 1);
k_sleep(K_MSEC(duty_cycle_event));
gpio_pin_set_dt(&enb, 0);
k_sleep(K_MSEC(100 - duty_cycle_event));
}
wdt.Feed();
}
return 0;
}