1 |
// File: Main.h |
---|
2 |
//------------------------------------------------------------------------------ |
---|
3 |
// Author: Giovanni de Sanctis |
---|
4 |
// Email: info@lateral-technologies.com |
---|
5 |
//------------------------------------------------------------------------------ |
---|
6 |
// Date: Dec 2018 |
---|
7 |
//------------------------------------------------------------------------------ |
---|
8 |
|
---|
9 |
#include "mcc_generated_files/mcc.h" |
---|
10 |
#include "Power.h" |
---|
11 |
#include "JDY08.h" |
---|
12 |
#include "Moves.h" |
---|
13 |
#include "Terminal.h" |
---|
14 |
#include "RF.h" |
---|
15 |
|
---|
16 |
#define T_PWR (5*60*50) //5 min * 60 sec * 1/20ms |
---|
17 |
#define N_RND_MOVES 9 //Number of random moves taken from the list |
---|
18 |
//~30*(9-1) = 4 minutes total duration. |
---|
19 |
|
---|
20 |
void main(void) |
---|
21 |
{ |
---|
22 |
uint8_t isRFver; |
---|
23 |
uint8_t movCntA=0; |
---|
24 |
uint8_t movCntB=0; |
---|
25 |
uint8_t movCntC=0; |
---|
26 |
uint8_t movCntD=0; |
---|
27 |
|
---|
28 |
uint8_t movCntRnd = 0; |
---|
29 |
uint8_t movRndPtr = 0; |
---|
30 |
|
---|
31 |
uint8_t ledCntA=0; |
---|
32 |
uint8_t ledCntB=0; |
---|
33 |
uint8_t ledCntC=0; |
---|
34 |
|
---|
35 |
AckEnum lastMoveAck=ACK_WAITING; |
---|
36 |
AckEnum lastLedsAck=ACK_WAITING; |
---|
37 |
|
---|
38 |
uint16_t tPwr = T_PWR; //Power off timeout if no commands received |
---|
39 |
uint16_t tRnd = 0; //Timeout for random moves |
---|
40 |
|
---|
41 |
|
---|
42 |
//Initialise the device |
---|
43 |
SYSTEM_Initialize(); |
---|
44 |
|
---|
45 |
powerBootstrap(); //Keeps power enable high after button is released |
---|
46 |
|
---|
47 |
movesInit(); |
---|
48 |
|
---|
49 |
//Check if an RF receiver is connected |
---|
50 |
RC4_SetDigitalInput(); |
---|
51 |
//Unlock peripheral select |
---|
52 |
PPSLOCK=0x55; //Magic sequence |
---|
53 |
PPSLOCK=0xAA; |
---|
54 |
PPSLOCK=0x00; //Reset PPSLOCKED bit |
---|
55 |
|
---|
56 |
RC4PPS = 0x00; //RC4-> I/O |
---|
57 |
//Re-lock peripheral select |
---|
58 |
PPSLOCK=0x55; //Magic sequence |
---|
59 |
PPSLOCK=0xAA; |
---|
60 |
PPSLOCK=0x01; //Set PPSLOCKED bit |
---|
61 |
|
---|
62 |
uint8_t i0=0; |
---|
63 |
uint8_t i1=0; |
---|
64 |
__delay_ms(500); |
---|
65 |
for (uint8_t i=0; i<250; i++) |
---|
66 |
{ |
---|
67 |
if (RC3_GetValue()) i1++; |
---|
68 |
else i0++; |
---|
69 |
__delay_us(1000); |
---|
70 |
} |
---|
71 |
|
---|
72 |
//If noise was coming from the RF input then RF receiver is connected |
---|
73 |
isRFver = (uint8_t)((i0>2) && (i1>2)); |
---|
74 |
|
---|
75 |
//if (isRFver) |
---|
76 |
#ifdef IS_RF_VER |
---|
77 |
{ |
---|
78 |
initRF(); //Inits the RF module |
---|
79 |
LED_RED_On(); //Blink Red LED twice to inform RF mode is active |
---|
80 |
__delay_ms(100); |
---|
81 |
LED_RED_Off(); |
---|
82 |
__delay_ms(100); |
---|
83 |
LED_RED_On(); |
---|
84 |
__delay_ms(100); |
---|
85 |
LED_RED_Off(); |
---|
86 |
|
---|
87 |
setBluePattern(LED_BLUE_CNNCTD); |
---|
88 |
|
---|
89 |
// Enable the Global Interrupts |
---|
90 |
INTERRUPT_GlobalInterruptEnable(); |
---|
91 |
// Enable the Peripheral Interrupts |
---|
92 |
INTERRUPT_PeripheralInterruptEnable(); |
---|
93 |
|
---|
94 |
|
---|
95 |
while(1) |
---|
96 |
{ |
---|
97 |
uint8_t rfCode = rfGetNewCode(); |
---|
98 |
switch (rfCode) |
---|
99 |
{ |
---|
100 |
case RF_A: |
---|
101 |
if (lastMoveAck==ACK_RUNNING) break; |
---|
102 |
setMove(moveGrpSlow[movCntA++]); |
---|
103 |
if (movCntA>=MAX_GRP_SLOW) movCntA=0; |
---|
104 |
break; |
---|
105 |
case RF_B: |
---|
106 |
if (lastMoveAck==ACK_RUNNING) break; |
---|
107 |
setMove(moveGrpFast[movCntB++]); |
---|
108 |
if (movCntB>=MAX_GRP_FAST) movCntB=0; |
---|
109 |
break; |
---|
110 |
case RF_C: |
---|
111 |
if (lastMoveAck==ACK_RUNNING) break; |
---|
112 |
setMove(moveGrpTrem[movCntC++]); |
---|
113 |
if (movCntC>=MAX_GRP_TREM) movCntC=0; |
---|
114 |
break; |
---|
115 |
case RF_D: |
---|
116 |
if (lastMoveAck==ACK_RUNNING) break; |
---|
117 |
setMove(moveGrpErec[movCntD++]); |
---|
118 |
if (movCntD>=MAX_GRP_EREC) movCntD=0; |
---|
119 |
break; |
---|
120 |
case RF_LONG_A: |
---|
121 |
//If any other pattern was running then turn off (first pattern of grp A) |
---|
122 |
//if (ACK_RUNNING == lastLedsAck) |
---|
123 |
setLEDs(ledsGrpA[ledCntA++]); |
---|
124 |
if (ledCntA>=MAX_LED_GRPA) ledCntA=0; |
---|
125 |
break; |
---|
126 |
case RF_LONG_B: |
---|
127 |
ledCntA = 1; |
---|
128 |
setLEDs(ledsGrpB[ledCntB++]); |
---|
129 |
if (ledCntB>=MAX_LED_GRPB) ledCntB=0; |
---|
130 |
break; |
---|
131 |
case RF_LONG_C: |
---|
132 |
ledCntA = 1; |
---|
133 |
setLEDs(ledsGrpC[ledCntC++]); |
---|
134 |
if (ledCntC>=MAX_LED_GRPC) ledCntC=0; |
---|
135 |
break; |
---|
136 |
case RF_LONG_D: //Random moves from first 2 groups |
---|
137 |
if (movCntRnd) movCntRnd = 0; |
---|
138 |
else movCntRnd = N_RND_MOVES; |
---|
139 |
tRnd = 0; |
---|
140 |
break; |
---|
141 |
} |
---|
142 |
wait20ms(); |
---|
143 |
lastMoveAck=updateMove(); |
---|
144 |
lastLedsAck=updateLEDs(); |
---|
145 |
|
---|
146 |
if ((lastMoveAck == ACK_WAITING) && (movCntRnd)) |
---|
147 |
{ |
---|
148 |
if (tRnd) tRnd--; |
---|
149 |
else |
---|
150 |
{ |
---|
151 |
tRnd = (uint16_t)moveRndGrpT[movRndPtr] * 50; |
---|
152 |
setMove(moveRndGrp[movRndPtr]); |
---|
153 |
if (movRndPtr<MAX_GRP_RAND) movRndPtr++; |
---|
154 |
else movRndPtr=0; |
---|
155 |
movCntRnd--; |
---|
156 |
} |
---|
157 |
} |
---|
158 |
|
---|
159 |
//If a code was received then reset the timeout, otherwise shut down if timeout=0 |
---|
160 |
if (movCntRnd == 0) tPwr--; //Decrement timeout only if not running random moves |
---|
161 |
if (rfCode) tPwr = T_PWR; |
---|
162 |
else if (tPwr == 0) shutDown(); |
---|
163 |
checkPowerButton(); |
---|
164 |
|
---|
165 |
if (isRFreceiving()) |
---|
166 |
{ |
---|
167 |
//setBluePattern(LED_BLUE_RFRX); |
---|
168 |
//__delay_ms(100); |
---|
169 |
setBluePattern(LED_BLUE_CNNCTD); |
---|
170 |
} |
---|
171 |
|
---|
172 |
} |
---|
173 |
} |
---|
174 |
#else |
---|
175 |
// { |
---|
176 |
//Initialise the device |
---|
177 |
//SYSTEM_Initialize(); |
---|
178 |
RC4_SetDigitalOutput(); |
---|
179 |
|
---|
180 |
//Unlock peripheral select |
---|
181 |
PPSLOCK=0x55; //Magic sequence |
---|
182 |
PPSLOCK=0xAA; |
---|
183 |
PPSLOCK=0x00; //Reset PPSLOCKED bit |
---|
184 |
|
---|
185 |
RC4PPS = 0x09; //RC4->EUSART:TX; |
---|
186 |
|
---|
187 |
//Re-lock peripheral select |
---|
188 |
PPSLOCK=0x55; //Magic sequence |
---|
189 |
PPSLOCK=0xAA; |
---|
190 |
PPSLOCK=0x01; //Set PPSLOCKED bit |
---|
191 |
|
---|
192 |
TMR1_StopTimer(); //Timer 1 not used in BT mode |
---|
193 |
|
---|
194 |
// Enable the Global Interrupts |
---|
195 |
INTERRUPT_GlobalInterruptEnable(); |
---|
196 |
// Enable the Peripheral Interrupts |
---|
197 |
INTERRUPT_PeripheralInterruptEnable(); |
---|
198 |
|
---|
199 |
initJDY08(); //Inits the bluetooth module |
---|
200 |
|
---|
201 |
//setAuto(1,5,240,2); |
---|
202 |
|
---|
203 |
while (1) |
---|
204 |
{ |
---|
205 |
AckEnum ack; |
---|
206 |
//Update move and leds (if running) and send a message at the end |
---|
207 |
ack = updateMove(); |
---|
208 |
ack |= updateLEDs(); |
---|
209 |
|
---|
210 |
terminal(ack); |
---|
211 |
|
---|
212 |
checkPowerButton(); //Read power button to power down the system |
---|
213 |
|
---|
214 |
wait20ms(); |
---|
215 |
} |
---|
216 |
} |
---|
217 |
//} |
---|
218 |
#endif |
---|