-
Notifications
You must be signed in to change notification settings - Fork 0
/
mate.c
315 lines (280 loc) · 9.52 KB
/
mate.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <avr/io.h>
#include <stdlib.h>
#include <math.h>
#include <util/delay.h>
#include <stdint.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include "mate.h"
#include "config.h"
#include "lpd8806.h"
#include "demo_rgb.h"
#include "demo_blink_left.h"
#include "demo_blink_right.h"
#include "demo_motor.h"
typedef uint8_t(*demoDelegate)(void);
typedef struct {
demoDelegate init;
demoDelegate tick;
uint8_t parameter[4];
uint8_t active;
} demo_t;
demo_t demos[] = {
{0, demo_rgb_tick, {0,0,0,0,}, 0},
{demo_blink_left_init, demo_blink_left_tick, {10,0,0,0,}, 0},
{demo_blink_right_init, demo_blink_right_tick, {10,0,0,0,}, 0},
{0, demo_motor_tick, {0,0,0,0,}, 1},
};
volatile uint16_t rcInput, rcPot, rcMot0, rcMot1;
volatile uint8_t rcSet;
uint8_t boehseTasetn[] = {
0x44, 0x49, 0x4f, 0x55,
0x5b, 0x60, 0x66, 0x6d,
0x74, 0x7c, 0xff, };
uint8_t *parameter;
uint8_t demo,
choosenParameter,
updatingParameter,
lastRcBarPoti,
dbg,
remoteMode,
lastChannel;
uint16_t rcFoo = 0xffff,
rcBar,
rcBarPoti,
rcBaz,
rcBuz,
rcBarPotiMin = 0xffff,
rcBarPotiMax,
rcLast = 0;
// 9 0 MENUMODE CONFIGMODE choose DEMO
//
// 8 1 p0 p2 choose parameter
//
// 7 2 p1 p3 choose parameter
//
// 6 3 p++ p-- modify parameter w/o poti
//
// 5 4 dbg resetScale debug last set value (binary output)
volatile uint32_t sun;
ISR(TIMER2_COMPA_vect) {
++sun;
}
ISR(PCINT1_vect) {
if(PINC & (1<<PC0)) {
rcLast = TCNT1;
lastChannel = 0;
} else if(PINC & (1<<PC1)) {
rcLast = TCNT1;
lastChannel = 1;
} else if(PINC & (1<<PC2)) {
rcLast = TCNT1;
lastChannel = 2;
} else if(PINC & (1<<PC3)) {
rcLast = TCNT1;
lastChannel = 3;
} else {
uint16_t time = TCNT1;
if(time<rcLast) {
//fix it
time = (PWM_PERIOD - rcLast) + time;
} else {
time = time - rcLast;
}
if(lastChannel == 0) {
rcInput = time;
} else if(lastChannel == 1) {
rcPot = time;
} else if(lastChannel == 2) {
rcMot0 = time;
} else if(lastChannel == 3) {
rcMot1 = time;
rcSet = 1;
}
}
}
#define MOTOR0 OCR1A
#define MOTOR1 OCR1B
// MAX REV 0x32
// STOP 0xBA
//
void setPwmOut(uint8_t chan, uint8_t value) {
cli();
if (chan == 0) {
MOTOR0 = 1964L + ((uint16_t)value<<3);
} else {
MOTOR1 = 1964L + ((uint16_t)value<<3);
}
sei();
}
void rc_io_init(void) {
////// OUT //////
OCR1A = 2000;
OCR1B = 2000;
ICR1 = PWM_PERIOD;
TCCR1B |= (1<<CS11) | (1<<WGM13) | (1<<WGM12); //prescaler 1024 and some of the pwmmodebits (mode 14 fast pwm)
TCCR1A |= (1<<WGM11) | (1<<COM1A1) | (1<<COM1B1); // pwmmodebit , waveformbits
//TCCR1B = 2; //set timer to CLK/8
DDRB |= (1<<PB2) | (1<<PB1);// configure PB0-PB2 as outputs
////// IN //////
DDRC &= ~(1<<PC0 | 1<<PC1 | 1<<PC2 | 1<<PC3); //DDR PWM (IN)
PCICR = 2; //PCINT 8...14 on
PCMSK1 = 0x0f; //PCINT 8...11 enable
}
void led_init(void) {
DDRB |= (1<<DATA) | (1<<CLOCK); //DDR DATA CLOCK (OUT)
}
void sun_init(void) {
TCCR2A = (1<<WGM21); //CTC (at least we guess so...)
TCCR2B = (1<<CS22) | (1<<CS21) | (1<<CS20); //prescale 1k
OCR2A = 156;
TIMSK2 |= (1<<OCIE2A);
}
int main(void) {
rc_io_init(); //rc I&O pwm
led_init(); //set ddrb for lpd8806...
sun_init(); //init 2nd timer (demo-tixx)
sei();
while(1) {
static uint8_t oldRcFoo;
static uint8_t sameCount;
if(rcSet) {
rcSet = 0;
cli(); rcFoo = rcInput >> 5; sei();
cli(); rcBarPoti = rcPot; sei();
cli(); rcBaz = rcMot0; sei();
cli(); rcBuz = rcMot1; sei();
//remember, remember...
if(rcBarPotiMin>rcBarPoti)
rcBarPotiMin = rcBarPoti;
if(rcBarPotiMax<rcBarPoti)
rcBarPotiMax = rcBarPoti;
//autoscale rcBarPoti 0...255 (calibrate vs historical max/min)
rcBarPoti = 0xffL*(rcBarPoti-rcBarPotiMin)/(rcBarPotiMax-rcBarPotiMin);
//dirty kewl hack - do not attempt to understand
//this huge dogpile of shit - you won't get it anyway...
for(uint8_t i=0;i<(sizeof(boehseTasetn)+1);i++)
if(rcFoo <= boehseTasetn[i]) {
rcFoo = i;
break;
}
--rcFoo;
//</dirtyhack>
//rcFoo holds the id of the pressed key (0...9)
//or $FFFF if no key is pressed
if(rcFoo != oldRcFoo) {
oldRcFoo = rcFoo;
rcFoo = 0xFFFF;
sameCount = 0;
} else {
sameCount++;
if(sameCount < 5) {
rcFoo = 0xFFFF;
} else if (sameCount == 5 || ((sameCount%10) == 0) || sameCount > 200) {
if(sameCount > 250)
sameCount = 200;
switch(rcFoo) {
case 9:
remoteMode = 0;
break;
case 0:
remoteMode = 1;
break;
}
if(remoteMode) {
switch(rcFoo) {
case 1:
choosenParameter = 2;
updatingParameter = 0;
break;
case 2:
choosenParameter = 3;
updatingParameter = 0;
break;
case 3:
if(rcBar == 0)
rcBar = 255;
else
--rcBar;
updatingParameter = 2;
break;
case 4:
aMin = uMin = rcBarPotiMin = 0xffff;
aMax = uMax = rcBarPotiMax = 0; //see demo_motor.c
break;
case 5:
dbg ^= 1;
break;
case 6:
if(++rcBar > 255)
rcBar = 0;
updatingParameter = 2;
break;
case 7:
choosenParameter = 1;
updatingParameter = 0;
break;
case 8:
choosenParameter = 0;
updatingParameter = 0;
break;
}
} else {
if(rcFoo >= (sizeof(demos) / sizeof(demo_t)) && rcFoo > 0) {
updatingParameter = 0;
demo = 8-rcFoo;
demos[demo].active ^= 1;
if(demos[demo].active)
if(demos[demo].init)
demos[demo].init();
}
}
} else {
rcFoo = 0xFFFF;
}
}
if (updatingParameter) {
demos[demo].parameter[choosenParameter] =
updatingParameter == 2
? rcBar
: (lastRcBarPoti = rcBarPoti);
} else if((updatingParameter == 0)
&& (abs((int16_t)rcBarPoti - lastRcBarPoti) > UPDATE_DELTA)) {
updatingParameter = 1;
}
}
for(uint8_t i=0;i<LEDS*3;i++)
state[i] = 0;
for(uint8_t i=0;i<(sizeof(demos) / sizeof(demo_t));i++) {
if(demos[i].active) {
parameter = demos[i].parameter;
demos[i].active = demos[i].tick();
}
}
if(dbg) {
uint8_t mask = 128;
uint8_t param = parameter[choosenParameter];
for(uint8_t i=0;i<8;i++) {
//GGGG GGGG RRRR RRRR BBBB BBBB
state[i*3+0] = (param & mask) ? 255 : 0;
mask >>= 1;
state[i*3+1] = 0;
state[i*3+2] = 0;
}
mask = 128;
for(uint8_t i=0;i<8;i++) {
//GGGG GGGG RRRR RRRR BBBB BBBB
state[(8+i)*3+1] = 0;
state[(8+i)*3+2] = 0;
state[(8+i)*3+0] = (rcBaz & mask) ? 255 : 0;
mask >>= 1;
}
for(uint8_t i=0;i<sizeof(demos)/sizeof(demo_t);i++) {
state[(16+i)*3+0] = 0;
state[(16+i)*3+1] = demos[i].active ? 255 : 0;
state[(16+i)*3+2] = 0;
}
}
updateState();
}
}