-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReflecta.h
181 lines (138 loc) · 5.24 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
#include <Arduino.h>
#include <Wire.h>
#include <Servo.h>
#ifndef REFLECTA_H
#define REFLECTA_H
namespace reflecta
{
// 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,
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 reflectaFrames
{
// Function definition for Frame Buffer Allocation function, to be optionally implemented by
// the calling library or application.
typedef byte (*frameBufferAllocationFunction)(byte** frameBuffer);
// Function definition for the Frame Received function.
typedef void (*frameReceivedFunction)(byte sequence, byte frameLength, byte* 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, byte code);
// Send a string message, generally used for debugging
void sendMessage(String message);
// Send a frame of data returning the sequence id
byte sendFrame(byte* frame, byte 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 reflectaFunctions
{
// Bind a function to an interfaceId so it can be called by Reflecta Functions. The assigned frame id is returned.
byte bind(String 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(byte parameterLength, byte* parameters);
// 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 byte* 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 byte* frameTop;
};
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 wireBeginMaster();
void wireRequestFrom();
void wireRequestFromStart();
void wireAvailable();
void wireRead();
void wireBeginTransmission();
void wireWrite();
void wireEndTransmission();
void servoAttach();
void servoDetach();
void servoWrite();
void servoWriteMicroseconds();
void pulseIn();
// Bind the Arduino core methods to the ARDU1 interface
void setup();
};
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
// Binds the setFrameRate function to QueryInterface
void setup();
// Gathers the HB data and sends it out when ready
void loop();
extern byte* 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();
};
#endif