User | Revision | Line number | New contents of line |
daan |
0:01be2d5eaf72
|
1
|
#include "PlayerBase.h"
|
daan |
0:01be2d5eaf72
|
2
|
|
daan |
0:01be2d5eaf72
|
3
|
#include "assert.h"
|
daan |
0:01be2d5eaf72
|
4
|
#include "VectorFormat.h"
|
daan |
0:01be2d5eaf72
|
5
|
|
daan |
0:01be2d5eaf72
|
6
|
//#include <iostream>
|
daan |
0:01be2d5eaf72
|
7
|
|
daan |
0:01be2d5eaf72
|
8
|
olc::PlayerBase::PlayerBase() :
|
daan |
0:01be2d5eaf72
|
9
|
mData(0),
|
daan |
0:01be2d5eaf72
|
10
|
mSize(0),
|
daan |
0:01be2d5eaf72
|
11
|
mPlayHead(0),
|
daan |
0:01be2d5eaf72
|
12
|
mPenDown(false),
|
daan |
0:01be2d5eaf72
|
13
|
mLaserPower(0xFFFF),
|
daan |
0:01be2d5eaf72
|
14
|
mGalvoX(0),
|
daan |
0:01be2d5eaf72
|
15
|
mGalvoY(0),
|
daan |
0:01be2d5eaf72
|
16
|
mDelay(0),
|
daan |
0:01be2d5eaf72
|
17
|
mStepSize(1)
|
daan |
0:01be2d5eaf72
|
18
|
{
|
daan |
0:01be2d5eaf72
|
19
|
}
|
daan |
0:01be2d5eaf72
|
20
|
|
daan |
0:01be2d5eaf72
|
21
|
void olc::PlayerBase::setGalvo(uint16_t x, uint16_t y) {
|
daan |
0:01be2d5eaf72
|
22
|
}
|
daan |
0:01be2d5eaf72
|
23
|
|
daan |
0:01be2d5eaf72
|
24
|
void olc::PlayerBase::setLaserPower(uint16_t power) {
|
daan |
0:01be2d5eaf72
|
25
|
// std::cout << "laser power " << power << std::endl;
|
daan |
0:01be2d5eaf72
|
26
|
}
|
daan |
0:01be2d5eaf72
|
27
|
|
daan |
0:01be2d5eaf72
|
28
|
void olc::PlayerBase::wait(uint16_t ms) {
|
daan |
0:01be2d5eaf72
|
29
|
}
|
daan |
0:01be2d5eaf72
|
30
|
|
daan |
0:01be2d5eaf72
|
31
|
void olc::PlayerBase::setData(uint8_t *aData, int aSize) {
|
daan |
0:01be2d5eaf72
|
32
|
reset();
|
daan |
0:01be2d5eaf72
|
33
|
assert(aSize > 0);
|
daan |
0:01be2d5eaf72
|
34
|
assert(aData != 0);
|
daan |
0:01be2d5eaf72
|
35
|
mSize = aSize;
|
daan |
0:01be2d5eaf72
|
36
|
mData = aData;
|
daan |
0:01be2d5eaf72
|
37
|
}
|
daan |
0:01be2d5eaf72
|
38
|
|
daan |
0:01be2d5eaf72
|
39
|
void olc::PlayerBase::reset() {
|
daan |
0:01be2d5eaf72
|
40
|
mPlayHead = 0;
|
daan |
0:01be2d5eaf72
|
41
|
setLaserPower(0);
|
daan |
0:01be2d5eaf72
|
42
|
}
|
daan |
0:01be2d5eaf72
|
43
|
|
daan |
0:01be2d5eaf72
|
44
|
bool olc::PlayerBase::hasNext() {
|
daan |
0:01be2d5eaf72
|
45
|
// std::cout << "bytes left " << bytesLeft() << std::endl;
|
daan |
0:01be2d5eaf72
|
46
|
if ( bytesLeft() > 0 ) return true;
|
daan |
0:01be2d5eaf72
|
47
|
return false;
|
daan |
0:01be2d5eaf72
|
48
|
}
|
daan |
0:01be2d5eaf72
|
49
|
|
daan |
0:01be2d5eaf72
|
50
|
bool olc::PlayerBase::playOne() {
|
daan |
0:01be2d5eaf72
|
51
|
if ( bytesLeft() == 0 ) return true;
|
daan |
0:01be2d5eaf72
|
52
|
|
daan |
1:578d6bbe9f09
|
53
|
// definition of variables neccesairy due to the switch statement. maybe
|
daan |
1:578d6bbe9f09
|
54
|
// use the if else if construction.
|
daan |
1:578d6bbe9f09
|
55
|
|
daan |
0:01be2d5eaf72
|
56
|
uint16_t x;
|
daan |
0:01be2d5eaf72
|
57
|
uint16_t y;
|
daan |
0:01be2d5eaf72
|
58
|
uint8_t header = get();
|
daan |
1:578d6bbe9f09
|
59
|
|
daan |
1:578d6bbe9f09
|
60
|
// this cannot be a byte because we overflow with 255 packets.
|
daan |
1:578d6bbe9f09
|
61
|
int size;
|
daan |
0:01be2d5eaf72
|
62
|
uint16_t power;
|
daan |
1:578d6bbe9f09
|
63
|
char messageString[256];
|
daan |
1:578d6bbe9f09
|
64
|
char *mesgPtr;
|
daan |
1:578d6bbe9f09
|
65
|
uint8_t messageLength;
|
daan |
1:578d6bbe9f09
|
66
|
uint8_t messageLevel;
|
daan |
1:578d6bbe9f09
|
67
|
uint16_t delay;
|
daan |
1:578d6bbe9f09
|
68
|
uint8_t r,g,b;
|
daan |
0:01be2d5eaf72
|
69
|
|
daan |
0:01be2d5eaf72
|
70
|
switch (header) {
|
daan |
0:01be2d5eaf72
|
71
|
case olc::MOVE_TO:
|
daan |
0:01be2d5eaf72
|
72
|
// if pen was down, make it go up.
|
daan |
0:01be2d5eaf72
|
73
|
if (mPenDown) {
|
daan |
0:01be2d5eaf72
|
74
|
penUp();
|
daan |
0:01be2d5eaf72
|
75
|
}
|
daan |
0:01be2d5eaf72
|
76
|
mGalvoX = decodeU16();
|
daan |
0:01be2d5eaf72
|
77
|
mGalvoY = decodeU16();
|
daan |
0:01be2d5eaf72
|
78
|
setGalvo(mGalvoX,mGalvoY);
|
daan |
0:01be2d5eaf72
|
79
|
//std::cout << "move to [" << mGalvoX << "," << mGalvoY << "]" << std::endl;
|
daan |
0:01be2d5eaf72
|
80
|
break;
|
daan |
1:578d6bbe9f09
|
81
|
|
daan |
0:01be2d5eaf72
|
82
|
case olc::LINE_TO:
|
daan |
0:01be2d5eaf72
|
83
|
// if pen was up, make it go down.
|
daan |
0:01be2d5eaf72
|
84
|
if (!mPenDown) {
|
daan |
0:01be2d5eaf72
|
85
|
penDown();
|
daan |
0:01be2d5eaf72
|
86
|
// setLaserPower(mLaserPower);
|
daan |
0:01be2d5eaf72
|
87
|
// mPenDown = true;
|
daan |
0:01be2d5eaf72
|
88
|
}
|
daan |
0:01be2d5eaf72
|
89
|
x = decodeU16();
|
daan |
0:01be2d5eaf72
|
90
|
y = decodeU16();
|
daan |
0:01be2d5eaf72
|
91
|
rasterLine(mGalvoX, mGalvoY, x, y, mStepSize);
|
daan |
0:01be2d5eaf72
|
92
|
mGalvoX = x;
|
daan |
0:01be2d5eaf72
|
93
|
mGalvoY = y;
|
daan |
0:01be2d5eaf72
|
94
|
// setGalvo(mGalvoX,mGalvoY);
|
daan |
0:01be2d5eaf72
|
95
|
//std::cout << "line to [" << mGalvoX << "," << mGalvoY << "]" << std::endl;
|
daan |
0:01be2d5eaf72
|
96
|
break;
|
daan |
0:01be2d5eaf72
|
97
|
case olc::STEP_DELAY:
|
daan |
0:01be2d5eaf72
|
98
|
mDelay = decodeU16();
|
daan |
0:01be2d5eaf72
|
99
|
break;
|
daan |
0:01be2d5eaf72
|
100
|
case olc::PEN_DOWN:
|
daan |
0:01be2d5eaf72
|
101
|
penDown();
|
daan |
0:01be2d5eaf72
|
102
|
break;
|
daan |
0:01be2d5eaf72
|
103
|
case olc::PEN_UP:
|
daan |
0:01be2d5eaf72
|
104
|
penUp();
|
daan |
0:01be2d5eaf72
|
105
|
break;
|
daan |
1:578d6bbe9f09
|
106
|
case olc::STEP_SIZE:
|
daan |
1:578d6bbe9f09
|
107
|
// std::cout << "step size" << std::endl;
|
daan |
1:578d6bbe9f09
|
108
|
mStepSize = decodeU16();
|
daan |
1:578d6bbe9f09
|
109
|
break;
|
daan |
1:578d6bbe9f09
|
110
|
// sending a message. length is as strlen: number of characters
|
daan |
1:578d6bbe9f09
|
111
|
// preceeding the terminating /0 character
|
daan |
2:ce4c7e5ab241
|
112
|
case olc:: REPORT_BUFFER_IN_SIZE:
|
daan |
2:ce4c7e5ab241
|
113
|
reportBufferInSize();
|
daan |
2:ce4c7e5ab241
|
114
|
break;
|
daan |
0:01be2d5eaf72
|
115
|
case olc::WAIT:
|
daan |
1:578d6bbe9f09
|
116
|
delay = decodeU16();
|
daan |
1:578d6bbe9f09
|
117
|
wait(delay);
|
daan |
1:578d6bbe9f09
|
118
|
break;
|
daan |
1:578d6bbe9f09
|
119
|
case olc::MESSAGE:
|
daan |
1:578d6bbe9f09
|
120
|
messageLevel = get();
|
daan |
1:578d6bbe9f09
|
121
|
messageLength = get();
|
daan |
1:578d6bbe9f09
|
122
|
assert(messageLength < 255);
|
daan |
1:578d6bbe9f09
|
123
|
// now skip x bytes
|
daan |
1:578d6bbe9f09
|
124
|
mesgPtr = &(messageString[0]);
|
daan |
1:578d6bbe9f09
|
125
|
while(1) {
|
daan |
1:578d6bbe9f09
|
126
|
*mesgPtr = get();
|
daan |
1:578d6bbe9f09
|
127
|
if (*mesgPtr == '\0') break;
|
daan |
1:578d6bbe9f09
|
128
|
mesgPtr++;
|
daan |
1:578d6bbe9f09
|
129
|
// check for buffer overflow.
|
daan |
1:578d6bbe9f09
|
130
|
if ( mesgPtr - &(messageString[0]) == 256 ) break;
|
daan |
1:578d6bbe9f09
|
131
|
}
|
daan |
1:578d6bbe9f09
|
132
|
message(messageLevel, messageLength, &(messageString[0]));
|
daan |
1:578d6bbe9f09
|
133
|
break;
|
daan |
1:578d6bbe9f09
|
134
|
|
daan |
1:578d6bbe9f09
|
135
|
case olc::CALIBRATION_SCAN:
|
daan |
1:578d6bbe9f09
|
136
|
calibrationScan();
|
daan |
1:578d6bbe9f09
|
137
|
break;
|
daan |
1:578d6bbe9f09
|
138
|
case olc::POINT:
|
daan |
1:578d6bbe9f09
|
139
|
x = decodeU16();
|
daan |
1:578d6bbe9f09
|
140
|
y = decodeU16();
|
daan |
1:578d6bbe9f09
|
141
|
point(x,y);
|
daan |
1:578d6bbe9f09
|
142
|
break;
|
daan |
2:ce4c7e5ab241
|
143
|
case olc::READ_COLOR:
|
daan |
2:ce4c7e5ab241
|
144
|
readColor();
|
daan |
0:01be2d5eaf72
|
145
|
break;
|
daan |
1:578d6bbe9f09
|
146
|
case olc::AYT:
|
daan |
1:578d6bbe9f09
|
147
|
areYouThere();
|
daan |
1:578d6bbe9f09
|
148
|
break;
|
daan |
1:578d6bbe9f09
|
149
|
case olc::READY:
|
daan |
1:578d6bbe9f09
|
150
|
ready();
|
daan |
1:578d6bbe9f09
|
151
|
break;
|
daan |
1:578d6bbe9f09
|
152
|
|
daan |
1:578d6bbe9f09
|
153
|
case olc::LASER_POWER_RGB:
|
daan |
1:578d6bbe9f09
|
154
|
r = get();
|
daan |
1:578d6bbe9f09
|
155
|
g = get();
|
daan |
1:578d6bbe9f09
|
156
|
b = get();
|
daan |
1:578d6bbe9f09
|
157
|
setLaserPowerRgb(r,g,b);
|
daan |
2:ce4c7e5ab241
|
158
|
break;
|
daan |
1:578d6bbe9f09
|
159
|
case olc::LASER_POWER:
|
daan |
1:578d6bbe9f09
|
160
|
//std::cout << "laser power" << std::endl;
|
daan |
1:578d6bbe9f09
|
161
|
mLaserPower = decodeU16();
|
daan |
1:578d6bbe9f09
|
162
|
//setLaserPower(mLaserPower);
|
daan |
1:578d6bbe9f09
|
163
|
break;
|
daan |
0:01be2d5eaf72
|
164
|
case olc::BITMAPU8:
|
daan |
0:01be2d5eaf72
|
165
|
//std::cout << "bitmapU8" << std::endl;
|
daan |
0:01be2d5eaf72
|
166
|
penDown();
|
daan |
1:578d6bbe9f09
|
167
|
size = (int)get();
|
daan |
1:578d6bbe9f09
|
168
|
for( int i = 0; i < size; i++ ) {
|
daan |
0:01be2d5eaf72
|
169
|
uint16_t delay = ((uint16_t) get() ) << 4;
|
daan |
0:01be2d5eaf72
|
170
|
wait(delay);
|
daan |
0:01be2d5eaf72
|
171
|
// setLaserPower( power );
|
daan |
0:01be2d5eaf72
|
172
|
mGalvoX += mStepSize;
|
daan |
0:01be2d5eaf72
|
173
|
setGalvo(mGalvoX, mGalvoY);
|
daan |
0:01be2d5eaf72
|
174
|
//std::cout << "power " << (int)power << std::endl;
|
daan |
1:578d6bbe9f09
|
175
|
}
|
daan |
0:01be2d5eaf72
|
176
|
break;
|
daan |
0:01be2d5eaf72
|
177
|
default:
|
daan |
0:01be2d5eaf72
|
178
|
//std::cout << "found unknown code " << (int)header << std::endl;
|
daan |
1:578d6bbe9f09
|
179
|
return false;
|
daan |
0:01be2d5eaf72
|
180
|
}
|
daan |
1:578d6bbe9f09
|
181
|
// if (mPlayHead >= mSize) return false;
|
daan |
0:01be2d5eaf72
|
182
|
return true;
|
daan |
0:01be2d5eaf72
|
183
|
}
|
daan |
0:01be2d5eaf72
|
184
|
|
daan |
0:01be2d5eaf72
|
185
|
// a simple bresenheim line integer algorithm
|
daan |
0:01be2d5eaf72
|
186
|
void olc::PlayerBase::rasterLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, int step)
|
daan |
0:01be2d5eaf72
|
187
|
{
|
daan |
0:01be2d5eaf72
|
188
|
|
daan |
0:01be2d5eaf72
|
189
|
int dy = y1 - y0;
|
daan |
0:01be2d5eaf72
|
190
|
int dx = x1 - x0;
|
daan |
0:01be2d5eaf72
|
191
|
int stepx, stepy;
|
daan |
0:01be2d5eaf72
|
192
|
if (dy < 0) { dy = -dy; stepy = -step; } else { stepy = step; }
|
daan |
0:01be2d5eaf72
|
193
|
if (dx < 0) { dx = -dx; stepx = -step; } else { stepx = step; }
|
daan |
0:01be2d5eaf72
|
194
|
dy <<= 2; // dy is now 2*dy
|
daan |
0:01be2d5eaf72
|
195
|
dx <<= 2; // dx is now 2*dx
|
daan |
0:01be2d5eaf72
|
196
|
|
daan |
0:01be2d5eaf72
|
197
|
if ( (dx == 0) && (dy == 0) ) return; // nothing to move.
|
daan |
0:01be2d5eaf72
|
198
|
|
daan |
0:01be2d5eaf72
|
199
|
if (dx > dy) {
|
daan |
0:01be2d5eaf72
|
200
|
int fraction = dy - (dx >> 1); // same as 2*dy - dx
|
daan |
0:01be2d5eaf72
|
201
|
if ( stepx > 0) {
|
daan |
0:01be2d5eaf72
|
202
|
while (x0 < x1) {
|
daan |
0:01be2d5eaf72
|
203
|
if (fraction >= 0) { y0 += stepy; fraction -= dx; }
|
daan |
0:01be2d5eaf72
|
204
|
x0 += stepx; fraction += dy;
|
daan |
0:01be2d5eaf72
|
205
|
setGalvo(x0, y0);
|
daan |
0:01be2d5eaf72
|
206
|
wait(mDelay);
|
daan |
0:01be2d5eaf72
|
207
|
}
|
daan |
0:01be2d5eaf72
|
208
|
} else {
|
daan |
0:01be2d5eaf72
|
209
|
while (x0 > x1) {
|
daan |
0:01be2d5eaf72
|
210
|
if (fraction >= 0) { y0 += stepy; fraction -= dx; }
|
daan |
0:01be2d5eaf72
|
211
|
x0 += stepx; fraction += dy;
|
daan |
0:01be2d5eaf72
|
212
|
setGalvo(x0, y0);
|
daan |
0:01be2d5eaf72
|
213
|
wait(mDelay);
|
daan |
0:01be2d5eaf72
|
214
|
}
|
daan |
0:01be2d5eaf72
|
215
|
}
|
daan |
0:01be2d5eaf72
|
216
|
} else {
|
daan |
0:01be2d5eaf72
|
217
|
int fraction = dx - (dy >> 1);
|
daan |
0:01be2d5eaf72
|
218
|
if ( stepy > 0) {
|
daan |
0:01be2d5eaf72
|
219
|
while (y0 < y1) {
|
daan |
0:01be2d5eaf72
|
220
|
if (fraction >= 0) { x0 += stepx; fraction -= dy; }
|
daan |
0:01be2d5eaf72
|
221
|
y0 += stepy;
|
daan |
0:01be2d5eaf72
|
222
|
fraction += dx;
|
daan |
0:01be2d5eaf72
|
223
|
setGalvo(x0, y0);
|
daan |
0:01be2d5eaf72
|
224
|
wait(mDelay);
|
daan |
0:01be2d5eaf72
|
225
|
}
|
daan |
0:01be2d5eaf72
|
226
|
} else {
|
daan |
0:01be2d5eaf72
|
227
|
while (y0 > y1) {
|
daan |
0:01be2d5eaf72
|
228
|
if (fraction >= 0) { x0 += stepx; fraction -= dy; }
|
daan |
0:01be2d5eaf72
|
229
|
y0 += stepy;
|
daan |
0:01be2d5eaf72
|
230
|
fraction += dx;
|
daan |
0:01be2d5eaf72
|
231
|
setGalvo(x0, y0);
|
daan |
0:01be2d5eaf72
|
232
|
wait(mDelay);
|
daan |
0:01be2d5eaf72
|
233
|
}
|
daan |
0:01be2d5eaf72
|
234
|
}
|
daan |
0:01be2d5eaf72
|
235
|
}
|
daan |
0:01be2d5eaf72
|
236
|
}
|
daan |
0:01be2d5eaf72
|
237
|
|