-
Notifications
You must be signed in to change notification settings - Fork 20
/
moat_pwm.c
140 lines (121 loc) · 2.68 KB
/
moat_pwm.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
/*
* 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 reading port pins via 1wire.
*/
#include <string.h> // memset
#include "moat_internal.h"
#include "dev_data.h"
#include "debug.h"
#include "onewire.h"
#include "port.h"
#include "pwm.h"
#include "timer.h"
#ifdef N_PWM
#define BLEN N_PWM*2+(N_PWM+7)/8
uint8_t read_pwm_len(uint8_t chan)
{
if(chan)
return 7;
else
return BLEN;
}
void read_pwm(uint8_t chan, uint8_t *buf)
{
pwm_t *t;
port_t *p;
if (chan) { // one PWM: send port state, remaining time, timer values
uint16_t tm;
if (chan > N_PWM)
next_idle('p');
t = &pwms[chan-1];
p = &ports[t->port-1];
tm=timer_remaining(&t->timer);
*buf++ = p->flags;
*buf++ = tm>>8;
*buf++ = tm;
*buf++ = t->t_on>>8;
*buf++ = t->t_on;
*buf++ = t->t_off>>8;
*buf++ = t->t_off;
} else { // all PWMs: send port state, time remaining
uint8_t i;
t = pwms;
for(i=0;i<N_PWM;i++,t++) {
uint16_t tm;
if(!(i&7)) {
p = &ports[t->port-1];
uint8_t j,v=0,m=1;
for(j=0;j<8;j++) {
if (i+j >= N_PWM) break;
if (p->flags & PFLG_CURRENT)
v |= m;
m <<= 1;
}
*buf++ = v;
}
tm=timer_remaining(&t->timer);
*buf++ = tm>>8;
*buf++ = tm;
}
}
}
void write_pwm_check(uint8_t chan, uint8_t *buf, uint8_t len)
{
if (chan == 0 || chan > N_PWM || (len != 2 && len != 4))
next_idle('w');
}
void write_pwm(uint8_t chan, uint8_t *buf, uint8_t len)
{
pwm_t *t = &pwms[chan-1];
uint16_t a,b,last;
last = ((t->flags & PWM_IS_ON) ? t->t_on : t->t_off);
if (len == 2) {
a = buf[0];
a |= a<<8;
b = buf[1];
b |= b<<8;
} else {
a = buf[0]<<8|buf[1];
b = buf[2]<<8|buf[3];
}
t->t_on = a;
t->t_off = b;
if(!last || (t->flags & PWM_FORCE))
timer_reset(&t->timer);
}
#ifdef CONDITIONAL_SEARCH
extern uint8_t pwm_changed_cache;
char alert_pwm_check(void)
{
return (pwm_changed_cache+7)>>3;
}
void alert_pwm_fill(uint8_t *buf)
{
uint8_t i;
pwm_t *t = pwms;
uint8_t m=1;
memset(buf,0,(N_PWM+7)>>3);
for(i=0; i < N_PWM; i++,t++) {
if (!m) {
m=1;
buf++;
}
if (t->flags & PWM_IS_ALERT)
*buf |= m;
m <<= 1;
}
}
#endif // conditional
#endif