-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathInterruptsIO.ino
573 lines (526 loc) · 12 KB
/
InterruptsIO.ino
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* Copyright (c) 2013 by Jerry Sy aka d0ughb0y
* mail-to: j3rry5y@gmail.com
* Commercial use of this software without
* permission is absolutely prohibited.
*/
volatile static uint16_t beepcount[6];
volatile static boolean buzzerbusy = false;
volatile boolean timerEnabled=false;
inline void enablePCINT(){
PCICR |= (1<<PCIE2);
}
inline void disablePCINT(){
PCICR &= ~(1<<PCIE2);
}
inline void initTimer() {
OCR1C=128;
TIMSK1 |= _BV(OCIE1C);
}
void startTimer() {
timerEnabled=true;
}
inline void chirp() {
beepx(25,0,0,0,0);
}
inline void chirp2() {
beepx(25,25,0,25,0);
}
#define beeplen 100
inline void beep() {
beepx(beeplen,0,0,0,0);
}
inline void beep2() {
beepx(beeplen,beeplen,0,50,0);
}
inline void beepOK() {
beepx(50,50,200,50,0);
}
inline void beepFail() {
beepx(200,200,50,50,0);
}
void beepx(uint8_t first, uint8_t second, uint8_t third,
uint8_t endpause, uint16_t cyclepause) {
if (buzzerbusy) return;
uint8_t SaveSREG = SREG;
cli();
beepcount[0]=first;
beepcount[1]=beepcount[3]=endpause;
beepcount[2]=second;
beepcount[4]=third;
beepcount[5]=cyclepause;
buzzerbusy=true;
SREG=SaveSREG;
}
inline void beepoff() {
buzzerbusy=false;
#ifdef PASSIVEBUZZER
TCCR2A &= ~_BV(COM2B0);
#endif
PORTH&=~_BV(PH6);
}
void alarmOn() {
if (buzzerbusy) return;
if (conf.soundalert) {
uint8_t saveSREG=SREG;
cli();
beepcount[0]=200;
beepcount[1]=50;
beepcount[2]=200;
beepcount[3]=50;
beepcount[4]=200;
beepcount[5]=2000;
buzzerbusy=true;
SREG=saveSREG;
}
}
ISR(PCINT2_vect) {
uint8_t pins = PINK;
static uint8_t lastPins;
uint8_t mask = pins ^ lastPins;
lastPins = pins;
#ifdef _SONAR
if (mask & _BV(PK5)) {
sonarHandler(pins);
}
#endif
#ifdef _PWMFAN
if (mask & _BV(PK0)){
if (pins & _BV(PK0))
PWMFanHandler(0);
}
#if (MAXPWMFANS>=2)
if (mask & _BV(PK1)) {
if (pins & _BV(PK1))
PWMFanHandler(1);
}
#endif
#if (MAXPWMFANS>=3)
if (mask & _BV(PK6)) {
if (pins & _BV(PK6))
PWMFanHandler(2);
}
#endif
#endif
#ifdef _FEEDER
if (mask & _BV(PK2)) { //feeder
feederHandler(pins);
}
#endif
}
ISR(TIMER1_COMPC_vect) { //interrupts every 1.02 ms
static uint16_t counter = 0;
static uint16_t outletmod = 10;
//call 1ms tasks, make sure tasks executes in under 100us
buzzerHandler();
if (!timerEnabled) return;
#ifdef _DOSER
doserHandler();
#endif
#ifdef _FEEDER_V2
feedHandler();
#endif
if (counter%outletmod==0) {//10ms
//call 10ms tasks, make sure tasks execute in under 1 ms
outletmod=outletHandlerA();
//after last outlet is processed, we make the next call after 1 full second
}
if (counter++%980==0) {//1second
counter=1;
//call 1 second tasks
outletHandlerB();
}
}
void initBuzzer(){
DDRH |= _BV(PH6); //pin D9 (PH6) output
PORTH &= ~_BV(PH6);
#ifdef PASSIVEBUZZER
TCCR2A=TCCR2B=0;
TCCR2A|=_BV(COM2B0)|_BV(WGM21); //ctc toggle
TCCR2B|=_BV(CS21)|_BV(CS20); //clk/32
OCR2A=108; //2300 hz
#endif
buzzerbusy=false;
}
inline void buzzerHandler(){
//buzzer pin D9 PH6
static uint8_t state = 0;
static uint16_t counter = 0;
if (!buzzerbusy) {
state=0;
counter=0;
return;
}
if (counter++ < beepcount[state]) {
if (!(state%2)) {
#ifdef PASSIVEBUZZER
TCCR2A |= _BV(COM2B0);
#else
PORTH |= _BV(PH6);
#endif
}
} else {
if (state==5 && beepcount[5]==0) {
buzzerbusy=false;
}
counter=0;
state++;
if (state>5) state=0;
#ifdef PASSIVEBUZZER
TCCR2A &= ~_BV(COM2B0);
#endif
PORTH &= ~_BV(PH6);
}
}
//////////////////////////////////////////
// ATO
//////////////////////////////////////////
//use polling code for ATO
void initATO(){
//ATO1 PK3 PCINT19
//ATO2 PK4 PCINT20
DDRK &= ~(_BV(PK3)|_BV(PK4)); //inputs
PORTK |= (_BV(PK3) | _BV(PK4)); //pullups
}
inline uint8_t getATO1(){
return PINK & _BV(PK3);
}
inline uint8_t getATO2() {
return PINK & _BV(PK4);
}
void checkATO(){
if (conf.outletRec[ATO].mode == _auto) {
if (!getATO2()&& !getATO1() && isOutletOn(Return)
#ifdef _SONAR
&& sonaravg<conf.sonaralertval*10
#endif
) {
_outletOn(ATO);
return;
}
_outletOff(ATO);
}
}
////////////////////////////////
// Doser PUMPS
////////////////////////////////
#ifdef _DOSER
void initDosers(){
//PG0 Digital 41 doser0
//PG2 Digital 39 doser1
//PG1 Digital 40 doser2
//PL7 Digital 42 doser3
readDoserStatus();
#if (MAXDOSERS>=1)
DDRG |= _BV(PG0);
PORTG&=~_BV(PG0);
#endif
#if (MAXDOSERS>=2)
DDRG |= _BV(PG2);
PORTG&=~_BV(PG2);
#endif
#if (MAXDOSERS>=3)
DDRG |= _BV(PG1);
PORTG&=~_BV(PG1);
#endif
#if (MAXDOSERS==4)
DDRL |= _BV(PL7);
PORTL&=~_BV(PL7);
#endif
if (conf.doser[0].rate==0||conf.doser[1].rate==0) {
p(F("Configure Doser."));
logMessage(F("Please configure doser pumps before using."));
} else {
p(F("Dosers OK. "));
logMessage(F("Doser pumps initialized."));
#if (MAXDOSERS>=1)
logMessage(F("Doser0 dosed volume since reset is "),dosedvolume[0]/100.0);
#endif
#if (MAXDOSERS>=2)
logMessage(F("Doser1 dosed volume since reset is "),dosedvolume[1]/100.0);
#endif
#if (MAXDOSERS>=3)
logMessage(F("Doser2 dosed volume since reset is "),dosedvolume[2]/100.0);
#endif
#if (MAXDOSERS==4)
logMessage(F("Doser3 dosed volume since reset is "),dosedvolume[3]/100.0);
#endif
}
cli();
for (int i=0;i<MAXDOSERS;i++){
doseractive[i]=false;
dosercounter[i]=dosercountmatch[i]=0ul;
}
updateDoserStatusFlag=false;
calibrationcount=0;
sei();
}
inline void doserHandler(){
for (int i=0;i<MAXDOSERS;i++) {
if (doseractive[i]) {
if (++dosercounter[i]>=dosercountmatch[i]) {
doserOff(i);
}
}
}
}
inline void doserOn(uint8_t i, uint32_t countmatch) {
if (isDoserOn(i) || dosercalibrating) return;
uint8_t SaveREG=SREG;
cli();
if (dosedvolume[i]>=conf.doser[i].fullvolume*100ul) {
SREG=SaveREG;
return;
}
dosercounter[i]=0;
dosercountmatch[i]=countmatch;
if (i==0) {
PORTG|=_BV(PG0);
_outlogentry(Doser0,true);
} else if (i==1) {
PORTG|=_BV(PG2);
_outlogentry(Doser1,true);
} else if (i==2) {
PORTG|=_BV(PG1);
_outlogentry(Doser2,true);
} else if (i==3) {
PORTL|=_BV(PL7);
_outlogentry(Doser3,true);
}
doseractive[i]=true;
SREG=SaveREG;
}
inline void doserOff(uint8_t i) {
if (dosercalibrating) return calstop(i);
if (isDoserOn(i)) {
uint8_t SaveSREG=SREG;
cli();
doseractive[i]=false;
if (i==0) {
PORTG&=~_BV(PG0);
_outlogentry(Doser0,false);
} else if (i==1) {
PORTG&=~_BV(PG2);
_outlogentry(Doser1,false);
} else if (i==2) {
PORTG&=~_BV(PG1);
_outlogentry(Doser2,false);
} else if (i==3) {
PORTL&=~_BV(PL7);
_outlogentry(Doser3,false);
}
//set log
if (conf.doser[i].rate>0)
dosedvolume[i]+=100ul*dosercounter[i]/conf.doser[i].rate;
dosercountmatch[i]=0;
dosercounter[i]=0;
if (updateDoserStatusFlag==false) {
updateDoserStatusFlag=true;
}
SREG=SaveSREG;
}
}
inline boolean isDoserOn(uint8_t i){
if (i==0)
return PORTG & _BV(PG0);
else if (i==1)
return PORTG & _BV(PG2);
else if (i==2)
return PORTG & _BV(PG1);
else if (i==3)
return PORTL & _BV(PL7);
}
void manualDoseOn(uint8_t i, uint16_t vol) {
if (conf.doser[i].rate==0 || dosercalibrating) return;
doserOn(i, vol/10.0 * conf.doser[i].rate);
}
void calstart(uint8_t i) {
cli();
calibrationcount=0;
dosercountmatch[i] = 0;
dosercounter[i] = 0;
dosercalibrating=true;
logMessage(F("Doser "),i);
logMessage(F("Calibration start."));
sei();
}
void calstop(uint8_t i) {
uint8_t SaveSREG = SREG;
cli();
doseractive[i]=false;
if (i==0) {
PORTG&=~_BV(PG0);
} else {
PORTG&=~_BV(PG2);
}
calibrationcount+=dosercounter[i];
dosercounter[i]=dosercountmatch[i]=0;
SREG = SaveSREG;
}
void caladjust(uint8_t i,uint32_t addcount) {
cli();
dosercountmatch[i] = addcount;
dosercounter[i] = 0;
if (i==0) {
PORTG|=_BV(PG0);
} else if (i==1) {
PORTG|=_BV(PG2);
} else if (i==2) {
PORTG|=_BV(PG1);
} else if (i==3) {
PORTL|=_BV(PL7);
}
doseractive[i]=true;
sei();
}
void calsave(uint8_t i, uint16_t vol) {
conf.doser[i].rate = calibrationcount/vol;
calibrationcount=0;
dosercalibrating=false;
writeEEPROM();
logMessage(F("Doser "),i);
logMessage(F("Calibration End. Dose rate = "),(int)conf.doser[i].rate);
}
void getdoserstatus(EthernetClient& client, uint8_t i) {
client << F("{\"n\":\"") << (const char*)conf.doser[i].name << F("\",\"dd\":\"") << conf.doser[i].dailydose;
client << F("\",\"dpd\":\"") << conf.doser[i].dosesperday << F("\",\"i\":\"") << conf.doser[i].interval;
client << F("\",\"st\":\"") << conf.doser[i].starttime;
uint8_t saveSREG = SREG;
cli();
client << F("\",\"dv\":\"") << dosedvolume[i] << F("\",\"fv\":\"") << conf.doser[i].fullvolume;
client << F("\",\"s\":\"") << (isDoserOn(i)?F("on"):F("off"));
SREG=saveSREG;
client << F("\"}") << (i<MAXDOSERS-1?F(","):F(""));
}
#endif
////////////////////////////////////
// FEEDER
////////////////////////////////////
//#define feedOutPin 49 //PL0 PORTL
//#define feedInPin A10 //PK2 PCINT18
#ifdef _FEEDER
void initFeeder() {
DDRK &= ~_BV(PK2);
PORTK |= _BV(PK2);
DDRL |= _BV(PL0);
PORTL |= _BV(PL0);
PCMSK2 |= (1<<PCINT18);
}
inline void feed(){
if (!(~PORTL & _BV(PL0))) {
PORTL &= ~_BV(PL0);
_outlogentry(Feeder,true);
}
}
inline void feederHandler(uint8_t pins) {
static unsigned long last_int_time = 0;
unsigned long int_time=micros();
if ((int_time-last_int_time)>=2000000) {
if (pins & _BV(PK2)) {
if (~PORTL & _BV(PL0)) {
PORTL |= _BV(PL0);
_outlogentry(Feeder,false);
}
}
}
last_int_time=int_time;
}
#endif
//////////////////////////////////////////////
// FEEDER V2
//////////////////////////////////////////////
#ifdef _FEEDER_V2
volatile uint8_t feed2counter=0;
void initFeeder(){
//use A10 PK2 only
//set PK2 to tristate mode
DDRK &= ~_BV(PK2);//input
PORTK &= ~_BV(PK2);//no pullup
}
inline void feed() {
DDRK |= _BV(PK2);//output
PORTK |= _BV(PK2);//high
feed2counter=100;
_outlogentry(Feeder,true);
}
inline void feedHandler(){
if (feed2counter>0) {
if (--feed2counter==0) {
PORTK &= ~_BV(PK2);//low
DDRK &= ~_BV(PK2);//input
_outlogentry(Feeder,false);
}
}
}
#endif
////////////////////////////
// SONAR
////////////////////////////
#define sonarEPin A13 //PK5 PCINT21
#define sonarTPin 47 //PL2
#define SONAR_TRIGGER_HI PORTL |= _BV(PL2);
#define SONAR_TRIGGER_LO PORTL &= ~_BV(PL2);
#define DURATION_TO_MM 6
#define DURATION_TO_CM 58
#define DURATION_TO_IN 148
#define SONAR_MAX 50 //400
#define SONARPINOUT DDRK |= _BV(PK5)
#define SONARPININ DDRK &= ~_BV(PK5)
volatile static uint16_t sonarDistance = 0;
void initSonar() {
pinMode(sonarEPin,INPUT);
pinMode(sonarTPin,OUTPUT);
digitalWrite(sonarEPin,HIGH);
digitalWrite(sonarTPin,LOW);
PCMSK2 |= _BV(PCINT21);
}
inline void sonarHandler(uint8_t pins) {
static unsigned long starttime = 0;
static const long maxval = SONAR_MAX*DURATION_TO_CM;
if (pins & _BV(PK5)) {
starttime = micros();
} else {
unsigned long duration = micros()-starttime;
if (duration<=maxval)
sonarDistance = duration/DURATION_TO_MM;
}
}
void updateSonar()
{
static uint32_t sum=0;
uint8_t saveSREG=SREG;
cli();
uint16_t tmpavg=sonaravg;
uint16_t tmpdist=sonarDistance;
SREG=saveSREG;
if (tmpdist>0) {
if (sum)
sum = (sum - tmpavg) + tmpdist;
else {
sum = tmpdist*16;
}
tmpavg = sum /16;
saveSREG=SREG;
cli();
sonaravg=tmpavg;
SREG=saveSREG;
}
SONAR_TRIGGER_LO
delayMicroseconds(2);
SONAR_TRIGGER_HI
delayMicroseconds(10);
SONAR_TRIGGER_LO
delayMicroseconds(2);
}
uint16_t getSonar() {
uint8_t saveSREG = SREG;
cli();
uint16_t tmp = sonaravg;
SREG=saveSREG;
return tmp;
}
uint8_t getSonarPct() {
uint16_t numerator = conf.sonarlow*10-getSonar();
uint16_t denominator = conf.sonarlow - conf.sonarhigh;
return numerator*10/denominator;
}