/
comms.c
142 lines (109 loc) · 2.48 KB
/
comms.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Standard Library Includes
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
/*
* AVR Includes (Defines and Primitives)
*/
#include <avr/io.h>
/*
* AVR Library Includes
*/
#include "lib_uart.h"
/*
* Local Application Includes
*/
#include "comms.h"
/*
* Protocol Library Includes
*/
#include "llap.h"
/*
* Utility Library Includes
*/
#include "util_memory_placement.h"
/*
* Local Application Includes
*/
#include "latrinesensor.h"
/*
* Private Variables
*/
static char txrxBuffer[13]; // Use same buffer for transmit and receive
static uint8_t rxIndex;
static LLAP_DEVICE llapDevice;
static char * deviceName = "PitSensor";
static char * deviceType = "U00000001";
static char * fwVersion = "4.0";
static char * serialNum = "PROTO01";
/*
* Private Function Prototypes
*/
static void uartCheck(void);
static void llapGenericHandler(LLAP_GENERIC_MSG_ENUM eMsgType, const char * genericStr, const char * msgBody);
static void llapApplicationHandler(const char * msgBody);
static void llapSendRequest(const char * msgBody);
/*
* Public Function Defintions
*/
void COMMS_Init(void)
{
rxIndex = 0;
UART_Init(UART0, 4800, 14, 14, false);
// Setup the LLAP device
llapDevice.id[0] = '-';
llapDevice.id[1] = '-';
llapDevice.devName = deviceName;
llapDevice.devType = deviceType;
llapDevice.fwVer = fwVersion;
llapDevice.serNum = serialNum;
llapDevice.msgBuffer = txrxBuffer;
// Link LLAP object to local handler functions
llapDevice.genericMsgHandler = llapGenericHandler;
llapDevice.applicationMsgHandler = llapApplicationHandler;
llapDevice.sendRequest = llapSendRequest;
LLAP_StartDevice(&llapDevice);
}
void COMMS_Send(char * s)
{
LLAP_SendOutgoingMessage(&llapDevice, s);
}
void COMMS_Check(void)
{
uartCheck();
}
static void uartCheck(void)
{
bool rx = false;
bool tx = false;
UART0_Task(&rx, &tx);
if (rx)
{
txrxBuffer[rxIndex] = UART_GetChar(UART0, NULL);
if (strlen(txrxBuffer) == LLAP_MESSAGE_LENGTH)
{
LLAP_HandleIncomingMessage(&llapDevice, txrxBuffer);
rxIndex = 0;
}
}
}
static void llapGenericHandler(LLAP_GENERIC_MSG_ENUM eMsgType, const char * genericStr, const char * msgBody)
{
(void)eMsgType;
(void)genericStr;
(void)msgBody;
}
static void llapApplicationHandler(const char * msgBody)
{
if ((msgBody[0] == 'T') && (msgBody[1] == 'H'))
{
APP_HandleNewThresholdSetting(&msgBody[2]);
}
}
static void llapSendRequest(const char * msgBody)
{
UART_PutStr(UART0, (uint8_t*)msgBody);
}