-
Notifications
You must be signed in to change notification settings - Fork 20
/
count.c
102 lines (89 loc) · 2.02 KB
/
count.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
/*
* Copyright © 2014-2015, Matthias Urlichs <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License (included; see the file LICENSE)
* for more details.
*/
/* This code implements counting transitions.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "pgm.h"
#include <string.h>
#include "port.h"
#include "count.h"
#include "features.h"
#include "moat.h"
#include "dev_data.h"
#include "debug.h"
#include "timer.h"
#include "moat_internal.h"
#ifdef N_COUNT
count_t counts[] = {
#include "_count.h"
};
#ifdef CONDITIONAL_SEARCH
uint8_t count_changed_cache;
static uint8_t max_seen = 0;
#endif
static uint8_t poll_next = 0;
void poll_count(void)
{
uint8_t i = poll_next;
uint8_t trigged=0;
count_t *t;
port_t *p;
if (i >= N_COUNT)
i = 0;
#ifdef CONDITIONAL_SEARCH
if (!i) {
count_changed_cache = max_seen;
max_seen = 0;
}
#endif
t = &counts[i];
i++;
poll_next = i;
p = &ports[t->port-1];
if(!(p->flags & PFLG_CURRENT) != !(t->flags & CF_IS_ON)) {
if (p->flags & PFLG_CURRENT) {
t->flags |= CF_IS_ON;
trigged = !(t->flags & CF_FLANK_MASK) ||
!!(t->flags & CF_RISING_ONLY);
}else{
t->flags &=~CF_IS_ON;
trigged = !(t->flags & CF_FLANK_MASK) ||
!!(t->flags & CF_FALLING_ONLY);
}
if(trigged) {
t->count++;
#ifdef CONDITIONAL_SEARCH
if(t->flags & CF_ALERTING) {
t->flags |= CF_IS_ALERT;
}
#endif
}
}
#ifdef CONDITIONAL_SEARCH
if(t->flags & CF_IS_ALERT) {
max_seen = i;
}
#endif
}
void init_count(void)
{
count_t *t = counts;
uint8_t i;
for(i=0;i<N_COUNT;i++,t++) {
t->count = 0;
}
}
#endif // any COUNT controllers