forked from aferodeveloper/afLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafLib.h
150 lines (101 loc) · 4.08 KB
/
afLib.h
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
142
143
144
145
146
147
148
149
150
/**
* Copyright 2015 Afero, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AFLIB_H__
#define AFLIB_H__
#include "iafLib.h"
#include "SPI.h"
#include "Command.h"
#include "StatusCommand.h"
#include "afSPI.h"
#define STATE_IDLE 0
#define STATE_STATUS_SYNC 1
#define STATE_STATUS_ACK 3
#define STATE_SEND_BYTES 4
#define STATE_RECV_BYTES 5
#define STATE_CMD_COMPLETE 6
#define SPI_FRAME_LEN 16
#define REQUEST_QUEUE_SIZE 10
typedef struct {
uint8_t messageType;
uint16_t attrId;
uint8_t requestId;
uint16_t valueLen;
uint8_t *p_value;
} request_t;
class afLib : public iafLib {
public:
afLib(const int mcuInterrupt, isr isrWrapper,
onAttributeSet attrSet, onAttributeSetComplete attrSetComplete, Stream *serial, afSPI *theSPI);
virtual void loop(void);
virtual int getAttribute(const uint16_t attrId);
virtual int setAttributeBool(const uint16_t attrId, const bool value);
virtual int setAttribute8(const uint16_t attrId, const int8_t value);
virtual int setAttribute16(const uint16_t attrId, const int16_t value);
virtual int setAttribute32(const uint16_t attrId, const int32_t value);
virtual int setAttribute64(const uint16_t attrId, const int64_t value);
virtual int setAttribute(const uint16_t attrId, const String &value);
virtual int setAttribute(const uint16_t attrId, const uint16_t valueLen, const char *value);
virtual int setAttribute(const uint16_t attrId, const uint16_t valueLen, const uint8_t *value);
virtual int setAttributeComplete(uint8_t requestId, const uint16_t attrId, const uint16_t valueLen, const uint8_t *value);
virtual bool isIdle();
virtual void mcuISR();
private:
Stream *_theLog;
afSPI *_theSPI;
//SPISettings _spiSettings;
volatile int _interrupts_pending;
int _state;
uint16_t _bytesToSend;
uint16_t _bytesToRecv;
uint8_t _requestId;
uint16_t _outstandingSetGetAttrId;
// Application Callbacks.
onAttributeSet _onAttrSet;
onAttributeSetComplete _onAttrSetComplete;
Command *_writeCmd;
uint16_t _writeBufferLen;
uint8_t *_writeBuffer;
Command *_readCmd;
uint16_t _readBufferLen;
uint8_t *_readBuffer;
int _writeCmdOffset;
int _readCmdOffset;
StatusCommand *_txStatus;
StatusCommand *_rxStatus;
request_t _request;
request_t _requestQueue[REQUEST_QUEUE_SIZE];
int parseCommand(const char *cmd);
void sendCommand(void);
void checkInterrupt(void);
void printState(int state);
//void beginSPI();
//void endSPI();
int exchangeStatus(StatusCommand *tx, StatusCommand *rx);
bool inSync(StatusCommand *tx, StatusCommand *rx);
int writeStatus(StatusCommand *c);
void sendBytes();
void recvBytes();
void onIdle();
void dumpBytes(char *label, int len, uint8_t *bytes);
void updateIntsPending(int amount);
void queueInit(void);
int queuePut(uint8_t messageType, uint8_t requestId, const uint16_t attributeId, uint16_t valueLen, const uint8_t *value);
int queueGet(uint8_t *messageType, uint8_t *requestId, uint16_t *attributeId, uint16_t *valueLen, uint8_t **value);
int doGetAttribute(uint8_t requestId, uint16_t attrId);
int doSetAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value);
int doUpdateAttribute(uint8_t requestId, uint16_t attrId, uint8_t status, uint16_t valueLen, uint8_t *value);
};
#endif // AFLIB_H__