-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputs.cpp
187 lines (152 loc) · 4.22 KB
/
Outputs.cpp
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
185
186
/*
* Copyright (C) 2014 Lars Marowsky-Bree <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "GPIO_Platform.h"
#include "Outputs.h"
#include "RingBuf.h"
#include "Lowlevel.h"
tOutputs Outputs;
// Putting the arrays into the Patterns array directly would require *v
// to have a static size, which I don't want to avoid wasting memory.
// e.g., since some patterns have 4096 values, I'd waste almost 4k on
// the (admittedly overly simple) flip pattern
static int _p_Flip[] = { 0, 4095 };
static int _p_Increment[4096];
static int _p_Sine[4096];
tPattern Patterns[] = {
{ .name = "flip", .len = 2, .v = _p_Flip },
{ .name = "inc", .len = 4096, .v = _p_Increment },
{ .name = "sine", .len = 4096, .v = _p_Sine },
};
const int PatternCount = sizeof(Patterns)/sizeof(tPattern);
// Some patterns are pre-computed. Fill those in here.
static void pattern_setup() {
int i;
for (i = 0; i < 4096; i++) {
_p_Increment[i] = i;
}
for (i = 0; i < 4096; i++) {
_p_Sine[i] = (sin(2.0 * PI * i / 4095) + 1) * 2047;
}
}
// Called with interrupts disabled only
static void output_reset(int i) {
tOutputEntry *out = &Outputs.out[i];
out->last_step = out->offset;
out->countdown = out->period;
_port_write(out->p, out->v->v[out->last_step]);
}
void output_del(const char k) {
int i;
tOutputEntry *out, *last;
for (i = 0; i < Outputs.entries; i++) {
out = &Outputs.out[i];
if (out->k == k)
break;
}
if (i == Outputs.entries) {
SerialUSB.print("ERROR Unknown output referenced: ");
SerialUSB.println(k);
return;
}
last = &Outputs.out[Outputs.entries-1];
noInterrupts();
if (last != out)
memcpy(out, last, sizeof(tOutputEntry));
memset(last, 0, sizeof(tOutputEntry));
Outputs.entries--;
master_period();
interrupts();
}
void outputs_reset(void) {
int i;
for (i = 0; i < Outputs.entries; i++) {
output_reset(i);
}
}
void outputs_setup(void) {
memset(&Outputs, 0, sizeof(tOutputs));
pattern_setup();
}
void output_add(const char k, char *portname, const int period, const int step, const int offset, const int mode, const char *name) {
int i;
tOutputEntry *out;
for (i = 0; i < Outputs.entries; i++) {
if (out->k == k) {
SerialUSB.println("ERROR Output key already in use.");
return;
}
}
for (i = 0; i <= PatternCount; i++) {
if (strcmp(Patterns[i].name, name) == 0)
break;
}
if (i > PatternCount) {
SerialUSB.print("ERROR Unknown pattern referenced: ");
SerialUSB.println(name);
return;
}
if (Outputs.entries == OUTPUT_SIZE) {
SerialUSB.println("ERROR Too many output patterns requested");
return;
}
out = &Outputs.out[Outputs.entries];
out->k = k;
out->p = port_lookup(portname);
if (out->p < 1 || !PortList[out->p].wfunc) {
SerialUSB.println("ERROR Invalid port for output");
return;
}
out->period = period;
out->step = step;
out->offset = offset;
out->mode = mode;
out->v = &Patterns[i];
noInterrupts();
output_reset(Outputs.entries);
Outputs.entries++;
master_period();
interrupts();
}
void outputs_push(void) {
int i;
unsigned long t;
t = micros();
for (i = 0; i < Outputs.entries; i++) {
tOutputEntry *out = &Outputs.out[i];
out->countdown -= Master.period;
if (out->countdown <= 0) {
int step = out->last_step + out->step;
out->countdown = out->period;
if (step >= out->v->len) {
if (!out->mode) {
step = 0;
} else {
out->step = -out->step;
step = out->v->len-1;
}
} else if (step < 0) {
step = 0;
if (out->mode) {
out->step = -out->step;
}
}
out->last_step = step;
_port_write(out->p, out->v->v[step]);
}
}
}