24
24
#include " analogWave.h"
25
25
#include " scheduler.h"
26
26
27
-
27
+ /* ########################################################################## */
28
28
/* __________________________ CONFIGURATION DEFINES _________________________ */
29
+ /* ########################################################################## */
29
30
#define USE_CHAT_AT
30
31
#define USE_TIMER
31
32
#define USE_DEBUG_CODE
32
33
33
-
34
+ /* ########################################################################## */
34
35
/* _____________________________ OTHERS DEFINES _____________________________ */
35
-
36
+ /* ########################################################################## */
36
37
#define ANRES 9
37
38
#define RES 100
38
39
#define SERIAL_AT Serial
39
40
41
+ /* ########################################################################## */
42
+ /* ____________________________GLOBAL VARIABLES _____________________________ */
43
+ /* ########################################################################## */
44
+
45
+
46
+
47
+ // Firmware version
48
+ uint8_t version[2 ]={0 ,6 };
49
+
50
+ char command=0 ;
51
+ byte data[6 ];
52
+
53
+ uint16_t wave[RES];
54
+
55
+ LedGauge frequency_1 (FQ_1, 10 , 10 , ROW_A_1, ROW_A_2, ROW_A_3, COL_A_1, COL_A_2, COL_A_3);
56
+ LedGauge frequency_2 (FQ_2, 10 , 10 , ROW_B_1, ROW_B_2, ROW_B_3, COL_B_1, COL_B_2, COL_B_3, true );
57
+
58
+ LedRange range (FQ_R, ROW_C_1, ROW_C_2, ROW_C_3, COL_C_1, COL_C_2);
59
+
60
+ analogWave wave1 (DAC,wave,RES,0 );
61
+ analogWave wave2 (DAC2,wave,RES,0 );
62
+
63
+ int previous_frequency_1 = 10 ;
64
+ int previous_frequency_2 = 10 ;
65
+ int freq1 = 10 ;
66
+ int freq2 = 10 ;
67
+
68
+ int previous_phase_1 = 0 ;
69
+ int previous_phase_2 = 0 ;
70
+
71
+ int phase1 = 0 ;
72
+ int phase2 = 0 ;
73
+
74
+ const float step = ADC_RES/float (RES);
75
+ const float phase_to_deg = 180 /float (RES);
76
+
77
+ bool test_flag = false ;
78
+ unsigned long time_rephase;
79
+ int rephase;
80
+
81
+ /* ########################################################################## */
82
+ /* ________CHAT AT___________________________________________________________ */
83
+ /* ########################################################################## */
84
+
85
+ #ifdef USE_CHAT_AT
86
+
87
+ #include " chATWrapper.h"
88
+ static CAtWrapper at (&SERIAL_AT);
89
+
90
+
91
+ #endif // USE_CHAT_AT
92
+
93
+ /* ########################################################################## */
40
94
/* ________TIMER_____________________________________________________________ */
95
+ /* ########################################################################## */
41
96
42
97
#ifdef USE_TIMER
43
98
#include " FspTimer.h"
46
101
static FspTimer timer;
47
102
static bool elapsed = false ;
48
103
104
+ /* -------------------------------------------------------------------------- */
105
+ void task10ms () {
106
+ /* -------------------------------------------------------------------------- */
107
+ frequency_1.refresh ();
108
+ frequency_2.refresh ();
109
+ range.update ();
110
+
111
+ freq1=frequency_1.getGaugeValue ()*range.getRange1 ();
112
+ freq2=frequency_2.getGaugeValue ()*range.getRange2 ();
113
+ }
114
+
115
+ /* -------------------------------------------------------------------------- */
116
+ void task15ms () {
117
+ /* -------------------------------------------------------------------------- */
118
+ phase1=analogRead (PH_1)/step;
119
+ if (phase1>(RES+10 )){
120
+ phase1=0 ;
121
+ }
122
+
123
+ phase2=analogRead (PH_2)/step;
124
+ if (phase2>RES){
125
+ phase2=0 ;
126
+ }
127
+
128
+ if (freq1!=previous_frequency_1){
129
+ wave1.freq (freq1);
130
+ previous_frequency_1=freq1;
131
+ wave1.sync (wave2);
132
+ }
133
+
134
+ if (freq2!=previous_frequency_2){
135
+ wave2.freq (freq2);
136
+ previous_frequency_2=freq2;
137
+ wave2.sync (wave1);
138
+ }
139
+ }
140
+
141
+ /* -------------------------------------------------------------------------- */
142
+ void task20ms () {
143
+ /* -------------------------------------------------------------------------- */
144
+ #ifdef USE_CHAT_AT
145
+ at.run ();
146
+ #endif
147
+ }
148
+
149
+
150
+ /* -------------------------------------------------------------------------- */
151
+ void task500ms () {
152
+ /* -------------------------------------------------------------------------- */
153
+ if (phase1!=previous_phase_1){
154
+ wave1.offset (phase1/2 );
155
+ previous_phase_1=phase1;
156
+ if (phase2==0 ){
157
+ wave1.sync (wave2);
158
+ }
159
+ }
160
+ if (phase2!=previous_phase_2){
161
+ wave2.offset (phase2/2 );
162
+ previous_phase_2=phase2;
163
+ if (phase1==0 ){
164
+ wave2.sync (wave1);
165
+ }
166
+ }
167
+ }
168
+
169
+
170
+
171
+
172
+
49
173
/* -------------------------------------------------------------------------- */
50
174
void timer_callback (timer_callback_args_t *arg) {
51
175
/* -------------------------------------------------------------------------- */
@@ -63,7 +187,7 @@ void set_up_timer() {
63
187
if (num >= 0 ) {
64
188
Serial.print (" Timer Available " );
65
189
Serial.println (type);
66
- timer.begin (TIMER_MODE_PERIODIC, type, num, 1 ,50 , timer_callback);
190
+ timer.begin (TIMER_MODE_PERIODIC, type, num, 1000 ,50 , timer_callback);
67
191
timer.setup_overflow_irq ();
68
192
if (timer.open ()) {
69
193
Serial.println (" FspTimer opened" );
@@ -72,6 +196,12 @@ void set_up_timer() {
72
196
Serial.println (" FspTimer started" );
73
197
}
74
198
}
199
+
200
+ CScheduler::getInstance ().add (task10ms,TASK_10ms);
201
+ CScheduler::getInstance ().add (task15ms,TASK_15ms);
202
+ CScheduler::getInstance ().add (task20ms,TASK_20ms);
203
+ CScheduler::getInstance ().add (task500ms,TASK_500ms);
204
+
75
205
}
76
206
77
207
/* -------------------------------------------------------------------------- */
@@ -84,51 +214,9 @@ bool is_tick_elapsed() {
84
214
return false ;
85
215
}
86
216
87
- #endif
88
217
89
- /* ________CHAT AT___________________________________________________________ */
90
-
91
- #ifdef USE_CHAT_AT
92
- #include " chATWrapper.h"
93
- static CAtWrapper at (&SERIAL_AT);
94
-
95
- #endif
96
-
97
-
98
- // Firmware version
99
- uint8_t version[2 ]={0 ,6 };
100
-
101
- char command=0 ;
102
- byte data[6 ];
103
-
104
- uint16_t wave[RES];
105
-
106
- LedGauge frequency_1 (FQ_1, 10 , 10 , ROW_A_1, ROW_A_2, ROW_A_3, COL_A_1, COL_A_2, COL_A_3);
107
- LedGauge frequency_2 (FQ_2, 10 , 10 , ROW_B_1, ROW_B_2, ROW_B_3, COL_B_1, COL_B_2, COL_B_3, true );
108
-
109
- LedRange range (FQ_R, ROW_C_1, ROW_C_2, ROW_C_3, COL_C_1, COL_C_2);
110
-
111
- analogWave wave1 (DAC,wave,RES,0 );
112
- analogWave wave2 (DAC2,wave,RES,0 );
113
-
114
- int previous_frequency_1 = 10 ;
115
- int previous_frequency_2 = 10 ;
116
- int freq1 = 10 ;
117
- int freq2 = 10 ;
118
-
119
- int previous_phase_1 = 0 ;
120
- int previous_phase_2 = 0 ;
121
-
122
- int phase1 = 0 ;
123
- int phase2 = 0 ;
124
-
125
- const float step = ADC_RES/float (RES);
126
- const float phase_to_deg = 180 /float (RES);
127
-
128
- bool test_flag = false ;
129
- unsigned long time_rephase;
130
- int rephase;
131
218
219
+ #endif // USE_TIMER
132
220
133
221
134
222
// This function loads an wave array with a sinewave using RES samples.
@@ -216,6 +304,59 @@ void setup() {
216
304
#endif
217
305
}
218
306
307
+ /* -------------------------------------------------------------------------- */
308
+ void all_led_on () {
309
+ /* -------------------------------------------------------------------------- */
310
+
311
+ for (int i = 0 ; i < 6 ;i++) {
312
+ range.set (i);
313
+ }
314
+
315
+ for (int i = 0 ; i < 18 ; i++) {
316
+ if (i < 9 ) {
317
+ frequency_1.set (i);
318
+ }
319
+ else if (i < 18 ) {
320
+ frequency_2.set (i - 9 );
321
+ }
322
+ }
323
+ }
324
+
325
+ /* -------------------------------------------------------------------------- */
326
+ void led_f1 () {
327
+ /* -------------------------------------------------------------------------- */
328
+ range.set (1 ,1 ,1 ,0 ,0 );
329
+ frequency_2.set (1 ,1 ,1 ,0 ,0 ,0 );
330
+ for (int i = 0 ; i < 9 ; i++) {
331
+ frequency_1.set (i);
332
+ }
333
+ }
334
+
335
+ /* -------------------------------------------------------------------------- */
336
+ void led_f2 () {
337
+ /* -------------------------------------------------------------------------- */
338
+ range.set (1 ,1 ,1 ,0 ,0 );
339
+ frequency_1.set (1 ,1 ,1 ,0 ,0 ,0 );
340
+ for (int i = 0 ; i < 9 ; i++) {
341
+ frequency_2.set (i);
342
+ }
343
+ }
344
+
345
+ /* -------------------------------------------------------------------------- */
346
+ void led_r () {
347
+ /* -------------------------------------------------------------------------- */
348
+ frequency_1.set (1 ,1 ,1 ,0 ,0 ,0 );
349
+ frequency_2.set (1 ,1 ,1 ,0 ,0 ,0 );
350
+ for (int i = 0 ; i < 6 ; i++) {
351
+ range.set (i);
352
+ }
353
+ }
354
+
355
+
356
+
357
+
358
+
359
+
219
360
220
361
/* _______________________________LOOP_______________________________________ */
221
362
@@ -226,6 +367,50 @@ void loop() {
226
367
#ifdef USE_DEBUG_CODE
227
368
// Serial.println("Msin loop running");
228
369
#endif
370
+
371
+
372
+ #ifdef USE_TIMER
373
+ AppSt_t st = at.getStatus ();
374
+
375
+ if (st == APP_NORMAL) {
376
+ CScheduler::getInstance ().run ();
377
+ }
378
+ else {
379
+ if (st == APP_DIAGNOSTIC_LED_ON) {
380
+ all_led_on ();
381
+ }
382
+ else if (st == APP_DIAGNOSTIC_LED_ON_F1) {
383
+ led_f1 ();
384
+ }
385
+ else if (st == APP_DIAGNOSTIC_LED_ON_F2) {
386
+ led_f2 ();
387
+ }
388
+ else if (st == APP_DIAGNOSTIC_LED_ON_R) {
389
+ led_r ();
390
+ }
391
+ float f1 = at.getF1 ();
392
+ if (f1 != 0.0 ) {
393
+ wave1.freq (f1);
394
+ at.resetF1 ();
395
+ }
396
+ float f2 = at.getF2 ();
397
+ if (f2 != 0.0 ) {
398
+ wave2.freq (f2);
399
+ at.resetF2 ();
400
+ }
401
+ float a1 = at.getA1 ();
402
+ if (a1 > 0.0 ) {
403
+ wave1.amplitude (a1);
404
+ at.resetA1 ();
405
+ }
406
+ float a2 = at.getA2 ();
407
+ if (a2 > 0.0 ) {
408
+ wave2.amplitude (a2);
409
+ at.resetA2 ();
410
+ }
411
+ at.run ();
412
+ }
413
+ #else
229
414
230
415
231
416
frequency_1.refresh ();
@@ -275,9 +460,10 @@ void loop() {
275
460
time_rephase=millis ();
276
461
}
277
462
278
- #ifdef USE_CHAT_AT
279
- at.run ();
463
+ delay (1 );
280
464
#endif
281
465
282
- delay (1 );
466
+
467
+
468
+
283
469
}
0 commit comments