|
|
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zboard.h"
#include "zport.h"
//
#include "../copyZ_zstm32uart_irq_rx_service/zstm32uart_irq_rx_service.h"
#include "gpio.h"
#include "iwdg.h"
#include "main.h"
#include "modbus_processer.h"
#include "tim.h"
#include "usart.h"
#include "zmodbus_slave.h"
static uint8_t modbus_rx_buf[kmodbus_processer_rx_buf_size]; static uint8_t modbus_tx_buf[kmodbus_processer_tx_buf_size];
//当触发闪光时候,自动触发拍照信号
uint16_t g_auto_trigger_shut_when_flash = 1;
// state
uint32_t g_light_flicker_trigger_ticket; bool g_flicker_on = false;
/***********************************************************************************************************************
* ========================================================GLOBAL======================================================== * ***********************************************************************************************************************/ typedef enum { klight_control_mode_always_close = 0, klight_control_mode_always_open, klight_control_mode_flicker, klight_control_mode_control_direct, klight_control_mode_max, } light_control_mode_t;
// config
uint16_t g_light_brightness = 0; uint16_t g_light_control_mode = klight_control_mode_always_close; uint16_t g_light_flicker_duration = 0;
void update_light_state() { if (g_light_control_mode == klight_control_mode_always_close) { port_set_led_brightness(0); } else if (g_light_control_mode == klight_control_mode_always_open) { port_set_led_brightness(g_light_brightness); } }
void trigger_one_time_flicker(uint32_t brightness, uint16_t duration_ms) { //
g_light_flicker_trigger_ticket = HAL_GetTick(); g_light_flicker_duration = duration_ms; g_flicker_on = true; port_set_led_brightness(brightness); if (g_auto_trigger_shut_when_flash > 0) { if (port_extra_gpio_get_mode(0) == kegm_camera_trigger_signal) { port_extra_gpio_set_level(0, true); } if (port_extra_gpio_get_mode(1) == kegm_camera_trigger_signal) { port_extra_gpio_set_level(1, true); } } }
void order_uart485_tx(uint8_t* rx, uint16_t len) { HAL_UART_Transmit(&ORDER_485_UART, rx, len, 0xffffffff); }
/***********************************************************************************************************************
* =======================================================UART======================================================== * ***********************************************************************************************************************/ static zstm32uart_t s_uarts[] = { { .huart = &ORDER_485_UART, } //
};
void modbux_process_rx(modbus_processer_context_t* context) { // 10 and 03
// 0->15 | 闪光灯控制
// 0(16Byte) | 闪光灯亮度(0->65536)
// 1(16Byte) | 闪光灯亮度(0->100)
// 2(16Byte) | mode(0:长关模式,1:常亮模式,2:触发模式)
// 3-4(32Byte) | 触发一次闪光,reg3亮度(0->65535),reg4持续时间ms
// 7(16Byte) | 触发模式下,是否驱动GPIO,输出拍照信号 (0:输出,1:不输出)
// ------------|-------------------------------
// 16->31 | 扩展GPIO1控制
// 16(16Byte) | 扩展GPIO功能(0:失能,1:触发模式输出)
// 17(16Byte) | 是否反向
// ------------|-------------------------------
// 32->47 | 扩展GPIO2控制
// 32(16Byte) | 扩展GPIO功能(0:失能,1:触发模式输出)
// 33(16Byte) | 是否反向
ModbusFunctionCode fcode = modbus_get_function_code(context); if (fcode == ModbusOrder10) { //写
// REG0
if (modbus_if_register_exists_10(context, 0)) { uint16_t brightness = modbus_get_reg_10(context, 0); g_light_brightness = brightness; LOGD("set light_brightness %d", brightness); update_light_state(); } // REG1
if (modbus_if_register_exists_10(context, 1)) { uint16_t brightness_100 = modbus_get_reg_10(context, 1); brightness_100 = brightness_100 > 100 ? 100 : brightness_100; uint16_t brightness = 65535 * (brightness_100 / 100.0); g_light_brightness = brightness; LOGD("set light_brightness %d%%(%d)", brightness_100, brightness); update_light_state(); } // REG2
if (modbus_if_register_exists_10(context, 2)) { if (g_light_control_mode < klight_control_mode_max) { g_light_control_mode = modbus_get_reg_10(context, 2); LOGD("set light_control_mode %d", g_light_control_mode); update_light_state(); } else { LOGE("set light_control_mode fail,parameter error"); } } // REG3,4
if (modbus_if_register_exists_10(context, 3) && modbus_if_register_exists_03(context, 4)) { //触发一次闪光
if (g_light_control_mode == klight_control_mode_flicker) { uint32_t brightness = modbus_get_reg_10(context, 3); uint16_t duration_ms = modbus_get_reg_10(context, 4); LOGD("trigger flicker %d %dms", brightness, duration_ms); trigger_one_time_flicker(brightness, duration_ms); } else { LOGE("trigger flicker fail,mode != klight_control_mode_flicker"); } }
// REG5
if (modbus_if_register_exists_10(context, 5)) { uint16_t setvalue = modbus_get_reg_10(context, 5);
LOGD("port_set_led_brightness1 %d%%", setvalue); port_set_led_brightness1(65535 * (setvalue / 100.0)); }
// REG6
if (modbus_if_register_exists_10(context, 6)) { uint16_t setvalue = modbus_get_reg_10(context, 6); LOGD("port_set_led_brightness2 %d%%", setvalue); port_set_led_brightness2(65535 * (setvalue / 100.0)); }
modbus_send_10(context, Modbus_OK); } else if (fcode == ModbusOrder03) { //读
if (modbus_if_register_exists_03(context, 0)) { modbus_set_tx_reg_03(context, 0, g_light_brightness); } if (modbus_if_register_exists_03(context, 1)) { modbus_set_tx_reg_03(context, 1, g_light_brightness / 65535.0 * 100); } if (modbus_if_register_exists_03(context, 2)) { modbus_set_tx_reg_03(context, 2, g_light_control_mode); } modbus_send_03(context, Modbus_OK); } }
modbus_processer_t s_modbus_processer = { .modbus_device_id = MODBUS_DEVICE_ID, .modbus_processer_rx_buf = modbus_rx_buf, .modbus_processer_rx_buf_size = sizeof(modbus_rx_buf), .modbus_processer_tx_buf = modbus_tx_buf, .modbus_processer_tx_buf_size = sizeof(modbus_tx_buf), /*************************************/ .modbus_baundrate_one_packet_delay_us = kmodbus_baundrate_one_packet_delay_us, .port_enter_critical = port_enter_critical, .port_exit_critical = port_exit_critical, .port_delay_us = port_delay_us, .tx = order_uart485_tx, /*************************************/ .process_rx = modbux_process_rx, };
/**
* @brief 调试指示灯控制 */ void port_do_debug_light_state() { static uint32_t lastprocess = 0; if (port_haspassedms(lastprocess) > 300) { lastprocess = HAL_GetTick(); HAL_GPIO_TogglePin(DEBUG_LIGHT_PORT, DEBUG_LIGHT_PIN); } }
/**
* @brief 串口数据接收回调 */ void HOOK_ZUART_RxCpltCallback(UART_HandleTypeDef* huart, uint8_t rxdata) { if (huart == &ORDER_485_UART) { //处理接收到485指令
modbus_processer_push_data(rxdata); } }
void user_main() { uart_service_start_all_uart_rx(s_uarts, ARRARY_SIZE(s_uarts)); modbus_processer_init(&s_modbus_processer);
while (true) { port_do_debug_light_state();
modbus_processer_try_process_data(); } }
|