-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
495 lines (415 loc) · 16.5 KB
/
main.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/**
Generated Main Source File
Company:
Microchip Technology Inc.
File Name:
main.c
Summary:
This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs
Description:
This header file provides implementations for driver APIs for all modules selected in the GUI.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.8
Device : PIC16LF1829
Driver Version : 2.00
*/
/*
(c) 2018 Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software and any
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
FOR A PARTICULAR PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/examples/i2c1_master_example.h"
#define KXTJ3_I2C_ADDR 0x0E
#define KXTJ3_WHO_AM_I 0x0F
#define X 0
#define Y 1
#define Z 2
char detectMovementDirection(int8_t axis[], int8_t prev_axis[]) {
//If there is more movement in Y axis (tilt), detect Up or Down tilt
if ((abs(axis[Y] - prev_axis[Y])) > (abs(axis[X] - prev_axis[X]))) {
if ((axis[Y] > prev_axis[Y]) && (abs(axis[Y] - prev_axis[Y]) > 10)) {
return 'U';
}
if ((axis[Y] < prev_axis[Y]) && (abs(axis[Y] - prev_axis[Y]) > 10)) {
return 'D';
}
}
//If there is more movement in X axis (roll), detect Left or Right roll
else if ((abs(axis[Y] - prev_axis[Y])) < (abs(axis[X] - prev_axis[X]))) {
if ((axis[X] > prev_axis[X]) && (axis[Z] < prev_axis[Z]) && (abs(axis[X] - prev_axis[X]) > 25)) {
return 'L';
}
if ((axis[X] < prev_axis[X]) && (axis[Z] < prev_axis[Z]) && (abs(axis[X] - prev_axis[X]) > 25)) {
return 'R';
}
}
//If there is not enough movement, fill sequence with empty "X"
return 'X';
}
void lumos() {
printf("\n\r LUMOS \n\r");
//Turn on yellowish light
EPWM1_LoadDutyValue(15); //B
EPWM2_LoadDutyValue(160); //R
PWM4_LoadDutyValue(160); //G
}
void nox() {
printf("\n\r NOX \n\r");
//Turn off lumos light
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(0); //G
}
void dark_magic_enter() {
printf("\n\r DARK MAGIC STATE \n\r");
//Turn on red glow
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(10); //R
PWM4_LoadDutyValue(0); //G
}
void expecto_patronum() {
printf("\n\r EXPECTO PATRONUM \n\r");
DELAY_milliseconds(1000);
//Turn on blue glow
EPWM1_LoadDutyValue(20); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(200);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(200);
EPWM1_LoadDutyValue(80); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(200);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(200);
EPWM1_LoadDutyValue(160); //B
EPWM2_LoadDutyValue(10); //R
PWM4_LoadDutyValue(10); //G
DELAY_milliseconds(4000);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(0); //G
}
void avada_kedavra() {
printf("\n\r AVADA KEDAVRA \n\r");
//Turn on green glow
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(20); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(40); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(60); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(80); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(0); //R
PWM4_LoadDutyValue(160); //G
DELAY_milliseconds(2500);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(10); //R
PWM4_LoadDutyValue(0); //G
}
void crucio() {
printf("\n\r CRUCIO \n\r");
//Turn on green glow
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(50); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(100); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(20); //B
EPWM2_LoadDutyValue(50); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(50); //B
EPWM2_LoadDutyValue(100); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(300);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(160); //R
PWM4_LoadDutyValue(0); //G
DELAY_milliseconds(2500);
EPWM1_LoadDutyValue(0); //B
EPWM2_LoadDutyValue(10); //R
PWM4_LoadDutyValue(0); //G
}
/*
Main application
*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
/* **************** INTERRUPT WORKFLOW **************** */
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
/* **************** INTERRUPT WORKFLOW **************** */
printf("\n\r Starting ... \n\r");
//RESET LEDS
EPWM1_LoadDutyValue(0); //BLUE
EPWM2_LoadDutyValue(0); //RED
PWM4_LoadDutyValue(0); //GREEN
printf("\n\r 1 ... \n\r");
/*EPWM2_LoadDutyValue(100); //RED
DELAY_milliseconds(1000);
EPWM2_LoadDutyValue(0); //RED
PWM4_LoadDutyValue(100); //GREEN
DELAY_milliseconds(1000);
PWM4_LoadDutyValue(0); //GREEN
EPWM1_LoadDutyValue(100); //BLUE
DELAY_milliseconds(1000);
EPWM1_LoadDutyValue(0); //BLUE */
/*uint8_t initialization_data;
data = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, KXTJ3_WHO_AM_I);
if(data == 0x35)
PWM4_LoadDutyValue(160); //green
else
EPWM2_LoadDutyValue(160); //red
DELAY_milliseconds(1000);*/
printf("\n\r 2 ... \n\r");
//Initialize accelerometer
I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1D, 0b10000000); // reset acceletometer
DELAY_milliseconds(1000); //wait until the reset sequence is finished
printf("\n\r 3 ... \n\r");
//Set registers for what we need to test
I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1E, 0b00101000);
I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1F, 0b10111111);
I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1B, 0b00000000);
//I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1B, 0b00000010);
I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1B, 0b10000010);
//I2C1_Write1ByteRegister(KXTJ3_I2C_ADDR, 0x1B, 0b10000000);
//initialization_data = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x1B); //read this register to clear interrupts
//RESET LEDS
EPWM1_LoadDutyValue(0); //BLUE
EPWM2_LoadDutyValue(0); //RED
PWM4_LoadDutyValue(0); //GREEN
printf("\n\r 4 ... \n\r");
int8_t discrete_counter = 0; //Spell time window counter
int8_t axis[3];
int8_t prev_axis[3];
prev_axis[X] = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x07);
prev_axis[Y] = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x09);
prev_axis[Z] = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x0B);
//Spell movements buffer - 0 index = newest, 3 index = oldest
char spell_movements[4] = {'N', 'N', 'N', 'N'};
char casting_state; //Current state of state machine
if (prev_axis[Z] <= -50) {
//If the wand is upside down, enter debug mode
casting_state = 'D';
EPWM2_LoadDutyValue(100); //RED ON
DELAY_milliseconds(1000); //1s
EPWM2_LoadDutyValue(0); //RED OFF
PWM4_LoadDutyValue(100); //GREEN ON
DELAY_milliseconds(1000); //1s
PWM4_LoadDutyValue(0); //GREEN OFF
EPWM1_LoadDutyValue(100); //BLUE ON
DELAY_milliseconds(1000); //1s
EPWM1_LoadDutyValue(0); //BLUE OFF
}
else {
//If the wand is in normal orientation, enter spell mode
casting_state = 'S';
}
printf("\n\r Running ... \n\r");
while (1)
{
if (discrete_counter == 0) { //If discrete time window is active
//Get accelerometer axis
axis[X] = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x07);
axis[Y] = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x09);
axis[Z] = I2C1_Read1ByteRegister(KXTJ3_I2C_ADDR, 0x0B);
//printf("%d %d %d \n\r", axis[X], axis[Y], axis[Z]);
//Shift spell movements buffer, Add new movement to buffer
spell_movements[3] = spell_movements[2];
spell_movements[2] = spell_movements[1];
spell_movements[1] = spell_movements[0];
spell_movements[0] = detectMovementDirection(axis, prev_axis);
}
//Register current axis data as previous data for next step
prev_axis[X] = axis[X];
prev_axis[Y] = axis[Y];
prev_axis[Z] = axis[Z];
//Debug spell movements
printf("%c %c %c %c \n\r", spell_movements[0], spell_movements[1], spell_movements[2], spell_movements[3]);
//START STATE MACHINE
if (casting_state == 'D') {
//DEBUG STATE, NO SUCCESSOR NODES
//Change RGB color based on wand orientation
if (axis[X] > 0) {
EPWM1_LoadDutyValue(axis[X]);
}
else {
EPWM1_LoadDutyValue(axis[X] * (-1));
}
if (axis[Y] > 0) {
EPWM2_LoadDutyValue(axis[Y]);
}
else {
EPWM2_LoadDutyValue(axis[Y] * (-1));
}
if (axis[Z] > 0) {
PWM4_LoadDutyValue(axis[Z]);
}
else {
PWM4_LoadDutyValue(axis[Z] * (-1));
}
}
if (casting_state == 'S') {
//STARTING POSITION
//Detect dark magic state change
if ((axis[Z] <= -60) &&
(spell_movements[0] == 'X') && (spell_movements[1] == 'X') && (spell_movements[2] == 'X') && (spell_movements[3] == 'X')) {
//Change state to dark magic starting state
dark_magic_enter();
casting_state = 'M';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
//Detect lumos casting
else if ((((spell_movements[0] == 'U') || (spell_movements[1] == 'U')) && ((spell_movements[2] == 'D') || (spell_movements[3] == 'D')))
&& (axis[Y] <= 35)) {
//Cast lumos and enter lumos state
lumos();
casting_state = 'L';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
//Detect expecto patronum casting
else if ((((spell_movements[0] == 'X') && (spell_movements[1] == 'X')) && ((spell_movements[2] == 'X') && (spell_movements[3] == 'X')))
&& (axis[Y] >= 62)) {
//Cast expecto_patronum, stay in white magic state
expecto_patronum();
casting_state = 'S';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
}
else if (casting_state == 'L') {
//Wait for nox condition inside lumos state
if ((((spell_movements[0] == 'L') || (spell_movements[1] == 'L')) &&
((spell_movements[2] == 'R') || (spell_movements[3] == 'R'))) &&
(abs(axis[X]) >= 40)) {
nox();
casting_state = 'S';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
}
else if (casting_state == 'M') {
//Wait for dark spell or white magic condition
//Detect white magic state change
if ((axis[Z] >= 60) &&
(spell_movements[0] == 'X') && (spell_movements[1] == 'X') && (spell_movements[2] == 'X') && (spell_movements[3] == 'X')) {
//Change state to white magic starting state, use nox to reset LED
nox();
casting_state = 'S';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
//Detect avada kedavra
else if ((((spell_movements[0] == 'L') || (spell_movements[1] == 'L')) && ((spell_movements[2] == 'D') || (spell_movements[3] == 'D')))
&& (axis[Y] <= 35)) {
//Cast avada kedavra
avada_kedavra();
casting_state = 'M';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
//Detect dark magic transition state
else if ((((spell_movements[0] == 'X') && (spell_movements[1] == 'X')) && ((spell_movements[2] == 'X') && (spell_movements[3] == 'X')))
&& (axis[Y] <= -60)) {
casting_state = 'T';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
}
else if (casting_state == 'T') {
//Wait for dark spell
//Detect crucio
if ((axis[Y] >= -5) &&
(spell_movements[0] == 'X') && (spell_movements[1] == 'X') && (spell_movements[2] == 'X') && (spell_movements[3] == 'X')) {
//Change state to white magic starting state, use nox to reset LED
crucio();
casting_state = 'M';
//Reset current spell movements buffer
spell_movements[0] = 'N';
spell_movements[1] = 'N';
spell_movements[2] = 'N';
spell_movements[3] = 'N';
}
}
discrete_counter++;
//printf("%d \n\r n\nr", discrete_counter);
if (discrete_counter >= 8) {
//printf("%d \n\r n\nr", i);
/*spell_movements[0] = 'X';
spell_movements[1] = 'X';
spell_movements[2] = 'X';
spell_movements[3] = 'X';*/
discrete_counter = 0;
}
}
}
/**
End of File
*/