259 lines
7.9 KiB
C
259 lines
7.9 KiB
C
#include "uart_lcd.h"
|
||
#include "usart.h"
|
||
#include "lwip.h"
|
||
#include "tim.h"
|
||
extern ip4_addr_t ipaddr;
|
||
extern float current_buff[2];
|
||
uart_lcd_t uart_lcd_state = {0};
|
||
ec11_t ec11_data = {0};
|
||
|
||
static void uart_lcd_current_channel1(void);
|
||
static void uart_lcd_current_channel2(void);
|
||
|
||
static void uart_lcd_page1(void);
|
||
static void uart_lcd_page2(void);
|
||
|
||
uint8_t uart_lcd_data1[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)
|
||
{
|
||
dma_usart_send(&huart4, uart_lcd_data1, ARRAY_LEN(uart_lcd_data1));
|
||
}
|
||
|
||
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接口。
|
||
*/
|
||
uint8_t ip_addr[24] = {0};
|
||
void uart_lcd_draw_ipaddr(void)
|
||
{
|
||
|
||
// 串口发送的固定值为串口屏指令指定的帧头帧尾
|
||
ip_addr[0] = 0xEE; // 帧头
|
||
ip_addr[1] = 0xB1;
|
||
ip_addr[2] = 0x10; // 命令码,此处为文本控件文本设置指令
|
||
ip_addr[3] = 0x00; // 画面ID高位地址
|
||
ip_addr[4] = 0x01; // 画面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;
|
||
// uart_lcd_page2();
|
||
// HAL_Delay(100);
|
||
uart_lcd_state.page_num = 1;
|
||
dma_usart_send(&huart4, ip_addr, ARRAY_LEN(ip_addr));
|
||
// HAL_Delay(100);
|
||
}
|
||
|
||
void uart_lcd_init(void)
|
||
{
|
||
uart_lcd_state.page_num = 1;
|
||
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);
|
||
}
|
||
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 (uart_lcd_state.page_num == 1)
|
||
{
|
||
return;
|
||
}
|
||
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));
|
||
}
|
||
// HAL_Delay(100);
|
||
}
|
||
|
||
void uart_lcd_channel_switch(uint8_t channel)
|
||
{
|
||
if (uart_lcd_state.page_num == 1)
|
||
{
|
||
return;
|
||
}
|
||
switch (channel)
|
||
{
|
||
case 0:
|
||
uart_lcd_current_channel1();
|
||
break;
|
||
case 1:
|
||
uart_lcd_current_channel2();
|
||
break;
|
||
default:
|
||
uart_lcd_current_channel1();
|
||
break;
|
||
}
|
||
// HAL_Delay(100);
|
||
}
|
||
|
||
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_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;
|
||
}
|
||
}
|
||
|
||
void uart_lcd_ecll_control_current_out(void)
|
||
{
|
||
if (ec11_data.confirm_key_flag == 0)
|
||
{
|
||
}
|
||
else if (ec11_data.confirm_key_flag == 1) // 确认按键确认电流输出
|
||
{
|
||
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) // 切换通道按键确认状<E8AEA4>??
|
||
{
|
||
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);
|
||
}
|
||
}
|