signal_generator/User/driver/uart_lcd.c

289 lines
10 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "uart_lcd.h"
#include "usart.h"
#include "lwip.h"
#include "tim.h"
extern ip4_addr_t ipaddr;
uart_lcd_t uart_lcd_state = {0};
lcd_makecurve_t lcd_makecurve_data = {0};
extern float current_buff[2];
uint8_t uart_lcd_data[24] = {0xEE, 0xB1, 0x12, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
static void uart_lcd_current_channel1(void);
static void uart_lcd_current_channel2(void);
static void uart_lcd_current_channel1(void)
{
dma_usart_send(&huart4, uart_lcd_data, ARRAY_LEN(uart_lcd_data));
}
uint8_t uart_lcd_data2[24] = {0xEE, 0xB1, 0x12, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x01, 0x01, 0xFF, 0xFC, 0xFF, 0xFF};
static void uart_lcd_current_channel2(void)
{
dma_usart_send(&huart4, uart_lcd_data2, ARRAY_LEN(uart_lcd_data2));
}
uint8_t uart_lcd_data3[9] = {0xEE, 0xB1, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
static void uart_lcd_page1(void)
{
uart_lcd_state.page_num = 0;
dma_usart_send(&huart4, uart_lcd_data3, ARRAY_LEN(uart_lcd_data3));
}
uint8_t uart_lcd_data4[9] = {0xEE, 0xB1, 0x00, 0x00, 0x01, 0xFF, 0xFC, 0xFF, 0xFF};
static void uart_lcd_page2(void)
{
uart_lcd_state.page_num = 1;
dma_usart_send(&huart4, uart_lcd_data4, ARRAY_LEN(uart_lcd_data4));
}
/**
* @brief 通过UART向LCD屏幕绘制IP地址
*
* 此函数通过UART接口向LCD屏幕发送IP地址以便在屏幕上显示。
*
* @note 此函数假定已经正确初始化了UART接口并且LCD屏幕已经正确连接到UART接口。
*/
void uart_lcd_draw_ipaddr(void)
{
uint8_t ip_addr[24] = {0};
// 串口发送的固定值为串口屏指令指定的帧头帧尾
ip_addr[0] = 0xEE; // 帧头
ip_addr[1] = 0xB1;
ip_addr[2] = 0x10; // 命令码,此处为文本控件文本设置指令
ip_addr[3] = 0x00; // 画面ID高位地址
ip_addr[4] = 0x00; // 画面ID低位地址
ip_addr[5] = 0x00; // 控件ID高位地址
ip_addr[6] = 0x03; // 控件ID低位地址
char *ip_addr_str = ipaddr_ntoa(&ipaddr);
MEMCPY(ip_addr + 7, (uint8_t *)ip_addr_str, strlen(ip_addr_str));
ip_addr[7 + strlen(ip_addr_str)] = 0xFF; // 帧尾,下列都为帧尾
ip_addr[8 + strlen(ip_addr_str)] = 0xFC;
ip_addr[9 + strlen(ip_addr_str)] = 0xFF;
ip_addr[10 + strlen(ip_addr_str)] = 0xFF;
dma_usart_send(&huart4, ip_addr, ARRAY_LEN(ip_addr));
}
void uart_lcd_init(void)
{
uart_lcd_state.page_num = 0;
uart_lcd_page_switch(uart_lcd_state.page_num); // 切换到第一个页面
uart_lcd_state.key_state[0] = 0;
uart_lcd_state.key_state[1] = 1;
uart_lcd_state.key_state[2] = 0;
uart_lcd_state.current_value[0] = 0;
uart_lcd_state.current_value[1] = 0;
uart_lcd_channel_switch(uart_lcd_state.key_state[0]);
HAL_Delay(10);
uart_lcd_current_out(0);
HAL_Delay(10);
uart_lcd_current_out(1);
lcd_makecurve_data.cmd_head = 0xEE; // 帧头
lcd_makecurve_data.cmd_type = 0xB1; // 命令类型
lcd_makecurve_data.cmd_ctrl_type = 0x32; // 写曲线数据
lcd_makecurve_data.cmd_page_id = 0x01; // 画面ID
lcd_makecurve_data.cmd_ctrl_id = 0x0E; // 控件ID
lcd_makecurve_data.cmd_curve_channel = 0x00; // 曲线通道
lcd_makecurve_data.cmd_tail = 0xFFFCFFFF; // 帧尾
}
uint8_t lcd_channel[15] = {0xEE, 0xB1, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
void uart_lcd_current_out(uint8_t channel)
{
if (channel == 0)
{
lcd_channel[6] = 0x04;
lcd_channel[10] = uart_lcd_state.current_value[0];
dma_usart_send(&huart4, lcd_channel, ARRAY_LEN(lcd_channel));
}
else if (channel == 1)
{
lcd_channel[6] = 0x05;
lcd_channel[10] = uart_lcd_state.current_value[1];
dma_usart_send(&huart4, lcd_channel, ARRAY_LEN(lcd_channel));
}
}
void uart_lcd_channel_switch(uint8_t channel)
{
switch (channel)
{
case 0:
uart_lcd_current_channel1();
break;
case 1:
uart_lcd_current_channel2();
break;
default:
uart_lcd_current_channel1();
break;
}
}
void uart_lcd_page_switch(uint8_t page)
{
switch (page)
{
case 0:
uart_lcd_page1();
break;
case 1:
uart_lcd_page2();
break;
default:
uart_lcd_page1();
break;
}
// HAL_Delay(100);
}
void uart_lcd_ecll_control_current_out(void)
{
if (ec11_data.confirm_key_flag == 0)
{
}
else if (ec11_data.confirm_key_flag == 1) // Confirm button - confirms current output
{
ec11_data.confirm_key_flag_last++;
if (ec11_data.confirm_key_flag_last > 2)
{
current_buff[0] = (float)uart_lcd_state.current_value[0];
current_buff[1] = (float)uart_lcd_state.current_value[1];
ec11_data.confirm_key_flag_last = 0;
ec11_data.confirm_key_flag = 0;
}
}
else if (ec11_data.confirm_key_flag == 2) // Switch the current output channel
{
ec11_data.confirm_key_flag_last++;
if (ec11_data.confirm_key_flag_last > 2)
{
if (uart_lcd_state.key_state[0] == 1)
{
uart_lcd_state.key_state[0] = 0;
uart_lcd_state.key_state[1] = 1;
uart_lcd_state.key_state[2] = 0;
}
else if (uart_lcd_state.key_state[0] == 0)
{
uart_lcd_state.key_state[0] = 1;
uart_lcd_state.key_state[1] = 0;
uart_lcd_state.key_state[2] = 1;
}
ec11_data.confirm_key_flag_last = 0;
ec11_data.confirm_key_flag = 0;
uart_lcd_channel_switch(uart_lcd_state.key_state[0]);
}
}
else if (ec11_data.confirm_key_flag == 3) // 切换界面
{
if (uart_lcd_state.page_num == 0)
{
uart_lcd_state.page_num = 1;
}
else if (uart_lcd_state.page_num == 1)
{
uart_lcd_state.page_num = 0;
}
ec11_data.confirm_key_flag_last = 0;
ec11_data.confirm_key_flag = 0;
uart_lcd_page_switch(uart_lcd_state.page_num);
}
}
void uart_lcd_ec11_control_current(void)
{
if (uart_lcd_state.page_num == 0) // 当前页面为电流调节页面时才对EC11编码器进行计数处理
{
ec11_data.direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim1);
ec11_data.encode_num = (short)__HAL_TIM_GET_COUNTER(&htim1);
if ((ec11_data.direction == 0) && (ec11_data.encode_num > ec11_data.encode_num_last))
{
if (uart_lcd_state.key_state[0] == 0)
{
uart_lcd_state.current_value[0] += 1;
if (uart_lcd_state.current_value[0] > 20)
{
uart_lcd_state.current_value[0] = 20;
}
}
else if (uart_lcd_state.key_state[0] == 1)
{
uart_lcd_state.current_value[1] += 1;
if (uart_lcd_state.current_value[1] > 20)
{
uart_lcd_state.current_value[1] = 20;
}
}
uart_lcd_current_out(uart_lcd_state.key_state[0]);
}
if ((ec11_data.direction == 1) && (ec11_data.encode_num < ec11_data.encode_num_last))
{
if (uart_lcd_state.key_state[0] == 0)
{
if (uart_lcd_state.current_value[0] < 2)
{
uart_lcd_state.current_value[0] = 0;
}
else
{
uart_lcd_state.current_value[0] -= 1;
}
}
else if (uart_lcd_state.key_state[0] == 1)
{
if (uart_lcd_state.current_value[1] < 2)
{
uart_lcd_state.current_value[1] = 0;
}
else
{
uart_lcd_state.current_value[1] -= 1;
}
}
uart_lcd_current_out(uart_lcd_state.key_state[0]);
}
ec11_data.encode_num_last = ec11_data.encode_num;
}
}
uint8_t uart_makecurve_data[128] = {0};
void uart_lcd_makecurvet(lcd_makecurve_t *makecurve_data)
{
uart_makecurve_data[0] = makecurve_data->cmd_head; // 帧头
uart_makecurve_data[1] = makecurve_data->cmd_type; // 命令类型
uart_makecurve_data[2] = makecurve_data->cmd_ctrl_type; // 控制类型
uart_makecurve_data[3] = (makecurve_data->cmd_page_id >> 8) & 0xFF; // 画面ID高位地址
uart_makecurve_data[4] = makecurve_data->cmd_page_id & 0xFF; // 画面ID低位地址
uart_makecurve_data[5] = (makecurve_data->cmd_ctrl_id >> 8) & 0xFF; // 控件ID高位地址
uart_makecurve_data[6] = makecurve_data->cmd_ctrl_id & 0xFF; // 控件ID低位地址
uart_makecurve_data[7] = makecurve_data->cmd_curve_channel; // 曲线通道
uart_makecurve_data[8] = (makecurve_data->cmd_data_len >> 8) & 0xFF; // 数据长度高位地址
uart_makecurve_data[9] = makecurve_data->cmd_data_len & 0xFF; // 数据长度低位地址
MEMCPY(uart_makecurve_data + 10, makecurve_data->cmd_data, makecurve_data->cmd_data_len); // 数据内容
uart_makecurve_data[10 + makecurve_data->cmd_data_len] = (makecurve_data->cmd_tail >> 24) & 0xFF; // 帧尾
uart_makecurve_data[11 + makecurve_data->cmd_data_len] = (makecurve_data->cmd_tail >> 16) & 0xFF; // 帧尾
uart_makecurve_data[12 + makecurve_data->cmd_data_len] = (makecurve_data->cmd_tail >> 8) & 0xFF; // 帧尾
uart_makecurve_data[13 + makecurve_data->cmd_data_len] = makecurve_data->cmd_tail & 0xFF; // 帧尾
dma_usart_send(&huart4, uart_makecurve_data, makecurve_data->cmd_data_len + 14);
}
void uart_lcd_makecurve_test(void)
{
lcd_makecurve_data.cmd_data_len = 1; // 假设数据长度为1
static uint8_t test_num = 0;
// if (uart_lcd_state.page_num == 1)
{
test_num++;
if (test_num > 100)
{
test_num = 0; // 重置计数器
}
lcd_makecurve_data.cmd_data[0] = test_num / 10 * 10; // 填充数据内容
uart_lcd_makecurvet(&lcd_makecurve_data); // 调用函数发送数据
}
}