197 lines
5.2 KiB
C
197 lines
5.2 KiB
C
#include "commons.h"
|
|
#include "proto.h"
|
|
|
|
#include "mcc_generated_files/system/system.h"
|
|
|
|
static uint8_t in_index;
|
|
static bool is_started = false;
|
|
static bool led_set = false;
|
|
static bool sensor_set = false;
|
|
static bool error_set = false;
|
|
static bool next_in_should_be_end = false;
|
|
|
|
extern bool tx_timed_out;
|
|
|
|
|
|
//==============================================================================
|
|
// Private Declarations
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
// Public
|
|
//==============================================================================
|
|
/*=****************************************************************************/
|
|
bool byte_in (uint8_t in, uint8_t* p_cmd, uint16_t* p_led1, uint16_t* p_led2)
|
|
{
|
|
if (!is_started)
|
|
{
|
|
if (in == START_DEL)
|
|
{
|
|
// Start at 1 since this 'in' is now useless
|
|
in_index = 1;
|
|
is_started = true;
|
|
next_in_should_be_end = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (ID_INDEX == in_index)
|
|
{
|
|
// Don't bother with messages not meant for us
|
|
if (MY_ID != in)
|
|
{
|
|
is_started = false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// If should be done
|
|
if (next_in_should_be_end)
|
|
{
|
|
is_started = false;
|
|
return in == END_DEL;
|
|
}
|
|
|
|
// Save command for when finished
|
|
if (CMD_INDEX == in_index)
|
|
{
|
|
// Invalid inputs for SLAVE
|
|
if (ERROR_CMD_ID == in ||
|
|
DATA_CMD_ID == in ||
|
|
MODEL_CMD_ID == in)
|
|
{
|
|
is_started = false;
|
|
return false;
|
|
}
|
|
*p_cmd = in;
|
|
if (GET_CMD_ID == in || GET_MODEL_CMD_ID == in)
|
|
next_in_should_be_end = true;
|
|
}
|
|
else if (in_index > CMD_INDEX)
|
|
{
|
|
switch (*p_cmd)
|
|
{
|
|
case GET_MODEL_CMD_ID:
|
|
case GET_CMD_ID:
|
|
// Do nothing
|
|
break;
|
|
case SET_CMD_ID:
|
|
// LED1
|
|
if (LED1_INDEX == in_index)
|
|
*p_led1 = in << 8;
|
|
else if (LED1_INDEX + 1 == in_index)
|
|
*p_led1 |= in;
|
|
// LED2
|
|
else if (LED2_INDEX == in_index)
|
|
*p_led2 = in << 8;
|
|
else if (LED2_INDEX + 1 == in_index)
|
|
{
|
|
*p_led2 |= in;
|
|
next_in_should_be_end = true;
|
|
led_set = true;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
is_started = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
++in_index;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
errors_t send_model (uint8_t peer_id, uint8_t model)
|
|
{
|
|
SET_WRITE();
|
|
|
|
errors_t ret = uart_tx_byte(START_DEL);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(peer_id);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(MODEL_CMD_ID);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(model);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(END_DEL);
|
|
|
|
SET_READ();
|
|
return ret;
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
errors_t send_data (uint8_t peer_id, uint16_t moisture, uint16_t light)
|
|
{
|
|
SET_WRITE();
|
|
|
|
errors_t ret = uart_tx_byte(START_DEL);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(peer_id);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(DATA_CMD_ID);
|
|
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte((moisture >> 8) & 0xFF);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(moisture & 0xFF);
|
|
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte((light >> 8) & 0xFF);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(light & 0xFF);
|
|
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(END_DEL);
|
|
|
|
SET_READ();
|
|
return ret;
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
errors_t send_error (uint8_t peer_id, errors_t err)
|
|
{
|
|
SET_WRITE();
|
|
|
|
errors_t ret = uart_tx_byte(START_DEL);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(peer_id);
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(ERROR_CMD_ID);
|
|
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(err);
|
|
|
|
if (RET_SUCCESS(ret)) ret = uart_tx_byte(END_DEL);
|
|
|
|
SET_READ();
|
|
return ret;
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
errors_t uart_rx_byte (uint8_t* p_byte)
|
|
{
|
|
SET_READ();
|
|
|
|
if(UART1.IsRxReady())
|
|
*p_byte = UART1.Read();
|
|
else
|
|
return NO_RX;
|
|
|
|
return SUCCESS;
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
errors_t uart_tx_byte (uint8_t byte)
|
|
{
|
|
tx_timed_out = false;
|
|
TMR_TX_Write(0);
|
|
TMR_TX_Start();
|
|
while (!UART1.IsTxReady() && !tx_timed_out)
|
|
;
|
|
TMR_TX_Stop();
|
|
if (tx_timed_out)
|
|
return TX_READY_ERR;
|
|
|
|
UART1.Write(byte);
|
|
|
|
tx_timed_out = false;
|
|
TMR_TX_Write(0);
|
|
TMR_TX_Start();
|
|
while (!UART1.IsTxDone() && !tx_timed_out)
|
|
;
|
|
TMR_TX_Stop();
|
|
if (tx_timed_out)
|
|
return TX_DONE_ERR;
|
|
|
|
return SUCCESS;
|
|
} |