forked from sysrun/Reflecta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflecta.h
184 lines (139 loc) · 5.44 KB
/
Reflecta.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <Arduino.h>
#include <avr/wdt.h>
#ifndef _REFLECTA_H_
#define _REFLECTA_H_
namespace reflecta {
const uint8_t kFrameSizeMax = 255;
// Frame Ids used by Reflecta Functions. These are reserved values for the
// first byte of the frame data.
enum FunctionId {
PushArray = 0x00,
QueryInterface = 0x01,
SendResponse = 0x02,
SendResponseCount = 0x03,
Version = 0x04,
Reset = 0x7A,
Heartbeat = 0x7B,
Response = 0x7C,
Message = 0x7D,
Warning = 0x7E,
Error = 0x7F
};
enum EventCode {
OutOfSequence = 0x00,
UnexpectedEscape = 0x01,
CrcMismatch = 0x02,
UnexpectedEnd = 0x03,
BufferOverflow = 0x04,
FrameTooSmall = 0x05,
FunctionConflict = 0x06,
FunctionNotFound = 0x07,
ParameterMismatch = 0x08,
StackOverflow = 0x09,
StackUnderflow = 0x0A,
WireNotAvailable = 0x0B
};
void setup(int speed);
void loop();
}; // namespace reflecta
namespace reflectaFrames {
// Function definition for Frame Buffer Allocation function, to be optionally
// implemented by the calling library or application.
typedef uint8_t (*frameBufferAllocationFunction)(uint8_t** frameBuffer);
// Function definition for the Frame Received function.
typedef void (*frameReceivedFunction)(
uint8_t sequence,
uint8_t frameLength,
uint8_t* frame);
// Set the Frame Received Callback
void setFrameReceivedCallback(frameReceivedFunction frameReceived);
// Set the Buffer Allocation Callback
void setBufferAllocationCallback(
frameBufferAllocationFunction frameBufferAllocation);
// Send a two byte frame notifying caller that something improper occured
void sendEvent(reflecta::FunctionId type, uint8_t code);
// Send a string message, generally used for debugging
void sendMessage(const char* message);
// Send a frame of data returning the sequence id
uint8_t sendFrame(uint8_t* frame, uint8_t frameLength);
// Reset the communications protocol (zero the sequence numbers & flush the
// communications buffers)
void reset();
// Setup the communications protocol, to be called in Arduino setup()
void setup(int speed);
// Service the incoming communications data, to be called in Arduino loop()
void loop();
// Millisecond counter for last time a frame was received. Can be used to
// implement a 'deadman switch' when communications with a host PC are lost or
// interrupted.
extern uint32_t lastFrameReceived;
}; // namespace reflectaFrames
namespace reflectaFunctions {
// Bind a function to an interfaceId so it can be called by Reflecta
// Functions. The assigned frame id is returned.
uint8_t bind(const char* interfaceId, void (*function)());
void push(int8_t b);
void push16(int16_t b);
int8_t pop();
int16_t pop16();
// Send a response to a function call
// callerSequence == the sequence number of the frame used to call the
// function used to correlate request/response on the caller side
// parameterLength & parameter byte* of the response data
void sendResponse(uint8_t parameterLength, uint8_t* parameters);
void setFirmwareVersion(const char* version);
// reflectaFunctions setup() to be called in the Arduino setup() method
void setup();
// Execution pointer for Reflecta Functions. To be used by functions that
// change the order of instruction execution in the incoming frame. Note:
// if you are not implementing your own 'scripting language', you shouldn't
// be using this.
extern uint8_t* execution;
// Top of the frame marker to be used when modifying the execution pointer.
// Generally speaking execution should not go beyong frameTop. When
// execution == frameTop, the Reflecta Functions frameReceived execution loop
// stops.
extern uint8_t* frameTop;
}; // namespace reflectaFunctions
namespace reflectaArduinoCore {
// ReflectaFunctions wrappers that receive the function call, parse the
// parameters, and call the matching Arduino libray functions
void pinMode();
void digitalRead();
void digitalWrite();
void analogRead();
void analogWrite();
void pulseIn();
// Bind the Arduino core methods to the ARDU1 interface
void setup();
}; // namespace reflectaArduinoCore
namespace reflectaHeartbeat {
// How HB works:
// Has its own stack for gathering HB data
// Waits for fps timeout
// Calls array of functions asynchronously
// Each function returns true if done (remove, don't call again) or false
// for 'still running'
// When function array is empty, sends contents of hb stack
// Counts number of 'idle loops' when hb has been sent and fps timer hasn't
// expired
// Pushes 'idle loops' to the top of the stack
// Returns the current state of isAlive
bool alive();
// Sets isAlive to true which is used to determine if the host is sending
// a keepAlive signal to the Arduino
void keepAlive();
// Binds the setFrameRate function to QueryInterface
void setup();
// Gathers the HB data and sends it out when ready
void loop();
extern uint8_t* frameTop;
void push(int8_t b);
void push16(int16_t w);
void pushf(float f);
// Bind a function to an interfaceId so it can be called by Reflecta Functions
// The assigned frame id is returned.
void bind(bool (*function)());
void setFrameRate();
}; // namespace reflectaHeartbeat
#endif // _REFLECTA_H_