-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReflectaArduinoCore.cpp
More file actions
139 lines (112 loc) · 2.76 KB
/
ReflectaArduinoCore.cpp
File metadata and controls
139 lines (112 loc) · 2.76 KB
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
/*
ReflectaArduinoCore.cpp - Library for exposing the core Arduino library functions over Reflecta
*/
#include "Reflecta.h"
using namespace reflecta;
using namespace reflectaFunctions;
namespace reflectaArduinoCore
{
void pinMode()
{
::pinMode(pop(), pop());
}
void digitalRead()
{
push(::digitalRead(pop()));
}
void digitalWrite()
{
::digitalWrite(pop(), pop());
}
void analogRead()
{
push16(::analogRead(pop()));
}
void analogWrite()
{
::analogWrite(pop(), pop());
}
void wireBeginMaster()
{
Wire.begin();
}
void wireRequestFrom()
{
Wire.requestFrom(pop(), pop());
}
void wireRequestFromStart()
{
Wire.requestFrom(pop(), pop(), false);
}
void wireAvailable()
{
push(Wire.available());
}
void wireRead()
{
if (Wire.available())
push(Wire.read());
else
reflectaFrames::sendEvent(Error, WireNotAvailable);
}
void wireBeginTransmission()
{
Wire.beginTransmission(pop());
}
// TODO: Support variants write(string) and write(data, length)
void wireWrite()
{
Wire.write(pop());
}
void wireEndTransmission()
{
Wire.endTransmission();
}
Servo servos[MAX_SERVOS];
// TODO: Support variant attach(pin, min, max)
void servoAttach()
{
int8_t pin = pop();
servos[pin].attach(pin);
}
void servoDetach()
{
servos[pop()].detach();
}
void servoWrite()
{
servos[pop()].write(pop());
}
void servoWriteMicroseconds()
{
servos[pop()].writeMicroseconds(pop16());
}
// TODO: Support variant pulseIn(pin, value, timeout)
void pulseIn()
{
// BUGBUG: Broken, returns a 32 bit result
push(::pulseIn(pop(), pop()));
}
// Bind the Arduino core methods to the ardu1 interface
void setup()
{
reflectaFunctions::bind("ardu1", pinMode);
reflectaFunctions::bind("ardu1", digitalRead);
reflectaFunctions::bind("ardu1", digitalWrite);
reflectaFunctions::bind("ardu1", analogRead);
reflectaFunctions::bind("ardu1", analogWrite);
reflectaFunctions::bind("ardu1", wireBeginMaster);
reflectaFunctions::bind("ardu1", wireRequestFrom);
reflectaFunctions::bind("ardu1", wireRequestFromStart);
reflectaFunctions::bind("ardu1", wireAvailable);
reflectaFunctions::bind("ardu1", wireRead);
reflectaFunctions::bind("ardu1", wireBeginTransmission);
reflectaFunctions::bind("ardu1", wireWrite);
reflectaFunctions::bind("ardu1", wireEndTransmission);
reflectaFunctions::bind("ardu1", servoAttach);
reflectaFunctions::bind("ardu1", servoDetach);
reflectaFunctions::bind("ardu1", servoWrite);
reflectaFunctions::bind("ardu1", servoWriteMicroseconds);
reflectaFunctions::bind("ardu1", pulseIn);
}
};