109 lines
2.8 KiB
C++
109 lines
2.8 KiB
C++
#include "client.h"
|
|
#include "protocol.h"
|
|
#include "uart.h"
|
|
#include "MyLCD.h"
|
|
|
|
struct Client hdlc;
|
|
struct DisplayData wait;
|
|
struct DisplayData recive_data;
|
|
bool wait_for_connect = false;
|
|
|
|
void setup() {
|
|
init_lcd();
|
|
init_hdlc_client(&hdlc, 200);
|
|
char str[] = "wait for connect... ";
|
|
strncpy(wait.topLine, str, sizeof(str));
|
|
wait.value1 = 1.0f;
|
|
wait.value2 = 20.0f;
|
|
wait.value3 = 300.0f;
|
|
UART_init();
|
|
}
|
|
|
|
void loop() {
|
|
if (!wait_for_connect){
|
|
print_lcd(&wait);
|
|
|
|
hdlc_connect(&hdlc);
|
|
uint8_t buffer[10];
|
|
|
|
hdlc_get_raw_frame(&hdlc, buffer, sizeof buffer);
|
|
|
|
UART_send(buffer, sizeof buffer);
|
|
|
|
wait.value2 = 21.0f;
|
|
|
|
bool recive_connect = false;
|
|
|
|
while(!recive_connect){
|
|
print_lcd(&wait);
|
|
|
|
uint8_t buffer_recive[10];
|
|
UART_receive(buffer_recive, sizeof buffer_recive);
|
|
|
|
int err = hdlc_timeout_handler(&hdlc, 1);
|
|
if (err == ERR_FRAME_TIME_OUT){
|
|
hdlc_get_raw_frame(&hdlc, buffer, sizeof buffer);
|
|
UART_send(buffer, sizeof buffer);
|
|
continue;
|
|
}
|
|
|
|
err = hdlc_decode_recived_raw_data(&hdlc, buffer_recive, sizeof buffer_recive, 0, 0);
|
|
if (err < 0){
|
|
if (err == ERR_INVALID_SEQ_NUMBER_FRAME){
|
|
uint8_t buffer_rej[10];
|
|
hdlc_get_raw_frame(&hdlc, buffer_rej, sizeof buffer_rej);
|
|
UART_send(buffer_rej, sizeof buffer_rej);
|
|
return err;
|
|
}
|
|
return err;
|
|
}
|
|
|
|
if (hdlc.state == READY_STATE){
|
|
wait_for_connect = true;
|
|
recive_connect = true;
|
|
}
|
|
}
|
|
} else {
|
|
bool flag_recive = false;
|
|
uint8_t data_recive[64];
|
|
size_t len_data_recive;
|
|
|
|
recive_data.value1 = 4.0f;
|
|
recive_data.value2 = 50.0f;
|
|
recive_data.value3 = 600.0f;
|
|
strncpy(recive_data.topLine, "Wait for data", sizeof(recive_data.topLine) - 1);
|
|
|
|
while (!flag_recive){
|
|
print_lcd(&recive_data);
|
|
|
|
uint8_t buffer_recive_data[72];
|
|
UART_receive(buffer_recive_data, sizeof buffer_recive_data);
|
|
|
|
// int err = hdlc_timeout_handler(&hdlc, 1);
|
|
// if (err == ERR_FRAME_TIME_OUT){
|
|
// hdlc_get_raw_frame(&hdlc, buffer_data, sizeof buffer_data);
|
|
// UART_send(buffer_data, sizeof buffer_data);
|
|
// continue;
|
|
// }
|
|
|
|
int err = hdlc_decode_recived_raw_data(&hdlc, buffer_recive_data, sizeof buffer_recive_data, data_recive, &len_data_recive);
|
|
if (err < 0){
|
|
if (err == ERR_INVALID_SEQ_NUMBER_FRAME){
|
|
uint8_t buffer_rej[10];
|
|
hdlc_get_raw_frame(&hdlc, buffer_rej, sizeof buffer_rej);
|
|
UART_send(buffer_rej, sizeof buffer_rej);
|
|
return err;
|
|
}
|
|
return err;
|
|
}
|
|
|
|
struct message resp;
|
|
protocol_decode(data_recive, len_data_recive, &resp);
|
|
|
|
recive_data.value1 = resp.numbers[0];
|
|
recive_data.value2 = resp.numbers[1];
|
|
recive_data.value3 = resp.numbers[2];
|
|
strncpy(recive_data.topLine, resp.str, sizeof(recive_data.topLine) - 1);
|
|
}
|
|
}
|
|
} |